NIMSDK-AOS  9.19.0
NIMUtil.java
浏览该文件的文档.
1 package com.netease.nimlib.sdk.util;
2 
3 import android.app.Application;
4 import android.content.Context;
5 import android.os.Build;
6 import android.os.Environment;
7 import android.text.TextUtils;
8 import android.util.Log;
9 import com.netease.nimlib.log.NimLog;
10 import com.netease.nimlib.log.model.LogDesensitizationConfigHelper;
11 
12 import java.io.BufferedReader;
13 import java.io.FileInputStream;
14 import java.io.IOException;
15 import java.io.InputStreamReader;
16 import java.lang.reflect.Method;
17 import java.util.Collection;
18 
19 /**
20  * 云信SDK工具类
21  */
22 
23 public final class NIMUtil {
24 
25  private static final String TAG = "NIMUtil";
26  private static String currentProcessName = null;
27 
28  private NIMUtil() {
29  }
30 
31  /**
32  * 判断当前进程是否是主进程(纯净版本),不会有隐私不合规风险,但是不保证100%判断正确
33  * @param context
34  * @return >0 表示返回主进程,=0 表示不是进程,<0 表示无法判断
35  */
36  public static int isMainProcessPure(Context context) {
37  if (context == null) {
38  if (LogDesensitizationConfigHelper.printToLogcat()) {
39  Log.d(TAG, " isMainProcessPure context == null");
40  }
41  return -1;
42  }
43 
44  String packageName = context.getApplicationContext().getPackageName();
45  String processName = NIMUtil.getProcessNamePure(context);
46  //无法获取当前进程名,无法判断
47  if(TextUtils.isEmpty(processName))
48  {
49  if (LogDesensitizationConfigHelper.printToLogcat()) {
50  Log.d(TAG, " isMainProcessPure can not get processName");
51  }
52  return -1;
53  }
54  if (LogDesensitizationConfigHelper.printToLogcat()) {
55  Log.d(TAG, " isMainProcessPure packageName = " + packageName + ",processName = " + processName);
56  }
57  return packageName.equals(processName) ? 1 : 0;
58  }
59 
60  /**
61  * 获取当前进程名(纯净版本),不会有隐私不合规风险,但是不保证100%获取到
62  * @param context
63  * @return
64  */
65  public static String getProcessNamePure(Context context) {
66  if(!TextUtils.isEmpty(currentProcessName))
67  {
68  if (LogDesensitizationConfigHelper.printToLogcat()) {
69  Log.d(TAG, "get processName from Cache = " + currentProcessName);
70  }
71  return currentProcessName;
72  }
73  currentProcessName = getCurrentProcessNameByApplication();
74  if(!TextUtils.isEmpty(currentProcessName))
75  {
76  if (LogDesensitizationConfigHelper.printToLogcat()) {
77  Log.d(TAG, "get processName from Application = " + currentProcessName);
78  }
79  return currentProcessName;
80  }
81 
82  currentProcessName = getCurrentProcessNameByActivityThread();
83 
84  if(!TextUtils.isEmpty(currentProcessName))
85  {
86  if (LogDesensitizationConfigHelper.printToLogcat()) {
87  Log.d(TAG, "get processName from ActivityThread = " + currentProcessName);
88  }
89  return currentProcessName;
90  }
91 
92  return currentProcessName;
93  }
94 
95  public static boolean isMainProcess(Context context) {
96  if (context == null) {
97  return false;
98  }
99 
100  String packageName = context.getApplicationContext().getPackageName();
101  String processName = NIMUtil.getProcessName(context);
102  return packageName.equals(processName);
103  }
104 
105  public static String getProcessName(Context context) {
106  if(!TextUtils.isEmpty(currentProcessName))
107  {
108  if (LogDesensitizationConfigHelper.printToLogcat()) {
109  Log.d(TAG, "get processName from Cache = " + currentProcessName);
110  }
111  return currentProcessName;
112  }
113  currentProcessName = getCurrentProcessNameByApplication();
114  if(!TextUtils.isEmpty(currentProcessName))
115  {
116  if (LogDesensitizationConfigHelper.printToLogcat()) {
117  Log.d(TAG, "get processName from Application = " + currentProcessName);
118  }
119  return currentProcessName;
120  }
121 
122  currentProcessName = getCurrentProcessNameByActivityThread();
123 
124  if(!TextUtils.isEmpty(currentProcessName))
125  {
126  if (LogDesensitizationConfigHelper.printToLogcat()) {
127  Log.d(TAG, "get processName from ActivityThread = " + currentProcessName);
128  }
129  return currentProcessName;
130  }
131 
132  currentProcessName = getProcessFromFile();
133 
134  if(!TextUtils.isEmpty(currentProcessName))
135  {
136  if (LogDesensitizationConfigHelper.printToLogcat()) {
137  Log.d(TAG, "get processName from File = " + currentProcessName);
138  }
139  return currentProcessName;
140  }
141 
142 // // 如果装了xposed一类的框架,上面可能会拿不到,回到遍历迭代的方式
143 // currentProcessName = getProcessNameByAM(context);
144  if (LogDesensitizationConfigHelper.printToLogcat()) {
145  Log.d(TAG, "get processName from ActivityManager = " + currentProcessName);
146  }
147  return currentProcessName;
148  }
149 
150  private static String getProcessFromFile() {
151  BufferedReader reader = null;
152  try {
153  int pid = android.os.Process.myPid();
154  String file = "/proc/" + pid + "/cmdline";
155  reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "iso-8859-1"));
156  int c;
157  StringBuilder processName = new StringBuilder();
158  while ((c = reader.read()) > 0) {
159  processName.append((char) c);
160  }
161  return processName.toString();
162  } catch (Exception e) {
163  return null;
164  } finally {
165  if (reader != null) {
166  try {
167  reader.close();
168  } catch (IOException e) {
169  e.printStackTrace();
170  }
171  }
172  }
173  }
174 
175 // private static String getProcessNameByAM(Context context) {
176 // String processName = null;
177 //
178 // ActivityManager am = ((ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE));
179 // if (am == null) {
180 // return null;
181 // }
182 //
183 // while (true) {
184 // List<ActivityManager.RunningAppProcessInfo> plist = am.getRunningAppProcesses();
185 // if (plist != null) {
186 // for (ActivityManager.RunningAppProcessInfo info : plist) {
187 // if (info.pid == android.os.Process.myPid()) {
188 // processName = info.processName;
189 //
190 // break;
191 // }
192 // }
193 // }
194 //
195 // if (!TextUtils.isEmpty(processName)) {
196 // return processName;
197 // }
198 //
199 // try {
200 // Thread.sleep(100L); // take a rest and again
201 // } catch (InterruptedException ex) {
202 // ex.printStackTrace();
203 // }
204 // }
205 // }
206 
207 // public static boolean isMainProcessLive(Context context) {
208 // if (context == null) {
209 // return false;
210 // }
211 //
212 // final String processName = context.getPackageName();
213 // ActivityManager am = ((ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE));
214 // if (am != null) {
215 // List<ActivityManager.RunningAppProcessInfo> plist = am.getRunningAppProcesses();
216 // if (plist != null) {
217 // for (ActivityManager.RunningAppProcessInfo info : plist) {
218 // if (info.processName.equals(processName)) {
219 // return true;
220 // }
221 // }
222 // }
223 // }
224 //
225 // return false;
226 // }
227 
228  public static String getNimDefaultCacheDir(Context context) {
229  String sdkStorageRoot = "";
230 
231  if (context.getCacheDir() != null) {
232  sdkStorageRoot = context.getCacheDir().getAbsolutePath();
233  } else {
234  NimLog.e(TAG, "loadStorageState context.getCacheDir() == null");
235  }
236 
237  if (TextUtils.isEmpty(sdkStorageRoot)) {
238  if (Environment.getExternalStorageDirectory() != null) {
239  String externalPath = Environment.getExternalStorageDirectory().getPath();
240  sdkStorageRoot = externalPath + "/" + context.getPackageName();
241  } else {
242  NimLog.e(TAG, "loadStorageState Environment.getExternalStorageDirectory() == null");
243  }
244  }
245 
246  if (!TextUtils.isEmpty(sdkStorageRoot)) {
247  sdkStorageRoot = sdkStorageRoot + "/nim/";
248  }
249 
250  return sdkStorageRoot;
251  }
252 
253  public static boolean isEmpty(Collection collection) {
254  return collection == null || collection.isEmpty();
255  }
256 
257  /**
258  * 通过Application新的API获取进程名,无需反射,无需IPC,效率最高。
259  */
260  private static String getCurrentProcessNameByApplication() {
261  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
262  return Application.getProcessName();
263  }
264  return null;
265  }
266 
267  /**
268  * 通过反射ActivityThread获取进程名,避免了ipc
269  */
270  private static String getCurrentProcessNameByActivityThread() {
271  String processName = null;
272  try {
273  final Method declaredMethod = Class.forName("android.app.ActivityThread", false, Application.class.getClassLoader())
274  .getDeclaredMethod("currentProcessName", (Class<?>[]) new Class[0]);
275  declaredMethod.setAccessible(true);
276  final Object invoke = declaredMethod.invoke(null, new Object[0]);
277  if (invoke instanceof String) {
278  processName = (String) invoke;
279  }
280  } catch (Throwable e) {
281  e.printStackTrace();
282  }
283  return processName;
284  }
285 }
static int isMainProcessPure(Context context)
判断当前进程是否是主进程(纯净版本),不会有隐私不合规风险,但是不保证100判断正确 ...
Definition: NIMUtil.java:36
static boolean isMainProcess(Context context)
Definition: NIMUtil.java:95
static String getProcessName(Context context)
Definition: NIMUtil.java:105
static String getNimDefaultCacheDir(Context context)
Definition: NIMUtil.java:228
static boolean isEmpty(Collection collection)
Definition: NIMUtil.java:253
static String getProcessNamePure(Context context)
获取当前进程名(纯净版本),不会有隐私不合规风险,但是不保证100获取到
Definition: NIMUtil.java:65