コード例 #1
0
  static void shutdownInner(final Context context, boolean confirm) {
    // ensure that only one thread is trying to power down.
    // any additional calls are just returned
    synchronized (sIsStartedGuard) {
      if (sIsStarted) {
        Log.d(TAG, "Request to shutdown already running, returning.");
        return;
      }
    }

    final int titleResourceId;
    final int resourceId;

    Log.d(TAG, "Notifying thread to start shutdown");

    if (mRebootSafeMode) {
      titleResourceId = com.android.internal.R.string.reboot_safemode_title;
      resourceId = com.android.internal.R.string.reboot_safemode_confirm;
    } else if (mReboot) {
      titleResourceId = com.android.internal.R.string.reboot_system;
      resourceId = com.android.internal.R.string.reboot_confirm;
    } else {

      final int longPressBehavior =
          context
              .getResources()
              .getInteger(com.android.internal.R.integer.config_longPressOnPowerBehavior);

      titleResourceId = com.android.internal.R.string.power_off;
      if (longPressBehavior == 2) {
        resourceId = com.android.internal.R.string.shutdown_confirm_question;
      } else {
        resourceId = com.android.internal.R.string.shutdown_confirm;
      }

      Log.d(TAG, "longPressBehavior=" + longPressBehavior);
    }

    if (confirm) {
      final CloseDialogReceiver closer = new CloseDialogReceiver(context);
      if (sConfirmDialog != null) {
        sConfirmDialog.dismiss();
        sConfirmDialog = null;
      }
      if (mReboot && !mRebootSafeMode) {
        // Determine if primary user is logged in
        boolean isPrimary = UserHandle.getCallingUserId() == UserHandle.USER_OWNER;

        // See if the advanced reboot menu is enabled (only if primary user) and check the keyguard
        // state
        boolean advancedReboot = isPrimary ? advancedRebootEnabled(context) : false;
        KeyguardManager km = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
        boolean locked = km.inKeyguardRestrictedInputMode();

        if (advancedReboot && !locked) {
          // Include options in power menu for rebooting into recovery or bootloader
          sConfirmDialog =
              new AlertDialog.Builder(context)
                  .setTitle(titleResourceId)
                  .setSingleChoiceItems(
                      com.android.internal.R.array.shutdown_reboot_options,
                      0,
                      new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                          if (which < 0) return;

                          String actions[] =
                              context
                                  .getResources()
                                  .getStringArray(
                                      com.android.internal.R.array.shutdown_reboot_actions);

                          if (actions != null && which < actions.length)
                            mRebootReason = actions[which];
                        }
                      })
                  .setPositiveButton(
                      com.android.internal.R.string.yes,
                      new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                          mReboot = true;
                          beginShutdownSequence(context);
                        }
                      })
                  .setNegativeButton(
                      com.android.internal.R.string.no,
                      new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                          mReboot = false;
                          dialog.cancel();
                        }
                      })
                  .create();
          sConfirmDialog.setOnKeyListener(
              new DialogInterface.OnKeyListener() {
                public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                  if (keyCode == KeyEvent.KEYCODE_BACK) {
                    mReboot = false;
                    dialog.cancel();
                  }
                  return true;
                }
              });
        }
      }

      if (sConfirmDialog == null) {
        sConfirmDialog =
            new AlertDialog.Builder(context)
                .setTitle(titleResourceId)
                .setMessage(resourceId)
                .setPositiveButton(
                    com.android.internal.R.string.yes,
                    new DialogInterface.OnClickListener() {
                      public void onClick(DialogInterface dialog, int which) {
                        beginShutdownSequence(context);
                      }
                    })
                .setNegativeButton(com.android.internal.R.string.no, null)
                .create();
      }

      closer.dialog = sConfirmDialog;
      sConfirmDialog.setOnDismissListener(closer);
      sConfirmDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);
      sConfirmDialog.show();

    } else {
      beginShutdownSequence(context);
    }
  }