さらに改善、したつもり
[ofnhwx.olib.git] / src / jp / gr / java_conf / ofnhwx / olib / utils / OUtil.java
blobcdc1d35947ae18fd294cdaabd1007d743f353961
1 package jp.gr.java_conf.ofnhwx.olib.utils;
3 import java.lang.reflect.Method;
4 import java.util.HashMap;
5 import java.util.Map;
7 import android.Manifest;
8 import android.content.ComponentName;
9 import android.content.Context;
10 import android.content.Intent;
11 import android.content.pm.PackageManager;
12 import android.os.Handler;
13 import android.provider.Settings.System;
14 import android.telephony.TelephonyManager;
15 import android.view.View;
16 import android.view.inputmethod.InputMethodManager;
18 /**
19 * 雑多な機能集.
20 * @author yuta
22 public abstract class OUtil {
24 public static enum NumberType {
25 /** 通知不可能. */ UNKNOWN ,
26 /** 非通知. */ PRIVATE ,
27 /** 公衆電話. */ PAYPHONE,
28 /** 通知. */ NORMAL ;
29 public static final NumberType get(String number) {
30 if (map.containsKey(number)) {
31 return map.get(number);
32 } else {
33 return NumberType.NORMAL;
36 // com.android.internal.telephony.CallerInfo
37 public static final String UNKNOWN_NUMBER = "-1";
38 public static final String PRIVATE_NUMBER = "-2";
39 public static final String PAYPHONE_NUMBER = "-3";
40 public static final String UNKNOWN_NUMBER_NO_HYPHEN = "1";
41 public static final String PRIVATE_NUMBER_NO_HYPHEN = "2";
42 public static final String PAYPHONE_NUMBER_NO_HYPHEN = "3";
44 private static final Map<String, NumberType> map = new HashMap<String, OUtil.NumberType>();
45 static {
46 map.put(UNKNOWN_NUMBER , UNKNOWN);
47 map.put(PRIVATE_NUMBER , PRIVATE);
48 map.put(PAYPHONE_NUMBER, PAYPHONE);
49 map.put(UNKNOWN_NUMBER_NO_HYPHEN , UNKNOWN);
50 map.put(PRIVATE_NUMBER_NO_HYPHEN , PRIVATE);
51 map.put(PAYPHONE_NUMBER_NO_HYPHEN, PAYPHONE);
54 public static final boolean isUnknown(String number) {
55 return get(number) == UNKNOWN;
57 public static final boolean isPrivate(String number) {
58 return get(number) == PRIVATE;
60 public static final boolean isPayphone(String number) {
61 return get(number) == PAYPHONE;
65 // コンポーネントの有効・無効切替え
66 private static final int COMPONENT_ENABLED = PackageManager.COMPONENT_ENABLED_STATE_ENABLED ;
67 private static final int COMPONENT_DISABLED = PackageManager.COMPONENT_ENABLED_STATE_DISABLED;
69 /**
70 * コンポーネントの有効・無効を切替える..
71 * @param context
72 * @param clazz
73 * @param enable
75 public static final void setComponentEnabled(Context context, Class<?> clazz, boolean enable) {
76 PackageManager pm = context.getPackageManager();
77 ComponentName component = new ComponentName(context, clazz);
78 if (enable) {
79 pm.setComponentEnabledSetting(component, COMPONENT_ENABLED , PackageManager.DONT_KILL_APP);
80 } else {
81 pm.setComponentEnabledSetting(component, COMPONENT_DISABLED, PackageManager.DONT_KILL_APP);
85 /**
86 * コンポーネントの有効・無効を取得.
87 * @param context
88 * @param clazz
89 * @return
91 public static final boolean getComponetnEnabled(Context context, Class<?> clazz) {
92 PackageManager pm = context.getPackageManager();
93 ComponentName component = new ComponentName(context, clazz);
94 return pm.getComponentEnabledSetting(component) == COMPONENT_ENABLED;
97 /**
98 * ITelephonyのインスタンスを取得
99 * @param context
100 * @return ITelephony, 失敗したら'null'
102 public static final Object connectToTelephonyService(Context context) {
103 if (!OUtil.hasPermission(context, Manifest.permission.MODIFY_PHONE_STATE)) {
104 return null;
106 try {
107 TelephonyManager tm = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
108 Class<?> clazz = Class.forName(tm.getClass().getName());
109 Method m = clazz.getDeclaredMethod("getITelephony");
110 m.setAccessible(true);
111 return m.invoke(tm);
112 } catch (Exception e) {
113 e.printStackTrace();
114 return null;
119 * ソフトキーボードの表示.
120 * @param context
121 * @param view
123 public static final void showSoftInput(final Context context, final View view) {
124 view.requestFocus();
125 new Handler().postDelayed(new Runnable() {
126 public void run() {
127 InputMethodManager imm = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
128 imm.showSoftInput(view, 0);
130 }, 100); // MEMO: ちょっと時間を置かないと表示されない?
134 * ソフトキーボードの非表示.
135 * @param context
137 public static final void hideSoftInput(Context context, View view) {
138 InputMethodManager imm = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
139 imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
143 * 入力時バイブレーションの設定を取得.
144 * @param context
145 * @return
147 public static final boolean hapticFeedbackEnabled(Context context) {
148 return System.getInt(context.getContentResolver(), System.HAPTIC_FEEDBACK_ENABLED, 0) == 1;
152 * 指定されたパーミッションがあるかを確認する.
153 * @param context
154 * @param permission
155 * @return
157 public static final boolean hasPermission(Context context, String permission) {
158 int res = context.checkCallingOrSelfPermission(permission);
159 return res == PackageManager.PERMISSION_GRANTED;
163 * 既定値を使用せず選択させる暗黙のインテント.
164 * @param context
165 * @param intent
166 * @param titleId
168 public static final void sendChoiceIntent(Context context, Intent intent, int titleId) {
169 Intent chooser = Intent.createChooser(intent, context.getText(titleId));
170 context.startActivity(chooser);
174 * 文字列からバイト配列への変換.
175 * @param data
176 * @return
178 public static final byte[] stringToByteArray(String data) {
179 int len = data.length() / 2;
180 byte[] result = new byte[len];
181 for (int i = 0; i < len; ++i) {
182 int j = i * 2;
183 int tmp = Integer.parseInt(data.substring(j, j + 2), 16);
184 if (tmp > 127) {
185 result[i] = (byte)(tmp - 256);
186 } else {
187 result[i] = (byte)tmp;
190 return result;
194 * バイト配列から文字列への変換.
195 * @param data
196 * @return
198 public static final String byteArrayToString(byte[] data) {
199 StringBuilder sb = new StringBuilder(data.length * 2);
200 for (byte b : data) {
201 String tmp = Integer.toHexString(b < 0 ? b + 256 : b);
202 if (tmp.length() == 1) {
203 sb.append('0');
205 sb.append(tmp.toUpperCase());
207 return sb.toString();