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