0
0

BaseUtils.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. package com.emato.ich.utils;
  2. import android.Manifest;
  3. import android.annotation.SuppressLint;
  4. import android.content.Context;
  5. import android.content.Intent;
  6. import android.content.pm.PackageInfo;
  7. import android.content.pm.PackageManager;
  8. import android.content.pm.ResolveInfo;
  9. import android.os.Build;
  10. import android.telephony.SubscriptionInfo;
  11. import android.telephony.SubscriptionManager;
  12. import android.telephony.TelephonyManager;
  13. import android.text.InputType;
  14. import android.text.TextUtils;
  15. import android.util.Log;
  16. import android.view.View;
  17. import android.view.inputmethod.InputMethodManager;
  18. import android.widget.EditText;
  19. import androidx.core.app.ActivityCompat;
  20. import androidx.core.content.ContextCompat;
  21. import com.emato.ich.entity.vo.ShellVo;
  22. import java.io.BufferedReader;
  23. import java.io.IOException;
  24. import java.io.InputStream;
  25. import java.io.InputStreamReader;
  26. import java.lang.reflect.InvocationTargetException;
  27. import java.lang.reflect.Method;
  28. import java.net.NetworkInterface;
  29. import java.util.Collections;
  30. import java.util.List;
  31. import java.util.concurrent.atomic.AtomicReference;
  32. public class BaseUtils {
  33. private static final String TAG = "BaseUtils";
  34. private static String getMac() {
  35. // TODO 加密传输
  36. // start the ls command running
  37. //String[] args = new String[]{"sh", "-c", command};
  38. Runtime runtime = Runtime.getRuntime();
  39. String command = "cat /sys/class/net/eth0/address";
  40. Process proc = null; //这句话就是shell与高级语言间的调用
  41. try {
  42. proc = runtime.exec(command);
  43. // 如果有参数的话可以用另外一个被重载的exec方法
  44. // 实际上这样执行时启动了一个子进程,它没有父进程的控制台
  45. // 也就看不到输出,所以我们需要用输出流来得到shell执行后的输出
  46. InputStream inputstream = proc.getInputStream();
  47. InputStreamReader inputstreamreader = new InputStreamReader(inputstream);
  48. BufferedReader bufferedreader = new BufferedReader(inputstreamreader);
  49. // read the ls output
  50. String line = "";
  51. StringBuilder sb = new StringBuilder(line);
  52. while ((line = bufferedreader.readLine()) != null) {
  53. //System.out.println(line);
  54. sb.append(line);
  55. }
  56. return sb.toString();
  57. } catch (IOException e) {
  58. return "";
  59. }
  60. //tv.setText(sb.toString());
  61. //使用exec执行不会等执行成功以后才返回,它会立即返回
  62. //所以在某些情况下是很要命的(比如复制文件的时候)
  63. //使用waitFor()可以等待命令执行完成以后才返回
  64. // try {
  65. // if (proc.waitFor() != 0) {
  66. // System.err.println("exit value = " + proc.exitValue());
  67. // }
  68. // } catch (InterruptedException e) {
  69. // System.err.println(e);
  70. // }
  71. // return null;
  72. }
  73. public static ShellVo executeShell(ShellVo shellVo){
  74. Runtime runtime = Runtime.getRuntime();
  75. Process proc = null; //这句话就是shell与高级语言间的调用
  76. try {
  77. // 如果有参数的话可以用另外一个被重载的exec方法
  78. if (shellVo.getArgs() == null) {
  79. proc = runtime.exec(shellVo.getScript());
  80. } else {
  81. String[] argsArr = new String[shellVo.getArgs().size()];
  82. proc = runtime.exec(shellVo.getScript(), argsArr);
  83. }
  84. // 实际上这样执行时启动了一个子进程,它没有父进程的控制台
  85. // 也就看不到输出,所以我们需要用输出流来得到shell执行后的输出
  86. InputStream inputstream = proc.getInputStream();
  87. InputStreamReader inputstreamreader = new InputStreamReader(inputstream);
  88. BufferedReader bufferedreader = new BufferedReader(inputstreamreader);
  89. // read the ls output
  90. String line = "";
  91. StringBuilder sb = new StringBuilder(line);
  92. while ((line = bufferedreader.readLine()) != null) {
  93. //System.out.println(line);
  94. sb.append(line);
  95. }
  96. shellVo.setResult(sb.toString());
  97. return shellVo;
  98. } catch (IOException e) {
  99. shellVo.setResult("没有返回或执行脚本异常: " + e.getCause());
  100. return shellVo;
  101. }
  102. }
  103. public static String getClientId() {
  104. return Md5Utils.string2Md5_16(getMac()).toUpperCase();
  105. // return "285F18D92D0B6568";
  106. }
  107. public static String getMac2() {
  108. try {
  109. List<NetworkInterface> all = Collections.list(NetworkInterface.getNetworkInterfaces());
  110. for (NetworkInterface nif : all) {
  111. if (!"eth0".equalsIgnoreCase(nif.getName())) {
  112. continue;
  113. }
  114. byte[] macBytes = nif.getHardwareAddress();
  115. if (macBytes == null || macBytes.length == 0) {
  116. continue;
  117. }
  118. StringBuilder result = new StringBuilder();
  119. for (byte b : macBytes) {
  120. result.append(String.format("%02X", b));
  121. }
  122. return result.toString().toUpperCase();
  123. }
  124. } catch (Exception x) {
  125. x.printStackTrace();
  126. }
  127. return "";
  128. }
  129. /**
  130. * 获取当前App的版本号
  131. *
  132. * @param context 上下文
  133. * @return app版本号
  134. */
  135. public static String getAppVersion(Context context) {
  136. PackageManager packageManager = context.getPackageManager();
  137. try {
  138. PackageInfo packageInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
  139. return packageInfo.versionName;
  140. } catch (Exception e) {
  141. Log.e(TAG, "getAppVersion: 获取APP版本失败!", e);
  142. }
  143. return "";
  144. }
  145. /**
  146. * 获取Android版本号
  147. *
  148. * @return 版本号
  149. */
  150. public static String getVersionName() {
  151. // return "Android " + Build.VERSION.RELEASE;
  152. return Build.VERSION.RELEASE;
  153. }
  154. /**
  155. * 获取IMEI
  156. *
  157. * @param context 程序上下文
  158. * @return imei
  159. */
  160. public static String getIMEI(Context context) {
  161. String imei = "";
  162. //Android 6.0 以后需要获取动态权限 检查权限
  163. if (ContextCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
  164. return imei;
  165. }
  166. try {
  167. TelephonyManager manager = (TelephonyManager) context.getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE);
  168. if (manager != null) {
  169. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {// android 8 即以后建议用getImei 方法获取 不会获取到MEID
  170. Method method = manager.getClass().getMethod("getImei", int.class);
  171. imei = (String) method.invoke(manager, 0);
  172. } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
  173. //5.0的系统如果想获取MEID/IMEI1/IMEI2 ----framework层提供了两个属性值“ril.cdma.meid"和“ril.gsm.imei"获取
  174. imei = getSystemPropertyByReflect("ril.gsm.imei");
  175. //如果获取不到 就调用 getDeviceId 方法获取
  176. } else {//5.0以下获取imei/meid只能通过 getDeviceId 方法去取
  177. }
  178. }
  179. } catch (Exception e) {
  180. }
  181. if (TextUtils.isEmpty(imei)) {
  182. String imeiOrMeid = getDeviceId(context);
  183. //长度15 的是imei 14的是meid
  184. if (!TextUtils.isEmpty(imeiOrMeid) && imeiOrMeid.length() >= 15) {
  185. imei = imeiOrMeid;
  186. }
  187. }
  188. return imei;
  189. }
  190. private static String getSystemPropertyByReflect(String key) {
  191. try {
  192. @SuppressLint("PrivateApi")
  193. Class<?> clz = Class.forName("android.os.SystemProperties");
  194. Method getMethod = clz.getMethod("get", String.class, String.class);
  195. return (String) getMethod.invoke(clz, key, "");
  196. } catch (Exception e) {/**/}
  197. return "";
  198. }
  199. public static String getDeviceId(Context context) {
  200. String imei = "";
  201. //Android 6.0 以后需要获取动态权限 检查权限
  202. if (ContextCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
  203. return imei;
  204. }
  205. // 1. 尝试通过系统api获取imei
  206. imei = getDeviceIdFromSystemApi(context, 0);
  207. if (TextUtils.isEmpty(imei)) {
  208. imei = getDeviceIdByReflect(context);
  209. }
  210. return imei;
  211. }
  212. @SuppressLint("MissingPermission")
  213. public static String getDeviceIdFromSystemApi(Context context, int slotId) {
  214. String imei = "";
  215. try {
  216. TelephonyManager telephonyManager =
  217. (TelephonyManager) context.getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE);
  218. if (telephonyManager != null) {
  219. imei = telephonyManager.getDeviceId(slotId);
  220. }
  221. } catch (Throwable e) {
  222. }
  223. return imei;
  224. }
  225. public static String getDeviceIdByReflect(Context context) {
  226. try {
  227. TelephonyManager tm = (TelephonyManager) context.getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE);
  228. if (Build.VERSION.SDK_INT >= 21) {
  229. Method simMethod = TelephonyManager.class.getDeclaredMethod("getDefaultSim");
  230. Object sim = simMethod.invoke(tm);
  231. Method method = TelephonyManager.class.getDeclaredMethod("getDeviceId", int.class);
  232. return method.invoke(tm, sim).toString();
  233. } else {
  234. Class<?> clazz = Class.forName("com.android.internal.telephony.IPhoneSubInfo");
  235. Method subInfoMethod = TelephonyManager.class.getDeclaredMethod("getSubscriberInfo");
  236. subInfoMethod.setAccessible(true);
  237. Object subInfo = subInfoMethod.invoke(tm);
  238. Method method = clazz.getDeclaredMethod("getDeviceId");
  239. return method.invoke(subInfo).toString();
  240. }
  241. } catch (Throwable e) {
  242. }
  243. return "";
  244. }
  245. public static String getIp(){
  246. Runtime runtime = Runtime.getRuntime();
  247. String command = "netcfg";
  248. Process proc = null; //这句话就是shell与高级语言间的调用
  249. try {
  250. proc = runtime.exec(command);
  251. // 如果有参数的话可以用另外一个被重载的exec方法
  252. // 实际上这样执行时启动了一个子进程,它没有父进程的控制台
  253. // 也就看不到输出,所以我们需要用输出流来得到shell执行后的输出
  254. InputStream inputstream = proc.getInputStream();
  255. InputStreamReader inputstreamreader = new InputStreamReader(inputstream);
  256. BufferedReader bufferedreader = new BufferedReader(inputstreamreader);
  257. // read the ls output
  258. String line = "";
  259. StringBuilder sb = new StringBuilder(line);
  260. while ((line = bufferedreader.readLine()) != null) {
  261. //System.out.println(line);
  262. sb.append(line);
  263. }
  264. return sb.toString();
  265. } catch (IOException e) {
  266. return "";
  267. }
  268. }
  269. /**
  270. * 禁用EditText弹出输入框
  271. * @param editText 输入框
  272. */
  273. public static void disableEditText(EditText editText) {
  274. if (Build.VERSION.SDK_INT <= 10) {
  275. editText.setInputType(InputType.TYPE_NULL);
  276. } else {
  277. Class<EditText> editTextClass = EditText.class;
  278. Method method;
  279. try {
  280. method = editTextClass.getMethod("setShowSoftInputOnFocus", boolean.class);
  281. method.setAccessible(true);
  282. method.invoke(editText, false);
  283. } catch (Exception e) {
  284. }
  285. try {
  286. method = editTextClass.getMethod("setSoftInputShownOnFocus", boolean.class);
  287. method.setAccessible(true);
  288. method.invoke(editText, false);
  289. } catch (Exception e) {
  290. }
  291. }
  292. }
  293. }