Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

隐私合规 #200

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import android.content.Context
import android.os.Build
import android.os.Process
import com.kwai.koom.base.MonitorManager.getApplication
import com.kwai.koom.base.utils.ProcessUtil
import java.io.File

enum class Abi {
Expand Down Expand Up @@ -92,18 +93,5 @@ private fun getProcessNameByProc(): String? {
}

private fun getProcessNameByAms(): String? {
try {
val activityManager = MonitorManager.getApplication().getSystemService(Context.ACTIVITY_SERVICE)
as ActivityManager

val appProcessList = activityManager.runningAppProcesses
for (processInfo in appProcessList.orEmpty()) {
if (processInfo.pid == Process.myPid()) {
return processInfo.processName
}
}
} catch (e: Exception) {
e.printStackTrace()
}
return null
return ProcessUtil.getCurrentProcessName(MonitorManager.getApplication())
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.kwai.koom.base.utils;

import android.app.Application;
import android.content.Context;
import android.os.Build;
import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.lang.reflect.Method;

/**
* @Created by Hailun on 2022/8/23 15:52
* @Description 获取当前进程名工具类
*/
public class ProcessUtil {
private static String currentProcessName;
/**
* @return 当前进程名
*/
@Nullable
public static String getCurrentProcessName(@NonNull Context context) {
if (!TextUtils.isEmpty(currentProcessName)) {
return currentProcessName;
}

//1)通过Application的API获取当前进程名
currentProcessName = getCurrentProcessNameByApplication();
if (!TextUtils.isEmpty(currentProcessName)) {
return currentProcessName;
}

//2)通过反射ActivityThread获取当前进程名
currentProcessName = getCurrentProcessNameByActivityThread();

return currentProcessName;
}

/**
* 通过Application新的API获取进程名,无需反射,无需IPC,效率最高。
*/
public static String getCurrentProcessNameByApplication() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
return Application.getProcessName();
}
return null;
}

/**
* 通过反射ActivityThread获取进程名,避免了ipc
*/
public static String getCurrentProcessNameByActivityThread() {
String processName = null;
try {
final Method declaredMethod = Class.forName("android.app.ActivityThread", false, Application.class.getClassLoader())
.getDeclaredMethod("currentProcessName", (Class<?>[]) new Class[0]);
declaredMethod.setAccessible(true);
final Object invoke = declaredMethod.invoke(null, new Object[0]);
if (invoke instanceof String) {
processName = (String) invoke;
}
} catch (Throwable e) {
e.printStackTrace();
}
return processName;
}
}