Skip to content

Instantly share code, notes, and snippets.

@aviraxp
Created August 30, 2019 09:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aviraxp/25623e11e39d2591ddca92b479e77314 to your computer and use it in GitHub Desktop.
Save aviraxp/25623e11e39d2591ddca92b479e77314 to your computer and use it in GitHub Desktop.
diff --git a/sdk/src/java/org/mokee/internal/util/ActionUtils.java b/sdk/src/java/org/mokee/internal/util/ActionUtils.java
index 3cb9902..a5fcbd9 100644
--- a/sdk/src/java/org/mokee/internal/util/ActionUtils.java
+++ b/sdk/src/java/org/mokee/internal/util/ActionUtils.java
@@ -1,6 +1,7 @@
package org.mokee.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.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.mokee.platform.internal.R;
@@ -43,43 +41,16 @@
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;
}
/**
@@ -132,18 +103,43 @@
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 iam = ActivityManager.getService();
+ final StackInfo focusedStack = iam.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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment