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;
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;
25 private static final String TAG =
"NIMUtil";
26 private static String currentProcessName = null;
37 if (context == null) {
38 if (LogDesensitizationConfigHelper.printToLogcat()) {
39 Log.d(TAG,
" isMainProcessPure context == null");
44 String packageName = context.getApplicationContext().getPackageName();
47 if(TextUtils.isEmpty(processName))
49 if (LogDesensitizationConfigHelper.printToLogcat()) {
50 Log.d(TAG,
" isMainProcessPure can not get processName");
54 if (LogDesensitizationConfigHelper.printToLogcat()) {
55 Log.d(TAG,
" isMainProcessPure packageName = " + packageName +
",processName = " + processName);
57 return packageName.equals(processName) ? 1 : 0;
66 if(!TextUtils.isEmpty(currentProcessName))
68 if (LogDesensitizationConfigHelper.printToLogcat()) {
69 Log.d(TAG,
"get processName from Cache = " + currentProcessName);
71 return currentProcessName;
73 currentProcessName = getCurrentProcessNameByApplication();
74 if(!TextUtils.isEmpty(currentProcessName))
76 if (LogDesensitizationConfigHelper.printToLogcat()) {
77 Log.d(TAG,
"get processName from Application = " + currentProcessName);
79 return currentProcessName;
82 currentProcessName = getCurrentProcessNameByActivityThread();
84 if(!TextUtils.isEmpty(currentProcessName))
86 if (LogDesensitizationConfigHelper.printToLogcat()) {
87 Log.d(TAG,
"get processName from ActivityThread = " + currentProcessName);
89 return currentProcessName;
92 return currentProcessName;
96 if (context == null) {
100 String packageName = context.getApplicationContext().getPackageName();
102 return packageName.equals(processName);
106 if(!TextUtils.isEmpty(currentProcessName))
108 if (LogDesensitizationConfigHelper.printToLogcat()) {
109 Log.d(TAG,
"get processName from Cache = " + currentProcessName);
111 return currentProcessName;
113 currentProcessName = getCurrentProcessNameByApplication();
114 if(!TextUtils.isEmpty(currentProcessName))
116 if (LogDesensitizationConfigHelper.printToLogcat()) {
117 Log.d(TAG,
"get processName from Application = " + currentProcessName);
119 return currentProcessName;
122 currentProcessName = getCurrentProcessNameByActivityThread();
124 if(!TextUtils.isEmpty(currentProcessName))
126 if (LogDesensitizationConfigHelper.printToLogcat()) {
127 Log.d(TAG,
"get processName from ActivityThread = " + currentProcessName);
129 return currentProcessName;
132 currentProcessName = getProcessFromFile();
134 if(!TextUtils.isEmpty(currentProcessName))
136 if (LogDesensitizationConfigHelper.printToLogcat()) {
137 Log.d(TAG,
"get processName from File = " + currentProcessName);
139 return currentProcessName;
144 if (LogDesensitizationConfigHelper.printToLogcat()) {
145 Log.d(TAG,
"get processName from ActivityManager = " + currentProcessName);
147 return currentProcessName;
150 private static String getProcessFromFile() {
151 BufferedReader reader = null;
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"));
157 StringBuilder processName =
new StringBuilder();
158 while ((c = reader.read()) > 0) {
159 processName.append((
char) c);
161 return processName.toString();
162 }
catch (Exception e) {
165 if (reader != null) {
168 }
catch (IOException e) {
229 String sdkStorageRoot =
"";
231 if (context.getCacheDir() != null) {
232 sdkStorageRoot = context.getCacheDir().getAbsolutePath();
234 NimLog.e(TAG,
"loadStorageState context.getCacheDir() == null");
237 if (TextUtils.isEmpty(sdkStorageRoot)) {
238 if (Environment.getExternalStorageDirectory() != null) {
239 String externalPath = Environment.getExternalStorageDirectory().getPath();
240 sdkStorageRoot = externalPath +
"/" + context.getPackageName();
242 NimLog.e(TAG,
"loadStorageState Environment.getExternalStorageDirectory() == null");
246 if (!TextUtils.isEmpty(sdkStorageRoot)) {
247 sdkStorageRoot = sdkStorageRoot +
"/nim/";
250 return sdkStorageRoot;
253 public static boolean isEmpty(Collection collection) {
254 return collection == null || collection.isEmpty();
260 private static String getCurrentProcessNameByApplication() {
261 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
262 return Application.getProcessName();
270 private static String getCurrentProcessNameByActivityThread() {
271 String processName = null;
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;
280 }
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获取到