BaseUtils.java 12 KB

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