Home | History | Annotate | Download | only in password
      1 /*
      2  * Copyright (C) 2014 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.password;
     18 
     19 import android.app.Activity;
     20 import android.app.Fragment;
     21 import android.app.admin.DevicePolicyManager;
     22 import android.content.Context;
     23 import android.content.Intent;
     24 import android.os.Bundle;
     25 import android.support.annotation.Nullable;
     26 import android.util.Log;
     27 import android.view.View;
     28 import android.widget.Button;
     29 import android.widget.LinearLayout;
     30 
     31 import com.android.internal.annotations.VisibleForTesting;
     32 import com.android.internal.widget.LockPatternUtils;
     33 import com.android.settings.R;
     34 import com.android.settings.SetupRedactionInterstitial;
     35 import com.android.settings.password.ChooseLockGeneric.ChooseLockGenericFragment;
     36 import com.android.settings.password.ChooseLockTypeDialogFragment.OnLockTypeSelectedListener;
     37 import com.android.setupwizardlib.util.WizardManagerHelper;
     38 
     39 /**
     40  * Setup Wizard's version of ChooseLockPassword screen. It inherits the logic and basic structure
     41  * from ChooseLockPassword class, and should remain similar to that behaviorally. This class should
     42  * only overload base methods for minor theme and behavior differences specific to Setup Wizard.
     43  * Other changes should be done to ChooseLockPassword class instead and let this class inherit
     44  * those changes.
     45  */
     46 public class SetupChooseLockPassword extends ChooseLockPassword {
     47 
     48     private static final String TAG = "SetupChooseLockPassword";
     49 
     50     public static Intent modifyIntentForSetup(
     51             Context context,
     52             Intent chooseLockPasswordIntent) {
     53         chooseLockPasswordIntent.setClass(context, SetupChooseLockPassword.class);
     54         chooseLockPasswordIntent.putExtra(EXTRA_PREFS_SHOW_BUTTON_BAR, false);
     55         return chooseLockPasswordIntent;
     56     }
     57 
     58     @Override
     59     protected boolean isValidFragment(String fragmentName) {
     60         return SetupChooseLockPasswordFragment.class.getName().equals(fragmentName);
     61     }
     62 
     63     @Override
     64     /* package */ Class<? extends Fragment> getFragmentClass() {
     65         return SetupChooseLockPasswordFragment.class;
     66     }
     67 
     68     @Override
     69     protected void onCreate(Bundle savedInstance) {
     70         super.onCreate(savedInstance);
     71         LinearLayout layout = (LinearLayout) findViewById(R.id.content_parent);
     72         layout.setFitsSystemWindows(false);
     73     }
     74 
     75     public static class SetupChooseLockPasswordFragment extends ChooseLockPasswordFragment
     76             implements OnLockTypeSelectedListener {
     77 
     78         @VisibleForTesting
     79         static final int REQUEST_SCREEN_LOCK_OPTIONS = 1;
     80 
     81         @Nullable
     82         private Button mOptionsButton;
     83 
     84         @Override
     85         public void onViewCreated(View view, Bundle savedInstanceState) {
     86             super.onViewCreated(view, savedInstanceState);
     87             final Activity activity = getActivity();
     88             ChooseLockGenericController chooseLockGenericController =
     89                     new ChooseLockGenericController(activity, mUserId);
     90             boolean anyOptionsShown = chooseLockGenericController.getVisibleScreenLockTypes(
     91                     DevicePolicyManager.PASSWORD_QUALITY_SOMETHING, false).size() > 0;
     92             boolean showOptionsButton = activity.getIntent().getBooleanExtra(
     93                     ChooseLockGenericFragment.EXTRA_SHOW_OPTIONS_BUTTON, false);
     94 
     95             if (!anyOptionsShown) {
     96                 Log.w(TAG, "Visible screen lock types is empty!");
     97             }
     98 
     99             if (showOptionsButton && anyOptionsShown) {
    100                 mOptionsButton = view.findViewById(R.id.screen_lock_options);
    101                 mOptionsButton.setVisibility(View.VISIBLE);
    102                 mOptionsButton.setOnClickListener(this);
    103             }
    104         }
    105 
    106         @Override
    107         public void onClick(View v) {
    108             switch (v.getId()) {
    109                 case R.id.screen_lock_options:
    110                     launchChooseLockGeneric();
    111                     break;
    112                 case R.id.cancel_button:
    113                     SetupSkipDialog dialog = SetupSkipDialog.newInstance(
    114                             getActivity().getIntent()
    115                                     .getBooleanExtra(SetupSkipDialog.EXTRA_FRP_SUPPORTED, false));
    116                     dialog.show(getFragmentManager());
    117                     break;
    118                 default:
    119                     super.onClick(v);
    120             }
    121         }
    122 
    123         private void launchChooseLockGeneric() {
    124             ChooseLockTypeDialogFragment.newInstance(mUserId)
    125                     .show(getChildFragmentManager(), null);
    126         }
    127 
    128         @Override
    129         protected Intent getRedactionInterstitialIntent(Context context) {
    130             // Setup wizard's redaction interstitial is deferred to optional step. Enable that
    131             // optional step if the lock screen was set up.
    132             SetupRedactionInterstitial.setEnabled(context, true);
    133             return null;
    134         }
    135 
    136         @Override
    137         public void onLockTypeSelected(ScreenLockType lock) {
    138             ScreenLockType currentLockType = mIsAlphaMode ?
    139                     ScreenLockType.PASSWORD : ScreenLockType.PIN;
    140             if (currentLockType.equals(lock)) {
    141                 // ignore same lock type.
    142                 return;
    143             }
    144             Intent activityIntent = getActivity().getIntent();
    145             Intent intent = new Intent(getContext(), SetupChooseLockGeneric.class);
    146 
    147             // Copy the original extras into the new intent
    148             if (activityIntent
    149                     .hasExtra(ChooseLockGenericFragment.EXTRA_CHOOSE_LOCK_GENERIC_EXTRAS)) {
    150                 intent.putExtras(activityIntent.getBundleExtra(
    151                         ChooseLockGenericFragment.EXTRA_CHOOSE_LOCK_GENERIC_EXTRAS));
    152             }
    153             intent.putExtra(LockPatternUtils.PASSWORD_TYPE_KEY, lock.defaultQuality);
    154 
    155             // Propagate the fingerprint challenge
    156             intent.putExtra(ChooseLockSettingsHelper.EXTRA_KEY_HAS_CHALLENGE,
    157                     activityIntent.getBooleanExtra(ChooseLockSettingsHelper.EXTRA_KEY_HAS_CHALLENGE,
    158                             false));
    159             intent.putExtra(ChooseLockSettingsHelper.EXTRA_KEY_CHALLENGE,
    160                     activityIntent.getLongExtra(ChooseLockSettingsHelper.EXTRA_KEY_CHALLENGE, 0));
    161 
    162             // The user is already given the choice of the what screen lock to set up. No need to
    163             // show this button again.
    164             intent.putExtra(ChooseLockGenericFragment.EXTRA_SHOW_OPTIONS_BUTTON, false);
    165 
    166             WizardManagerHelper.copyWizardManagerExtras(activityIntent, intent);
    167 
    168             startActivityForResult(intent, REQUEST_SCREEN_LOCK_OPTIONS);
    169         }
    170 
    171         @Override
    172         public void onActivityResult(int requestCode, int resultCode, Intent data) {
    173             super.onActivityResult(requestCode, resultCode, data);
    174             if (requestCode == REQUEST_SCREEN_LOCK_OPTIONS) {
    175                 if (resultCode != Activity.RESULT_CANCELED) {
    176                     Activity activity = getActivity();
    177                     activity.setResult(resultCode, data);
    178                     activity.finish();
    179                 }
    180             }
    181         }
    182 
    183         @Override
    184         protected void updateUi() {
    185             super.updateUi();
    186             if (mForFingerprint) {
    187                 mCancelButton.setVisibility(View.GONE);
    188             } else {
    189                 mCancelButton.setText(R.string.skip_label);
    190             }
    191 
    192             if (mOptionsButton != null) {
    193                 mOptionsButton.setVisibility(
    194                         mUiStage == Stage.Introduction ? View.VISIBLE : View.GONE);
    195             }
    196         }
    197     }
    198 }
    199