1
0

BaseUtils.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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. sb.append("\n");
  96. }
  97. shellVo.setResult(sb.toString());
  98. return shellVo;
  99. } catch (IOException e) {
  100. shellVo.setResult("没有返回或执行脚本异常: " + e.getCause());
  101. return shellVo;
  102. }
  103. }
  104. public static String getClientId() {
  105. return Md5Utils.string2Md5_16(getMac()).toUpperCase();
  106. // return "285F18D92D0B6568";// 8F00B204E9800998
  107. }
  108. public static String getMac2() {
  109. try {
  110. List<NetworkInterface> all = Collections.list(NetworkInterface.getNetworkInterfaces());
  111. for (NetworkInterface nif : all) {
  112. if (!"eth0".equalsIgnoreCase(nif.getName())) {
  113. continue;
  114. }
  115. byte[] macBytes = nif.getHardwareAddress();
  116. if (macBytes == null || macBytes.length == 0) {
  117. continue;
  118. }
  119. StringBuilder result = new StringBuilder();
  120. for (byte b : macBytes) {
  121. result.append(String.format("%02X", b));
  122. }
  123. return result.toString().toUpperCase();
  124. }
  125. } catch (Exception x) {
  126. x.printStackTrace();
  127. }
  128. return "";
  129. }
  130. /**
  131. * 获取当前App的版本号
  132. *
  133. * @param context 上下文
  134. * @return app版本号
  135. */
  136. public static String getAppVersion(Context context) {
  137. PackageManager packageManager = context.getPackageManager();
  138. try {
  139. PackageInfo packageInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
  140. return packageInfo.versionName;
  141. } catch (Exception e) {
  142. Log.e(TAG, "getAppVersion: 获取APP版本失败!", e);
  143. LoggingUtils.sendErrorLog("业务异常: 获取APP版本失败! ", e);
  144. }
  145. return "";
  146. }
  147. /**
  148. * 获取Android版本号
  149. *
  150. * @return 版本号
  151. */
  152. public static String getVersionName() {
  153. // return "Android " + Build.VERSION.RELEASE;
  154. return Build.VERSION.RELEASE;
  155. }
  156. /**
  157. * 获取IMEI
  158. *
  159. * @param context 程序上下文
  160. * @return imei
  161. */
  162. public static String getIMEI(Context context) {
  163. String imei = "";
  164. //Android 6.0 以后需要获取动态权限 检查权限
  165. if (ContextCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
  166. return imei;
  167. }
  168. try {
  169. TelephonyManager manager = (TelephonyManager) context.getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE);
  170. if (manager != null) {
  171. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {// android 8 即以后建议用getImei 方法获取 不会获取到MEID
  172. Method method = manager.getClass().getMethod("getImei", int.class);
  173. imei = (String) method.invoke(manager, 0);
  174. } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
  175. //5.0的系统如果想获取MEID/IMEI1/IMEI2 ----framework层提供了两个属性值“ril.cdma.meid"和“ril.gsm.imei"获取
  176. imei = getSystemPropertyByReflect("ril.gsm.imei");
  177. //如果获取不到 就调用 getDeviceId 方法获取
  178. } else {//5.0以下获取imei/meid只能通过 getDeviceId 方法去取
  179. }
  180. }
  181. } catch (Exception e) {
  182. }
  183. if (TextUtils.isEmpty(imei)) {
  184. String imeiOrMeid = getDeviceId(context);
  185. //长度15 的是imei 14的是meid
  186. if (!TextUtils.isEmpty(imeiOrMeid) && imeiOrMeid.length() >= 15) {
  187. imei = imeiOrMeid;
  188. }
  189. }
  190. return imei;
  191. }
  192. private static String getSystemPropertyByReflect(String key) {
  193. try {
  194. @SuppressLint("PrivateApi")
  195. Class<?> clz = Class.forName("android.os.SystemProperties");
  196. Method getMethod = clz.getMethod("get", String.class, String.class);
  197. return (String) getMethod.invoke(clz, key, "");
  198. } catch (Exception e) {/**/}
  199. return "";
  200. }
  201. public static String getDeviceId(Context context) {
  202. String imei = "";
  203. //Android 6.0 以后需要获取动态权限 检查权限
  204. if (ContextCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
  205. return imei;
  206. }
  207. // 1. 尝试通过系统api获取imei
  208. imei = getDeviceIdFromSystemApi(context, 0);
  209. if (TextUtils.isEmpty(imei)) {
  210. imei = getDeviceIdByReflect(context);
  211. }
  212. return imei;
  213. }
  214. @SuppressLint("MissingPermission")
  215. public static String getDeviceIdFromSystemApi(Context context, int slotId) {
  216. String imei = "";
  217. try {
  218. TelephonyManager telephonyManager =
  219. (TelephonyManager) context.getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE);
  220. if (telephonyManager != null) {
  221. imei = telephonyManager.getDeviceId(slotId);
  222. }
  223. } catch (Throwable e) {
  224. }
  225. return imei;
  226. }
  227. public static String getDeviceIdByReflect(Context context) {
  228. try {
  229. TelephonyManager tm = (TelephonyManager) context.getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE);
  230. if (Build.VERSION.SDK_INT >= 21) {
  231. Method simMethod = TelephonyManager.class.getDeclaredMethod("getDefaultSim");
  232. Object sim = simMethod.invoke(tm);
  233. Method method = TelephonyManager.class.getDeclaredMethod("getDeviceId", int.class);
  234. return method.invoke(tm, sim).toString();
  235. } else {
  236. Class<?> clazz = Class.forName("com.android.internal.telephony.IPhoneSubInfo");
  237. Method subInfoMethod = TelephonyManager.class.getDeclaredMethod("getSubscriberInfo");
  238. subInfoMethod.setAccessible(true);
  239. Object subInfo = subInfoMethod.invoke(tm);
  240. Method method = clazz.getDeclaredMethod("getDeviceId");
  241. return method.invoke(subInfo).toString();
  242. }
  243. } catch (Throwable e) {
  244. }
  245. return "";
  246. }
  247. public static String getIp(){
  248. Runtime runtime = Runtime.getRuntime();
  249. String command = "netcfg";
  250. Process proc = null; //这句话就是shell与高级语言间的调用
  251. try {
  252. proc = runtime.exec(command);
  253. // 如果有参数的话可以用另外一个被重载的exec方法
  254. // 实际上这样执行时启动了一个子进程,它没有父进程的控制台
  255. // 也就看不到输出,所以我们需要用输出流来得到shell执行后的输出
  256. InputStream inputstream = proc.getInputStream();
  257. InputStreamReader inputstreamreader = new InputStreamReader(inputstream);
  258. BufferedReader bufferedreader = new BufferedReader(inputstreamreader);
  259. // read the ls output
  260. String line = "";
  261. StringBuilder sb = new StringBuilder(line);
  262. while ((line = bufferedreader.readLine()) != null) {
  263. //System.out.println(line);
  264. sb.append(line);
  265. }
  266. return sb.toString();
  267. } catch (IOException e) {
  268. return "";
  269. }
  270. }
  271. /**
  272. * 禁用EditText弹出输入框
  273. * @param editText 输入框
  274. */
  275. public static void disableEditText(EditText editText) {
  276. if (Build.VERSION.SDK_INT <= 10) {
  277. editText.setInputType(InputType.TYPE_NULL);
  278. } else {
  279. Class<EditText> editTextClass = EditText.class;
  280. Method method;
  281. try {
  282. method = editTextClass.getMethod("setShowSoftInputOnFocus", boolean.class);
  283. method.setAccessible(true);
  284. method.invoke(editText, false);
  285. } catch (Exception e) {
  286. }
  287. try {
  288. method = editTextClass.getMethod("setSoftInputShownOnFocus", boolean.class);
  289. method.setAccessible(true);
  290. method.invoke(editText, false);
  291. } catch (Exception e) {
  292. }
  293. }
  294. }
  295. }