Maurice Lam | edb3944 | 2017-04-27 18:54:33 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.settings.password; |
| 18 | |
Ajay Nadathur | a1314df | 2018-01-04 13:06:46 -0800 | [diff] [blame] | 19 | import android.app.Activity; |
Maurice Lam | edb3944 | 2017-04-27 18:54:33 -0700 | [diff] [blame] | 20 | import android.app.Dialog; |
Maurice Lam | edb3944 | 2017-04-27 18:54:33 -0700 | [diff] [blame] | 21 | import android.app.admin.DevicePolicyManager; |
Fan Zhang | 31b2100 | 2019-01-16 13:49:47 -0800 | [diff] [blame] | 22 | import android.app.settings.SettingsEnums; |
Maurice Lam | edb3944 | 2017-04-27 18:54:33 -0700 | [diff] [blame] | 23 | import android.content.Context; |
| 24 | import android.content.DialogInterface; |
| 25 | import android.content.DialogInterface.OnClickListener; |
Ajay Nadathur | a1314df | 2018-01-04 13:06:46 -0800 | [diff] [blame] | 26 | import android.content.Intent; |
Maurice Lam | 35c9abd | 2017-05-19 15:23:26 -0700 | [diff] [blame] | 27 | import android.graphics.drawable.Drawable; |
Maurice Lam | edb3944 | 2017-04-27 18:54:33 -0700 | [diff] [blame] | 28 | import android.os.Bundle; |
| 29 | import android.view.LayoutInflater; |
| 30 | import android.view.View; |
| 31 | import android.view.ViewGroup; |
| 32 | import android.widget.ArrayAdapter; |
| 33 | import android.widget.TextView; |
| 34 | |
Fan Zhang | 23f8d59 | 2018-08-28 15:11:40 -0700 | [diff] [blame] | 35 | import androidx.appcompat.app.AlertDialog.Builder; |
| 36 | import androidx.fragment.app.Fragment; |
| 37 | |
Ajay Nadathur | a1314df | 2018-01-04 13:06:46 -0800 | [diff] [blame] | 38 | import com.android.internal.widget.LockPatternUtils; |
Maurice Lam | 35c9abd | 2017-05-19 15:23:26 -0700 | [diff] [blame] | 39 | import com.android.settings.R; |
Maurice Lam | edb3944 | 2017-04-27 18:54:33 -0700 | [diff] [blame] | 40 | import com.android.settings.core.instrumentation.InstrumentedDialogFragment; |
Ajay Nadathur | a1314df | 2018-01-04 13:06:46 -0800 | [diff] [blame] | 41 | import com.android.settings.password.ChooseLockGeneric.ChooseLockGenericFragment; |
Pasty Chang | c1f8600 | 2018-12-11 02:22:55 +0000 | [diff] [blame] | 42 | |
| 43 | import com.google.android.setupcompat.util.WizardManagerHelper; |
Maurice Lam | edb3944 | 2017-04-27 18:54:33 -0700 | [diff] [blame] | 44 | |
| 45 | import 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 | */ |
| 51 | public class ChooseLockTypeDialogFragment extends InstrumentedDialogFragment |
| 52 | implements OnClickListener { |
| 53 | |
| 54 | private static final String ARG_USER_ID = "userId"; |
Maurice Lam | edb3944 | 2017-04-27 18:54:33 -0700 | [diff] [blame] | 55 | |
| 56 | private ScreenLockAdapter mAdapter; |
| 57 | private ChooseLockGenericController mController; |
| 58 | |
Ajay Nadathur | a5f7392 | 2017-06-19 16:13:31 -0700 | [diff] [blame] | 59 | public static ChooseLockTypeDialogFragment newInstance(int userId) { |
Maurice Lam | edb3944 | 2017-04-27 18:54:33 -0700 | [diff] [blame] | 60 | Bundle args = new Bundle(); |
| 61 | args.putInt(ARG_USER_ID, userId); |
Maurice Lam | edb3944 | 2017-04-27 18:54:33 -0700 | [diff] [blame] | 62 | ChooseLockTypeDialogFragment fragment = new ChooseLockTypeDialogFragment(); |
| 63 | fragment.setArguments(args); |
| 64 | return fragment; |
| 65 | } |
| 66 | |
| 67 | public interface OnLockTypeSelectedListener { |
| 68 | void onLockTypeSelected(ScreenLockType lock); |
Ajay Nadathur | a1314df | 2018-01-04 13:06:46 -0800 | [diff] [blame] | 69 | |
| 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 Nadathur | b14b996 | 2018-01-10 11:37:23 -0800 | [diff] [blame] | 76 | copyBooleanExtra(activityIntent, intent, |
| 77 | ChooseLockSettingsHelper.EXTRA_KEY_HAS_CHALLENGE, false); |
| 78 | copyBooleanExtra(activityIntent, intent, |
| 79 | ChooseLockGenericFragment.EXTRA_SHOW_OPTIONS_BUTTON, false); |
Ajay Nadathur | a1314df | 2018-01-04 13:06:46 -0800 | [diff] [blame] | 80 | 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 Nadathur | a1314df | 2018-01-04 13:06:46 -0800 | [diff] [blame] | 86 | intent.putExtra(ChooseLockSettingsHelper.EXTRA_KEY_CHALLENGE, |
| 87 | activityIntent.getLongExtra(ChooseLockSettingsHelper.EXTRA_KEY_CHALLENGE, 0)); |
Ajay Nadathur | a1314df | 2018-01-04 13:06:46 -0800 | [diff] [blame] | 88 | WizardManagerHelper.copyWizardManagerExtras(activityIntent, intent); |
Ajay Nadathur | a1314df | 2018-01-04 13:06:46 -0800 | [diff] [blame] | 89 | activity.startActivity(intent); |
| 90 | activity.finish(); |
| 91 | } |
Ajay Nadathur | b14b996 | 2018-01-10 11:37:23 -0800 | [diff] [blame] | 92 | |
Maurice Lam | edb3944 | 2017-04-27 18:54:33 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Ajay Nadathur | b14b996 | 2018-01-10 11:37:23 -0800 | [diff] [blame] | 95 | 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 Lam | edb3944 | 2017-04-27 18:54:33 -0700 | [diff] [blame] | 101 | @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 Lam | edb3944 | 2017-04-27 18:54:33 -0700 | [diff] [blame] | 133 | mAdapter = new ScreenLockAdapter(context, locks, mController); |
| 134 | builder.setAdapter(mAdapter, this); |
Maurice Lam | 35c9abd | 2017-05-19 15:23:26 -0700 | [diff] [blame] | 135 | builder.setTitle(R.string.setup_lock_settings_options_dialog_title); |
tmfang | 27c84de | 2018-06-28 11:39:05 +0800 | [diff] [blame] | 136 | Dialog alertDialog = builder.create(); |
Maurice Lam | 35c9abd | 2017-05-19 15:23:26 -0700 | [diff] [blame] | 137 | return alertDialog; |
Maurice Lam | edb3944 | 2017-04-27 18:54:33 -0700 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | @Override |
| 141 | public int getMetricsCategory() { |
Fan Zhang | 31b2100 | 2019-01-16 13:49:47 -0800 | [diff] [blame] | 142 | return SettingsEnums.SETTINGS_CHOOSE_LOCK_DIALOG; |
Maurice Lam | edb3944 | 2017-04-27 18:54:33 -0700 | [diff] [blame] | 143 | } |
| 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 Lam | 35c9abd | 2017-05-19 15:23:26 -0700 | [diff] [blame] | 153 | super(context, R.layout.choose_lock_dialog_item, locks); |
Maurice Lam | edb3944 | 2017-04-27 18:54:33 -0700 | [diff] [blame] | 154 | mController = controller; |
| 155 | } |
| 156 | |
| 157 | @Override |
| 158 | public View getView(int position, View view, ViewGroup parent) { |
Maurice Lam | 35c9abd | 2017-05-19 15:23:26 -0700 | [diff] [blame] | 159 | Context context = parent.getContext(); |
Maurice Lam | edb3944 | 2017-04-27 18:54:33 -0700 | [diff] [blame] | 160 | if (view == null) { |
Maurice Lam | 35c9abd | 2017-05-19 15:23:26 -0700 | [diff] [blame] | 161 | view = LayoutInflater.from(context) |
| 162 | .inflate(R.layout.choose_lock_dialog_item, parent, false); |
Maurice Lam | edb3944 | 2017-04-27 18:54:33 -0700 | [diff] [blame] | 163 | } |
Maurice Lam | 35c9abd | 2017-05-19 15:23:26 -0700 | [diff] [blame] | 164 | 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 Lam | edb3944 | 2017-04-27 18:54:33 -0700 | [diff] [blame] | 169 | return view; |
| 170 | } |
Maurice Lam | 35c9abd | 2017-05-19 15:23:26 -0700 | [diff] [blame] | 171 | |
| 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 Lam | edb3944 | 2017-04-27 18:54:33 -0700 | [diff] [blame] | 187 | } |
| 188 | } |