JPushPlugin.java 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  1. package cn.jiguang.cordova.push;
  2. import android.annotation.TargetApi;
  3. import android.app.Activity;
  4. import android.app.AppOpsManager;
  5. import android.content.Context;
  6. import android.content.pm.ApplicationInfo;
  7. import android.os.Build;
  8. import android.text.TextUtils;
  9. import android.util.Log;
  10. import org.apache.cordova.CallbackContext;
  11. import org.apache.cordova.CordovaInterface;
  12. import org.apache.cordova.CordovaPlugin;
  13. import org.apache.cordova.CordovaWebView;
  14. import org.json.JSONArray;
  15. import org.json.JSONException;
  16. import org.json.JSONObject;
  17. import java.lang.reflect.Field;
  18. import java.lang.reflect.InvocationTargetException;
  19. import java.lang.reflect.Method;
  20. import java.util.Arrays;
  21. import java.util.HashMap;
  22. import java.util.HashSet;
  23. import java.util.Iterator;
  24. import java.util.List;
  25. import java.util.Map;
  26. import java.util.Map.Entry;
  27. import java.util.Set;
  28. import java.util.concurrent.ExecutorService;
  29. import java.util.concurrent.Executors;
  30. import cn.jpush.android.api.BasicPushNotificationBuilder;
  31. import cn.jpush.android.api.JPushInterface;
  32. import cn.jpush.android.api.TagAliasCallback;
  33. import cn.jpush.android.data.JPushLocalNotification;
  34. public class JPushPlugin extends CordovaPlugin {
  35. private final static List<String> methodList =
  36. Arrays.asList(
  37. "addLocalNotification",
  38. "areNotificationEnabled",
  39. "clearAllNotification",
  40. "clearLocalNotifications",
  41. "clearNotificationById",
  42. "getNotification",
  43. "getRegistrationID",
  44. "init",
  45. "isPushStopped",
  46. "onPause",
  47. "onResume",
  48. "requestPermission",
  49. "removeLocalNotification",
  50. "reportNotificationOpened",
  51. "resumePush",
  52. "setAlias",
  53. "setBasicPushNotificationBuilder",
  54. "setCustomPushNotificationBuilder",
  55. "setDebugMode",
  56. "setLatestNotificationNum",
  57. "setPushTime",
  58. "setTags",
  59. "setTagsWithAlias",
  60. "setSilenceTime",
  61. "setStatisticsOpen",
  62. "stopPush"
  63. );
  64. private ExecutorService threadPool = Executors.newFixedThreadPool(1);
  65. private static JPushPlugin instance;
  66. private static Activity cordovaActivity;
  67. private static String TAG = "JPushPlugin";
  68. private static boolean shouldCacheMsg = false;
  69. private static boolean isStatisticsOpened = false; // 是否开启统计分析功能
  70. public static String notificationTitle;
  71. public static String notificationAlert;
  72. public static Map<String, Object> notificationExtras = new HashMap<String, Object>();
  73. public static String openNotificationTitle;
  74. public static String openNotificationAlert;
  75. public static Map<String, Object> openNotificationExtras = new HashMap<String, Object>();
  76. public JPushPlugin() {
  77. instance = this;
  78. }
  79. @Override
  80. public void initialize(CordovaInterface cordova, CordovaWebView webView) {
  81. Log.i(TAG, "JPush initialize.");
  82. super.initialize(cordova, webView);
  83. JPushInterface.init(cordova.getActivity().getApplicationContext());
  84. cordovaActivity = cordova.getActivity();
  85. //如果同时缓存了打开事件 openNotificationAlert 和 消息事件 notificationAlert,只向 UI 发打开事件。
  86. //这样做是为了和 iOS 统一。
  87. if (openNotificationAlert != null) {
  88. notificationAlert = null;
  89. transmitNotificationOpen(openNotificationTitle, openNotificationAlert,
  90. openNotificationExtras);
  91. }
  92. if (notificationAlert != null) {
  93. transmitNotificationReceive(notificationTitle, notificationAlert,
  94. notificationExtras);
  95. }
  96. }
  97. public void onPause(boolean multitasking) {
  98. Log.i(TAG, "---------------- onPause");
  99. shouldCacheMsg = true;
  100. if (isStatisticsOpened && multitasking) {
  101. JPushInterface.onPause(this.cordova.getActivity());
  102. }
  103. }
  104. public void onResume(boolean multitasking) {
  105. shouldCacheMsg = false;
  106. Log.i(TAG, "---------------- onResume" + "-" + openNotificationAlert
  107. + "-" + notificationAlert);
  108. if (isStatisticsOpened && multitasking) {
  109. JPushInterface.onResume(this.cordova.getActivity());
  110. }
  111. if (openNotificationAlert != null) {
  112. notificationAlert = null;
  113. transmitNotificationOpen(openNotificationTitle, openNotificationAlert,
  114. openNotificationExtras);
  115. }
  116. if (notificationAlert != null) {
  117. transmitNotificationReceive(notificationTitle, notificationAlert,
  118. notificationExtras);
  119. }
  120. }
  121. @Override
  122. public void onDestroy() {
  123. super.onDestroy();
  124. cordovaActivity = null;
  125. instance = null;
  126. }
  127. private static JSONObject getMessageObject(String message,
  128. Map<String, Object> extras) {
  129. JSONObject data = new JSONObject();
  130. try {
  131. data.put("message", message);
  132. JSONObject jExtras = new JSONObject();
  133. for (Entry<String, Object> entry : extras.entrySet()) {
  134. if (entry.getKey().equals("cn.jpush.android.EXTRA")) {
  135. JSONObject jo = null;
  136. if (TextUtils.isEmpty((String) entry.getValue())) {
  137. jo = new JSONObject();
  138. } else {
  139. jo = new JSONObject((String) entry.getValue());
  140. String key;
  141. Iterator keys = jo.keys();
  142. while (keys.hasNext()) {
  143. key = keys.next().toString();
  144. jExtras.put(key, jo.getString(key));
  145. }
  146. }
  147. jExtras.put("cn.jpush.android.EXTRA", jo);
  148. } else {
  149. jExtras.put(entry.getKey(), entry.getValue());
  150. }
  151. }
  152. if (jExtras.length() > 0) {
  153. data.put("extras", jExtras);
  154. }
  155. } catch (JSONException e) {
  156. e.printStackTrace();
  157. }
  158. return data;
  159. }
  160. private static JSONObject getNotificationObject(String title,
  161. String alert, Map<String, Object> extras) {
  162. JSONObject data = new JSONObject();
  163. try {
  164. data.put("title", title);
  165. data.put("alert", alert);
  166. JSONObject jExtras = new JSONObject();
  167. for (Entry<String, Object> entry : extras.entrySet()) {
  168. if (entry.getKey().equals("cn.jpush.android.EXTRA")) {
  169. JSONObject jo;
  170. if (TextUtils.isEmpty((String) entry.getValue())) {
  171. jo = new JSONObject();
  172. } else {
  173. jo = new JSONObject((String) entry.getValue());
  174. String key;
  175. Iterator keys = jo.keys();
  176. while (keys.hasNext()) {
  177. key = keys.next().toString();
  178. jExtras.put(key, jo.getString(key));
  179. }
  180. }
  181. jExtras.put("cn.jpush.android.EXTRA", jo);
  182. } else {
  183. jExtras.put(entry.getKey(), entry.getValue());
  184. }
  185. }
  186. if (jExtras.length() > 0) {
  187. data.put("extras", jExtras);
  188. }
  189. } catch (JSONException e) {
  190. e.printStackTrace();
  191. }
  192. return data;
  193. }
  194. static void transmitMessageReceive(String message, Map<String, Object> extras) {
  195. if (instance == null) {
  196. return;
  197. }
  198. JSONObject data = getMessageObject(message, extras);
  199. String format = "window.plugins.jPushPlugin.receiveMessageInAndroidCallback(%s);";
  200. final String js = String.format(format, data.toString());
  201. cordovaActivity.runOnUiThread(new Runnable() {
  202. @Override
  203. public void run() {
  204. instance.webView.loadUrl("javascript:" + js);
  205. }
  206. });
  207. }
  208. static void transmitNotificationOpen(String title, String alert,
  209. Map<String, Object> extras) {
  210. if (instance == null) {
  211. return;
  212. }
  213. JSONObject data = getNotificationObject(title, alert, extras);
  214. String format = "window.plugins.jPushPlugin.openNotificationInAndroidCallback(%s);";
  215. final String js = String.format(format, data.toString());
  216. cordovaActivity.runOnUiThread(new Runnable() {
  217. @Override
  218. public void run() {
  219. instance.webView.loadUrl("javascript:" + js);
  220. }
  221. });
  222. JPushPlugin.openNotificationTitle = null;
  223. JPushPlugin.openNotificationAlert = null;
  224. }
  225. static void transmitNotificationReceive(String title, String alert,
  226. Map<String, Object> extras) {
  227. if (instance == null) {
  228. return;
  229. }
  230. JSONObject data = getNotificationObject(title, alert, extras);
  231. String format = "window.plugins.jPushPlugin.receiveNotificationInAndroidCallback(%s);";
  232. final String js = String.format(format, data.toString());
  233. cordovaActivity.runOnUiThread(new Runnable() {
  234. @Override
  235. public void run() {
  236. instance.webView.loadUrl("javascript:" + js);
  237. }
  238. });
  239. JPushPlugin.notificationTitle = null;
  240. JPushPlugin.notificationAlert = null;
  241. }
  242. static void transmitReceiveRegistrationId(String rId) {
  243. if (instance == null) {
  244. return;
  245. }
  246. JSONObject data = new JSONObject();
  247. try {
  248. data.put("registrationId", rId);
  249. } catch (JSONException e) {
  250. e.printStackTrace();
  251. }
  252. String format = "window.plugins.jPushPlugin.receiveRegistrationIdInAndroidCallback(%s);";
  253. final String js = String.format(format, data.toString());
  254. cordovaActivity.runOnUiThread(new Runnable() {
  255. @Override
  256. public void run() {
  257. instance.webView.loadUrl("javascript:" + js);
  258. }
  259. });
  260. }
  261. @Override
  262. public boolean execute(final String action, final JSONArray data,
  263. final CallbackContext callbackContext) throws JSONException {
  264. if (!methodList.contains(action)) {
  265. return false;
  266. }
  267. threadPool.execute(new Runnable() {
  268. @Override
  269. public void run() {
  270. try {
  271. Method method = JPushPlugin.class.getDeclaredMethod(action,
  272. JSONArray.class, CallbackContext.class);
  273. method.invoke(JPushPlugin.this, data, callbackContext);
  274. } catch (Exception e) {
  275. Log.e(TAG, e.toString());
  276. }
  277. }
  278. });
  279. return true;
  280. }
  281. void init(JSONArray data, CallbackContext callbackContext) {
  282. JPushInterface.init(this.cordova.getActivity().getApplicationContext());
  283. }
  284. void setDebugMode(JSONArray data, CallbackContext callbackContext) {
  285. boolean mode;
  286. try {
  287. mode = data.getBoolean(0);
  288. JPushInterface.setDebugMode(mode);
  289. callbackContext.success();
  290. } catch (JSONException e) {
  291. e.printStackTrace();
  292. }
  293. }
  294. void stopPush(JSONArray data, CallbackContext callbackContext) {
  295. JPushInterface.stopPush(this.cordova.getActivity().getApplicationContext());
  296. callbackContext.success();
  297. }
  298. void resumePush(JSONArray data, CallbackContext callbackContext) {
  299. JPushInterface.resumePush(this.cordova.getActivity().getApplicationContext());
  300. callbackContext.success();
  301. }
  302. void isPushStopped(JSONArray data, CallbackContext callbackContext) {
  303. boolean isStopped = JPushInterface.isPushStopped(
  304. this.cordova.getActivity().getApplicationContext());
  305. if (isStopped) {
  306. callbackContext.success(1);
  307. } else {
  308. callbackContext.success(0);
  309. }
  310. }
  311. void areNotificationEnabled(JSONArray data, final CallbackContext callback) {
  312. int isEnabled;
  313. if (hasPermission("OP_POST_NOTIFICATION")) {
  314. isEnabled = 1;
  315. } else {
  316. isEnabled = 0;
  317. }
  318. callback.success(isEnabled);
  319. }
  320. void setLatestNotificationNum(JSONArray data, CallbackContext callbackContext) {
  321. int num = -1;
  322. try {
  323. num = data.getInt(0);
  324. } catch (JSONException e) {
  325. e.printStackTrace();
  326. callbackContext.error("error reading num json");
  327. }
  328. if (num != -1) {
  329. JPushInterface.setLatestNotificationNumber(
  330. this.cordova.getActivity().getApplicationContext(), num);
  331. } else {
  332. callbackContext.error("error num");
  333. }
  334. }
  335. void setPushTime(JSONArray data, CallbackContext callbackContext) {
  336. Set<Integer> days = new HashSet<Integer>();
  337. JSONArray dayArray;
  338. int startHour = -1;
  339. int endHour = -1;
  340. try {
  341. dayArray = data.getJSONArray(0);
  342. for (int i = 0; i < dayArray.length(); i++) {
  343. days.add(dayArray.getInt(i));
  344. }
  345. } catch (JSONException e) {
  346. e.printStackTrace();
  347. callbackContext.error("error reading days json");
  348. }
  349. try {
  350. startHour = data.getInt(1);
  351. endHour = data.getInt(2);
  352. } catch (JSONException e) {
  353. callbackContext.error("error reading hour json");
  354. }
  355. Context context = this.cordova.getActivity().getApplicationContext();
  356. JPushInterface.setPushTime(context, days, startHour, endHour);
  357. callbackContext.success();
  358. }
  359. void getRegistrationID(JSONArray data, CallbackContext callbackContext) {
  360. Context context = this.cordova.getActivity().getApplicationContext();
  361. String regID = JPushInterface.getRegistrationID(context);
  362. callbackContext.success(regID);
  363. }
  364. void onResume(JSONArray data, CallbackContext callbackContext) {
  365. JPushInterface.onResume(this.cordova.getActivity());
  366. }
  367. void onPause(JSONArray data, CallbackContext callbackContext) {
  368. JPushInterface.onPause(this.cordova.getActivity());
  369. }
  370. void reportNotificationOpened(JSONArray data, CallbackContext callbackContext) {
  371. try {
  372. String msgID;
  373. msgID = data.getString(0);
  374. JPushInterface.reportNotificationOpened(this.cordova.getActivity(), msgID);
  375. } catch (JSONException e) {
  376. e.printStackTrace();
  377. }
  378. }
  379. void setTags(JSONArray data, CallbackContext callbackContext) {
  380. try {
  381. HashSet<String> tags = new HashSet<String>();
  382. for (int i = 0; i < data.length(); i++) {
  383. tags.add(data.getString(i));
  384. }
  385. JPushInterface.setTags(this.cordova.getActivity().getApplicationContext(),
  386. tags, mTagWithAliasCallback);
  387. callbackContext.success();
  388. } catch (JSONException e) {
  389. e.printStackTrace();
  390. callbackContext.error("Error reading tags JSON");
  391. }
  392. }
  393. void setAlias(JSONArray data, CallbackContext callbackContext) {
  394. try {
  395. String alias = data.getString(0);
  396. JPushInterface.setAlias(this.cordova.getActivity().getApplicationContext(),
  397. alias, mTagWithAliasCallback);
  398. callbackContext.success();
  399. } catch (JSONException e) {
  400. e.printStackTrace();
  401. callbackContext.error("Error reading alias JSON");
  402. }
  403. }
  404. void setTagsWithAlias(JSONArray data, CallbackContext callbackContext) {
  405. HashSet<String> tags = new HashSet<String>();
  406. String alias;
  407. try {
  408. alias = data.getString(0);
  409. JSONArray tagsArray = data.getJSONArray(1);
  410. for (int i = 0; i < tagsArray.length(); i++) {
  411. tags.add(tagsArray.getString(i));
  412. }
  413. JPushInterface.setAliasAndTags(this.cordova.getActivity().getApplicationContext(),
  414. alias, tags, mTagWithAliasCallback);
  415. callbackContext.success();
  416. } catch (JSONException e) {
  417. e.printStackTrace();
  418. callbackContext.error("Error reading tagAlias JSON");
  419. }
  420. }
  421. void getConnectionState(JSONArray data, CallbackContext callback) {
  422. boolean isConnected = JPushInterface.getConnectionState(cordovaActivity.getApplicationContext());
  423. callback.success(String.valueOf(isConnected));
  424. }
  425. /**
  426. * 自定义通知行为,声音、震动、呼吸灯等。
  427. */
  428. void setBasicPushNotificationBuilder(JSONArray data,
  429. CallbackContext callbackContext) {
  430. BasicPushNotificationBuilder builder = new BasicPushNotificationBuilder(
  431. this.cordova.getActivity());
  432. builder.developerArg0 = "Basic builder 1";
  433. JPushInterface.setPushNotificationBuilder(1, builder);
  434. JSONObject obj = new JSONObject();
  435. try {
  436. obj.put("id", 1);
  437. } catch (JSONException e) {
  438. e.printStackTrace();
  439. }
  440. }
  441. /**
  442. * 自定义推送通知栏样式,需要自己实现具体代码。
  443. * http://docs.jiguang.cn/client/android_tutorials/#_11
  444. */
  445. void setCustomPushNotificationBuilder(JSONArray data,
  446. CallbackContext callbackContext) {
  447. // CustomPushNotificationBuilder builder = new CustomPushNotificationBuilder(
  448. // this.cordova.getActivity(), R.layout.test_notification_layout,
  449. // R.id.icon, R.id.title, R.id.text);
  450. // JPushInterface.setPushNotificationBuilder(2, builder);
  451. // JPushInterface.setDefaultPushNotificationBuilder(builder);
  452. }
  453. void clearAllNotification(JSONArray data, CallbackContext callbackContext) {
  454. JPushInterface.clearAllNotifications(this.cordova.getActivity());
  455. }
  456. void clearNotificationById(JSONArray data, CallbackContext callbackContext) {
  457. int notificationId = -1;
  458. try {
  459. notificationId = data.getInt(0);
  460. } catch (JSONException e) {
  461. e.printStackTrace();
  462. callbackContext.error("error reading id json");
  463. return;
  464. }
  465. if (notificationId != -1) {
  466. JPushInterface.clearNotificationById(this.cordova.getActivity(), notificationId);
  467. } else {
  468. callbackContext.error("error id");
  469. }
  470. }
  471. void addLocalNotification(JSONArray data, CallbackContext callbackContext)
  472. throws JSONException {
  473. int builderId = data.getInt(0);
  474. String content = data.getString(1);
  475. String title = data.getString(2);
  476. int notificationID = data.getInt(3);
  477. int broadcastTime = data.getInt(4);
  478. String extrasStr = data.isNull(5) ? "" : data.getString(5);
  479. JSONObject extras = new JSONObject();
  480. if (!extrasStr.isEmpty()) {
  481. extras = new JSONObject(extrasStr);
  482. }
  483. JPushLocalNotification ln = new JPushLocalNotification();
  484. ln.setBuilderId(builderId);
  485. ln.setContent(content);
  486. ln.setTitle(title);
  487. ln.setNotificationId(notificationID);
  488. ln.setBroadcastTime(System.currentTimeMillis() + broadcastTime);
  489. ln.setExtras(extras.toString());
  490. JPushInterface.addLocalNotification(this.cordova.getActivity(), ln);
  491. }
  492. void removeLocalNotification(JSONArray data, CallbackContext callbackContext)
  493. throws JSONException {
  494. int notificationID = data.getInt(0);
  495. JPushInterface.removeLocalNotification(this.cordova.getActivity(), notificationID);
  496. }
  497. void clearLocalNotifications(JSONArray data, CallbackContext callbackContext) {
  498. JPushInterface.clearLocalNotifications(this.cordova.getActivity());
  499. }
  500. /**
  501. * 决定是否启用统计分析功能。
  502. */
  503. void setStatisticsOpen(JSONArray data, CallbackContext callbackContext) {
  504. try {
  505. isStatisticsOpened = data.getBoolean(0);
  506. } catch (JSONException e) {
  507. e.printStackTrace();
  508. }
  509. }
  510. /**
  511. * 设置通知静默时间
  512. * http://docs.jpush.io/client/android_api/#api_5
  513. */
  514. void setSilenceTime(JSONArray data, CallbackContext callbackContext) {
  515. try {
  516. int startHour = data.getInt(0);
  517. int startMinute = data.getInt(1);
  518. int endHour = data.getInt(2);
  519. int endMinute = data.getInt(3);
  520. if (!isValidHour(startHour) || !isValidMinute(startMinute)) {
  521. callbackContext.error("开始时间数值错误");
  522. return;
  523. }
  524. if (!isValidHour(endHour) || !isValidMinute(endMinute)) {
  525. callbackContext.error("结束时间数值错误");
  526. return;
  527. }
  528. JPushInterface.setSilenceTime(this.cordova.getActivity(), startHour, startMinute,
  529. endHour, endMinute);
  530. } catch (JSONException e) {
  531. e.printStackTrace();
  532. callbackContext.error("error: reading json data.");
  533. }
  534. }
  535. private boolean isValidHour(int hour) {
  536. return !(hour < 0 || hour > 23);
  537. }
  538. private boolean isValidMinute(int minute) {
  539. return !(minute < 0 || minute > 59);
  540. }
  541. /**
  542. * 用于 Android 6.0 以上系统申请权限,具体可参考:
  543. * http://docs.Push.io/client/android_api/#android-60
  544. */
  545. void requestPermission(JSONArray data, CallbackContext callbackContext) {
  546. JPushInterface.requestPermission(this.cordova.getActivity());
  547. }
  548. private final TagAliasCallback mTagWithAliasCallback = new TagAliasCallback() {
  549. @Override
  550. public void gotResult(int code, String alias, Set<String> tags) {
  551. if (instance == null) {
  552. return;
  553. }
  554. JSONObject data = new JSONObject();
  555. try {
  556. data.put("resultCode", code);
  557. data.put("tags", tags);
  558. data.put("alias", alias);
  559. final String jsEvent = String.format(
  560. "cordova.fireDocumentEvent('jpush.setTagsWithAlias',%s)",
  561. data.toString());
  562. cordova.getActivity().runOnUiThread(new Runnable() {
  563. @Override
  564. public void run() {
  565. instance.webView.loadUrl("javascript:" + jsEvent);
  566. }
  567. });
  568. } catch (JSONException e) {
  569. e.printStackTrace();
  570. }
  571. }
  572. };
  573. @TargetApi(Build.VERSION_CODES.KITKAT)
  574. private boolean hasPermission(String appOpsServiceId) {
  575. Context context = cordova.getActivity().getApplicationContext();
  576. AppOpsManager mAppOps = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
  577. ApplicationInfo appInfo = context.getApplicationInfo();
  578. String pkg = context.getPackageName();
  579. int uid = appInfo.uid;
  580. Class appOpsClazz = null;
  581. try {
  582. appOpsClazz = Class.forName(AppOpsManager.class.getName());
  583. Method checkOpNoThrowMethod = appOpsClazz.getMethod("checkOpNoThrow",
  584. Integer.TYPE, Integer.TYPE, String.class);
  585. Field opValue = appOpsClazz.getDeclaredField(appOpsServiceId);
  586. int value = opValue.getInt(Integer.class);
  587. Object result = checkOpNoThrowMethod.invoke(mAppOps, value, uid, pkg);
  588. return Integer.parseInt(result.toString()) == AppOpsManager.MODE_ALLOWED;
  589. } catch (InvocationTargetException e) {
  590. e.printStackTrace();
  591. } catch (IllegalAccessException e) {
  592. e.printStackTrace();
  593. } catch (NoSuchMethodException e) {
  594. e.printStackTrace();
  595. } catch (NoSuchFieldException e) {
  596. e.printStackTrace();
  597. } catch (ClassNotFoundException e) {
  598. e.printStackTrace();
  599. }
  600. return true;
  601. }
  602. }