MDMApi.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. package com.action.actionmdm;
  2. import android.content.ContentResolver;
  3. import android.content.Context;
  4. import android.content.Intent;
  5. import android.net.Uri;
  6. import android.os.Build;
  7. import android.os.Bundle;
  8. import android.support.v4.content.FileProvider;
  9. import android.text.TextUtils;
  10. import android.util.Log;
  11. import java.io.File;
  12. import java.util.Arrays;
  13. import java.util.Iterator;
  14. import java.util.List;
  15. public class MDMApi {
  16. private static final String TAG = "act-mdm";
  17. private Context mContext;
  18. private ContentResolver resolver = null;
  19. private static final Uri ACTION_MDM_URI = Uri.parse("content://com.action.mdm_provider");
  20. private static final String GET_MEID_IMEI = "get_meid_imei";
  21. private static final String GET_DISABLE_INSTALL_PKGS = "get_disable_install_pkgs";
  22. private static final String SET_DISABLE_INSTALL_PKGS = "set_disable_install_pkgs";
  23. private static final String IS_DISABLE_INSTALL_ALL_APP = "is_disable_install_all_app";
  24. private static final String SET_DISABLE_INSTALL_ALL_APP = "set_disable_install_all_app";
  25. private static final String IS_DISABLED_STATUSBAR = "is_disabled_statusbar";
  26. private static final String SET_DISABLED_STATUSBAR = "set_disabled_statusbar";
  27. private static final String GET_NOT_DISPLAY_DESKTOP_ICONS = "get_not_display_desktop_icons";
  28. private static final String SET_NOT_DISPLAY_DESKTOP_ICONS = "set_not_display_desktop_icons";
  29. private static final String UNINSTALLER_APP = "uninstaller_app";
  30. private static final String UNINSTALLER_PKG = "uninstaller_pkg";
  31. private static final String UNINSTALLER_ALLUSERS = "uninstaller_allusers";
  32. private static final String UNINSTALLER_SILENCE = "uninstaller_pkg_silence";
  33. private static final String SET_TIME = "set_time";
  34. private static final String REBOOT_DEVICE_KEY = "reboot_device";
  35. private static final String SHUTDOWN_DEVICE_KEY = "shutdown_device";
  36. private static final String SET_TRICOLOR_LAMP = "set_tricolor_lamp";
  37. private static final String DEFAULT_OPEN = "default_open";
  38. public MDMApi(Context context) {
  39. this.mContext = context;
  40. this.resolver = this.mContext.getContentResolver();
  41. }
  42. public List<String> getMeidAndImei() {
  43. List<String> res = null;
  44. String meidImei = null;
  45. if (this.resolver == null) {
  46. this.resolver = this.mContext.getContentResolver();
  47. }
  48. if (this.resolver.acquireContentProviderClient(ACTION_MDM_URI) != null) {
  49. Log.d("act-mdm", "find provider " + ACTION_MDM_URI);
  50. Bundle extras = new Bundle();
  51. extras = this.resolver.call(ACTION_MDM_URI, "get_meid_imei", (String) null, extras);
  52. if (extras != null) {
  53. meidImei = extras.getString("get_meid_imei", (String) null);
  54. }
  55. if (!TextUtils.isEmpty(meidImei)) {
  56. String[] ids = meidImei.split(",");
  57. res = Arrays.asList(ids);
  58. }
  59. } else {
  60. Log.d("act-mdm", "Could Not Find Provider " + ACTION_MDM_URI);
  61. }
  62. return res;
  63. }
  64. public List<String> getDisableInstallPkgs() {
  65. List<String> res = null;
  66. String pkgs = null;
  67. if (this.resolver == null) {
  68. this.resolver = this.mContext.getContentResolver();
  69. }
  70. if (this.resolver.acquireContentProviderClient(ACTION_MDM_URI) != null) {
  71. Log.d("act-mdm", "find provider " + ACTION_MDM_URI);
  72. Bundle extras = new Bundle();
  73. extras = this.resolver.call(ACTION_MDM_URI, "get_disable_install_pkgs", (String) null, extras);
  74. if (extras != null) {
  75. pkgs = extras.getString("get_disable_install_pkgs", (String) null);
  76. }
  77. if (!TextUtils.isEmpty(pkgs)) {
  78. String[] pkgArray = pkgs.split(",");
  79. res = Arrays.asList(pkgArray);
  80. }
  81. } else {
  82. Log.d("act-mdm", "Could Not Find Provider " + ACTION_MDM_URI);
  83. }
  84. return res;
  85. }
  86. public void setDisableInstallPkgs(List<String> pkgList) {
  87. if (pkgList != null && pkgList.size() > 0) {
  88. if (this.resolver == null) {
  89. this.resolver = this.mContext.getContentResolver();
  90. }
  91. String pkgs = "";
  92. String pkg;
  93. for (Iterator i$ = pkgList.iterator(); i$.hasNext(); pkgs = pkgs + pkg + ",") {
  94. pkg = (String) i$.next();
  95. }
  96. int len = pkgs.length();
  97. pkgs = pkgs.substring(0, len - 1);
  98. if (this.resolver.acquireContentProviderClient(ACTION_MDM_URI) != null) {
  99. Log.d("act-mdm", "find provider " + ACTION_MDM_URI);
  100. Bundle extras = new Bundle();
  101. extras.putString("set_disable_install_pkgs", pkgs);
  102. this.resolver.call(ACTION_MDM_URI, "set_disable_install_pkgs", (String) null, extras);
  103. } else {
  104. Log.d("act-mdm", "Could Not Find Provider " + ACTION_MDM_URI);
  105. }
  106. }
  107. }
  108. public boolean isDisableInstallAllApk() {
  109. boolean isDisable = false;
  110. if (this.resolver == null) {
  111. this.resolver = this.mContext.getContentResolver();
  112. }
  113. if (this.resolver.acquireContentProviderClient(ACTION_MDM_URI) != null) {
  114. Log.d("act-mdm", "find provider " + ACTION_MDM_URI);
  115. Bundle extras = new Bundle();
  116. extras = this.resolver.call(ACTION_MDM_URI, "is_disable_install_all_app", (String) null, extras);
  117. if (extras != null) {
  118. isDisable = extras.getBoolean("is_disable_install_all_app", false);
  119. }
  120. } else {
  121. Log.d("act-mdm", "Could Not Find Provider " + ACTION_MDM_URI);
  122. }
  123. return isDisable;
  124. }
  125. public void setDisableInstallAllApk(boolean isDisable) {
  126. if (this.resolver == null) {
  127. this.resolver = this.mContext.getContentResolver();
  128. }
  129. if (this.resolver.acquireContentProviderClient(ACTION_MDM_URI) != null) {
  130. Log.d("act-mdm", "find provider " + ACTION_MDM_URI);
  131. Bundle extras = new Bundle();
  132. extras.putBoolean("set_disable_install_all_app", isDisable);
  133. this.resolver.call(ACTION_MDM_URI, "set_disable_install_all_app", (String) null, extras);
  134. } else {
  135. Log.d("act-mdm", "Could Not Find Provider " + ACTION_MDM_URI);
  136. }
  137. }
  138. public boolean isDisableStatusBar() {
  139. boolean isDisable = false;
  140. if (this.resolver == null) {
  141. this.resolver = this.mContext.getContentResolver();
  142. }
  143. if (this.resolver.acquireContentProviderClient(ACTION_MDM_URI) != null) {
  144. Log.d("act-mdm", "find provider " + ACTION_MDM_URI);
  145. Bundle extras = new Bundle();
  146. extras = this.resolver.call(ACTION_MDM_URI, "is_disabled_statusbar", (String) null, extras);
  147. if (extras != null) {
  148. isDisable = extras.getBoolean("is_disabled_statusbar", false);
  149. }
  150. } else {
  151. Log.d("act-mdm", "Could Not Find Provider " + ACTION_MDM_URI);
  152. }
  153. return isDisable;
  154. }
  155. public void setDisableStatusBar(boolean isDisable) {
  156. if (this.resolver == null) {
  157. this.resolver = this.mContext.getContentResolver();
  158. }
  159. if (this.resolver.acquireContentProviderClient(ACTION_MDM_URI) != null) {
  160. Log.d("act-mdm", "find provider " + ACTION_MDM_URI);
  161. Bundle extras = new Bundle();
  162. extras.putBoolean("set_disabled_statusbar", isDisable);
  163. this.resolver.call(ACTION_MDM_URI, "set_disabled_statusbar", (String) null, extras);
  164. } else {
  165. Log.d("act-mdm", "Could Not Find Provider " + ACTION_MDM_URI);
  166. }
  167. }
  168. public List<String> getNotDisplayDesktopIcons() {
  169. List<String> res = null;
  170. String pkgs = null;
  171. if (this.resolver == null) {
  172. this.resolver = this.mContext.getContentResolver();
  173. }
  174. if (this.resolver.acquireContentProviderClient(ACTION_MDM_URI) != null) {
  175. Log.d("act-mdm", "find provider " + ACTION_MDM_URI);
  176. Bundle extras = new Bundle();
  177. extras = this.resolver.call(ACTION_MDM_URI, "get_not_display_desktop_icons", (String) null, extras);
  178. if (extras != null) {
  179. pkgs = extras.getString("get_not_display_desktop_icons", (String) null);
  180. }
  181. if (!TextUtils.isEmpty(pkgs)) {
  182. String[] pkgArray = pkgs.split(",");
  183. res = Arrays.asList(pkgArray);
  184. }
  185. } else {
  186. Log.d("act-mdm", "Could Not Find Provider " + ACTION_MDM_URI);
  187. }
  188. return res;
  189. }
  190. public void setNotDisplayDesktopIcons(List<String> pkgList) {
  191. if (pkgList != null && pkgList.size() > 0) {
  192. if (this.resolver == null) {
  193. this.resolver = this.mContext.getContentResolver();
  194. }
  195. String pkgs = "";
  196. String pkg;
  197. for (Iterator i$ = pkgList.iterator(); i$.hasNext(); pkgs = pkgs + pkg + ",") {
  198. pkg = (String) i$.next();
  199. }
  200. int len = pkgs.length();
  201. pkgs = pkgs.substring(0, len - 1);
  202. if (this.resolver.acquireContentProviderClient(ACTION_MDM_URI) != null) {
  203. Log.d("act-mdm", "find provider " + ACTION_MDM_URI);
  204. Bundle extras = new Bundle();
  205. extras.putString("set_not_display_desktop_icons", pkgs);
  206. this.resolver.call(ACTION_MDM_URI, "set_not_display_desktop_icons", (String) null, extras);
  207. } else {
  208. Log.d("act-mdm", "Could Not Find Provider " + ACTION_MDM_URI);
  209. }
  210. }
  211. }
  212. public void uninstallApp(String pkg, boolean allUsers, boolean isSilence) {
  213. if (this.resolver == null) {
  214. this.resolver = this.mContext.getContentResolver();
  215. }
  216. if (this.resolver.acquireContentProviderClient(ACTION_MDM_URI) != null) {
  217. Log.d("act-mdm", "find provider " + ACTION_MDM_URI);
  218. Bundle extras = new Bundle();
  219. extras.putString("uninstaller_pkg", pkg);
  220. extras.putBoolean("uninstaller_allusers", allUsers);
  221. extras.putBoolean("uninstaller_pkg_silence", isSilence);
  222. this.resolver.call(ACTION_MDM_URI, "uninstaller_app", (String) null, extras);
  223. } else {
  224. Log.d("act-mdm", "Could Not Find Provider " + ACTION_MDM_URI);
  225. }
  226. }
  227. public void setTime(long timeMillis) {
  228. if (this.resolver == null) {
  229. this.resolver = this.mContext.getContentResolver();
  230. }
  231. if (this.resolver.acquireContentProviderClient(ACTION_MDM_URI) != null) {
  232. Log.d("act-mdm", "find provider " + ACTION_MDM_URI);
  233. Bundle extras = new Bundle();
  234. extras.putLong("set_time", timeMillis);
  235. this.resolver.call(ACTION_MDM_URI, "set_time", (String) null, extras);
  236. } else {
  237. Log.d("act-mdm", "Could Not Find Provider " + ACTION_MDM_URI);
  238. }
  239. }
  240. public void reboot() {
  241. if (this.resolver == null) {
  242. this.resolver = this.mContext.getContentResolver();
  243. }
  244. if (this.resolver.acquireContentProviderClient(ACTION_MDM_URI) != null) {
  245. Log.d("act-mdm", "find provider " + ACTION_MDM_URI);
  246. Bundle extras = new Bundle();
  247. this.resolver.call(ACTION_MDM_URI, "reboot_device", (String) null, extras);
  248. } else {
  249. Log.d("act-mdm", "Could Not Find Provider " + ACTION_MDM_URI);
  250. }
  251. }
  252. public void shutdown() {
  253. if (this.resolver == null) {
  254. this.resolver = this.mContext.getContentResolver();
  255. }
  256. if (this.resolver.acquireContentProviderClient(ACTION_MDM_URI) != null) {
  257. Log.d("act-mdm", "find provider " + ACTION_MDM_URI);
  258. Bundle extras = new Bundle();
  259. this.resolver.call(ACTION_MDM_URI, "shutdown_device", (String) null, extras);
  260. } else {
  261. Log.d("act-mdm", "Could Not Find Provider " + ACTION_MDM_URI);
  262. }
  263. }
  264. public void setTricolor(int value) {
  265. if (this.resolver == null) {
  266. this.resolver = this.mContext.getContentResolver();
  267. }
  268. if (this.resolver.acquireContentProviderClient(ACTION_MDM_URI) != null) {
  269. Log.d("act-mdm", "find provider " + ACTION_MDM_URI);
  270. Bundle extras = new Bundle();
  271. extras.putInt("set_tricolor_lamp", value);
  272. this.resolver.call(ACTION_MDM_URI, "set_tricolor_lamp", (String) null, extras);
  273. } else {
  274. Log.d("act-mdm", "Could Not Find Provider " + ACTION_MDM_URI);
  275. }
  276. }
  277. public void install(String path, boolean runAfterInstall) {
  278. File apkFile = new File(path);
  279. if (!apkFile.exists()) {
  280. return;
  281. }
  282. Intent intent = new Intent();
  283. intent.setAction(Intent.ACTION_VIEW);
  284. intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  285. if (Build.VERSION.SDK_INT >= 24) {
  286. Log.w(TAG, "版本大于 N ,开始使用 fileProvider 进行安装…");
  287. intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
  288. // 安装结束是否打开应用
  289. intent.putExtra(DEFAULT_OPEN, runAfterInstall);
  290. Uri contentUri = FileProvider.getUriForFile(mContext,
  291. "com.wubin.otg.otgmaster.fileprovider", apkFile);
  292. intent.setDataAndType(contentUri,
  293. "application/vnd.android.package-archive-silence");
  294. } else {
  295. Log.w(TAG, "正常进行安装…");
  296. intent.setDataAndType(Uri.fromFile(apkFile),
  297. "application/vnd.android.package-archive-silence");
  298. }
  299. mContext.startActivity(intent);
  300. }
  301. public boolean hasAdbEnable() {
  302. boolean isEnabled = false;
  303. if (this.resolver == null) {
  304. this.resolver = this.mContext.getContentResolver();
  305. }
  306. if (this.resolver.acquireContentProviderClient(ACTION_MDM_URI) != null) {
  307. Log.d("act-mdm", "find provider " + ACTION_MDM_URI);
  308. Bundle extras = new Bundle();
  309. extras = this.resolver.call(ACTION_MDM_URI, "get_adb_state", null, extras);
  310. if (extras != null) {
  311. isEnabled = extras.getBoolean("get_adb_state", false);
  312. }
  313. } else {
  314. Log.d("act-mdm", "Could Not Find Provider " + ACTION_MDM_URI);
  315. }
  316. return isEnabled;
  317. }
  318. public void setAdbEnable(boolean enable) {
  319. if (this.resolver == null) {
  320. this.resolver = this.mContext.getContentResolver();
  321. }
  322. if (this.resolver.acquireContentProviderClient(ACTION_MDM_URI) != null) {
  323. Log.d("act-mdm", "find provider " + ACTION_MDM_URI);
  324. Bundle extras = new Bundle();
  325. extras.putBoolean("set_adb_state", enable);
  326. this.resolver.call(ACTION_MDM_URI, "set_adb_state", null, extras);
  327. } else {
  328. Log.d("act-mdm", "Could Not Find Provider " + ACTION_MDM_URI);
  329. }
  330. }
  331. public boolean hasVirtualKeyEnable(String key) {
  332. boolean isEnabled = false;
  333. if (this.resolver == null) {
  334. this.resolver = this.mContext.getContentResolver();
  335. }
  336. if (this.resolver.acquireContentProviderClient(ACTION_MDM_URI) != null) {
  337. Log.d("act-mdm", "find provider " + ACTION_MDM_URI);
  338. Bundle extras = new Bundle();
  339. extras.putString("get_key_state", key);
  340. extras = this.resolver.call(ACTION_MDM_URI, "get_key_state", null, extras);
  341. if (extras != null) {
  342. isEnabled = extras.getBoolean("get_key_state", false);
  343. }
  344. } else {
  345. Log.d("act-mdm", "Could Not Find Provider " + ACTION_MDM_URI);
  346. }
  347. return !isEnabled;
  348. }
  349. public void setVirtualKeyEnable(String key, boolean enable) {
  350. if (this.resolver == null) {
  351. this.resolver = this.mContext.getContentResolver();
  352. }
  353. if (this.resolver.acquireContentProviderClient(ACTION_MDM_URI) != null) {
  354. Log.d("act-mdm", "find provider " + ACTION_MDM_URI);
  355. Bundle extras = new Bundle();
  356. extras.putString("set_key_state_key", key);
  357. extras.putBoolean("set_key_state_value", !enable);
  358. this.resolver.call(ACTION_MDM_URI, "set_key_state", null, extras);
  359. } else {
  360. Log.d("act-mdm", "Could Not Find Provider " + ACTION_MDM_URI);
  361. }
  362. }
  363. }