lineage-sdk: Refactor ActionUtils
* Use getFocusedStackInfo() to check active task, also do general cleanups for this class. Hopefully this will get rid of the strange issues of "kill foreground app" feature. Change-Id: Ice1ca8e088ea4fec050fa171d990cb938dd3272c
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package org.lineageos.internal.util;
|
||||
|
||||
import android.app.ActivityManager;
|
||||
import android.app.ActivityManager.StackInfo;
|
||||
import android.app.ActivityManagerNative;
|
||||
import android.app.ActivityOptions;
|
||||
import android.app.IActivityManager;
|
||||
@@ -8,12 +9,9 @@ import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.content.pm.ResolveInfo;
|
||||
import android.os.Process;
|
||||
import android.os.RemoteException;
|
||||
import android.os.UserHandle;
|
||||
import android.util.Log;
|
||||
import android.view.HapticFeedbackConstants;
|
||||
import android.widget.Toast;
|
||||
|
||||
import org.lineageos.platform.internal.R;
|
||||
|
||||
@@ -43,43 +41,16 @@ public class ActionUtils {
|
||||
|
||||
private static boolean killForegroundAppInternal(Context context, int userId)
|
||||
throws RemoteException {
|
||||
try {
|
||||
final Intent intent = new Intent(Intent.ACTION_MAIN);
|
||||
String defaultHomePackage = "com.android.launcher";
|
||||
intent.addCategory(Intent.CATEGORY_HOME);
|
||||
final ResolveInfo res = context.getPackageManager().resolveActivity(intent, 0);
|
||||
final String packageName = getForegroundTaskPackageName(context, userId);
|
||||
|
||||
if (res.activityInfo != null && !res.activityInfo.packageName.equals("android")) {
|
||||
defaultHomePackage = res.activityInfo.packageName;
|
||||
}
|
||||
|
||||
IActivityManager am = ActivityManagerNative.getDefault();
|
||||
List<ActivityManager.RunningAppProcessInfo> apps = am.getRunningAppProcesses();
|
||||
for (ActivityManager.RunningAppProcessInfo appInfo : apps) {
|
||||
int uid = appInfo.uid;
|
||||
// Make sure it's a foreground user application (not system,
|
||||
// root, phone, etc.)
|
||||
if (uid >= Process.FIRST_APPLICATION_UID && uid <= Process.LAST_APPLICATION_UID
|
||||
&& appInfo.importance ==
|
||||
ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
|
||||
if (appInfo.pkgList != null && (appInfo.pkgList.length > 0)) {
|
||||
for (String pkg : appInfo.pkgList) {
|
||||
if (!pkg.equals(SYSTEMUI_PACKAGE)
|
||||
&& !pkg.equals(defaultHomePackage)) {
|
||||
am.forceStopPackage(pkg, UserHandle.USER_CURRENT);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
Process.killProcess(appInfo.pid);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (RemoteException remoteException) {
|
||||
// Do nothing; just let it go.
|
||||
if (packageName == null) {
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
|
||||
final IActivityManager am = ActivityManagerNative.getDefault();
|
||||
am.forceStopPackage(packageName, UserHandle.USER_CURRENT);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -120,8 +91,8 @@ public class ActionUtils {
|
||||
private static ActivityManager.RecentTaskInfo getLastTask(Context context, int userId)
|
||||
throws RemoteException {
|
||||
final String defaultHomePackage = resolveCurrentLauncherPackage(context, userId);
|
||||
final IActivityManager iam = ActivityManager.getService();
|
||||
final List<ActivityManager.RecentTaskInfo> tasks = iam.getRecentTasks(5,
|
||||
final IActivityManager am = ActivityManager.getService();
|
||||
final List<ActivityManager.RecentTaskInfo> tasks = am.getRecentTasks(5,
|
||||
ActivityManager.RECENT_IGNORE_UNAVAILABLE, userId).getList();
|
||||
|
||||
for (int i = 1; i < tasks.size(); i++) {
|
||||
@@ -132,18 +103,43 @@ public class ActionUtils {
|
||||
String packageName = task.baseIntent.getComponent().getPackageName();
|
||||
if (!packageName.equals(defaultHomePackage)
|
||||
&& !packageName.equals(SYSTEMUI_PACKAGE)) {
|
||||
return tasks.get(i);
|
||||
return task;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private static String getForegroundTaskPackageName(Context context, int userId)
|
||||
throws RemoteException {
|
||||
final String defaultHomePackage = resolveCurrentLauncherPackage(context, userId);
|
||||
final IActivityManager am = ActivityManager.getService();
|
||||
final StackInfo focusedStack = am.getFocusedStackInfo();
|
||||
|
||||
if (focusedStack == null || focusedStack.topActivity == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
final String packageName = focusedStack.topActivity.getPackageName();
|
||||
if (!packageName.equals(defaultHomePackage)
|
||||
&& !packageName.equals(SYSTEMUI_PACKAGE)) {
|
||||
return packageName;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private static String resolveCurrentLauncherPackage(Context context, int userId) {
|
||||
final Intent launcherIntent = new Intent(Intent.ACTION_MAIN)
|
||||
.addCategory(Intent.CATEGORY_HOME);
|
||||
final PackageManager pm = context.getPackageManager();
|
||||
final ResolveInfo launcherInfo = pm.resolveActivityAsUser(launcherIntent, 0, userId);
|
||||
return launcherInfo.activityInfo.packageName;
|
||||
|
||||
if (launcherInfo.activityInfo != null &&
|
||||
!launcherInfo.activityInfo.packageName.equals("android")) {
|
||||
return launcherInfo.activityInfo.packageName;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user