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