diff options
author | LuK1337 <priv.luk@gmail.com> | 2021-05-03 23:57:30 +0200 |
---|---|---|
committer | Bruno Martins <bgcngm@gmail.com> | 2021-05-04 13:40:17 +0200 |
commit | 7378e7371301334485ae61c66b35be15d5bee067 (patch) | |
tree | 0690fa60ecee11f8c6073a9c7413873ba5c27ca8 | |
parent | 44427ae3ef1f50f78ef6ac200de16affeedddb0d (diff) |
lineage-sdk: Add KILL_APP button action
This partially reverts commit 9eca90278d2936b83b420081bbc78d475627ba7e.
Change-Id: I9bfc039540936e4dbd4313c8f368ce3e3e445290
4 files changed, 76 insertions, 13 deletions
diff --git a/lineage/res/res/values/config.xml b/lineage/res/res/values/config.xml index a902ce5f..659a5ac6 100644 --- a/lineage/res/res/values/config.xml +++ b/lineage/res/res/values/config.xml @@ -162,6 +162,7 @@ 7 - Sleep 8 - Last app 9 - Toggle split screen + 10 - Kill foreground app This needs to match the enums in sdk/src/java/org/lineageos/internal/util/DeviceKeysConstants.java. --> @@ -178,6 +179,7 @@ 7 - Sleep 8 - Last app 9 - Toggle split screen + 10 - Kill foreground app This needs to match the enums in sdk/src/java/org/lineageos/internal/util/DeviceKeysConstants.java. --> @@ -194,6 +196,7 @@ 7 - Sleep 8 - Last app 9 - Toggle split screen + 10 - Kill foreground app This needs to match the enums in sdk/src/java/org/lineageos/internal/util/DeviceKeysConstants.java. --> @@ -210,6 +213,7 @@ 7 - Sleep 8 - Last app 9 - Toggle split screen + 10 - Kill foreground app This needs to match the enums in sdk/src/java/org/lineageos/internal/util/DeviceKeysConstants.java. --> @@ -226,6 +230,7 @@ 7 - Sleep 8 - Last app 9 - Toggle split screen + 10 - Kill foreground app This needs to match the enums in sdk/src/java/org/lineageos/internal/util/DeviceKeysConstants.java. --> diff --git a/sdk/src/java/lineageos/providers/LineageSettings.java b/sdk/src/java/lineageos/providers/LineageSettings.java index 4d5a3416..47a59067 100644 --- a/sdk/src/java/lineageos/providers/LineageSettings.java +++ b/sdk/src/java/lineageos/providers/LineageSettings.java @@ -1058,12 +1058,13 @@ public final class LineageSettings { * 7 - Action Sleep * 8 - Last app * 9 - Toggle split screen + * 10 - Kill foreground app */ public static final String KEY_HOME_LONG_PRESS_ACTION = "key_home_long_press_action"; /** @hide */ public static final Validator KEY_HOME_LONG_PRESS_ACTION_VALIDATOR = - new InclusiveIntegerRangeValidator(0, 9); + new InclusiveIntegerRangeValidator(0, 10); /** * Action to perform when the home key is double-tapped. @@ -1074,7 +1075,7 @@ public final class LineageSettings { /** @hide */ public static final Validator KEY_HOME_DOUBLE_TAP_ACTION_VALIDATOR = - new InclusiveIntegerRangeValidator(0, 9); + new InclusiveIntegerRangeValidator(0, 10); /** * Action to perform when the back key is long-pressed. @@ -1085,7 +1086,7 @@ public final class LineageSettings { /** @hide */ public static final Validator KEY_BACK_LONG_PRESS_ACTION_VALIDATOR = - new InclusiveIntegerRangeValidator(0, 9); + new InclusiveIntegerRangeValidator(0, 10); /** * Whether to wake the screen with the back key, the value is boolean. @@ -1134,7 +1135,7 @@ public final class LineageSettings { /** @hide */ public static final Validator KEY_MENU_ACTION_VALIDATOR = - new InclusiveIntegerRangeValidator(0, 9); + new InclusiveIntegerRangeValidator(0, 10); /** * Action to perform when the menu key is long-pressed. @@ -1145,7 +1146,7 @@ public final class LineageSettings { /** @hide */ public static final Validator KEY_MENU_LONG_PRESS_ACTION_VALIDATOR = - new InclusiveIntegerRangeValidator(0, 9); + new InclusiveIntegerRangeValidator(0, 10); /** * Action to perform when the assistant (search) key is pressed. (Default is 3) @@ -1155,7 +1156,7 @@ public final class LineageSettings { /** @hide */ public static final Validator KEY_ASSIST_ACTION_VALIDATOR = - new InclusiveIntegerRangeValidator(0, 9); + new InclusiveIntegerRangeValidator(0, 10); /** * Action to perform when the assistant (search) key is long-pressed. (Default is 4) @@ -1165,7 +1166,7 @@ public final class LineageSettings { /** @hide */ public static final Validator KEY_ASSIST_LONG_PRESS_ACTION_VALIDATOR = - new InclusiveIntegerRangeValidator(0, 9); + new InclusiveIntegerRangeValidator(0, 10); /** * Action to perform when the app switch key is pressed. (Default is 2) @@ -1175,7 +1176,7 @@ public final class LineageSettings { /** @hide */ public static final Validator KEY_APP_SWITCH_ACTION_VALIDATOR = - new InclusiveIntegerRangeValidator(0, 9); + new InclusiveIntegerRangeValidator(0, 10); /** * Action to perform when the app switch key is long-pressed. (Default is 0) @@ -1185,7 +1186,7 @@ public final class LineageSettings { /** @hide */ public static final Validator KEY_APP_SWITCH_LONG_PRESS_ACTION_VALIDATOR = - new InclusiveIntegerRangeValidator(0, 9); + new InclusiveIntegerRangeValidator(0, 10); /** * Action to perform when the screen edge is long-swiped. (Default is 0) @@ -1195,7 +1196,7 @@ public final class LineageSettings { /** @hide */ public static final Validator KEY_EDGE_LONG_SWIPE_ACTION_VALIDATOR = - new InclusiveIntegerRangeValidator(0, 9); + new InclusiveIntegerRangeValidator(0, 10); /** * Whether to wake the screen with the home key, the value is boolean. diff --git a/sdk/src/java/org/lineageos/internal/util/ActionUtils.java b/sdk/src/java/org/lineageos/internal/util/ActionUtils.java index 299c4a29..a7955814 100644 --- a/sdk/src/java/org/lineageos/internal/util/ActionUtils.java +++ b/sdk/src/java/org/lineageos/internal/util/ActionUtils.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2020 The LineageOS Project + * Copyright (C) 2018-2021 The LineageOS Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,6 +17,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; @@ -25,7 +26,11 @@ import android.content.Intent; import android.content.pm.PackageManager; import android.content.pm.ResolveInfo; import android.os.RemoteException; +import android.os.UserHandle; import android.util.Log; +import android.widget.Toast; + +import org.lineageos.platform.internal.R; import java.util.List; @@ -35,6 +40,38 @@ public class ActionUtils { private static final String SYSTEMUI_PACKAGE = "com.android.systemui"; /** + * Kills the top most / most recent user application, but leaves out the launcher. + * + * @param context the current context, used to retrieve the package manager. + * @param userId the ID of the currently active user + * @return {@code true} when a user application was found and closed. + */ + public static boolean killForegroundApp(Context context, int userId) { + try { + return killForegroundAppInternal(context, userId); + } catch (RemoteException e) { + Log.e(TAG, "Could not kill foreground app"); + } + return false; + } + + private static boolean killForegroundAppInternal(Context context, int userId) + throws RemoteException { + final String packageName = getForegroundTaskPackageName(context, userId); + + if (packageName == null) { + return false; + } + + final IActivityManager am = ActivityManagerNative.getDefault(); + am.forceStopPackage(packageName, UserHandle.USER_CURRENT); + + Toast.makeText(context, R.string.app_killed_message, Toast.LENGTH_SHORT).show(); + + return true; + } + + /** * Attempt to bring up the last activity in the stack before the current active one. * * @param context @@ -92,6 +129,25 @@ public class ActionUtils { 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); diff --git a/sdk/src/java/org/lineageos/internal/util/DeviceKeysConstants.java b/sdk/src/java/org/lineageos/internal/util/DeviceKeysConstants.java index 29ed678c..8fd7723b 100644 --- a/sdk/src/java/org/lineageos/internal/util/DeviceKeysConstants.java +++ b/sdk/src/java/org/lineageos/internal/util/DeviceKeysConstants.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2017 The LineageOS Project + * Copyright (C) 2018,2021 The LineageOS Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -35,7 +35,8 @@ public class DeviceKeysConstants { LAUNCH_CAMERA, SLEEP, LAST_APP, - SPLIT_SCREEN; + SPLIT_SCREEN, + KILL_APP; public static Action fromIntSafe(int id) { if (id < NOTHING.ordinal() || id > Action.values().length) { |