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;
13 import java.io.BufferedReader;
14 import java.io.FileInputStream;
15 import java.io.IOException;
16 import java.io.InputStreamReader;
17 import java.lang.reflect.Method;
18 import java.util.Collection;
26 private static final String TAG =
"NIMUtil";
27 private static String currentProcessName = null;
38 if (context == null) {
39 if (LogDesensitizationConfigHelper.printToLogcat()) {
40 Log.d(TAG,
" isMainProcessPure context == null");
45 String packageName = context.getApplicationContext().getPackageName();
48 if(TextUtils.isEmpty(processName))
50 if (LogDesensitizationConfigHelper.printToLogcat()) {
51 Log.d(TAG,
" isMainProcessPure can not get processName");
55 if (LogDesensitizationConfigHelper.printToLogcat()) {
56 Log.d(TAG,
" isMainProcessPure packageName = " + packageName +
",processName = " + processName);
58 return packageName.equals(processName) ? 1 : 0;
67 if(!TextUtils.isEmpty(currentProcessName))
69 if (LogDesensitizationConfigHelper.printToLogcat()) {
70 Log.d(TAG,
"get processName from Cache = " + currentProcessName);
72 return currentProcessName;
74 currentProcessName = getCurrentProcessNameByApplication();
75 if(!TextUtils.isEmpty(currentProcessName))
77 if (LogDesensitizationConfigHelper.printToLogcat()) {
78 Log.d(TAG,
"get processName from Application = " + currentProcessName);
80 return currentProcessName;
83 currentProcessName = getCurrentProcessNameByActivityThread();
85 if(!TextUtils.isEmpty(currentProcessName))
87 if (LogDesensitizationConfigHelper.printToLogcat()) {
88 Log.d(TAG,
"get processName from ActivityThread = " + currentProcessName);
90 return currentProcessName;
93 return currentProcessName;
97 if (context == null) {
101 String packageName = context.getApplicationContext().getPackageName();
103 return packageName.equals(processName);
107 if(!TextUtils.isEmpty(currentProcessName))
109 if (LogDesensitizationConfigHelper.printToLogcat()) {
110 Log.d(TAG,
"get processName from Cache = " + currentProcessName);
112 NimLog.i(TAG,
"get processName from Cache = " + currentProcessName);
113 return currentProcessName;
115 currentProcessName = getCurrentProcessNameByApplication();
116 if(!TextUtils.isEmpty(currentProcessName))
118 if (LogDesensitizationConfigHelper.printToLogcat()) {
119 Log.d(TAG,
"get processName from Application = " + currentProcessName);
121 return currentProcessName;
124 currentProcessName = getCurrentProcessNameByActivityThread();
126 if(!TextUtils.isEmpty(currentProcessName))
128 if (LogDesensitizationConfigHelper.printToLogcat()) {
129 Log.d(TAG,
"get processName from ActivityThread = " + currentProcessName);
131 return currentProcessName;
134 currentProcessName = getProcessFromFile();
136 if(!TextUtils.isEmpty(currentProcessName))
138 if (LogDesensitizationConfigHelper.printToLogcat()) {
139 Log.d(TAG,
"get processName from File = " + currentProcessName);
141 return currentProcessName;
146 if (LogDesensitizationConfigHelper.printToLogcat()) {
147 Log.d(TAG,
"get processName from ActivityManager = " + currentProcessName);
149 return currentProcessName;
152 private static String getProcessFromFile() {
153 BufferedReader reader = null;
155 int pid = android.os.Process.myPid();
156 String file =
"/proc/" + pid +
"/cmdline";
157 reader =
new BufferedReader(
new InputStreamReader(
new FileInputStream(file),
"iso-8859-1"));
159 StringBuilder processName =
new StringBuilder();
160 while ((c = reader.read()) > 0) {
161 processName.append((
char) c);
163 return processName.toString();
164 }
catch (Exception e) {
167 if (reader != null) {
170 }
catch (IOException e) {
231 String sdkStorageRoot =
"";
233 if (context.getCacheDir() != null) {
234 sdkStorageRoot = context.getCacheDir().getAbsolutePath();
236 NimLog.e(TAG,
"loadStorageState context.getCacheDir() == null");
239 if (TextUtils.isEmpty(sdkStorageRoot)) {
240 if (Environment.getExternalStorageDirectory() != null) {
241 String externalPath = Environment.getExternalStorageDirectory().getPath();
242 sdkStorageRoot = externalPath +
"/" + context.getPackageName();
244 NimLog.e(TAG,
"loadStorageState Environment.getExternalStorageDirectory() == null");
248 if (!TextUtils.isEmpty(sdkStorageRoot)) {
249 sdkStorageRoot = sdkStorageRoot +
"/nim/";
252 return sdkStorageRoot;
255 public static boolean isEmpty(Collection collection) {
256 return collection == null || collection.isEmpty();
262 private static String getCurrentProcessNameByApplication() {
263 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
264 return Application.getProcessName();
272 private static String getCurrentProcessNameByActivityThread() {
273 String processName = null;
275 final Method declaredMethod = Class.forName(
"android.app.ActivityThread",
false, Application.class.getClassLoader())
276 .getDeclaredMethod(
"currentProcessName", (Class<?>[])
new Class[0]);
277 declaredMethod.setAccessible(
true);
278 final Object invoke = declaredMethod.invoke(null,
new Object[0]);
279 if (invoke instanceof String) {
280 processName = (String) invoke;
282 }
catch (Throwable e) {
289 return loginType == IAuthService.AuthType.DYNAMIC || loginType == IAuthService.AuthType.THIRDPARTY;
293 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)