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.os.Looper;
8 import android.text.TextUtils;
9 import android.util.Log;
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;
28 private static final String TAG =
"NIMUtil";
29 private static String currentProcessName = null;
40 if (context == null) {
41 if (LogDesensitizationConfigHelper.printToLogcat()) {
42 Log.d(TAG,
" isMainProcessPure context == null");
47 String packageName = context.getApplicationContext().getPackageName();
50 if(TextUtils.isEmpty(processName))
52 if (LogDesensitizationConfigHelper.printToLogcat()) {
53 Log.d(TAG,
" isMainProcessPure can not get processName");
57 if (LogDesensitizationConfigHelper.printToLogcat()) {
58 Log.d(TAG,
" isMainProcessPure packageName = " + packageName +
",processName = " + processName);
60 return packageName.equals(processName) ? 1 : 0;
69 if(!TextUtils.isEmpty(currentProcessName))
71 if (LogDesensitizationConfigHelper.printToLogcat()) {
72 Log.d(TAG,
"get processName from Cache = " + currentProcessName);
74 return currentProcessName;
76 currentProcessName = getCurrentProcessNameByApplication();
77 if(!TextUtils.isEmpty(currentProcessName))
79 if (LogDesensitizationConfigHelper.printToLogcat()) {
80 Log.d(TAG,
"get processName from Application = " + currentProcessName);
82 return currentProcessName;
85 currentProcessName = getCurrentProcessNameByActivityThread();
87 if(!TextUtils.isEmpty(currentProcessName))
89 if (LogDesensitizationConfigHelper.printToLogcat()) {
90 Log.d(TAG,
"get processName from ActivityThread = " + currentProcessName);
92 return currentProcessName;
95 return currentProcessName;
99 if (context == null) {
103 String packageName = context.getApplicationContext().getPackageName();
105 return packageName.equals(processName);
109 if(!TextUtils.isEmpty(currentProcessName))
111 if (LogDesensitizationConfigHelper.printToLogcat()) {
112 Log.d(TAG,
"get processName from Cache = " + currentProcessName);
114 return currentProcessName;
116 currentProcessName = getCurrentProcessNameByApplication();
117 if(!TextUtils.isEmpty(currentProcessName))
119 if (LogDesensitizationConfigHelper.printToLogcat()) {
120 Log.d(TAG,
"get processName from Application = " + currentProcessName);
122 return currentProcessName;
125 currentProcessName = getCurrentProcessNameByActivityThread();
127 if(!TextUtils.isEmpty(currentProcessName))
129 if (LogDesensitizationConfigHelper.printToLogcat()) {
130 Log.d(TAG,
"get processName from ActivityThread = " + currentProcessName);
132 return currentProcessName;
135 currentProcessName = getProcessFromFile();
137 if(!TextUtils.isEmpty(currentProcessName))
139 if (LogDesensitizationConfigHelper.printToLogcat()) {
140 Log.d(TAG,
"get processName from File = " + currentProcessName);
142 return currentProcessName;
147 if (LogDesensitizationConfigHelper.printToLogcat()) {
148 Log.d(TAG,
"get processName from ActivityManager = " + currentProcessName);
150 return currentProcessName;
153 private static String getProcessFromFile() {
154 BufferedReader reader = null;
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"));
160 StringBuilder processName =
new StringBuilder();
161 while ((c = reader.read()) > 0) {
162 processName.append((
char) c);
164 return processName.toString();
165 }
catch (Exception e) {
168 if (reader != null) {
171 }
catch (IOException e) {
232 String sdkStorageRoot =
"";
234 if (context.getCacheDir() != null) {
235 sdkStorageRoot = context.getCacheDir().getAbsolutePath();
237 NimLog.e(TAG,
"loadStorageState context.getCacheDir() == null");
240 if (TextUtils.isEmpty(sdkStorageRoot)) {
241 if (Environment.getExternalStorageDirectory() != null) {
242 String externalPath = Environment.getExternalStorageDirectory().getPath();
243 sdkStorageRoot = externalPath +
"/" + context.getPackageName();
245 NimLog.e(TAG,
"loadStorageState Environment.getExternalStorageDirectory() == null");
249 if (!TextUtils.isEmpty(sdkStorageRoot)) {
250 sdkStorageRoot = sdkStorageRoot +
"/nim/";
253 return sdkStorageRoot;
256 public static boolean isEmpty(Collection collection) {
257 return collection == null || collection.isEmpty();
263 private static String getCurrentProcessNameByApplication() {
264 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
265 return Application.getProcessName();
273 private static String getCurrentProcessNameByActivityThread() {
274 String processName = null;
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;
283 }
catch (Throwable e) {
290 return loginType == IAuthService.AuthType.DYNAMIC || loginType == IAuthService.AuthType.THIRDPARTY;
294 return Looper.myLooper() == Looper.getMainLooper();
static int isMainProcessPure(Context context)
判断当前进程是否是主进程(纯净版本),不会有隐私不合规风险,但是不保证100判断正确 ...
static boolean isMainProcess(Context context)
static String getProcessName(Context context)
static String getNimDefaultCacheDir(Context context)
static boolean isMainThread()
static boolean isEmpty(Collection collection)
static String getProcessNamePure(Context context)
获取当前进程名(纯净版本),不会有隐私不合规风险,但是不保证100获取到
static boolean isEnableDynamicLoginToken(int loginType)