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