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; 18 19 import android.accessibilityservice.AccessibilityServiceInfo; 20 import android.app.Activity; 21 import android.app.Dialog; 22 import android.app.admin.DevicePolicyManager; 23 import android.app.settings.SettingsEnums; 24 import android.content.Context; 25 import android.content.DialogInterface; 26 import android.content.Intent; 27 import android.content.res.Resources; 28 import android.os.Bundle; 29 import android.util.Log; 30 import android.view.LayoutInflater; 31 import android.view.View; 32 import android.view.ViewGroup; 33 import android.view.accessibility.AccessibilityManager; 34 import android.widget.TextView; 35 36 import androidx.appcompat.app.AlertDialog; 37 38 import com.android.settings.core.InstrumentedFragment; 39 import com.android.settings.core.instrumentation.InstrumentedDialogFragment; 40 import com.android.settings.password.ChooseLockSettingsHelper; 41 42 import com.google.android.setupcompat.template.FooterBarMixin; 43 import com.google.android.setupcompat.template.FooterButton; 44 import com.google.android.setupdesign.GlifLayout; 45 46 import java.util.List; 47 48 public class EncryptionInterstitial extends SettingsActivity { 49 private static final String TAG = EncryptionInterstitial.class.getSimpleName(); 50 51 protected static final String EXTRA_PASSWORD_QUALITY = "extra_password_quality"; 52 protected static final String EXTRA_UNLOCK_METHOD_INTENT = "extra_unlock_method_intent"; 53 public static final String EXTRA_REQUIRE_PASSWORD = "extra_require_password"; 54 private static final int CHOOSE_LOCK_REQUEST = 100; 55 56 @Override getIntent()57 public Intent getIntent() { 58 Intent modIntent = new Intent(super.getIntent()); 59 modIntent.putExtra(EXTRA_SHOW_FRAGMENT, EncryptionInterstitialFragment.class.getName()); 60 return modIntent; 61 } 62 63 @Override onApplyThemeResource(Resources.Theme theme, int resid, boolean first)64 protected void onApplyThemeResource(Resources.Theme theme, int resid, boolean first) { 65 final int new_resid = SetupWizardUtils.getTheme(this, getIntent()); 66 super.onApplyThemeResource(theme, new_resid, first); 67 } 68 69 @Override isValidFragment(String fragmentName)70 protected boolean isValidFragment(String fragmentName) { 71 return EncryptionInterstitialFragment.class.getName().equals(fragmentName); 72 } 73 createStartIntent(Context ctx, int quality, boolean requirePasswordDefault, Intent unlockMethodIntent)74 public static Intent createStartIntent(Context ctx, int quality, 75 boolean requirePasswordDefault, Intent unlockMethodIntent) { 76 return new Intent(ctx, EncryptionInterstitial.class) 77 .putExtra(EXTRA_PASSWORD_QUALITY, quality) 78 .putExtra(EXTRA_SHOW_FRAGMENT_TITLE_RESID, R.string.encryption_interstitial_header) 79 .putExtra(EXTRA_REQUIRE_PASSWORD, requirePasswordDefault) 80 .putExtra(EXTRA_UNLOCK_METHOD_INTENT, unlockMethodIntent); 81 } 82 83 @Override onCreate(Bundle savedInstance)84 protected void onCreate(Bundle savedInstance) { 85 super.onCreate(savedInstance); 86 findViewById(R.id.content_parent).setFitsSystemWindows(false); 87 } 88 89 public static class EncryptionInterstitialFragment extends InstrumentedFragment { 90 91 private boolean mPasswordRequired; 92 private Intent mUnlockMethodIntent; 93 private int mRequestedPasswordQuality; 94 95 @Override getMetricsCategory()96 public int getMetricsCategory() { 97 return SettingsEnums.ENCRYPTION; 98 } 99 100 @Override onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)101 public View onCreateView( 102 LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 103 return inflater.inflate(R.layout.encryption_interstitial, container, false); 104 } 105 106 @Override onViewCreated(View view, Bundle savedInstanceState)107 public void onViewCreated(View view, Bundle savedInstanceState) { 108 super.onViewCreated(view, savedInstanceState); 109 110 final boolean forFingerprint = getActivity().getIntent().getBooleanExtra( 111 ChooseLockSettingsHelper.EXTRA_KEY_FOR_FINGERPRINT, false); 112 final boolean forFace = getActivity().getIntent() 113 .getBooleanExtra(ChooseLockSettingsHelper.EXTRA_KEY_FOR_FACE, false); 114 final boolean forBiometrics = getActivity().getIntent() 115 .getBooleanExtra(ChooseLockSettingsHelper.EXTRA_KEY_FOR_BIOMETRICS, false); 116 Intent intent = getActivity().getIntent(); 117 mRequestedPasswordQuality = intent.getIntExtra(EXTRA_PASSWORD_QUALITY, 0); 118 mUnlockMethodIntent = intent.getParcelableExtra(EXTRA_UNLOCK_METHOD_INTENT); 119 final int msgId; 120 switch (mRequestedPasswordQuality) { 121 case DevicePolicyManager.PASSWORD_QUALITY_SOMETHING: 122 msgId = forFingerprint ? 123 R.string.encryption_interstitial_message_pattern_for_fingerprint : 124 forFace ? 125 R.string.encryption_interstitial_message_pattern_for_face : 126 forBiometrics ? 127 R.string.encryption_interstitial_message_pattern_for_biometrics : 128 R.string.encryption_interstitial_message_pattern; 129 break; 130 case DevicePolicyManager.PASSWORD_QUALITY_NUMERIC: 131 case DevicePolicyManager.PASSWORD_QUALITY_NUMERIC_COMPLEX: 132 msgId = forFingerprint ? 133 R.string.encryption_interstitial_message_pin_for_fingerprint : 134 forFace ? 135 R.string.encryption_interstitial_message_pin_for_face : 136 forBiometrics ? 137 R.string.encryption_interstitial_message_pin_for_biometrics : 138 R.string.encryption_interstitial_message_pin; 139 break; 140 default: 141 msgId = forFingerprint ? 142 R.string.encryption_interstitial_message_password_for_fingerprint : 143 forFace ? 144 R.string.encryption_interstitial_message_password_for_face : 145 forBiometrics ? 146 R.string.encryption_interstitial_message_password_for_biometrics : 147 R.string.encryption_interstitial_message_password; 148 break; 149 } 150 TextView message = (TextView) getActivity().findViewById(R.id.sud_layout_description); 151 message.setText(msgId); 152 153 setRequirePasswordState(getActivity().getIntent().getBooleanExtra( 154 EXTRA_REQUIRE_PASSWORD, true)); 155 156 GlifLayout layout = (GlifLayout) view; 157 layout.setHeaderText(getActivity().getTitle()); 158 159 final FooterBarMixin mixin = layout.getMixin(FooterBarMixin.class); 160 mixin.setSecondaryButton( 161 new FooterButton.Builder(getContext()) 162 .setText(R.string.encryption_interstitial_no) 163 .setListener(this::onNoButtonClicked) 164 .setButtonType(FooterButton.ButtonType.SKIP) 165 .setTheme(R.style.SudGlifButton_Secondary) 166 .build() 167 ); 168 169 mixin.setPrimaryButton( 170 new FooterButton.Builder(getContext()) 171 .setText(R.string.encryption_interstitial_yes) 172 .setListener(this::onYesButtonClicked) 173 .setButtonType(FooterButton.ButtonType.NEXT) 174 .setTheme(R.style.SudGlifButton_Primary) 175 .build() 176 ); 177 } 178 startLockIntent()179 protected void startLockIntent() { 180 if (mUnlockMethodIntent != null) { 181 mUnlockMethodIntent.putExtra(EXTRA_REQUIRE_PASSWORD, mPasswordRequired); 182 startActivityForResult(mUnlockMethodIntent, CHOOSE_LOCK_REQUEST); 183 } else { 184 Log.wtf(TAG, "no unlock intent to start"); 185 finish(); 186 } 187 } 188 189 @Override onActivityResult(int requestCode, int resultCode, Intent data)190 public void onActivityResult(int requestCode, int resultCode, Intent data) { 191 super.onActivityResult(requestCode, resultCode, data); 192 if (requestCode == CHOOSE_LOCK_REQUEST && resultCode != RESULT_CANCELED) { 193 getActivity().setResult(resultCode, data); 194 finish(); 195 } 196 } 197 onYesButtonClicked(View view)198 private void onYesButtonClicked(View view) { 199 final boolean accEn = AccessibilityManager.getInstance(getActivity()).isEnabled(); 200 if (accEn && !mPasswordRequired) { 201 setRequirePasswordState(false); // clear the UI state 202 AccessibilityWarningDialogFragment.newInstance(mRequestedPasswordQuality) 203 .show( 204 getChildFragmentManager(), 205 AccessibilityWarningDialogFragment.TAG); 206 } else { 207 setRequirePasswordState(true); 208 startLockIntent(); 209 } 210 } 211 onNoButtonClicked(View view)212 private void onNoButtonClicked(View view) { 213 setRequirePasswordState(false); 214 startLockIntent(); 215 } 216 setRequirePasswordState(boolean required)217 private void setRequirePasswordState(boolean required) { 218 mPasswordRequired = required; 219 } 220 finish()221 public void finish() { 222 Activity activity = getActivity(); 223 if (activity == null) return; 224 if (getFragmentManager().getBackStackEntryCount() > 0) { 225 getFragmentManager().popBackStack(); 226 } else { 227 activity.finish(); 228 } 229 } 230 } 231 232 public static class AccessibilityWarningDialogFragment extends InstrumentedDialogFragment 233 implements DialogInterface.OnClickListener { 234 235 public static final String TAG = "AccessibilityWarningDialog"; 236 newInstance(int passwordQuality)237 public static AccessibilityWarningDialogFragment newInstance(int passwordQuality) { 238 AccessibilityWarningDialogFragment fragment = new AccessibilityWarningDialogFragment(); 239 Bundle args = new Bundle(1); 240 args.putInt(EXTRA_PASSWORD_QUALITY, passwordQuality); 241 fragment.setArguments(args); 242 return fragment; 243 } 244 245 @Override onCreateDialog(Bundle savedInstanceState)246 public Dialog onCreateDialog(Bundle savedInstanceState) { 247 final int titleId; 248 final int messageId; 249 switch (getArguments().getInt(EXTRA_PASSWORD_QUALITY)) { 250 case DevicePolicyManager.PASSWORD_QUALITY_SOMETHING: 251 titleId = R.string.encrypt_talkback_dialog_require_pattern; 252 messageId = R.string.encrypt_talkback_dialog_message_pattern; 253 break; 254 case DevicePolicyManager.PASSWORD_QUALITY_NUMERIC: 255 case DevicePolicyManager.PASSWORD_QUALITY_NUMERIC_COMPLEX: 256 titleId = R.string.encrypt_talkback_dialog_require_pin; 257 messageId = R.string.encrypt_talkback_dialog_message_pin; 258 break; 259 default: 260 titleId = R.string.encrypt_talkback_dialog_require_password; 261 messageId = R.string.encrypt_talkback_dialog_message_password; 262 break; 263 } 264 265 266 final Activity activity = getActivity(); 267 List<AccessibilityServiceInfo> list = 268 AccessibilityManager.getInstance(activity) 269 .getEnabledAccessibilityServiceList( 270 AccessibilityServiceInfo.FEEDBACK_ALL_MASK); 271 final CharSequence exampleAccessibility; 272 if (list.isEmpty()) { 273 // This should never happen. But we shouldn't crash 274 exampleAccessibility = ""; 275 } else { 276 exampleAccessibility = list.get(0).getResolveInfo() 277 .loadLabel(activity.getPackageManager()); 278 } 279 return new AlertDialog.Builder(activity) 280 .setTitle(titleId) 281 .setMessage(getString(messageId, exampleAccessibility)) 282 .setCancelable(true) 283 .setPositiveButton(android.R.string.ok, this) 284 .setNegativeButton(android.R.string.cancel, this) 285 .create(); 286 } 287 288 @Override getMetricsCategory()289 public int getMetricsCategory() { 290 return SettingsEnums.DIALOG_ENCRYPTION_INTERSTITIAL_ACCESSIBILITY; 291 } 292 293 @Override onClick(DialogInterface dialog, int which)294 public void onClick(DialogInterface dialog, int which) { 295 EncryptionInterstitialFragment fragment = 296 (EncryptionInterstitialFragment) getParentFragment(); 297 if (fragment != null) { 298 if (which == DialogInterface.BUTTON_POSITIVE) { 299 fragment.setRequirePasswordState(true); 300 fragment.startLockIntent(); 301 } else if (which == DialogInterface.BUTTON_NEGATIVE) { 302 fragment.setRequirePasswordState(false); 303 } 304 } 305 } 306 } 307 } 308