1 /*
2  * Copyright (C) 2016 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.AlertDialog;
21 import android.app.Dialog;
22 import android.app.settings.SettingsEnums;
23 import android.content.DialogInterface;
24 import android.os.Bundle;
25 import android.view.View;
26 import android.view.inputmethod.InputMethodManager;
27 
28 import androidx.annotation.NonNull;
29 import androidx.annotation.StringRes;
30 import androidx.fragment.app.FragmentManager;
31 
32 import com.android.settings.R;
33 import com.android.settings.core.instrumentation.InstrumentedDialogFragment;
34 
35 public class SetupSkipDialog extends InstrumentedDialogFragment
36         implements DialogInterface.OnClickListener {
37 
38     public static final String EXTRA_FRP_SUPPORTED = ":settings:frp_supported";
39 
40     private static final String ARG_FRP_SUPPORTED = "frp_supported";
41     // The key indicates type of lock screen is pattern setup.
42     private static final String ARG_LOCK_TYPE_PATTERN = "lock_type_pattern";
43     // The key indicates type of lock screen setup is alphanumeric for password setup.
44     private static final String ARG_LOCK_TYPE_ALPHANUMERIC = "lock_type_alphanumeric";
45     private static final String TAG_SKIP_DIALOG = "skip_dialog";
46     public static final int RESULT_SKIP = Activity.RESULT_FIRST_USER + 10;
47 
newInstance(boolean isFrpSupported, boolean isPatternMode, boolean isAlphanumericMode, boolean forFingerprint, boolean forFace, boolean forBiometrics)48     public static SetupSkipDialog newInstance(boolean isFrpSupported, boolean isPatternMode,
49             boolean isAlphanumericMode, boolean forFingerprint, boolean forFace,
50             boolean forBiometrics) {
51         SetupSkipDialog dialog = new SetupSkipDialog();
52         Bundle args = new Bundle();
53         args.putBoolean(ARG_FRP_SUPPORTED, isFrpSupported);
54         args.putBoolean(ARG_LOCK_TYPE_PATTERN, isPatternMode);
55         args.putBoolean(ARG_LOCK_TYPE_ALPHANUMERIC, isAlphanumericMode);
56         args.putBoolean(ChooseLockSettingsHelper.EXTRA_KEY_FOR_FINGERPRINT, forFingerprint);
57         args.putBoolean(ChooseLockSettingsHelper.EXTRA_KEY_FOR_FACE, forFace);
58         args.putBoolean(ChooseLockSettingsHelper.EXTRA_KEY_FOR_BIOMETRICS, forBiometrics);
59         dialog.setArguments(args);
60         return dialog;
61     }
62 
63     @Override
getMetricsCategory()64     public int getMetricsCategory() {
65         return SettingsEnums.DIALOG_FINGERPRINT_SKIP_SETUP;
66     }
67 
68     @Override
onCreateDialog(Bundle savedInstanceState)69     public Dialog onCreateDialog(Bundle savedInstanceState) {
70         return onCreateDialogBuilder().create();
71     }
72 
73     @NonNull
onCreateDialogBuilder()74     public AlertDialog.Builder onCreateDialogBuilder() {
75         Bundle args = getArguments();
76         final boolean forFace =
77                 args.getBoolean(ChooseLockSettingsHelper.EXTRA_KEY_FOR_FACE);
78         final boolean forFingerprint =
79                 args.getBoolean(ChooseLockSettingsHelper.EXTRA_KEY_FOR_FINGERPRINT);
80         final boolean forBiometrics =
81                 args.getBoolean(ChooseLockSettingsHelper.EXTRA_KEY_FOR_BIOMETRICS);
82         if (forFace || forFingerprint || forBiometrics) {
83             final boolean hasFace = forFace || forBiometrics;
84             final boolean hasFingerprint = forFingerprint || forBiometrics;
85 
86             final int titleId;
87             final int msgResId;
88             if (args.getBoolean(ARG_LOCK_TYPE_PATTERN)) {
89                 titleId = getPatternSkipTitleRes(hasFace, hasFingerprint);
90                 msgResId = getPatternSkipMessageRes(hasFace, hasFingerprint);
91             } else if (args.getBoolean(ARG_LOCK_TYPE_ALPHANUMERIC)) {
92                 titleId = getPasswordSkipTitleRes(hasFace, hasFingerprint);
93                 msgResId = getPasswordSkipMessageRes(hasFace, hasFingerprint);
94             } else {
95                 titleId = getPinSkipTitleRes(hasFace, hasFingerprint);
96                 msgResId = getPinSkipMessageRes(hasFace, hasFingerprint);
97             }
98 
99             return new AlertDialog.Builder(getContext())
100                     .setPositiveButton(R.string.skip_lock_screen_dialog_button_label, this)
101                     .setNegativeButton(R.string.cancel_lock_screen_dialog_button_label, this)
102                     .setTitle(titleId)
103                     .setMessage(msgResId);
104         } else {
105             return new AlertDialog.Builder(getContext())
106                     .setPositiveButton(R.string.skip_anyway_button_label, this)
107                     .setNegativeButton(R.string.go_back_button_label, this)
108                     .setTitle(R.string.lock_screen_intro_skip_title)
109                     .setMessage(args.getBoolean(ARG_FRP_SUPPORTED) ?
110                             R.string.lock_screen_intro_skip_dialog_text_frp :
111                             R.string.lock_screen_intro_skip_dialog_text);
112         }
113     }
114 
115     @StringRes
getPatternSkipTitleRes(boolean hasFace, boolean hasFingerprint)116     private int getPatternSkipTitleRes(boolean hasFace, boolean hasFingerprint) {
117         if (hasFace && hasFingerprint) {
118             return R.string.lock_screen_pattern_skip_biometrics_title;
119         } else if (hasFace) {
120             return R.string.lock_screen_pattern_skip_face_title;
121         } else if (hasFingerprint) {
122             return R.string.lock_screen_pattern_skip_fingerprint_title;
123         } else {
124             return R.string.lock_screen_pattern_skip_title;
125         }
126     }
127 
128     @StringRes
getPatternSkipMessageRes(boolean hasFace, boolean hasFingerprint)129     private int getPatternSkipMessageRes(boolean hasFace, boolean hasFingerprint) {
130         if (hasFace && hasFingerprint) {
131             return R.string.lock_screen_pattern_skip_biometrics_message;
132         } else if (hasFace) {
133             return R.string.lock_screen_pattern_skip_face_message;
134         } else if (hasFingerprint) {
135             return R.string.lock_screen_pattern_skip_fingerprint_message;
136         } else {
137             return R.string.lock_screen_pattern_skip_message;
138         }
139     }
140 
141     @StringRes
getPasswordSkipTitleRes(boolean hasFace, boolean hasFingerprint)142     private int getPasswordSkipTitleRes(boolean hasFace, boolean hasFingerprint) {
143         if (hasFace && hasFingerprint) {
144             return R.string.lock_screen_password_skip_biometrics_title;
145         } else if (hasFace) {
146             return R.string.lock_screen_password_skip_face_title;
147         } else if (hasFingerprint) {
148             return R.string.lock_screen_password_skip_fingerprint_title;
149         } else {
150             return R.string.lock_screen_password_skip_title;
151         }
152     }
153 
154     @StringRes
getPasswordSkipMessageRes(boolean hasFace, boolean hasFingerprint)155     private int getPasswordSkipMessageRes(boolean hasFace, boolean hasFingerprint) {
156         if (hasFace && hasFingerprint) {
157             return R.string.lock_screen_password_skip_biometrics_message;
158         } else if (hasFace) {
159             return R.string.lock_screen_password_skip_face_message;
160         } else if (hasFingerprint) {
161             return R.string.lock_screen_password_skip_fingerprint_message;
162         } else {
163             return R.string.lock_screen_password_skip_message;
164         }
165     }
166 
167     @StringRes
getPinSkipTitleRes(boolean hasFace, boolean hasFingerprint)168     private int getPinSkipTitleRes(boolean hasFace, boolean hasFingerprint) {
169         if (hasFace && hasFingerprint) {
170             return R.string.lock_screen_pin_skip_biometrics_title;
171         } else if (hasFace) {
172             return R.string.lock_screen_pin_skip_face_title;
173         } else if (hasFingerprint) {
174             return R.string.lock_screen_pin_skip_fingerprint_title;
175         } else {
176             return R.string.lock_screen_pin_skip_title;
177         }
178     }
179 
180     @StringRes
getPinSkipMessageRes(boolean hasFace, boolean hasFingerprint)181     private int getPinSkipMessageRes(boolean hasFace, boolean hasFingerprint) {
182         if (hasFace && hasFingerprint) {
183             return R.string.lock_screen_pin_skip_biometrics_message;
184         } else if (hasFace) {
185             return R.string.lock_screen_pin_skip_face_message;
186         } else if (hasFingerprint) {
187             return R.string.lock_screen_pin_skip_fingerprint_message;
188         } else {
189             return R.string.lock_screen_pin_skip_message;
190         }
191     }
192 
193     @Override
onClick(DialogInterface dialog, int button)194     public void onClick(DialogInterface dialog, int button) {
195         Activity activity = getActivity();
196         switch (button) {
197             case DialogInterface.BUTTON_POSITIVE:
198                 activity.setResult(RESULT_SKIP);
199                 activity.finish();
200                 break;
201             case DialogInterface.BUTTON_NEGATIVE:
202                 View view = activity.getCurrentFocus();
203                 if(view != null) {
204                     view.requestFocus();
205                     InputMethodManager imm = (InputMethodManager) activity
206                             .getSystemService(Activity.INPUT_METHOD_SERVICE);
207                     imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);
208                 }
209                 break;
210         }
211     }
212 
show(FragmentManager manager)213     public void show(FragmentManager manager) {
214         show(manager, TAG_SKIP_DIALOG);
215     }
216 }