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

Commit db3817d1 authored by DvTonder's avatar DvTonder
Browse files

Framework: Forward port Long press back to kill app

Original author: DaneshM

Change-Id: I54f4fb861897fe713cffa15422535c5091a0c095
parent 8b33f828
Loading
Loading
Loading
Loading
+6 −7
Original line number Diff line number Diff line
@@ -4679,6 +4679,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";

        /**
         * This are the settings to be backed up.
         *
@@ -4947,13 +4953,6 @@ public final class Settings {
         */
        public static final String POWER_SOUNDS_ENABLED = "power_sounds_enabled";

        /**
         * 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";


        /**
         * Whether we keep the device on while the device is plugged in.
         * Supported values are:
+3 −0
Original line number Diff line number Diff line
@@ -3839,6 +3839,9 @@
    <!-- STK setup Call -->
    <string name="SetupCallDefault">Accept call?</string>

    <!-- Long-press back kill application -->
    <string name="app_killed_message">Application killed</string>

    <!-- Title for a button to choose the currently selected activity
         as the default in the activity resolver. [CHAR LIMIT=25] -->
    <string name="activity_resolver_use_always">Always</string>
+4 −0
Original line number Diff line number Diff line
@@ -1822,6 +1822,7 @@
  <!-- Config.xml entries -->
  <java-symbol type="bool" name="config_forceDisableHardwareKeyboard" />
  <java-symbol type="bool" name="config_hasRotationLockSwitch" />
  <java-symbol type="integer" name="config_backKillTimeout" />

  <!-- Notification and battery light -->
  <java-symbol type="bool" name="config_intrusiveBatteryLed" />
@@ -1857,4 +1858,7 @@
  <java-symbol type="string" name="second" />
  <java-symbol type="string" name="seconds" />

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

</resources>
+75 −2
Original line number Diff line number Diff line
/*
 * File modifications copyright (C) 2012 The CyanogenMod Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
@@ -16,7 +17,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.ProgressDialog;
import android.app.SearchManager;
import android.app.UiModeManager;
@@ -30,6 +33,7 @@ import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.content.res.CompatibilityInfo;
import android.content.res.Configuration;
import android.content.res.Resources;
@@ -50,6 +54,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;
@@ -148,11 +153,13 @@ import android.view.KeyCharacterMap.FallbackAction;
import android.view.accessibility.AccessibilityEvent;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Toast;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * WindowManagerPolicy implementation for the Android phone UI.  This
@@ -319,7 +326,7 @@ public class PhoneWindowManager implements WindowManagerPolicy {
    boolean mOrientationSensorEnabled = false;
    int mCurrentAppOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
    boolean mHasSoftInput = false;
    
    int mBackKillTimeout;
    int mPointerLocationMode = 0; // guarded by mLock

    // The last window we were told about in focusChanged.
@@ -813,6 +820,57 @@ public class PhoneWindowManager implements WindowManagerPolicy {
        }
    };

    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);
                        /*
                        TODO: Displaying a Toast
                        The fix for bug 7048792: Use correct WindowManager for Toast - Using
                        the application Context instead of the activity Context to retrieve a
                        WindowManager with no parent window causes a NPE and reboot in 4.2 if we
                        try to display the Toast below. Disable until we can figure out a solution.
                        */
                        //Toast.makeText(mContext, R.string.app_killed_message, Toast.LENGTH_SHORT).show();
                        break;
                    }
                }
            } catch (RemoteException remoteException) {
                // Do nothing; just let it go.
            }
        }
    };

    void showGlobalActionsDialog() {
        if (mGlobalActions == null) {
            mGlobalActions = new GlobalActions(mContext, mWindowManagerFuncs);
@@ -966,6 +1024,9 @@ public class PhoneWindowManager implements WindowManagerPolicy {
                com.android.internal.R.integer.config_lidNavigationAccessibility);
        mLidControlsSleep = mContext.getResources().getBoolean(
                com.android.internal.R.bool.config_lidControlsSleep);
        mBackKillTimeout = mContext.getResources().getInteger(
                com.android.internal.R.integer.config_backKillTimeout);

        // register for dock events
        IntentFilter filter = new IntentFilter();
        filter.addAction(UiModeManager.ACTION_ENTER_CAR_MODE);
@@ -1607,13 +1668,14 @@ public class PhoneWindowManager implements WindowManagerPolicy {
            try {
                context = context.createPackageContext(packageName, 0);
                if (theme != context.getThemeResId()) {

                    context.setTheme(theme);
                }
            } catch (PackageManager.NameNotFoundException e) {
                // Ignore
            }

            // Construct the Toast

            Window win = PolicyManager.makeNewWindow(context);
            final TypedArray ta = win.getWindowStyle();
            if (ta.getBoolean(
@@ -1895,6 +1957,10 @@ public class PhoneWindowManager implements WindowManagerPolicy {
            }
        }

        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
@@ -2054,6 +2120,13 @@ public class PhoneWindowManager implements WindowManagerPolicy {
                }
            }
            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