• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2007 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 
20 import java.util.Observable;
21 import java.util.Observer;
22 
23 import android.app.Activity;
24 import android.app.AlertDialog;
25 import android.app.Dialog;
26 import android.app.admin.DevicePolicyManager;
27 import android.content.ContentQueryMap;
28 import android.content.ContentResolver;
29 import android.content.Context;
30 import android.content.DialogInterface;
31 import android.content.Intent;
32 import android.database.Cursor;
33 import android.location.LocationManager;
34 import android.os.Bundle;
35 import android.os.SystemProperties;
36 import android.preference.CheckBoxPreference;
37 import android.preference.ListPreference;
38 import android.preference.Preference;
39 import android.preference.PreferenceActivity;
40 import android.preference.PreferenceCategory;
41 import android.preference.PreferenceManager;
42 import android.preference.PreferenceScreen;
43 import android.preference.Preference.OnPreferenceChangeListener;
44 import android.provider.Settings;
45 import android.security.Credentials;
46 import android.security.KeyStore;
47 import android.telephony.TelephonyManager;
48 import android.util.Log;
49 import android.view.View;
50 import android.widget.TextView;
51 import android.widget.Toast;
52 
53 import com.android.internal.widget.LockPatternUtils;
54 
55 /**
56  * Gesture lock pattern settings.
57  */
58 public class SecuritySettings extends PreferenceActivity {
59     private static final String KEY_UNLOCK_SET_OR_CHANGE = "unlock_set_or_change";
60 
61     // Lock Settings
62     private static final String PACKAGE = "com.android.settings";
63     private static final String ICC_LOCK_SETTINGS = PACKAGE + ".IccLockSettings";
64 
65     private static final String KEY_LOCK_ENABLED = "lockenabled";
66     private static final String KEY_VISIBLE_PATTERN = "visiblepattern";
67     private static final String KEY_TACTILE_FEEDBACK_ENABLED = "unlock_tactile_feedback";
68 
69     // Encrypted File Systems constants
70     private static final String PROPERTY_EFS_ENABLED = "persist.security.efs.enabled";
71     private static final String PROPERTY_EFS_TRANSITION = "persist.security.efs.trans";
72 
73     private CheckBoxPreference mVisiblePattern;
74     private CheckBoxPreference mTactileFeedback;
75 
76     private CheckBoxPreference mShowPassword;
77 
78     // Location Settings
79     private static final String LOCATION_NETWORK = "location_network";
80     private static final String LOCATION_GPS = "location_gps";
81     private static final String ASSISTED_GPS = "assisted_gps";
82     private static final int SET_OR_CHANGE_LOCK_METHOD_REQUEST = 123;
83 
84     // Credential storage
85     private CredentialStorage mCredentialStorage = new CredentialStorage();
86 
87     // Encrypted file system
88     private  CheckBoxPreference mEncryptedFSEnabled;
89 
90     private CheckBoxPreference mNetwork;
91     private CheckBoxPreference mGps;
92     private CheckBoxPreference mAssistedGps;
93 
94     DevicePolicyManager mDPM;
95 
96     // These provide support for receiving notification when Location Manager settings change.
97     // This is necessary because the Network Location Provider can change settings
98     // if the user does not confirm enabling the provider.
99     private ContentQueryMap mContentQueryMap;
100     private ChooseLockSettingsHelper mChooseLockSettingsHelper;
101     private LockPatternUtils mLockPatternUtils;
102     private final class SettingsObserver implements Observer {
update(Observable o, Object arg)103         public void update(Observable o, Object arg) {
104             updateToggles();
105         }
106     }
107 
108     @Override
onCreate(Bundle savedInstanceState)109     protected void onCreate(Bundle savedInstanceState) {
110         super.onCreate(savedInstanceState);
111 
112         mLockPatternUtils = new LockPatternUtils(this);
113 
114         mDPM = (DevicePolicyManager)getSystemService(Context.DEVICE_POLICY_SERVICE);
115 
116         mChooseLockSettingsHelper = new ChooseLockSettingsHelper(this);
117 
118         createPreferenceHierarchy();
119 
120         updateToggles();
121 
122         // listen for Location Manager settings changes
123         Cursor settingsCursor = getContentResolver().query(Settings.Secure.CONTENT_URI, null,
124                 "(" + Settings.System.NAME + "=?)",
125                 new String[]{Settings.Secure.LOCATION_PROVIDERS_ALLOWED},
126                 null);
127         mContentQueryMap = new ContentQueryMap(settingsCursor, Settings.System.NAME, true, null);
128         mContentQueryMap.addObserver(new SettingsObserver());
129     }
130 
createPreferenceHierarchy()131     private PreferenceScreen createPreferenceHierarchy() {
132         PreferenceScreen root = this.getPreferenceScreen();
133         if (root != null) {
134             root.removeAll();
135         }
136         addPreferencesFromResource(R.xml.security_settings);
137         root = this.getPreferenceScreen();
138 
139         mNetwork = (CheckBoxPreference) getPreferenceScreen().findPreference(LOCATION_NETWORK);
140         mGps = (CheckBoxPreference) getPreferenceScreen().findPreference(LOCATION_GPS);
141         mAssistedGps = (CheckBoxPreference) getPreferenceScreen().findPreference(ASSISTED_GPS);
142 
143         PreferenceManager pm = getPreferenceManager();
144 
145         // Lock screen
146         if (!mLockPatternUtils.isSecure()) {
147             addPreferencesFromResource(R.xml.security_settings_chooser);
148         } else {
149             switch (mLockPatternUtils.getKeyguardStoredPasswordQuality()) {
150                 case DevicePolicyManager.PASSWORD_QUALITY_SOMETHING:
151                     addPreferencesFromResource(R.xml.security_settings_pattern);
152                     break;
153                 case DevicePolicyManager.PASSWORD_QUALITY_NUMERIC:
154                     addPreferencesFromResource(R.xml.security_settings_pin);
155                     break;
156                 case DevicePolicyManager.PASSWORD_QUALITY_ALPHABETIC:
157                 case DevicePolicyManager.PASSWORD_QUALITY_ALPHANUMERIC:
158                     addPreferencesFromResource(R.xml.security_settings_password);
159                     break;
160             }
161         }
162 
163         // set or change current. Should be common to all unlock preference screens
164         // mSetOrChange = (PreferenceScreen) pm.findPreference(KEY_UNLOCK_SET_OR_CHANGE);
165 
166         // visible pattern
167         mVisiblePattern = (CheckBoxPreference) pm.findPreference(KEY_VISIBLE_PATTERN);
168 
169         // tactile feedback. Should be common to all unlock preference screens.
170         mTactileFeedback = (CheckBoxPreference) pm.findPreference(KEY_TACTILE_FEEDBACK_ENABLED);
171 
172         int activePhoneType = TelephonyManager.getDefault().getPhoneType();
173 
174         // do not display SIM lock for CDMA phone
175         if (TelephonyManager.PHONE_TYPE_CDMA != activePhoneType)
176         {
177             PreferenceScreen simLockPreferences = getPreferenceManager()
178                     .createPreferenceScreen(this);
179             simLockPreferences.setTitle(R.string.sim_lock_settings_category);
180             // Intent to launch SIM lock settings
181             simLockPreferences.setIntent(new Intent().setClassName(PACKAGE, ICC_LOCK_SETTINGS));
182             PreferenceCategory simLockCat = new PreferenceCategory(this);
183             simLockCat.setTitle(R.string.sim_lock_settings_title);
184             root.addPreference(simLockCat);
185             simLockCat.addPreference(simLockPreferences);
186         }
187 
188         // Passwords
189         PreferenceCategory passwordsCat = new PreferenceCategory(this);
190         passwordsCat.setTitle(R.string.security_passwords_title);
191         root.addPreference(passwordsCat);
192 
193         CheckBoxPreference showPassword = mShowPassword = new CheckBoxPreference(this);
194         showPassword.setKey("show_password");
195         showPassword.setTitle(R.string.show_password);
196         showPassword.setSummary(R.string.show_password_summary);
197         showPassword.setPersistent(false);
198         passwordsCat.addPreference(showPassword);
199 
200         // Device policies
201         PreferenceCategory devicePoliciesCat = new PreferenceCategory(this);
202         devicePoliciesCat.setTitle(R.string.device_admin_title);
203         root.addPreference(devicePoliciesCat);
204 
205         Preference deviceAdminButton = new Preference(this);
206         deviceAdminButton.setTitle(R.string.manage_device_admin);
207         deviceAdminButton.setSummary(R.string.manage_device_admin_summary);
208         Intent deviceAdminIntent = new Intent();
209         deviceAdminIntent.setClass(this, DeviceAdminSettings.class);
210         deviceAdminButton.setIntent(deviceAdminIntent);
211         devicePoliciesCat.addPreference(deviceAdminButton);
212 
213         // Credential storage
214         PreferenceCategory credentialsCat = new PreferenceCategory(this);
215         credentialsCat.setTitle(R.string.credentials_category);
216         root.addPreference(credentialsCat);
217         mCredentialStorage.createPreferences(credentialsCat, CredentialStorage.TYPE_KEYSTORE);
218 
219         // File System Encryption
220         PreferenceCategory encryptedfsCat = new PreferenceCategory(this);
221         encryptedfsCat.setTitle(R.string.encrypted_fs_category);
222         //root.addPreference(encryptedfsCat);
223         mCredentialStorage.createPreferences(encryptedfsCat, CredentialStorage.TYPE_ENCRYPTEDFS);
224         return root;
225     }
226 
227     @Override
onResume()228     protected void onResume() {
229         super.onResume();
230 
231         final LockPatternUtils lockPatternUtils = mChooseLockSettingsHelper.utils();
232         if (mVisiblePattern != null) {
233             mVisiblePattern.setChecked(lockPatternUtils.isVisiblePatternEnabled());
234         }
235         if (mTactileFeedback != null) {
236             mTactileFeedback.setChecked(lockPatternUtils.isTactileFeedbackEnabled());
237         }
238 
239         mShowPassword.setChecked(Settings.System.getInt(getContentResolver(),
240                 Settings.System.TEXT_SHOW_PASSWORD, 1) != 0);
241 
242         mCredentialStorage.resume();
243     }
244 
245     @Override
onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference)246     public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen,
247             Preference preference) {
248         final String key = preference.getKey();
249 
250         final LockPatternUtils lockPatternUtils = mChooseLockSettingsHelper.utils();
251         if (KEY_UNLOCK_SET_OR_CHANGE.equals(key)) {
252             Intent intent = new Intent(this, ChooseLockGeneric.class);
253             startActivityForResult(intent, SET_OR_CHANGE_LOCK_METHOD_REQUEST);
254         } else if (KEY_LOCK_ENABLED.equals(key)) {
255             lockPatternUtils.setLockPatternEnabled(isToggled(preference));
256         } else if (KEY_VISIBLE_PATTERN.equals(key)) {
257             lockPatternUtils.setVisiblePatternEnabled(isToggled(preference));
258         } else if (KEY_TACTILE_FEEDBACK_ENABLED.equals(key)) {
259             lockPatternUtils.setTactileFeedbackEnabled(isToggled(preference));
260         } else if (preference == mShowPassword) {
261             Settings.System.putInt(getContentResolver(), Settings.System.TEXT_SHOW_PASSWORD,
262                     mShowPassword.isChecked() ? 1 : 0);
263         } else if (preference == mNetwork) {
264             Settings.Secure.setLocationProviderEnabled(getContentResolver(),
265                     LocationManager.NETWORK_PROVIDER, mNetwork.isChecked());
266         } else if (preference == mGps) {
267             boolean enabled = mGps.isChecked();
268             Settings.Secure.setLocationProviderEnabled(getContentResolver(),
269                     LocationManager.GPS_PROVIDER, enabled);
270             if (mAssistedGps != null) {
271                 mAssistedGps.setEnabled(enabled);
272             }
273         } else if (preference == mAssistedGps) {
274             Settings.Secure.putInt(getContentResolver(), Settings.Secure.ASSISTED_GPS_ENABLED,
275                     mAssistedGps.isChecked() ? 1 : 0);
276         }
277 
278         return false;
279     }
280 
281     /*
282      * Creates toggles for each available location provider
283      */
updateToggles()284     private void updateToggles() {
285         ContentResolver res = getContentResolver();
286         boolean gpsEnabled = Settings.Secure.isLocationProviderEnabled(
287                 res, LocationManager.GPS_PROVIDER);
288         mNetwork.setChecked(Settings.Secure.isLocationProviderEnabled(
289                 res, LocationManager.NETWORK_PROVIDER));
290         mGps.setChecked(gpsEnabled);
291         if (mAssistedGps != null) {
292             mAssistedGps.setChecked(Settings.Secure.getInt(res,
293                     Settings.Secure.ASSISTED_GPS_ENABLED, 2) == 1);
294             mAssistedGps.setEnabled(gpsEnabled);
295         }
296     }
297 
isToggled(Preference pref)298     private boolean isToggled(Preference pref) {
299         return ((CheckBoxPreference) pref).isChecked();
300     }
301 
302     /**
303      * @see #confirmPatternThenDisableAndClear
304      */
305     @Override
onActivityResult(int requestCode, int resultCode, Intent data)306     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
307         super.onActivityResult(requestCode, resultCode, data);
308         createPreferenceHierarchy();
309     }
310 
311     private class CredentialStorage implements DialogInterface.OnClickListener,
312             DialogInterface.OnDismissListener, Preference.OnPreferenceChangeListener,
313             Preference.OnPreferenceClickListener {
314         private static final int MINIMUM_PASSWORD_LENGTH = 8;
315 
316         private static final int TYPE_KEYSTORE = 0;
317         private static final int TYPE_ENCRYPTEDFS = 1;
318 
319         // Dialog identifiers
320         private static final int DLG_BASE = 0;
321         private static final int DLG_UNLOCK = DLG_BASE + 1;
322         private static final int DLG_PASSWORD = DLG_UNLOCK + 1;
323         private static final int DLG_RESET = DLG_PASSWORD + 1;
324         private static final int DLG_ENABLE_EFS = DLG_RESET + 1;
325 
326         private KeyStore mKeyStore = KeyStore.getInstance();
327         private int mState;
328         private boolean mSubmit = false;
329         private boolean mExternal = false;
330 
331         private boolean mWillEnableEncryptedFS;
332         private int mShowingDialog = 0;
333 
334         // Key Store controls
335         private CheckBoxPreference mAccessCheckBox;
336         private Preference mInstallButton;
337         private Preference mPasswordButton;
338         private Preference mResetButton;
339 
340 
341         // Encrypted file system controls
342         private  CheckBoxPreference mEncryptedFSEnabled;
343 
resume()344         void resume() {
345             mState = mKeyStore.test();
346             updatePreferences(mState);
347 
348             Intent intent = getIntent();
349             if (!mExternal && intent != null &&
350                     Credentials.UNLOCK_ACTION.equals(intent.getAction())) {
351                 mExternal = true;
352                 if (mState == KeyStore.UNINITIALIZED) {
353                     showPasswordDialog();
354                 } else if (mState == KeyStore.LOCKED) {
355                     showUnlockDialog();
356                 } else {
357                     finish();
358                 }
359             }
360         }
361 
initialize(String password)362         private void initialize(String password) {
363             mKeyStore.password(password);
364             updatePreferences(KeyStore.NO_ERROR);
365         }
366 
reset()367         private void reset() {
368             mKeyStore.reset();
369             updatePreferences(KeyStore.UNINITIALIZED);
370         }
371 
lock()372         private void lock() {
373             mKeyStore.lock();
374             updatePreferences(KeyStore.LOCKED);
375         }
376 
unlock(String password)377         private int unlock(String password) {
378             mKeyStore.unlock(password);
379             return mKeyStore.getLastError();
380         }
381 
changePassword(String oldPassword, String newPassword)382         private int changePassword(String oldPassword, String newPassword) {
383             mKeyStore.password(oldPassword, newPassword);
384             return mKeyStore.getLastError();
385         }
386 
onPreferenceChange(Preference preference, Object value)387         public boolean onPreferenceChange(Preference preference, Object value) {
388             if (preference == mAccessCheckBox) {
389                 if ((Boolean) value) {
390                     showUnlockDialog();
391                 } else {
392                     lock();
393                 }
394                 return true;
395             } else if (preference == mEncryptedFSEnabled) {
396                 Boolean bval = (Boolean)value;
397                 mWillEnableEncryptedFS = bval.booleanValue();
398                 showSwitchEncryptedFSDialog();
399             }
400             return true;
401         }
402 
onPreferenceClick(Preference preference)403         public boolean onPreferenceClick(Preference preference) {
404             if (preference == mInstallButton) {
405                 Credentials.getInstance().installFromSdCard(SecuritySettings.this);
406             } else if (preference == mPasswordButton) {
407                 showPasswordDialog();
408             } else if (preference == mResetButton) {
409                 showResetDialog();
410             } else {
411                 return false;
412             }
413             return true;
414         }
415 
onClick(DialogInterface dialog, int button)416         public void onClick(DialogInterface dialog, int button) {
417             if (mShowingDialog != DLG_ENABLE_EFS) {
418                 mSubmit = (button == DialogInterface.BUTTON_POSITIVE);
419                 if (button == DialogInterface.BUTTON_NEUTRAL) {
420                     reset();
421                 }
422             } else {
423                 if (button == DialogInterface.BUTTON_POSITIVE) {
424                     Intent intent = new Intent("android.intent.action.MASTER_CLEAR");
425                     intent.putExtra("enableEFS", mWillEnableEncryptedFS);
426                     sendBroadcast(intent);
427                     updatePreferences(mState);
428                 } else if (button == DialogInterface.BUTTON_NEGATIVE) {
429                     // Cancel action
430                     Toast.makeText(SecuritySettings.this, R.string.encrypted_fs_cancel_confirm,
431                             Toast.LENGTH_SHORT).show();
432                     updatePreferences(mState);
433                 } else {
434                     // Unknown - should not happen
435                     return;
436                 }
437             }
438         }
439 
onDismiss(DialogInterface dialog)440         public void onDismiss(DialogInterface dialog) {
441             if (mSubmit && !isFinishing()) {
442                 mSubmit = false;
443                 if (!checkPassword((Dialog) dialog)) {
444                     ((Dialog) dialog).show();
445                     return;
446                 }
447             }
448             updatePreferences(mState);
449             if (mExternal) {
450                 finish();
451             }
452         }
453 
454         // Return true if there is no error.
checkPassword(Dialog dialog)455         private boolean checkPassword(Dialog dialog) {
456             String oldPassword = getText(dialog, R.id.old_password);
457             String newPassword = getText(dialog, R.id.new_password);
458             String confirmPassword = getText(dialog, R.id.confirm_password);
459 
460             if (oldPassword != null && oldPassword.length() == 0) {
461                 showError(dialog, R.string.credentials_password_empty);
462                 return false;
463             } else if (newPassword == null) {
464                 return !checkError(dialog, unlock(oldPassword));
465             } else if (newPassword.length() == 0 || confirmPassword.length() == 0) {
466                 showError(dialog, R.string.credentials_passwords_empty);
467             } else if (newPassword.length() < MINIMUM_PASSWORD_LENGTH) {
468                 showError(dialog, R.string.credentials_password_too_short);
469             } else if (!newPassword.equals(confirmPassword)) {
470                 showError(dialog, R.string.credentials_passwords_mismatch);
471             } else if (oldPassword == null) {
472                 initialize(newPassword);
473                 return true;
474             } else {
475                 return !checkError(dialog, changePassword(oldPassword, newPassword));
476             }
477             return false;
478         }
479 
480         // Return false if there is no error.
checkError(Dialog dialog, int error)481         private boolean checkError(Dialog dialog, int error) {
482             if (error == KeyStore.NO_ERROR) {
483                 updatePreferences(KeyStore.NO_ERROR);
484                 return false;
485             }
486             if (error == KeyStore.UNINITIALIZED) {
487                 updatePreferences(KeyStore.UNINITIALIZED);
488                 return false;
489             }
490             if (error < KeyStore.WRONG_PASSWORD) {
491                 return false;
492             }
493             int count = error - KeyStore.WRONG_PASSWORD + 1;
494             if (count > 3) {
495                 showError(dialog, R.string.credentials_wrong_password);
496             } else if (count == 1) {
497                 showError(dialog, R.string.credentials_reset_warning);
498             } else {
499                 showError(dialog, R.string.credentials_reset_warning_plural, count);
500             }
501             return true;
502         }
503 
getText(Dialog dialog, int viewId)504         private String getText(Dialog dialog, int viewId) {
505             TextView view = (TextView) dialog.findViewById(viewId);
506             return (view == null || view.getVisibility() == View.GONE) ? null :
507                             view.getText().toString();
508         }
509 
showError(Dialog dialog, int stringId, Object... formatArgs)510         private void showError(Dialog dialog, int stringId, Object... formatArgs) {
511             TextView view = (TextView) dialog.findViewById(R.id.error);
512             if (view != null) {
513                 if (formatArgs == null || formatArgs.length == 0) {
514                     view.setText(stringId);
515                 } else {
516                     view.setText(dialog.getContext().getString(stringId, formatArgs));
517                 }
518                 view.setVisibility(View.VISIBLE);
519             }
520         }
521 
createPreferences(PreferenceCategory category, int type)522         private void createPreferences(PreferenceCategory category, int type) {
523             switch(type) {
524             case TYPE_KEYSTORE:
525                 mAccessCheckBox = new CheckBoxPreference(SecuritySettings.this);
526                 mAccessCheckBox.setTitle(R.string.credentials_access);
527                 mAccessCheckBox.setSummary(R.string.credentials_access_summary);
528                 mAccessCheckBox.setOnPreferenceChangeListener(this);
529                 category.addPreference(mAccessCheckBox);
530 
531                 mInstallButton = new Preference(SecuritySettings.this);
532                 mInstallButton.setTitle(R.string.credentials_install_certificates);
533                 mInstallButton.setSummary(R.string.credentials_install_certificates_summary);
534                 mInstallButton.setOnPreferenceClickListener(this);
535                 category.addPreference(mInstallButton);
536 
537                 mPasswordButton = new Preference(SecuritySettings.this);
538                 mPasswordButton.setTitle(R.string.credentials_set_password);
539                 mPasswordButton.setSummary(R.string.credentials_set_password_summary);
540                 mPasswordButton.setOnPreferenceClickListener(this);
541                 category.addPreference(mPasswordButton);
542 
543                 mResetButton = new Preference(SecuritySettings.this);
544                 mResetButton.setTitle(R.string.credentials_reset);
545                 mResetButton.setSummary(R.string.credentials_reset_summary);
546                 mResetButton.setOnPreferenceClickListener(this);
547                 category.addPreference(mResetButton);
548                 break;
549 
550             case TYPE_ENCRYPTEDFS:
551                 mEncryptedFSEnabled = new CheckBoxPreference(SecuritySettings.this);
552                 mEncryptedFSEnabled.setTitle(R.string.encrypted_fs_enable);
553                 mEncryptedFSEnabled.setSummary(R.string.encrypted_fs_enable_summary);
554                 mEncryptedFSEnabled.setOnPreferenceChangeListener(this);
555                 // category.addPreference(mEncryptedFSEnabled);
556                 break;
557             }
558         }
559 
updatePreferences(int state)560         private void updatePreferences(int state) {
561             mAccessCheckBox.setChecked(state == KeyStore.NO_ERROR);
562             boolean encFSEnabled = SystemProperties.getBoolean(PROPERTY_EFS_ENABLED,
563                     false);
564             mResetButton.setEnabled((!encFSEnabled) && (state != KeyStore.UNINITIALIZED));
565             mAccessCheckBox.setEnabled((state != KeyStore.UNINITIALIZED) && (!encFSEnabled));
566 
567             // Encrypted File system preferences
568             mEncryptedFSEnabled.setChecked(encFSEnabled);
569 
570             // Show a toast message if the state is changed.
571             if (mState == state) {
572                 return;
573             } else if (state == KeyStore.NO_ERROR) {
574                 Toast.makeText(SecuritySettings.this, R.string.credentials_enabled,
575                         Toast.LENGTH_SHORT).show();
576             } else if (state == KeyStore.UNINITIALIZED) {
577                 Toast.makeText(SecuritySettings.this, R.string.credentials_erased,
578                         Toast.LENGTH_SHORT).show();
579             } else if (state == KeyStore.LOCKED) {
580                 Toast.makeText(SecuritySettings.this, R.string.credentials_disabled,
581                         Toast.LENGTH_SHORT).show();
582             }
583             mState = state;
584         }
585 
showUnlockDialog()586         private void showUnlockDialog() {
587             View view = View.inflate(SecuritySettings.this,
588                     R.layout.credentials_unlock_dialog, null);
589 
590             // Show extra hint only when the action comes from outside.
591             if (mExternal) {
592                 view.findViewById(R.id.hint).setVisibility(View.VISIBLE);
593             }
594 
595             Dialog dialog = new AlertDialog.Builder(SecuritySettings.this)
596                     .setView(view)
597                     .setTitle(R.string.credentials_unlock)
598                     .setPositiveButton(android.R.string.ok, this)
599                     .setNegativeButton(android.R.string.cancel, this)
600                     .create();
601             dialog.setOnDismissListener(this);
602             mShowingDialog = DLG_UNLOCK;
603             dialog.show();
604         }
605 
showPasswordDialog()606         private void showPasswordDialog() {
607             View view = View.inflate(SecuritySettings.this,
608                     R.layout.credentials_password_dialog, null);
609 
610             if (mState == KeyStore.UNINITIALIZED) {
611                 view.findViewById(R.id.hint).setVisibility(View.VISIBLE);
612             } else {
613                 view.findViewById(R.id.old_password_prompt).setVisibility(View.VISIBLE);
614                 view.findViewById(R.id.old_password).setVisibility(View.VISIBLE);
615             }
616 
617             Dialog dialog = new AlertDialog.Builder(SecuritySettings.this)
618                     .setView(view)
619                     .setTitle(R.string.credentials_set_password)
620                     .setPositiveButton(android.R.string.ok, this)
621                     .setNegativeButton(android.R.string.cancel, this)
622                     .create();
623             dialog.setOnDismissListener(this);
624             mShowingDialog = DLG_PASSWORD;
625             dialog.show();
626         }
627 
showResetDialog()628         private void showResetDialog() {
629             mShowingDialog = DLG_RESET;
630             new AlertDialog.Builder(SecuritySettings.this)
631                     .setTitle(android.R.string.dialog_alert_title)
632                     .setIcon(android.R.drawable.ic_dialog_alert)
633                     .setMessage(R.string.credentials_reset_hint)
634                     .setNeutralButton(getString(android.R.string.ok), this)
635                     .setNegativeButton(getString(android.R.string.cancel), this)
636                     .create().show();
637         }
638 
showSwitchEncryptedFSDialog()639         private void showSwitchEncryptedFSDialog() {
640             AlertDialog.Builder builder = new AlertDialog.Builder(SecuritySettings.this)
641                     .setCancelable(false)
642                     .setTitle(R.string.encrypted_fs_alert_dialog_title);
643 
644             mShowingDialog = DLG_ENABLE_EFS;
645             if (mWillEnableEncryptedFS) {
646                  builder.setMessage(R.string.encrypted_fs_enable_dialog)
647                          .setPositiveButton(R.string.encrypted_fs_enable_button, this)
648                          .setNegativeButton(R.string.encrypted_fs_cancel_button, this)
649                          .create().show();
650             } else {
651                 builder.setMessage(R.string.encrypted_fs_disable_dialog)
652                         .setPositiveButton(R.string.encrypted_fs_disable_button, this)
653                         .setNegativeButton(R.string.encrypted_fs_cancel_button, this)
654                         .create().show();
655             }
656         }
657     }
658 }
659