/**
     * Invokes an activity to change the user's pattern, password or PIN based on given quality and
     * minimum quality specified by DevicePolicyManager. If quality is {@link
     * DevicePolicyManager#PASSWORD_QUALITY_UNSPECIFIED}, password is cleared.
     *
     * @param quality the desired quality. Ignored if DevicePolicyManager requires more security
     * @param disabled whether or not to show LockScreen at all. Only meaningful when quality is
     *     {@link DevicePolicyManager#PASSWORD_QUALITY_UNSPECIFIED}
     */
    void updateUnlockMethodAndFinish(int quality, boolean disabled) {
      // Sanity check. We should never get here without confirming user's existing password.
      if (!mPasswordConfirmed) {
        throw new IllegalStateException("Tried to update password without confirming it");
      }

      final boolean isFallback =
          getActivity()
              .getIntent()
              .getBooleanExtra(LockPatternUtils.LOCKSCREEN_BIOMETRIC_WEAK_FALLBACK, false);

      quality = upgradeQuality(quality, null);

      if (quality >= DevicePolicyManager.PASSWORD_QUALITY_NUMERIC) {
        int minLength = mDPM.getPasswordMinimumLength(null);
        if (minLength < MIN_PASSWORD_LENGTH) {
          minLength = MIN_PASSWORD_LENGTH;
        }
        final int maxLength = mDPM.getPasswordMaximumLength(quality);
        Intent intent = new Intent().setClass(getActivity(), ChooseLockPassword.class);
        intent.putExtra(LockPatternUtils.PASSWORD_TYPE_KEY, quality);
        intent.putExtra(ChooseLockPassword.PASSWORD_MIN_KEY, minLength);
        intent.putExtra(ChooseLockPassword.PASSWORD_MAX_KEY, maxLength);
        intent.putExtra(CONFIRM_CREDENTIALS, false);
        intent.putExtra(LockPatternUtils.LOCKSCREEN_BIOMETRIC_WEAK_FALLBACK, isFallback);
        if (isFallback) {
          startActivityForResult(intent, FALLBACK_REQUEST);
          return;
        } else {
          mFinishPending = true;
          intent.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);
          startActivity(intent);
        }
      } else if (quality == DevicePolicyManager.PASSWORD_QUALITY_SOMETHING) {
        Intent intent = new Intent(getActivity(), ChooseLockPattern.class);
        intent.putExtra("key_lock_method", "pattern");
        intent.putExtra(CONFIRM_CREDENTIALS, false);
        intent.putExtra(LockPatternUtils.LOCKSCREEN_BIOMETRIC_WEAK_FALLBACK, isFallback);
        if (isFallback) {
          startActivityForResult(intent, FALLBACK_REQUEST);
          return;
        } else {
          mFinishPending = true;
          intent.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);
          startActivity(intent);
        }
      } else if (quality == DevicePolicyManager.PASSWORD_QUALITY_BIOMETRIC_WEAK) {
        Intent intent = getBiometricSensorIntent();
        mFinishPending = true;
        startActivity(intent);
      } else if (quality == DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED) {
        mChooseLockSettingsHelper.utils().clearLock(false);
        mChooseLockSettingsHelper.utils().setLockScreenDisabled(disabled);
        getActivity().setResult(Activity.RESULT_OK);
        finish();
      } else {
        finish();
      }
    }
  /**
   * API: Query used to determine if a given policy is "active" (the device is operating at the
   * required security level).
   *
   * <p>This can be used when syncing a specific account, by passing a specific set of policies for
   * that account. Or, it can be used at any time to compare the device state against the aggregate
   * set of device policies stored in all accounts.
   *
   * <p>This method is for queries only, and does not trigger any change in device state.
   *
   * <p>NOTE: If there are multiple accounts with password expiration policies, the device password
   * will be set to expire in the shortest required interval (most secure). This method will return
   * 'false' as soon as the password expires - irrespective of which account caused the expiration.
   * In other words, all accounts (that require expiration) will run/stop based on the requirements
   * of the account with the shortest interval.
   *
   * @param policy the policies requested, or null to check aggregate stored policies
   * @return zero if the requested policies are active, non-zero bits indicates that more work is
   *     needed (typically, by the user) before the required security polices are fully active.
   */
  public int getInactiveReasons(Policy policy) {
    // select aggregate set if needed
    if (policy == null) {
      policy = getAggregatePolicy();
    }
    // quick check for the "empty set" of no policies
    if (policy == Policy.NO_POLICY) {
      return 0;
    }
    int reasons = 0;
    DevicePolicyManager dpm = getDPM();
    if (isActiveAdmin()) {
      // check each policy explicitly
      if (policy.mPasswordMinLength > 0) {
        if (dpm.getPasswordMinimumLength(mAdminName) < policy.mPasswordMinLength) {
          reasons |= INACTIVE_NEED_PASSWORD;
        }
      }
      if (policy.mPasswordMode > 0) {
        if (dpm.getPasswordQuality(mAdminName) < policy.getDPManagerPasswordQuality()) {
          reasons |= INACTIVE_NEED_PASSWORD;
        }
        if (!dpm.isActivePasswordSufficient()) {
          reasons |= INACTIVE_NEED_PASSWORD;
        }
      }
      if (policy.mMaxScreenLockTime > 0) {
        // Note, we use seconds, dpm uses milliseconds
        if (dpm.getMaximumTimeToLock(mAdminName) > policy.mMaxScreenLockTime * 1000) {
          reasons |= INACTIVE_NEED_CONFIGURATION;
        }
      }
      if (policy.mPasswordExpirationDays > 0) {
        // confirm that expirations are currently set
        long currentTimeout = dpm.getPasswordExpirationTimeout(mAdminName);
        if (currentTimeout == 0
            || currentTimeout > policy.getDPManagerPasswordExpirationTimeout()) {
          reasons |= INACTIVE_NEED_PASSWORD;
        }
        // confirm that the current password hasn't expired
        long expirationDate = dpm.getPasswordExpiration(mAdminName);
        long timeUntilExpiration = expirationDate - System.currentTimeMillis();
        boolean expired = timeUntilExpiration < 0;
        if (expired) {
          reasons |= INACTIVE_NEED_PASSWORD;
        }
      }
      if (policy.mPasswordHistory > 0) {
        if (dpm.getPasswordHistoryLength(mAdminName) < policy.mPasswordHistory) {
          // There's no user action for changes here; this is just a configuration change
          reasons |= INACTIVE_NEED_CONFIGURATION;
        }
      }
      if (policy.mPasswordComplexChars > 0) {
        if (dpm.getPasswordMinimumNonLetter(mAdminName) < policy.mPasswordComplexChars) {
          reasons |= INACTIVE_NEED_PASSWORD;
        }
      }
      if (policy.mRequireEncryption) {
        int encryptionStatus = getDPM().getStorageEncryptionStatus();
        if (encryptionStatus != DevicePolicyManager.ENCRYPTION_STATUS_ACTIVE) {
          reasons |= INACTIVE_NEED_ENCRYPTION;
        }
      }
      if (policy.mDontAllowCamera && !dpm.getCameraDisabled(mAdminName)) {
        reasons |= INACTIVE_NEED_CONFIGURATION;
      }
      // password failures are counted locally - no test required here
      // no check required for remote wipe (it's supported, if we're the admin)

      if (policy.mProtocolPoliciesUnsupported != null) {
        reasons |= INACTIVE_PROTOCOL_POLICIES;
      }

      // If we made it all the way, reasons == 0 here.  Otherwise it's a list of grievances.
      return reasons;
    }
    // return false, not active
    return INACTIVE_NEED_ACTIVATION;
  }