Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit bed8ca15 authored by Danesh Mondegarian's avatar Danesh Mondegarian Committed by Steve Kondik
Browse files

Framework: Forward port Long press back to kill app (2/2)

Change-Id: Ia88d8a2753e203afb8d6117a643c33127f991c4a
parent 8f04e62e
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
@@ -5028,6 +5028,12 @@ public final class Settings {
         */
        public static final String SCREENSAVER_DEFAULT_COMPONENT = "screensaver_default_component";

        /**
         * Whether to allow killing of the foreground app by long-pressing the Back button
         * @hide
         */
        public static final String KILL_APP_LONGPRESS_BACK = "kill_app_longpress_back";

        /**
         * The default NFC payment component
         * @hide
+2 −0
Original line number Diff line number Diff line
@@ -77,4 +77,6 @@
    <!-- Button to soft reboot the device, within the Reboot Options dialog -->
    <string name="reboot_soft">Soft reboot</string>

    <!-- Long-press back kill application -->
    <string name="app_killed_message">Application killed</string>
</resources>
+5 −0
Original line number Diff line number Diff line
@@ -2081,4 +2081,9 @@
    -->
    <bool name="config_proximityCheckOnWake">false</bool>
    <integer name="config_proximityCheckTimeout">250</integer>

    <!-- Timeout in MS for how long you have to long-press the back key to
         kill the foreground app. -->
    <integer name="config_backKillTimeout">2000</integer>

</resources>
+7 −0
Original line number Diff line number Diff line
@@ -2199,4 +2199,11 @@

  <!-- Proximity check timeout -->
  <java-symbol type="integer" name="config_proximityCheckTimeout" />

  <!-- Developer settings - Kill app back press -->
  <java-symbol type="string" name="app_killed_message" />

  <!-- Config.xml entries -->
  <java-symbol type="integer" name="config_backKillTimeout" />

</resources>
+62 −0
Original line number Diff line number Diff line
@@ -16,7 +16,9 @@
package com.android.internal.policy.impl;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.app.ActivityManagerNative;
import android.app.IActivityManager;
import android.app.AppOpsManager;
import android.app.IUiModeManager;
import android.app.ProgressDialog;
@@ -57,6 +59,7 @@ import android.os.Looper;
import android.os.Message;
import android.os.Messenger;
import android.os.PowerManager;
import android.os.Process;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.os.SystemClock;
@@ -103,6 +106,7 @@ import android.view.accessibility.AccessibilityManager;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.widget.Toast;

import com.android.internal.R;
import com.android.internal.os.DeviceKeyHandler;
@@ -388,6 +392,7 @@ public class PhoneWindowManager implements WindowManagerPolicy {
    int mCurrentAppOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
    boolean mHasSoftInput = false;
    boolean mTranslucentDecorEnabled = true;
    int mBackKillTimeout;

    int mDeviceHardwareKeys;

@@ -1069,6 +1074,50 @@ public class PhoneWindowManager implements WindowManagerPolicy {
        mHandler.sendEmptyMessage(MSG_DISPATCH_SHOW_GLOBAL_ACTIONS);
    }

    Runnable mBackLongPress = new Runnable() {
        public void run() {
            try {
                final Intent intent = new Intent(Intent.ACTION_MAIN);
                String defaultHomePackage = "com.android.launcher";
                intent.addCategory(Intent.CATEGORY_HOME);
                final ResolveInfo res = mContext.getPackageManager().resolveActivity(intent, 0);
                if (res.activityInfo != null && !res.activityInfo.packageName.equals("android")) {
                    defaultHomePackage = res.activityInfo.packageName;
                }
                boolean targetKilled = false;
                IActivityManager am = ActivityManagerNative.getDefault();
                List<RunningAppProcessInfo> apps = am.getRunningAppProcesses();
                for (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 == RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
                        if (appInfo.pkgList != null && (appInfo.pkgList.length > 0)) {
                            for (String pkg : appInfo.pkgList) {
                                if (!pkg.equals("com.android.systemui") && !pkg.equals(defaultHomePackage)) {
                                    am.forceStopPackage(pkg, UserHandle.USER_CURRENT);
                                    targetKilled = true;
                                    break;
                                }
                            }
                        } else {
                            Process.killProcess(appInfo.pid);
                            targetKilled = true;
                        }
                    }
                    if (targetKilled) {
                        performHapticFeedbackLw(null, HapticFeedbackConstants.LONG_PRESS, false);
                        Toast.makeText(mContext, R.string.app_killed_message, Toast.LENGTH_SHORT).show();
                        break;
                    }
                }
            } catch (RemoteException remoteException) {
                // Do nothing; just let it go.
            }
        }
    };

    void showGlobalActionsInternal() {
        sendCloseSystemWindows(SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS);
        if (mGlobalActions == null) {
@@ -1220,6 +1269,8 @@ public class PhoneWindowManager implements WindowManagerPolicy {
                com.android.internal.R.bool.config_enableTranslucentDecor);
        mDeviceHardwareKeys = mContext.getResources().getInteger(
                com.android.internal.R.integer.config_deviceHardwareKeys);
        mBackKillTimeout = mContext.getResources().getInteger(
                com.android.internal.R.integer.config_backKillTimeout);

        updateKeyAssignments();

@@ -2503,6 +2554,10 @@ public class PhoneWindowManager implements WindowManagerPolicy {
            mPendingMetaAction = false;
        }

        if (keyCode == KeyEvent.KEYCODE_BACK && !down) {
            mHandler.removeCallbacks(mBackLongPress);
        }

        // First we always handle the home key here, so applications
        // can never break it, although if keyguard is on, we do let
        // it handle it, because that gives us the correct 5 second
@@ -2797,6 +2852,13 @@ public class PhoneWindowManager implements WindowManagerPolicy {
                launchAssistAction(Intent.EXTRA_ASSIST_INPUT_HINT_KEYBOARD);
            }
            return -1;
        } else if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (Settings.Secure.getInt(mContext.getContentResolver(),
                    Settings.Secure.KILL_APP_LONGPRESS_BACK, 0) == 1) {
                if (down && repeatCount == 0) {
                    mHandler.postDelayed(mBackLongPress, mBackKillTimeout);
                }
            }
        }

        // Shortcuts are invoked through Search+key, so intercept those here