1 package com.netease.nimlib.sdk.util;
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;
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;
23 private static final String TAG =
"NIMUtil";
24 private static String currentProcessName = null;
35 if (context == null) {
36 Log.d(TAG,
" isMainProcessPure context == null");
40 String packageName = context.getApplicationContext().getPackageName();
43 if(TextUtils.isEmpty(processName))
45 Log.d(TAG,
" isMainProcessPure can not get processName");
48 Log.d(TAG,
" isMainProcessPure packageName = "+packageName+
",processName = "+processName);
49 return packageName.equals(processName) ? 1 : 0;
58 if(!TextUtils.isEmpty(currentProcessName))
60 Log.d(TAG,
"get processName from Cache = "+currentProcessName);
61 return currentProcessName;
63 currentProcessName = getCurrentProcessNameByApplication();
64 if(!TextUtils.isEmpty(currentProcessName))
66 Log.d(TAG,
"get processName from Application = "+currentProcessName);
67 return currentProcessName;
70 currentProcessName = getCurrentProcessNameByActivityThread();
72 if(!TextUtils.isEmpty(currentProcessName))
74 Log.d(TAG,
"get processName from ActivityThread = "+currentProcessName);
75 return currentProcessName;
78 return currentProcessName;
82 if (context == null) {
86 String packageName = context.getApplicationContext().getPackageName();
88 return packageName.equals(processName);
92 if(!TextUtils.isEmpty(currentProcessName))
94 Log.d(TAG,
"get processName from Cache = "+currentProcessName);
95 return currentProcessName;
97 currentProcessName = getCurrentProcessNameByApplication();
98 if(!TextUtils.isEmpty(currentProcessName))
100 Log.d(TAG,
"get processName from Application = "+currentProcessName);
101 return currentProcessName;
104 currentProcessName = getCurrentProcessNameByActivityThread();
106 if(!TextUtils.isEmpty(currentProcessName))
108 Log.d(TAG,
"get processName from ActivityThread = "+currentProcessName);
109 return currentProcessName;
112 currentProcessName = getProcessFromFile();
114 if(!TextUtils.isEmpty(currentProcessName))
116 Log.d(TAG,
"get processName from File = "+currentProcessName);
117 return currentProcessName;
122 Log.d(TAG,
"get processName from ActivityManager = "+currentProcessName);
123 return currentProcessName;
126 private static String getProcessFromFile() {
127 BufferedReader reader = null;
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"));
133 StringBuilder processName =
new StringBuilder();
134 while ((c = reader.read()) > 0) {
135 processName.append((
char) c);
137 return processName.toString();
138 }
catch (Exception e) {
141 if (reader != null) {
144 }
catch (IOException e) {
205 String sdkStorageRoot =
"";
207 if (context.getCacheDir() != null) {
208 sdkStorageRoot = context.getCacheDir().getAbsolutePath();
210 NimLog.e(TAG,
"loadStorageState context.getCacheDir() == null");
213 if (TextUtils.isEmpty(sdkStorageRoot)) {
214 if (Environment.getExternalStorageDirectory() != null) {
215 String externalPath = Environment.getExternalStorageDirectory().getPath();
216 sdkStorageRoot = externalPath +
"/" + context.getPackageName();
218 NimLog.e(TAG,
"loadStorageState Environment.getExternalStorageDirectory() == null");
222 if (!TextUtils.isEmpty(sdkStorageRoot)) {
223 sdkStorageRoot = sdkStorageRoot +
"/nim/";
226 return sdkStorageRoot;
229 public static boolean isEmpty(Collection collection) {
230 return collection == null || collection.isEmpty();
236 private static String getCurrentProcessNameByApplication() {
237 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
238 return Application.getProcessName();
246 private static String getCurrentProcessNameByActivityThread() {
247 String processName = null;
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;
256 }
catch (Throwable e) {
static int isMainProcessPure(Context context)
判断当前进程是否是主进程(纯净版本),不会有隐私不合规风险,但是不保证100判断正确 ...
static boolean isMainProcess(Context context)
static String getProcessName(Context context)
static String getNimDefaultCacheDir(Context context)
static boolean isEmpty(Collection collection)
static String getProcessNamePure(Context context)
获取当前进程名(纯净版本),不会有隐私不合规风险,但是不保证100获取到