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

Commit 40a57185 authored by Evan Charlton's avatar Evan Charlton Committed by Steve Kondik
Browse files

Kill foreground process by long-pressing BACK

This patch allows the user to force the running foreground application to be
killed. One example use case for this is applications which trap buttons,
leaving the user stuck. Another would be a memory-heavy application that
traps the BACK button (so they user wouldn't want to leave it in memory by
pressing HOME). Browser is a good example of this second use case.

Change-Id: I2c42e299cc63bed76840a075bbbda617c73d81ab
parent ac797fe6
Loading
Loading
Loading
Loading
+7 −0
Original line number Diff line number Diff line
@@ -3909,6 +3909,13 @@ public final class Settings {
        public static final String ALLOW_MOVE_ALL_APPS_EXTERNAL =
                "allow_move_all_apps_external";

        /**
         * Whether to allow killing of the foreground process by long-pressing
         * the device's BACK button.
         * @hide
         */
        public static final String KILL_APP_LONGPRESS_BACK = "kill_app_on_longpress_back";
        
        /**
         * @hide
         */
+41 −1
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@
package com.android.internal.policy.impl;

import android.app.Activity;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.app.ActivityManagerNative;
import android.app.IActivityManager;
import android.app.IUiModeManager;
@@ -39,6 +40,7 @@ import android.os.IBinder;
import android.os.LocalPowerManager;
import android.os.Looper;
import android.os.PowerManager;
import android.os.Process;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.os.SystemClock;
@@ -113,6 +115,7 @@ import android.media.AudioManager;

import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;

/**
 * WindowManagerPolicy implementation for the Android phone UI.  This
@@ -518,6 +521,33 @@ public class PhoneWindowManager implements WindowManagerPolicy {
        }
    };

    Runnable mBackLongPress = new Runnable() {
        public void run() {
            if (Settings.Secure.getInt(mContext.getContentResolver(),
                    Settings.Secure.KILL_APP_LONGPRESS_BACK, 0) == 0) {
                // Bail out unless the user has elected to turn this on.
                return;
            }
            try {
                IActivityManager mgr = ActivityManagerNative.getDefault();
                List<RunningAppProcessInfo> apps = mgr.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) {
                        // Kill the entire pid
                        Process.killProcess(appInfo.pid);
                        break;
                    }
                }
            } catch (RemoteException remoteException) {
                // Do nothing; just let it go.
            }
        }
    };

    /**
     * When a volumeup-key longpress expires, skip songs based on key press
     */
@@ -1161,6 +1191,11 @@ public class PhoneWindowManager implements WindowManagerPolicy {
            mHandler.removeCallbacks(mHomeLongPress);
        }

        // Clear a pending BACK longpress if the user releases Back.
        if ((keyCode == KeyEvent.KEYCODE_BACK) && !down) {
            mHandler.removeCallbacks(mBackLongPress);
        }

        // If the HOME button is currently being held, then we do special
        // chording with it.
        if (mHomePressed) {
@@ -1233,6 +1268,11 @@ public class PhoneWindowManager implements WindowManagerPolicy {
                mHomePressed = true;
            }
            return true;
        } else if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (down && repeatCount == 0) {
                mHandler.postDelayed(mBackLongPress, ViewConfiguration.getGlobalActionKeyTimeout());
            }
            return false;
        } else if (keyCode == KeyEvent.KEYCODE_MENU) {
            // Hijack modified menu keys for debugging features
            final int chordBug = KeyEvent.META_SHIFT_ON;