BaseUtils.java 12 KB

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