1 package jp
.gr
.java_conf
.ofnhwx
.olib
.utils
;
3 import java
.lang
.reflect
.Method
;
4 import java
.util
.HashMap
;
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
;
22 public abstract class OUtil
{
24 public static enum NumberType
{
25 /** 通知不可能. */ UNKNOWN
,
27 /** 公衆電話. */ PAYPHONE
,
29 public static final NumberType
get(String number
) {
30 if (map
.containsKey(number
)) {
31 return map
.get(number
);
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
>();
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
;
66 private static final int COMPONENT_ENABLED
= PackageManager
.COMPONENT_ENABLED_STATE_ENABLED
;
67 private static final int COMPONENT_DISABLED
= PackageManager
.COMPONENT_ENABLED_STATE_DISABLED
;
70 * コンポーネントの有効・無効を切替える..
75 public static final void setComponentEnabled(Context context
, Class
<?
> clazz
, boolean enable
) {
76 PackageManager pm
= context
.getPackageManager();
77 ComponentName component
= new ComponentName(context
, clazz
);
79 pm
.setComponentEnabledSetting(component
, COMPONENT_ENABLED
, PackageManager
.DONT_KILL_APP
);
81 pm
.setComponentEnabledSetting(component
, COMPONENT_DISABLED
, PackageManager
.DONT_KILL_APP
);
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
;
98 * ITelephonyのインスタンスを取得
100 * @return ITelephony, 失敗したら'null'
102 public static final Object
connectToTelephonyService(Context context
) {
103 if (!OUtil
.hasPermission(context
, Manifest
.permission
.MODIFY_PHONE_STATE
)) {
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);
112 } catch (Exception e
) {
123 public static final void showSoftInput(final Context context
, final View view
) {
125 new Handler().postDelayed(new Runnable() {
127 InputMethodManager imm
= (InputMethodManager
)context
.getSystemService(Context
.INPUT_METHOD_SERVICE
);
128 imm
.showSoftInput(view
, 0);
130 }, 100); // MEMO: ちょっと時間を置かないと表示されない?
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);
147 public static final boolean hapticFeedbackEnabled(Context context
) {
148 return System
.getInt(context
.getContentResolver(), System
.HAPTIC_FEEDBACK_ENABLED
, 0) == 1;
152 * 指定されたパーミッションがあるかを確認する.
157 public static final boolean hasPermission(Context context
, String permission
) {
158 int res
= context
.checkCallingOrSelfPermission(permission
);
159 return res
== PackageManager
.PERMISSION_GRANTED
;
163 * 既定値を使用せず選択させる暗黙のインテント.
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
);
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
) {
183 int tmp
= Integer
.parseInt(data
.substring(j
, j
+ 2), 16);
185 result
[i
] = (byte)(tmp
- 256);
187 result
[i
] = (byte)tmp
;
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) {
205 sb
.append(tmp
.toUpperCase());
207 return sb
.toString();