blob: 8793471e206639f42e7867f6063c8b50156cdefe [file] [log] [blame]
Maurice Lamedb39442017-04-27 18:54:33 -07001/*
2 * Copyright (C) 2017 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
17package com.android.settings.password;
18
Ajay Nadathura1314df2018-01-04 13:06:46 -080019import android.app.Activity;
Maurice Lamedb39442017-04-27 18:54:33 -070020import android.app.Dialog;
Maurice Lamedb39442017-04-27 18:54:33 -070021import android.app.admin.DevicePolicyManager;
Fan Zhang31b21002019-01-16 13:49:47 -080022import android.app.settings.SettingsEnums;
Maurice Lamedb39442017-04-27 18:54:33 -070023import android.content.Context;
24import android.content.DialogInterface;
25import android.content.DialogInterface.OnClickListener;
Ajay Nadathura1314df2018-01-04 13:06:46 -080026import android.content.Intent;
Maurice Lam35c9abd2017-05-19 15:23:26 -070027import android.graphics.drawable.Drawable;
Maurice Lamedb39442017-04-27 18:54:33 -070028import android.os.Bundle;
29import android.view.LayoutInflater;
30import android.view.View;
31import android.view.ViewGroup;
32import android.widget.ArrayAdapter;
33import android.widget.TextView;
34
Fan Zhang23f8d592018-08-28 15:11:40 -070035import androidx.appcompat.app.AlertDialog.Builder;
36import androidx.fragment.app.Fragment;
37
Ajay Nadathura1314df2018-01-04 13:06:46 -080038import com.android.internal.widget.LockPatternUtils;
Maurice Lam35c9abd2017-05-19 15:23:26 -070039import com.android.settings.R;
Maurice Lamedb39442017-04-27 18:54:33 -070040import com.android.settings.core.instrumentation.InstrumentedDialogFragment;
Ajay Nadathura1314df2018-01-04 13:06:46 -080041import com.android.settings.password.ChooseLockGeneric.ChooseLockGenericFragment;
Pasty Changc1f86002018-12-11 02:22:55 +000042
43import com.google.android.setupcompat.util.WizardManagerHelper;
Maurice Lamedb39442017-04-27 18:54:33 -070044
45import java.util.List;
46
47/**
48 * A dialog fragment similar to {@link ChooseLockGeneric} where the user can select from a few
49 * lock screen types.
50 */
51public class ChooseLockTypeDialogFragment extends InstrumentedDialogFragment
52 implements OnClickListener {
53
54 private static final String ARG_USER_ID = "userId";
Maurice Lamedb39442017-04-27 18:54:33 -070055
56 private ScreenLockAdapter mAdapter;
57 private ChooseLockGenericController mController;
58
Ajay Nadathura5f73922017-06-19 16:13:31 -070059 public static ChooseLockTypeDialogFragment newInstance(int userId) {
Maurice Lamedb39442017-04-27 18:54:33 -070060 Bundle args = new Bundle();
61 args.putInt(ARG_USER_ID, userId);
Maurice Lamedb39442017-04-27 18:54:33 -070062 ChooseLockTypeDialogFragment fragment = new ChooseLockTypeDialogFragment();
63 fragment.setArguments(args);
64 return fragment;
65 }
66
67 public interface OnLockTypeSelectedListener {
68 void onLockTypeSelected(ScreenLockType lock);
Ajay Nadathura1314df2018-01-04 13:06:46 -080069
70 default void startChooseLockActivity(ScreenLockType selectedLockType, Activity activity) {
71 Intent activityIntent = activity.getIntent();
72 Intent intent = new Intent(activity, SetupChooseLockGeneric.class);
73 intent.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);
74
75 // Copy the original extras into the new intent
Ajay Nadathurb14b9962018-01-10 11:37:23 -080076 copyBooleanExtra(activityIntent, intent,
77 ChooseLockSettingsHelper.EXTRA_KEY_HAS_CHALLENGE, false);
78 copyBooleanExtra(activityIntent, intent,
79 ChooseLockGenericFragment.EXTRA_SHOW_OPTIONS_BUTTON, false);
Ajay Nadathura1314df2018-01-04 13:06:46 -080080 if (activityIntent.hasExtra(
81 ChooseLockGenericFragment.EXTRA_CHOOSE_LOCK_GENERIC_EXTRAS)) {
82 intent.putExtras(activityIntent.getBundleExtra(
83 ChooseLockGenericFragment.EXTRA_CHOOSE_LOCK_GENERIC_EXTRAS));
84 }
85 intent.putExtra(LockPatternUtils.PASSWORD_TYPE_KEY, selectedLockType.defaultQuality);
Ajay Nadathura1314df2018-01-04 13:06:46 -080086 intent.putExtra(ChooseLockSettingsHelper.EXTRA_KEY_CHALLENGE,
87 activityIntent.getLongExtra(ChooseLockSettingsHelper.EXTRA_KEY_CHALLENGE, 0));
Ajay Nadathura1314df2018-01-04 13:06:46 -080088 WizardManagerHelper.copyWizardManagerExtras(activityIntent, intent);
Ajay Nadathura1314df2018-01-04 13:06:46 -080089 activity.startActivity(intent);
90 activity.finish();
91 }
Ajay Nadathurb14b9962018-01-10 11:37:23 -080092
Maurice Lamedb39442017-04-27 18:54:33 -070093 }
94
Ajay Nadathurb14b9962018-01-10 11:37:23 -080095 private static void copyBooleanExtra(Intent from, Intent to, String name,
96 boolean defaultValue) {
97 to.putExtra(name, from.getBooleanExtra(name, defaultValue));
98 }
99
100
Maurice Lamedb39442017-04-27 18:54:33 -0700101 @Override
102 public void onCreate(Bundle savedInstanceState) {
103 super.onCreate(savedInstanceState);
104 final int userId = getArguments().getInt(ARG_USER_ID);
105 mController = new ChooseLockGenericController(getContext(), userId);
106 }
107
108 @Override
109 public void onClick(DialogInterface dialogInterface, int i) {
110 OnLockTypeSelectedListener listener = null;
111 Fragment parentFragment = getParentFragment();
112 if (parentFragment instanceof OnLockTypeSelectedListener) {
113 listener = (OnLockTypeSelectedListener) parentFragment;
114 } else {
115 Context context = getContext();
116 if (context instanceof OnLockTypeSelectedListener) {
117 listener = (OnLockTypeSelectedListener) context;
118 }
119 }
120 if (listener != null) {
121 listener.onLockTypeSelected(mAdapter.getItem(i));
122 }
123 }
124
125 @Override
126 public Dialog onCreateDialog(Bundle savedInstanceState) {
127 Context context = getContext();
128 Builder builder = new Builder(context);
129 List<ScreenLockType> locks =
130 mController.getVisibleScreenLockTypes(
131 DevicePolicyManager.PASSWORD_QUALITY_SOMETHING,
132 false /* includeDisabled */);
Maurice Lamedb39442017-04-27 18:54:33 -0700133 mAdapter = new ScreenLockAdapter(context, locks, mController);
134 builder.setAdapter(mAdapter, this);
Maurice Lam35c9abd2017-05-19 15:23:26 -0700135 builder.setTitle(R.string.setup_lock_settings_options_dialog_title);
tmfang27c84de2018-06-28 11:39:05 +0800136 Dialog alertDialog = builder.create();
Maurice Lam35c9abd2017-05-19 15:23:26 -0700137 return alertDialog;
Maurice Lamedb39442017-04-27 18:54:33 -0700138 }
139
140 @Override
141 public int getMetricsCategory() {
Fan Zhang31b21002019-01-16 13:49:47 -0800142 return SettingsEnums.SETTINGS_CHOOSE_LOCK_DIALOG;
Maurice Lamedb39442017-04-27 18:54:33 -0700143 }
144
145 private static class ScreenLockAdapter extends ArrayAdapter<ScreenLockType> {
146
147 private final ChooseLockGenericController mController;
148
149 ScreenLockAdapter(
150 Context context,
151 List<ScreenLockType> locks,
152 ChooseLockGenericController controller) {
Maurice Lam35c9abd2017-05-19 15:23:26 -0700153 super(context, R.layout.choose_lock_dialog_item, locks);
Maurice Lamedb39442017-04-27 18:54:33 -0700154 mController = controller;
155 }
156
157 @Override
158 public View getView(int position, View view, ViewGroup parent) {
Maurice Lam35c9abd2017-05-19 15:23:26 -0700159 Context context = parent.getContext();
Maurice Lamedb39442017-04-27 18:54:33 -0700160 if (view == null) {
Maurice Lam35c9abd2017-05-19 15:23:26 -0700161 view = LayoutInflater.from(context)
162 .inflate(R.layout.choose_lock_dialog_item, parent, false);
Maurice Lamedb39442017-04-27 18:54:33 -0700163 }
Maurice Lam35c9abd2017-05-19 15:23:26 -0700164 ScreenLockType lock = getItem(position);
165 TextView textView = (TextView) view;
166 textView.setText(mController.getTitle(lock));
167 textView.setCompoundDrawablesRelativeWithIntrinsicBounds(
168 getIconForScreenLock(context, lock), null, null, null);
Maurice Lamedb39442017-04-27 18:54:33 -0700169 return view;
170 }
Maurice Lam35c9abd2017-05-19 15:23:26 -0700171
172 private static Drawable getIconForScreenLock(Context context, ScreenLockType lock) {
173 switch (lock) {
174 case PATTERN:
175 return context.getDrawable(R.drawable.ic_pattern);
176 case PIN:
177 return context.getDrawable(R.drawable.ic_pin);
178 case PASSWORD:
179 return context.getDrawable(R.drawable.ic_password);
180 case NONE:
181 case SWIPE:
182 case MANAGED:
183 default:
184 return null;
185 }
186 }
Maurice Lamedb39442017-04-27 18:54:33 -0700187 }
188}