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