Home | History | Annotate | Download | only in settings
      1 /*
      2  * Copyright (C) 2010 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.settings;
     18 
     19 import android.annotation.Nullable;
     20 import android.app.Activity;
     21 import android.app.Fragment;
     22 import android.app.admin.DevicePolicyManager;
     23 import android.content.Intent;
     24 import android.os.UserHandle;
     25 
     26 import com.android.internal.widget.LockPatternUtils;
     27 
     28 public final class ChooseLockSettingsHelper {
     29 
     30     static final String EXTRA_KEY_TYPE = "type";
     31     static final String EXTRA_KEY_PASSWORD = "password";
     32     public static final String EXTRA_KEY_HAS_CHALLENGE = "has_challenge";
     33     public static final String EXTRA_KEY_CHALLENGE = "challenge";
     34     public static final String EXTRA_KEY_CHALLENGE_TOKEN = "hw_auth_token";
     35     public static final String EXTRA_KEY_FOR_FINGERPRINT = "for_fingerprint";
     36 
     37 
     38     private LockPatternUtils mLockPatternUtils;
     39     private Activity mActivity;
     40     private Fragment mFragment;
     41 
     42     public ChooseLockSettingsHelper(Activity activity) {
     43         mActivity = activity;
     44         mLockPatternUtils = new LockPatternUtils(activity);
     45     }
     46 
     47     public ChooseLockSettingsHelper(Activity activity, Fragment fragment) {
     48         this(activity);
     49         mFragment = fragment;
     50     }
     51 
     52     public LockPatternUtils utils() {
     53         return mLockPatternUtils;
     54     }
     55 
     56     /**
     57      * If a pattern, password or PIN exists, prompt the user before allowing them to change it.
     58      *
     59      * @param title title of the confirmation screen; shown in the action bar
     60      * @return true if one exists and we launched an activity to confirm it
     61      * @see Activity#onActivityResult(int, int, android.content.Intent)
     62      */
     63     boolean launchConfirmationActivity(int request, CharSequence title) {
     64         return launchConfirmationActivity(request, title, null, null, false, false);
     65     }
     66 
     67     /**
     68      * If a pattern, password or PIN exists, prompt the user before allowing them to change it.
     69      *
     70      * @param title title of the confirmation screen; shown in the action bar
     71      * @param returnCredentials if true, put credentials into intent. Note that if this is true,
     72      *                          this can only be called internally.
     73      * @return true if one exists and we launched an activity to confirm it
     74      * @see Activity#onActivityResult(int, int, android.content.Intent)
     75      */
     76     boolean launchConfirmationActivity(int request, CharSequence title, boolean returnCredentials) {
     77         return launchConfirmationActivity(request, title, null, null, returnCredentials, false);
     78     }
     79 
     80     /**
     81      * If a pattern, password or PIN exists, prompt the user before allowing them to change it.
     82      *
     83      * @param title title of the confirmation screen; shown in the action bar
     84      * @param header header of the confirmation screen; shown as large text
     85      * @param description description of the confirmation screen
     86      * @param returnCredentials if true, put credentials into intent. Note that if this is true,
     87      *                          this can only be called internally.
     88      * @param external specifies whether this activity is launched externally, meaning that it will
     89      *                 get a dark theme and allow fingerprint authentication
     90      * @return true if one exists and we launched an activity to confirm it
     91      * @see Activity#onActivityResult(int, int, android.content.Intent)
     92      */
     93     boolean launchConfirmationActivity(int request, @Nullable CharSequence title,
     94             @Nullable CharSequence header, @Nullable CharSequence description,
     95             boolean returnCredentials, boolean external) {
     96         return launchConfirmationActivity(request, title, header, description,
     97                 returnCredentials, external, false, 0);
     98     }
     99 
    100     /**
    101      * If a pattern, password or PIN exists, prompt the user before allowing them to change it.
    102      * @param message optional message to display about the action about to be done
    103      * @param details optional detail message to display
    104      * @param challenge a challenge to be verified against the device credential.
    105      *                  This method can only be called internally.
    106      * @return true if one exists and we launched an activity to confirm it
    107      * @see #onActivityResult(int, int, android.content.Intent)
    108      */
    109     public boolean launchConfirmationActivity(int request, @Nullable CharSequence title,
    110             @Nullable CharSequence header, @Nullable CharSequence description,
    111             long challenge) {
    112         return launchConfirmationActivity(request, title, header, description,
    113                 false, false, true, challenge);
    114     }
    115 
    116     private boolean launchConfirmationActivity(int request, @Nullable CharSequence title,
    117             @Nullable CharSequence header, @Nullable CharSequence description,
    118             boolean returnCredentials, boolean external, boolean hasChallenge,
    119             long challenge) {
    120         boolean launched = false;
    121 
    122         int effectiveUserId = Utils.getEffectiveUserId(mActivity);
    123 
    124         switch (mLockPatternUtils.getKeyguardStoredPasswordQuality(effectiveUserId)) {
    125             case DevicePolicyManager.PASSWORD_QUALITY_SOMETHING:
    126                 launched = launchConfirmationActivity(request, title, header, description,
    127                         returnCredentials || hasChallenge
    128                                 ? ConfirmLockPattern.InternalActivity.class
    129                                 : ConfirmLockPattern.class, external,
    130                                 hasChallenge, challenge);
    131                 break;
    132             case DevicePolicyManager.PASSWORD_QUALITY_NUMERIC:
    133             case DevicePolicyManager.PASSWORD_QUALITY_NUMERIC_COMPLEX:
    134             case DevicePolicyManager.PASSWORD_QUALITY_ALPHABETIC:
    135             case DevicePolicyManager.PASSWORD_QUALITY_ALPHANUMERIC:
    136             case DevicePolicyManager.PASSWORD_QUALITY_COMPLEX:
    137                 launched = launchConfirmationActivity(request, title, header, description,
    138                         returnCredentials || hasChallenge
    139                                 ? ConfirmLockPassword.InternalActivity.class
    140                                 : ConfirmLockPassword.class, external,
    141                                 hasChallenge, challenge);
    142                 break;
    143         }
    144         return launched;
    145     }
    146 
    147     private boolean launchConfirmationActivity(int request, CharSequence title, CharSequence header,
    148             CharSequence message, Class<?> activityClass, boolean external, boolean hasChallenge,
    149             long challenge) {
    150         final Intent intent = new Intent();
    151         intent.putExtra(ConfirmDeviceCredentialBaseFragment.TITLE_TEXT, title);
    152         intent.putExtra(ConfirmDeviceCredentialBaseFragment.HEADER_TEXT, header);
    153         intent.putExtra(ConfirmDeviceCredentialBaseFragment.DETAILS_TEXT, message);
    154         intent.putExtra(ConfirmDeviceCredentialBaseFragment.ALLOW_FP_AUTHENTICATION, external);
    155         intent.putExtra(ConfirmDeviceCredentialBaseFragment.DARK_THEME, external);
    156         intent.putExtra(ConfirmDeviceCredentialBaseFragment.SHOW_CANCEL_BUTTON, external);
    157         intent.putExtra(ConfirmDeviceCredentialBaseFragment.SHOW_WHEN_LOCKED, external);
    158         intent.putExtra(ChooseLockSettingsHelper.EXTRA_KEY_HAS_CHALLENGE, hasChallenge);
    159         intent.putExtra(ChooseLockSettingsHelper.EXTRA_KEY_CHALLENGE, challenge);
    160         intent.setClassName(ConfirmDeviceCredentialBaseFragment.PACKAGE, activityClass.getName());
    161         if (mFragment != null) {
    162             mFragment.startActivityForResult(intent, request);
    163         } else {
    164             mActivity.startActivityForResult(intent, request);
    165         }
    166         return true;
    167     }
    168 }
    169