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

Commit 8060499e authored by Ricardo Cerqueira's avatar Ricardo Cerqueira Committed by Steve Kondik
Browse files

framework: Add advanced reboot options (2 of 2)



This commit is an squash of the commits below and re-adds the reboot
logic to CM.

I took the liberty to rewrite some of the ShutdownThread code because
it had some lingering issues present since I originally wrote this
for CM7 (!!). Namely, the reboot reason was being changed when the items
are pressed instead of when the user presses the positive button. This
made some people add workarounds like handling back button presses and
whatnot, these are gone now.

commit 588464bea40b92b04c4aeee35c47f7becceeed56
Author: Roman Birg <roman@cyngn.com>
Date:   Thu Jul 10 14:20:42 2014 -0700

    advanced reboot: add soft reboot option

    Adds a "Soft reboot" option which restarts zygote, as requested in JIRA
    CYAN-3998

    Change-Id: Ia6e5ed9b053e1c5ebd1fc55e5692858b97f7e0fc
    Signed-off-by: default avatarRoman Birg <roman@cyngn.com>

    Conflicts:
        core/res/res/values/cm_arrays.xml
        core/res/res/values/cm_strings.xml
        services/java/com/android/server/power/ShutdownThread.java

commit 3e7b92551bbf818ace41cd9b9532473ed7d18f2f
Author: Veeti Paananen <veeti.paananen@rojekti.fi>
Date:   Tue Jul 23 16:34:35 2013 +0300

    Show advanced reboot if using an insecure lock screen

    Enables the advanced reboot menu when locked if the selected lock method
    is insecure (= slide unlock).

    Change-Id: I759b7b31c5411597e2537a85183d73ec02eb2461

    Conflicts:
        services/java/com/android/server/power/ShutdownThread.java

commit cf42ed3993d5d05c9b2591883fefb6338fbcdd40
Author: DvTonder <david.vantonder@gmail.com>
Date:   Thu Feb 21 18:20:01 2013 -0500

    Framework: Show the Advanced reboot menu only for the primary user

    Change-Id: I73f82eade26f268e2b4608fa23cabcf0bb8fc862

    Conflicts:
        services/java/com/android/server/power/ShutdownThread.java

commit 08aa6fd643e0359114b86f50053921dd2d86fc50
Author: Ricardo Cerqueira <cyanogenmod@cerqueira.org>
Date:   Mon Nov 4 03:37:40 2013 +0000

    Framework: Add Advanced reboot (2 of 2)

    This commit responds to a setting in Development settings for including
    options in the power menu for rebooting into recovery or bootloader. It
    is defauled to off.

    When enabled, the Advanced reboot options will only be available once the
    device is unlocked.

Change-Id: I2bc1e7024abb69cb62154d3081df5a3eb7e79df9
parent f3ce641d
Loading
Loading
Loading
Loading
+8 −1
Original line number Diff line number Diff line
@@ -5146,6 +5146,12 @@ public final class Settings {
         */
        public static final String PRIVACY_GUARD_DEFAULT = "privacy_guard_default";

        /**
         * Whether to include options in power menu for rebooting into recovery or bootloader
         * @hide
         */
        public static final String ADVANCED_REBOOT = "advanced_reboot";

        /**
         * This are the settings to be backed up.
         *
@@ -5197,7 +5203,8 @@ public final class Settings {
            MOUNT_UMS_NOTIFY_ENABLED,
            UI_NIGHT_MODE,
            SLEEP_TIMEOUT,
            PRIVACY_GUARD_DEFAULT
            PRIVACY_GUARD_DEFAULT,
            ADVANCED_REBOOT
        };

        /**
+34 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<!--
          Copyright (C) 2013-2014 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.
     You may obtain a copy of the License at

          http://www.apache.org/licenses/LICENSE-2.0

     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
    <!-- Defines the shutdown options shown in the reboot dialog. -->
    <array name="shutdown_reboot_options" translatable="false">
        <item>@string/reboot_reboot</item>
        <item>@string/reboot_soft</item>
        <item>@string/reboot_recovery</item>
        <item>@string/reboot_bootloader</item>
    </array>

    <!-- Do not translate. Defines the shutdown actions passed to the kernel.
         The first item should be empty for regular reboot. -->
    <string-array name="shutdown_reboot_actions" translatable="false">
        <item></item>
        <item>soft_reboot</item>
        <item>recovery</item>
        <item>bootloader</item>
     </string-array>
</resources>
+10 −0
Original line number Diff line number Diff line
@@ -67,4 +67,14 @@
    <string name="privacy_guard_dialog_title">Privacy Guard</string>
    <string name="privacy_guard_dialog_summary"><xliff:g id="app">%1$s</xliff:g> would like to <xliff:g id="op">%2$s</xliff:g>.</string>

    <!-- Reboot menu -->
    <!-- Button to reboot the phone, within the Reboot Options dialog -->
    <string name="reboot_reboot">Reboot</string>
    <!-- Button to reboot the phone into recovery, within the Reboot Options dialog -->
    <string name="reboot_recovery">Recovery</string>
    <!-- Button to reboot the phone into bootloader, within the Reboot Options dialog -->
    <string name="reboot_bootloader">Bootloader</string>
    <!-- Button to soft reboot the device, within the Reboot Options dialog -->
    <string name="reboot_soft">Soft reboot</string>

</resources>
+2 −0
Original line number Diff line number Diff line
@@ -1437,6 +1437,8 @@
  <java-symbol type="array" name="config_safeModeDisabledVibePattern" />
  <java-symbol type="array" name="config_safeModeEnabledVibePattern" />
  <java-symbol type="array" name="config_virtualKeyVibePattern" />
  <java-symbol type="array" name="shutdown_reboot_options" />
  <java-symbol type="array" name="shutdown_reboot_actions" />
  <java-symbol type="attr" name="actionModePopupWindowStyle" />
  <java-symbol type="attr" name="dialogCustomTitleDecorLayout" />
  <java-symbol type="attr" name="dialogTitleDecorLayout" />
+69 −9
Original line number Diff line number Diff line
/*
 * Copyright (C) 2008 The Android Open Source Project
 * Copyright (C) 2013 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.
@@ -21,6 +22,7 @@ import android.app.ActivityManagerNative;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.IActivityManager;
import android.app.KeyguardManager;
import android.app.ProgressDialog;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.IBluetoothManager;
@@ -47,6 +49,8 @@ import android.os.Vibrator;
import android.os.SystemVibrator;
import android.os.storage.IMountService;
import android.os.storage.IMountShutdownObserver;
import android.provider.Settings;
import android.widget.ListView;

import com.android.internal.telephony.ITelephony;
import com.android.server.pm.PackageManagerService;
@@ -75,6 +79,8 @@ public final class ShutdownThread extends Thread {
    private static final int MAX_SHUTDOWN_WAIT_TIME = 20*1000;
    private static final int MAX_RADIO_WAIT_TIME = 12*1000;

    private static final String SOFT_REBOOT = "soft_reboot";

    // length of vibration before shutting down
    private static final int SHUTDOWN_VIBRATE_MS = 500;
    
@@ -138,6 +144,16 @@ public final class ShutdownThread extends Thread {
        shutdownInner(context, confirm);
    }

    private static boolean isAdvancedRebootPossible(final Context context) {
        KeyguardManager km = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
        boolean keyguardLocked = km.inKeyguardRestrictedInputMode() && km.isKeyguardSecure();
        boolean advancedRebootEnabled = Settings.Secure.getInt(context.getContentResolver(),
            Settings.Secure.ADVANCED_REBOOT, 0) == 1;
        boolean isPrimaryUser = UserHandle.getCallingUserId() == UserHandle.USER_OWNER;

        return advancedRebootEnabled && !keyguardLocked && isPrimaryUser;
    }

    static void shutdownInner(final Context context, boolean confirm) {
        // ensure that only one thread is trying to power down.
        // any additional calls are just returned
@@ -172,24 +188,56 @@ public final class ShutdownThread extends Thread {

        if (confirm) {
            final CloseDialogReceiver closer = new CloseDialogReceiver(context);
            final boolean advancedReboot = isAdvancedRebootPossible(context);

            if (sConfirmDialog != null) {
                sConfirmDialog.dismiss();
                sConfirmDialog = null;
            }
            sConfirmDialog = new AlertDialog.Builder(context)
            AlertDialog.Builder confirmDialogBuilder = new AlertDialog.Builder(context)
                    .setTitle(mRebootSafeMode
                            ? com.android.internal.R.string.reboot_safemode_title
                            : showRebootOption
                                    ? com.android.internal.R.string.reboot_title
                                    : com.android.internal.R.string.power_off)
                    .setMessage(resourceId)
                    .setPositiveButton(com.android.internal.R.string.yes,
                                    : com.android.internal.R.string.power_off);

            if (!advancedReboot) {
                confirmDialogBuilder.setMessage(resourceId);
            } else {
                confirmDialogBuilder
                      .setSingleChoiceItems(com.android.internal.R.array.shutdown_reboot_options,
                              0, null);
            }

            confirmDialogBuilder.setPositiveButton(com.android.internal.R.string.yes,
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            if (advancedReboot) {
                                boolean softReboot = false;
                                ListView reasonsList = ((AlertDialog)dialog).getListView();
                                int selected = reasonsList.getCheckedItemPosition();
                                if (selected != ListView.INVALID_POSITION) {
                                    String actions[] = context.getResources().getStringArray(
                                            com.android.internal.R.array.shutdown_reboot_actions);
                                    if (selected >= 0 && selected < actions.length) {
                                        mRebootReason = actions[selected];
                                        if (actions[selected].equals(SOFT_REBOOT)) {
                                            doSoftReboot();
                                            return;
                                        }
                                    }
                                }

                                mReboot = true;
                            }
                            beginShutdownSequence(context);
                      }
                    })
                    .setNegativeButton(com.android.internal.R.string.no, null)
                    .create();
                  });

            confirmDialogBuilder.setNegativeButton(com.android.internal.R.string.no, null);
            sConfirmDialog = confirmDialogBuilder.create();

            closer.dialog = sConfirmDialog;
            sConfirmDialog.setOnDismissListener(closer);
            sConfirmDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);
@@ -199,6 +247,18 @@ public final class ShutdownThread extends Thread {
        }
    }

    private static void doSoftReboot() {
        try {
            final IActivityManager am =
                  ActivityManagerNative.asInterface(ServiceManager.checkService("activity"));
            if (am != null) {
                am.restart();
            }
        } catch (RemoteException e) {
            Log.e(TAG, "failure trying to perform soft reboot", e);
        }
    }

    private static class CloseDialogReceiver extends BroadcastReceiver
            implements DialogInterface.OnDismissListener {
        private Context mContext;