1afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project/*
2afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project * Copyright (C) 2007 The Android Open Source Project
3afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project *
4afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project * Licensed under the Apache License, Version 2.0 (the "License");
5afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project * you may not use this file except in compliance with the License.
6afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project * You may obtain a copy of the License at
7afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project *
8afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project *      http://www.apache.org/licenses/LICENSE-2.0
9afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project *
10afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project * Unless required by applicable law or agreed to in writing, software
11afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS,
12afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project * See the License for the specific language governing permissions and
14afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project * limitations under the License.
15afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project */
16afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project
17afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Projectpackage com.android.settings;
18afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project
19afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project
20487b16f3400b62b79f8108ca669cc3e937c69defDanielle Millettimport android.app.Activity;
212c88a877f88b142fa8fd36c1773ab484b5fd7338Gilles Debunneimport android.app.AlertDialog;
224037c7f27c849a1ada0980ad881636fc940a0b85Dianne Hackbornimport android.app.admin.DevicePolicyManager;
23524a63b493c2c9692dd09669173c96e69e09297cIdo Ofirimport android.content.ComponentName;
24abc3dc64cf61c9f5c28b6f2640b221d63cb86a0aDianne Hackbornimport android.content.Context;
252c88a877f88b142fa8fd36c1773ab484b5fd7338Gilles Debunneimport android.content.DialogInterface;
26afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Projectimport android.content.Intent;
27bfbdceffe60e20a8db91b54cc81cfe49ea0dd96arich canningsimport android.content.pm.PackageManager;
28bfbdceffe60e20a8db91b54cc81cfe49ea0dd96arich canningsimport android.content.pm.ResolveInfo;
29d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglioimport android.content.res.Resources;
303bcd76ce63c77b229e2d25a0191ccb568824f5c4Jason Monkimport android.hardware.fingerprint.Fingerprint;
313bcd76ce63c77b229e2d25a0191ccb568824f5c4Jason Monkimport android.hardware.fingerprint.FingerprintManager;
32afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Projectimport android.os.Bundle;
3380753bc275ba82a08671325771f96306848d41c3Junda Liuimport android.os.PersistableBundle;
34bb06a427eb471227cf42a8078a54ca0581309493Dianne Hackbornimport android.os.UserHandle;
35783ea85bec4ec3b2f0fa9dd06cf065e527daf04cJim Millerimport android.os.UserManager;
3686624a21540e2cd336573a55adb5d3997e4a717dJim Millerimport android.preference.ListPreference;
37afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Projectimport android.preference.Preference;
38f8b25796f0ce1096a95cae2b51d07d96e148277cMathew Inwoodimport android.preference.Preference.OnPreferenceChangeListener;
393bcd76ce63c77b229e2d25a0191ccb568824f5c4Jason Monkimport android.preference.Preference.OnPreferenceClickListener;
40f8b25796f0ce1096a95cae2b51d07d96e148277cMathew Inwoodimport android.preference.PreferenceGroup;
41afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Projectimport android.preference.PreferenceScreen;
423bcd76ce63c77b229e2d25a0191ccb568824f5c4Jason Monkimport android.preference.SwitchPreference;
43d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglioimport android.provider.SearchIndexableResource;
44afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Projectimport android.provider.Settings;
459cec698a1fbbe5e8d23a96347d4e75003f4b8cb6Chia-chi Yehimport android.security.KeyStore;
466f482447b05492906755d91ac4c29ec505a8a544Ido Ofirimport android.service.trust.TrustAgentService;
4780753bc275ba82a08671325771f96306848d41c3Junda Liuimport android.telephony.CarrierConfigManager;
48ca7566125e00ed267aba831bd1d6d1ad78d90c17Wink Savilleimport android.telephony.SubscriptionInfo;
493bcd76ce63c77b229e2d25a0191ccb568824f5c4Jason Monkimport android.telephony.SubscriptionManager;
503bcd76ce63c77b229e2d25a0191ccb568824f5c4Jason Monkimport android.telephony.TelephonyManager;
51524a63b493c2c9692dd09669173c96e69e09297cIdo Ofirimport android.text.TextUtils;
5286624a21540e2cd336573a55adb5d3997e4a717dJim Millerimport android.util.Log;
53f06d8698a4a2de1973a25df0c6563bdfcba4eb03Amith Yamasani
548a963babe2e36b7a41f77b8d2598c97658196e58Chris Wrenimport com.android.internal.logging.MetricsLogger;
55a6a8a1479b970d8a84395453703348fe42d17438Gilles Debunneimport com.android.internal.widget.LockPatternUtils;
56fe7655914de3852093c35600df996824d03ddf63Jim Millerimport com.android.settings.TrustAgentUtils.TrustAgentComponentInfo;
57ec8500438cf897098ff4a22d81de34f4214f44d8Selim Cinekimport com.android.settings.fingerprint.FingerprintEnrollIntroduction;
585ad75f07b1861b802b75c720830db260b4f93417Jorim Jaggiimport com.android.settings.fingerprint.FingerprintSettings;
59d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglioimport com.android.settings.search.BaseSearchIndexProvider;
60fe7655914de3852093c35600df996824d03ddf63Jim Millerimport com.android.settings.search.Index;
61d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglioimport com.android.settings.search.Indexable;
62d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglioimport com.android.settings.search.SearchIndexableRaw;
63a6a8a1479b970d8a84395453703348fe42d17438Gilles Debunne
6486624a21540e2cd336573a55adb5d3997e4a717dJim Millerimport java.util.ArrayList;
65bfbdceffe60e20a8db91b54cc81cfe49ea0dd96arich canningsimport java.util.List;
665ed2c4ad43f2c3c85a025da04becd2eaa75927a1Mike Lockwood
67ec8500438cf897098ff4a22d81de34f4214f44d8Selim Cinekimport static android.provider.Settings.System.SCREEN_OFF_TIMEOUT;
68ec8500438cf897098ff4a22d81de34f4214f44d8Selim Cinek
69afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project/**
70afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project * Gesture lock pattern settings.
71afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project */
72565653cef1039ab4e34e505185f1c77d847357cdJulia Reynoldspublic class SecuritySettings extends SettingsPreferenceFragment
73d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio        implements OnPreferenceChangeListener, DialogInterface.OnClickListener, Indexable {
749218687e4baaf0226ef16b58f78123d979711e5dJim Miller
759218687e4baaf0226ef16b58f78123d979711e5dJim Miller    private static final String TAG = "SecuritySettings";
76250b9bee685de8f4060dd410f0894c22de1909cfJim Miller    private static final String TRUST_AGENT_CLICK_INTENT = "trust_agent_click_intent";
776f482447b05492906755d91ac4c29ec505a8a544Ido Ofir    private static final Intent TRUST_AGENT_INTENT =
786f482447b05492906755d91ac4c29ec505a8a544Ido Ofir            new Intent(TrustAgentService.SERVICE_INTERFACE);
796f8fb436178d12d11d6ab59671168f0603bdbdcbJason parks
80afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    // Lock Settings
8191d65a20af2d1bfb06f32d0bd821e8558afe939dChia-chi Yeh    private static final String KEY_UNLOCK_SET_OR_CHANGE = "unlock_set_or_change";
82afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    private static final String KEY_VISIBLE_PATTERN = "visiblepattern";
83edac9af662f01026fd2b4acf263e1b5d1dfdeaa9Amith Yamasani    private static final String KEY_SECURITY_CATEGORY = "security_category";
84f04211443e9cae815ad89f821b6ec83ec2790c0crich cannings    private static final String KEY_DEVICE_ADMIN_CATEGORY = "device_admin_category";
8591d65a20af2d1bfb06f32d0bd821e8558afe939dChia-chi Yeh    private static final String KEY_LOCK_AFTER_TIMEOUT = "lock_after_timeout";
868666b9e170c23a9595d611a49b7c8262e357ced1Amith Yamasani    private static final String KEY_OWNER_INFO_SETTINGS = "owner_info_settings";
87fe7655914de3852093c35600df996824d03ddf63Jim Miller    private static final String KEY_ADVANCED_SECURITY = "advanced_security";
88fe7655914de3852093c35600df996824d03ddf63Jim Miller    private static final String KEY_MANAGE_TRUST_AGENTS = "manage_trust_agents";
899218687e4baaf0226ef16b58f78123d979711e5dJim Miller    private static final String KEY_FINGERPRINT_SETTINGS = "fingerprint_settings";
90ef3e701b78134baf753484c44b7d1915029faa3fJim Miller
9191d65a20af2d1bfb06f32d0bd821e8558afe939dChia-chi Yeh    private static final int SET_OR_CHANGE_LOCK_METHOD_REQUEST = 123;
926f482447b05492906755d91ac4c29ec505a8a544Ido Ofir    private static final int CHANGE_TRUST_AGENT_SETTINGS = 126;
937031ab0d756327496ac3ce1e082b53c94be2f3b1Hung-ying Tyan
9491d65a20af2d1bfb06f32d0bd821e8558afe939dChia-chi Yeh    // Misc Settings
9591d65a20af2d1bfb06f32d0bd821e8558afe939dChia-chi Yeh    private static final String KEY_SIM_LOCK = "sim_lock";
9691d65a20af2d1bfb06f32d0bd821e8558afe939dChia-chi Yeh    private static final String KEY_SHOW_PASSWORD = "show_password";
97028634b6af87665c5d62b0aee669ce376cef065dKenny Root    private static final String KEY_CREDENTIAL_STORAGE_TYPE = "credential_storage_type";
98233ccd0cf798de779628984441f48c239fe86921Julia Reynolds    private static final String KEY_RESET_CREDENTIALS = "credentials_reset";
996e1102d9faf1b8f038857709412c291ef6140aa9Geoffrey Borggaard    private static final String KEY_CREDENTIALS_INSTALL = "credentials_install";
1002c88a877f88b142fa8fd36c1773ab484b5fd7338Gilles Debunne    private static final String KEY_TOGGLE_INSTALL_APPLICATIONS = "toggle_install_applications";
101071742d838f9b0c312af309c87eaf2c444aeeab8Jim Miller    private static final String KEY_POWER_INSTANTLY_LOCKS = "power_button_instantly_locks";
1027fb4f850eabc017bab6090bf9268cf3fae1e08ebAmith Yamasani    private static final String KEY_CREDENTIALS_MANAGER = "credentials_management";
103bfbdceffe60e20a8db91b54cc81cfe49ea0dd96arich cannings    private static final String PACKAGE_MIME_TYPE = "application/vnd.android.package-archive";
1046f482447b05492906755d91ac4c29ec505a8a544Ido Ofir    private static final String KEY_TRUST_AGENT = "trust_agent";
10527d7ea56f3f02ea7efaf1e57b5d6758ddb56c353Jason Monk    private static final String KEY_SCREEN_PINNING = "screen_pinning_settings";
10647d380f1e6c823cea444004cb799d4c791145cc6Jim Miller
1074a9065e6547c24c751b0aecbc151e57bcbcf92eaJim Miller    // These switch preferences need special handling since they're not all stored in Settings.
1084a9065e6547c24c751b0aecbc151e57bcbcf92eaJim Miller    private static final String SWITCH_PREFERENCE_KEYS[] = { KEY_LOCK_AFTER_TIMEOUT,
1096b879f50f6a7452a14b311e20e4ed8d20e744613Adrian Roos            KEY_VISIBLE_PATTERN, KEY_POWER_INSTANTLY_LOCKS, KEY_SHOW_PASSWORD,
1106b879f50f6a7452a14b311e20e4ed8d20e744613Adrian Roos            KEY_TOGGLE_INSTALL_APPLICATIONS };
1114a9065e6547c24c751b0aecbc151e57bcbcf92eaJim Miller
112fe7655914de3852093c35600df996824d03ddf63Jim Miller    // Only allow one trust agent on the platform.
113fe7655914de3852093c35600df996824d03ddf63Jim Miller    private static final boolean ONLY_ONE_TRUST_AGENT = true;
114fe7655914de3852093c35600df996824d03ddf63Jim Miller
1155437588abd69bf3eff59684528aaf50e09b6cffcAdrian Roos    private static final int MY_USER_ID = UserHandle.myUserId();
1165437588abd69bf3eff59684528aaf50e09b6cffcAdrian Roos
117f48206e37e8a88a2f5f9b019995488696f8bf964Maggie Benthall    private DevicePolicyManager mDPM;
1180183fb5394f105d544472a52b2b4ba628e01da92Wink Saville    private SubscriptionManager mSubscriptionManager;
119122b6c85a9bc7076eacc388142f8c4fccc5462c9Jim Miller
12000d2476d8fc122b4eef59d6be6f7aa41e9c56fdeJim Miller    private ChooseLockSettingsHelper mChooseLockSettingsHelper;
121122b6c85a9bc7076eacc388142f8c4fccc5462c9Jim Miller    private LockPatternUtils mLockPatternUtils;
12286624a21540e2cd336573a55adb5d3997e4a717dJim Miller    private ListPreference mLockAfter;
12336972bb7e952ca8a7bd8e57e32246d60873d7479Jim Miller
1244a9065e6547c24c751b0aecbc151e57bcbcf92eaJim Miller    private SwitchPreference mVisiblePattern;
12591d65a20af2d1bfb06f32d0bd821e8558afe939dChia-chi Yeh
1264a9065e6547c24c751b0aecbc151e57bcbcf92eaJim Miller    private SwitchPreference mShowPassword;
12791d65a20af2d1bfb06f32d0bd821e8558afe939dChia-chi Yeh
128028634b6af87665c5d62b0aee669ce376cef065dKenny Root    private KeyStore mKeyStore;
12991d65a20af2d1bfb06f32d0bd821e8558afe939dChia-chi Yeh    private Preference mResetCredentials;
1305ed2c4ad43f2c3c85a025da04becd2eaa75927a1Mike Lockwood
1314a9065e6547c24c751b0aecbc151e57bcbcf92eaJim Miller    private SwitchPreference mToggleAppInstallation;
1322c88a877f88b142fa8fd36c1773ab484b5fd7338Gilles Debunne    private DialogInterface mWarnInstallApps;
1334a9065e6547c24c751b0aecbc151e57bcbcf92eaJim Miller    private SwitchPreference mPowerButtonInstantlyLocks;
1342c88a877f88b142fa8fd36c1773ab484b5fd7338Gilles Debunne
1357fb4f850eabc017bab6090bf9268cf3fae1e08ebAmith Yamasani    private boolean mIsPrimary;
1367fb4f850eabc017bab6090bf9268cf3fae1e08ebAmith Yamasani
137250b9bee685de8f4060dd410f0894c22de1909cfJim Miller    private Intent mTrustAgentClickIntent;
1383bcd76ce63c77b229e2d25a0191ccb568824f5c4Jason Monk    private Preference mOwnerInfoPref;
1391682a0d1be53562a8ca2d0eb8fcd41b07af2f144Ido Ofir
140afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    @Override
1418a963babe2e36b7a41f77b8d2598c97658196e58Chris Wren    protected int getMetricsCategory() {
1428a963babe2e36b7a41f77b8d2598c97658196e58Chris Wren        return MetricsLogger.SECURITY;
1438a963babe2e36b7a41f77b8d2598c97658196e58Chris Wren    }
1448a963babe2e36b7a41f77b8d2598c97658196e58Chris Wren
1458a963babe2e36b7a41f77b8d2598c97658196e58Chris Wren    @Override
146d79934731c8d33f6fc63b21c120b9ffba5d06f54Amith Yamasani    public void onCreate(Bundle savedInstanceState) {
147afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        super.onCreate(savedInstanceState);
148122b6c85a9bc7076eacc388142f8c4fccc5462c9Jim Miller
1490183fb5394f105d544472a52b2b4ba628e01da92Wink Saville        mSubscriptionManager = SubscriptionManager.from(getActivity());
1500183fb5394f105d544472a52b2b4ba628e01da92Wink Saville
151d79934731c8d33f6fc63b21c120b9ffba5d06f54Amith Yamasani        mLockPatternUtils = new LockPatternUtils(getActivity());
152afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project
153abc3dc64cf61c9f5c28b6f2640b221d63cb86a0aDianne Hackborn        mDPM = (DevicePolicyManager)getSystemService(Context.DEVICE_POLICY_SERVICE);
154122b6c85a9bc7076eacc388142f8c4fccc5462c9Jim Miller
155d79934731c8d33f6fc63b21c120b9ffba5d06f54Amith Yamasani        mChooseLockSettingsHelper = new ChooseLockSettingsHelper(getActivity());
156250b9bee685de8f4060dd410f0894c22de1909cfJim Miller
157250b9bee685de8f4060dd410f0894c22de1909cfJim Miller        if (savedInstanceState != null
158250b9bee685de8f4060dd410f0894c22de1909cfJim Miller                && savedInstanceState.containsKey(TRUST_AGENT_CLICK_INTENT)) {
159250b9bee685de8f4060dd410f0894c22de1909cfJim Miller            mTrustAgentClickIntent = savedInstanceState.getParcelable(TRUST_AGENT_CLICK_INTENT);
160250b9bee685de8f4060dd410f0894c22de1909cfJim Miller        }
16102cf71a5916d1b0155e8fff3f62987cdc5c2b7c3Amith Yamasani    }
1625ed2c4ad43f2c3c85a025da04becd2eaa75927a1Mike Lockwood
163d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio    private static int getResIdForLockUnlockScreen(Context context,
164d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio            LockPatternUtils lockPatternUtils) {
16536972bb7e952ca8a7bd8e57e32246d60873d7479Jim Miller        int resid = 0;
1665437588abd69bf3eff59684528aaf50e09b6cffcAdrian Roos        if (!lockPatternUtils.isSecure(MY_USER_ID)) {
1675437588abd69bf3eff59684528aaf50e09b6cffcAdrian Roos            if (lockPatternUtils.isLockScreenDisabled(MY_USER_ID)) {
16836972bb7e952ca8a7bd8e57e32246d60873d7479Jim Miller                resid = R.xml.security_settings_lockscreen;
16936972bb7e952ca8a7bd8e57e32246d60873d7479Jim Miller            } else {
17036972bb7e952ca8a7bd8e57e32246d60873d7479Jim Miller                resid = R.xml.security_settings_chooser;
17136972bb7e952ca8a7bd8e57e32246d60873d7479Jim Miller            }
172122b6c85a9bc7076eacc388142f8c4fccc5462c9Jim Miller        } else {
1735437588abd69bf3eff59684528aaf50e09b6cffcAdrian Roos            switch (lockPatternUtils.getKeyguardStoredPasswordQuality(MY_USER_ID)) {
174af366a3ed66dcb3c3ecb1dd101e5d8869b518598Jim Miller                case DevicePolicyManager.PASSWORD_QUALITY_SOMETHING:
17536972bb7e952ca8a7bd8e57e32246d60873d7479Jim Miller                    resid = R.xml.security_settings_pattern;
176af366a3ed66dcb3c3ecb1dd101e5d8869b518598Jim Miller                    break;
177af366a3ed66dcb3c3ecb1dd101e5d8869b518598Jim Miller                case DevicePolicyManager.PASSWORD_QUALITY_NUMERIC:
1788fd852e7c2f488650095024aca9c51f8c7ddc3b3Nicolas Prevot                case DevicePolicyManager.PASSWORD_QUALITY_NUMERIC_COMPLEX:
17936972bb7e952ca8a7bd8e57e32246d60873d7479Jim Miller                    resid = R.xml.security_settings_pin;
180af366a3ed66dcb3c3ecb1dd101e5d8869b518598Jim Miller                    break;
181af366a3ed66dcb3c3ecb1dd101e5d8869b518598Jim Miller                case DevicePolicyManager.PASSWORD_QUALITY_ALPHABETIC:
182af366a3ed66dcb3c3ecb1dd101e5d8869b518598Jim Miller                case DevicePolicyManager.PASSWORD_QUALITY_ALPHANUMERIC:
18357fbf694a53afb9c1924d81272179dceb3acfa4dKonstantin Lopyrev                case DevicePolicyManager.PASSWORD_QUALITY_COMPLEX:
18436972bb7e952ca8a7bd8e57e32246d60873d7479Jim Miller                    resid = R.xml.security_settings_password;
185af366a3ed66dcb3c3ecb1dd101e5d8869b518598Jim Miller                    break;
18600d2476d8fc122b4eef59d6be6f7aa41e9c56fdeJim Miller            }
187122b6c85a9bc7076eacc388142f8c4fccc5462c9Jim Miller        }
188d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio        return resid;
189d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio    }
190d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio
191d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio    /**
192d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio     * Important!
193d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio     *
1946f482447b05492906755d91ac4c29ec505a8a544Ido Ofir     * Don't forget to update the SecuritySearchIndexProvider if you are doing any change in the
195d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio     * logic or adding/removing preferences here.
196d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio     */
197d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio    private PreferenceScreen createPreferenceHierarchy() {
198d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio        PreferenceScreen root = getPreferenceScreen();
199d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio        if (root != null) {
200d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio            root.removeAll();
201d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio        }
202d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio        addPreferencesFromResource(R.xml.security_settings);
203d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio        root = getPreferenceScreen();
204122b6c85a9bc7076eacc388142f8c4fccc5462c9Jim Miller
205d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio        // Add options for lock/unlock screen
206d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio        final int resid = getResIdForLockUnlockScreen(getActivity(), mLockPatternUtils);
207d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio        addPreferencesFromResource(resid);
2084112199ed654f8bd1cea2c9a29a24be3c148b2edJason parks
2094112199ed654f8bd1cea2c9a29a24be3c148b2edJason parks        // Add options for device encryption
2105437588abd69bf3eff59684528aaf50e09b6cffcAdrian Roos        mIsPrimary = MY_USER_ID == UserHandle.USER_OWNER;
2117fb4f850eabc017bab6090bf9268cf3fae1e08ebAmith Yamasani
2123bcd76ce63c77b229e2d25a0191ccb568824f5c4Jason Monk        mOwnerInfoPref = findPreference(KEY_OWNER_INFO_SETTINGS);
2133bcd76ce63c77b229e2d25a0191ccb568824f5c4Jason Monk        if (mOwnerInfoPref != null) {
2143bcd76ce63c77b229e2d25a0191ccb568824f5c4Jason Monk            mOwnerInfoPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {
2153bcd76ce63c77b229e2d25a0191ccb568824f5c4Jason Monk                @Override
2163bcd76ce63c77b229e2d25a0191ccb568824f5c4Jason Monk                public boolean onPreferenceClick(Preference preference) {
2173bcd76ce63c77b229e2d25a0191ccb568824f5c4Jason Monk                    OwnerInfoSettings.show(SecuritySettings.this);
2183bcd76ce63c77b229e2d25a0191ccb568824f5c4Jason Monk                    return true;
219b0171715876b672c94c8a5aaafde95ff47b234b4Amith Yamasani                }
2203bcd76ce63c77b229e2d25a0191ccb568824f5c4Jason Monk            });
2218666b9e170c23a9595d611a49b7c8262e357ced1Amith Yamasani        }
2228666b9e170c23a9595d611a49b7c8262e357ced1Amith Yamasani
2237fb4f850eabc017bab6090bf9268cf3fae1e08ebAmith Yamasani        if (mIsPrimary) {
2240698a216311acd9ab52224602d1a75c08bf89f04Jim Miller            if (LockPatternUtils.isDeviceEncryptionEnabled()) {
225b810a0ddf9dd93a08789aff38901eeab9bfaf812Amith Yamasani                // The device is currently encrypted.
226b810a0ddf9dd93a08789aff38901eeab9bfaf812Amith Yamasani                addPreferencesFromResource(R.xml.security_settings_encrypted);
2270698a216311acd9ab52224602d1a75c08bf89f04Jim Miller            } else {
228b810a0ddf9dd93a08789aff38901eeab9bfaf812Amith Yamasani                // This device supports encryption but isn't encrypted.
229b810a0ddf9dd93a08789aff38901eeab9bfaf812Amith Yamasani                addPreferencesFromResource(R.xml.security_settings_unencrypted);
230b810a0ddf9dd93a08789aff38901eeab9bfaf812Amith Yamasani            }
2314112199ed654f8bd1cea2c9a29a24be3c148b2edJason parks        }
2324112199ed654f8bd1cea2c9a29a24be3c148b2edJason parks
23394d4bd9390b9b9003788a2e779e2b4b256d07c3bJim Miller        // Fingerprint and trust agents
2341682a0d1be53562a8ca2d0eb8fcd41b07af2f144Ido Ofir        PreferenceGroup securityCategory = (PreferenceGroup)
2351682a0d1be53562a8ca2d0eb8fcd41b07af2f144Ido Ofir                root.findPreference(KEY_SECURITY_CATEGORY);
2369d25005e7ab39bcc14722f7bdb977ceccb6107dcJim Miller        if (securityCategory != null) {
23794d4bd9390b9b9003788a2e779e2b4b256d07c3bJim Miller            maybeAddFingerprintPreference(securityCategory);
23894d4bd9390b9b9003788a2e779e2b4b256d07c3bJim Miller            addTrustAgentSettings(securityCategory);
2391682a0d1be53562a8ca2d0eb8fcd41b07af2f144Ido Ofir        }
2401682a0d1be53562a8ca2d0eb8fcd41b07af2f144Ido Ofir
24186624a21540e2cd336573a55adb5d3997e4a717dJim Miller        // lock after preference
24291d65a20af2d1bfb06f32d0bd821e8558afe939dChia-chi Yeh        mLockAfter = (ListPreference) root.findPreference(KEY_LOCK_AFTER_TIMEOUT);
24391d65a20af2d1bfb06f32d0bd821e8558afe939dChia-chi Yeh        if (mLockAfter != null) {
24491d65a20af2d1bfb06f32d0bd821e8558afe939dChia-chi Yeh            setupLockAfterPreference();
24591d65a20af2d1bfb06f32d0bd821e8558afe939dChia-chi Yeh            updateLockAfterPreferenceSummary();
24691d65a20af2d1bfb06f32d0bd821e8558afe939dChia-chi Yeh        }
247afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project
248afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        // visible pattern
2494a9065e6547c24c751b0aecbc151e57bcbcf92eaJim Miller        mVisiblePattern = (SwitchPreference) root.findPreference(KEY_VISIBLE_PATTERN);
250afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project
251071742d838f9b0c312af309c87eaf2c444aeeab8Jim Miller        // lock instantly on power key press
2524a9065e6547c24c751b0aecbc151e57bcbcf92eaJim Miller        mPowerButtonInstantlyLocks = (SwitchPreference) root.findPreference(
253071742d838f9b0c312af309c87eaf2c444aeeab8Jim Miller                KEY_POWER_INSTANTLY_LOCKS);
2541682a0d1be53562a8ca2d0eb8fcd41b07af2f144Ido Ofir        Preference trustAgentPreference = root.findPreference(KEY_TRUST_AGENT);
2551682a0d1be53562a8ca2d0eb8fcd41b07af2f144Ido Ofir        if (mPowerButtonInstantlyLocks != null &&
2561682a0d1be53562a8ca2d0eb8fcd41b07af2f144Ido Ofir                trustAgentPreference != null &&
2571682a0d1be53562a8ca2d0eb8fcd41b07af2f144Ido Ofir                trustAgentPreference.getTitle().length() > 0) {
2581682a0d1be53562a8ca2d0eb8fcd41b07af2f144Ido Ofir            mPowerButtonInstantlyLocks.setSummary(getString(
2591682a0d1be53562a8ca2d0eb8fcd41b07af2f144Ido Ofir                    R.string.lockpattern_settings_power_button_instantly_locks_summary,
2601682a0d1be53562a8ca2d0eb8fcd41b07af2f144Ido Ofir                    trustAgentPreference.getTitle()));
2611682a0d1be53562a8ca2d0eb8fcd41b07af2f144Ido Ofir        }
262071742d838f9b0c312af309c87eaf2c444aeeab8Jim Miller
26391d65a20af2d1bfb06f32d0bd821e8558afe939dChia-chi Yeh        // Append the rest of the settings
26491d65a20af2d1bfb06f32d0bd821e8558afe939dChia-chi Yeh        addPreferencesFromResource(R.xml.security_settings_misc);
26591d65a20af2d1bfb06f32d0bd821e8558afe939dChia-chi Yeh
2665c68e12493d3c81324179c7c3ba6cd58279d648bAmith Yamasani        // Do not display SIM lock for devices without an Icc card
267cd05e8859599658a546b5d4ee09068dd2833b901Wink Saville        TelephonyManager tm = TelephonyManager.getDefault();
26880753bc275ba82a08671325771f96306848d41c3Junda Liu        CarrierConfigManager cfgMgr = (CarrierConfigManager)
26980753bc275ba82a08671325771f96306848d41c3Junda Liu                getActivity().getSystemService(Context.CARRIER_CONFIG_SERVICE);
27080753bc275ba82a08671325771f96306848d41c3Junda Liu        PersistableBundle b = cfgMgr.getConfig();
27180753bc275ba82a08671325771f96306848d41c3Junda Liu        if (!mIsPrimary || !isSimIccReady() ||
27280753bc275ba82a08671325771f96306848d41c3Junda Liu                b.getBoolean(CarrierConfigManager.KEY_HIDE_SIM_LOCK_SETTINGS_BOOL)) {
27391d65a20af2d1bfb06f32d0bd821e8558afe939dChia-chi Yeh            root.removePreference(root.findPreference(KEY_SIM_LOCK));
274a98c353f8b18607f0c2e9af26b91bf6ffbe8dc78Kazuhiro Ondo        } else {
27550e6ecacf99c30bf239e68bd54895e3632315063PauloftheWest            // Disable SIM lock if there is no ready SIM card.
27650e6ecacf99c30bf239e68bd54895e3632315063PauloftheWest            root.findPreference(KEY_SIM_LOCK).setEnabled(isSimReady());
277386278a338d740dce95b7fa1514662b0eb5683e4Chouting Zhang        }
278b08853aad9f3ef2dc3ba1f1c7a513a16220d4d9eJason Monk        if (Settings.System.getInt(getContentResolver(),
279b08853aad9f3ef2dc3ba1f1c7a513a16220d4d9eJason Monk                Settings.System.LOCK_TO_APP_ENABLED, 0) != 0) {
280b08853aad9f3ef2dc3ba1f1c7a513a16220d4d9eJason Monk            root.findPreference(KEY_SCREEN_PINNING).setSummary(
281b08853aad9f3ef2dc3ba1f1c7a513a16220d4d9eJason Monk                    getResources().getString(R.string.switch_on_text));
28227d7ea56f3f02ea7efaf1e57b5d6758ddb56c353Jason Monk        }
283afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project
28491d65a20af2d1bfb06f32d0bd821e8558afe939dChia-chi Yeh        // Show password
2854a9065e6547c24c751b0aecbc151e57bcbcf92eaJim Miller        mShowPassword = (SwitchPreference) root.findPreference(KEY_SHOW_PASSWORD);
2866e1102d9faf1b8f038857709412c291ef6140aa9Geoffrey Borggaard        mResetCredentials = root.findPreference(KEY_RESET_CREDENTIALS);
287feff65213231ba6a0f6185f98e660042b5b14595Dianne Hackborn
288c5550c2012c505dfb27d9a8da8ecb4787f366283Kenny Root        // Credential storage
28911bd33a564d4ae9a48195ad11dbbd09d80e59b55Emily Bernier        final UserManager um = (UserManager) getActivity().getSystemService(Context.USER_SERVICE);
290f48206e37e8a88a2f5f9b019995488696f8bf964Maggie Benthall        mKeyStore = KeyStore.getInstance(); // needs to be initialized for onResume()
29111bd33a564d4ae9a48195ad11dbbd09d80e59b55Emily Bernier        if (!um.hasUserRestriction(UserManager.DISALLOW_CONFIG_CREDENTIALS)) {
29211bd33a564d4ae9a48195ad11dbbd09d80e59b55Emily Bernier            Preference credentialStorageType = root.findPreference(KEY_CREDENTIAL_STORAGE_TYPE);
293028634b6af87665c5d62b0aee669ce376cef065dKenny Root
29411bd33a564d4ae9a48195ad11dbbd09d80e59b55Emily Bernier            final int storageSummaryRes =
295c5550c2012c505dfb27d9a8da8ecb4787f366283Kenny Root                mKeyStore.isHardwareBacked() ? R.string.credential_storage_type_hardware
296c5550c2012c505dfb27d9a8da8ecb4787f366283Kenny Root                        : R.string.credential_storage_type_software;
29711bd33a564d4ae9a48195ad11dbbd09d80e59b55Emily Bernier            credentialStorageType.setSummary(storageSummaryRes);
29811bd33a564d4ae9a48195ad11dbbd09d80e59b55Emily Bernier        } else {
299233ccd0cf798de779628984441f48c239fe86921Julia Reynolds            PreferenceGroup credentialsManager = (PreferenceGroup)
300233ccd0cf798de779628984441f48c239fe86921Julia Reynolds                    root.findPreference(KEY_CREDENTIALS_MANAGER);
301233ccd0cf798de779628984441f48c239fe86921Julia Reynolds            credentialsManager.removePreference(root.findPreference(KEY_RESET_CREDENTIALS));
302233ccd0cf798de779628984441f48c239fe86921Julia Reynolds            credentialsManager.removePreference(root.findPreference(KEY_CREDENTIALS_INSTALL));
303233ccd0cf798de779628984441f48c239fe86921Julia Reynolds            credentialsManager.removePreference(root.findPreference(KEY_CREDENTIAL_STORAGE_TYPE));
30411bd33a564d4ae9a48195ad11dbbd09d80e59b55Emily Bernier        }
3058877f74901b456d45aaf7a47f6d2208fab9e283cOscar Montemayor
306c5550c2012c505dfb27d9a8da8ecb4787f366283Kenny Root        // Application install
3076f482447b05492906755d91ac4c29ec505a8a544Ido Ofir        PreferenceGroup deviceAdminCategory = (PreferenceGroup)
3080c5a401a58b30041d5fe712d78fccbd47f5b744dMaggie Benthall                root.findPreference(KEY_DEVICE_ADMIN_CATEGORY);
3094a9065e6547c24c751b0aecbc151e57bcbcf92eaJim Miller        mToggleAppInstallation = (SwitchPreference) findPreference(
3102c88a877f88b142fa8fd36c1773ab484b5fd7338Gilles Debunne                KEY_TOGGLE_INSTALL_APPLICATIONS);
3112c88a877f88b142fa8fd36c1773ab484b5fd7338Gilles Debunne        mToggleAppInstallation.setChecked(isNonMarketAppsAllowed());
3120c5a401a58b30041d5fe712d78fccbd47f5b744dMaggie Benthall        // Side loading of apps.
313ceceb2ab07aab457a4229d051de222ad88ed62a1Amith Yamasani        // Disable for restricted profiles. For others, check if policy disallows it.
3145437588abd69bf3eff59684528aaf50e09b6cffcAdrian Roos        mToggleAppInstallation.setEnabled(!um.getUserInfo(MY_USER_ID).isRestricted());
315565653cef1039ab4e34e505185f1c77d847357cdJulia Reynolds        if (um.hasUserRestriction(UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES)
316565653cef1039ab4e34e505185f1c77d847357cdJulia Reynolds                || um.hasUserRestriction(UserManager.DISALLOW_INSTALL_APPS)) {
317565653cef1039ab4e34e505185f1c77d847357cdJulia Reynolds            mToggleAppInstallation.setEnabled(false);
318565653cef1039ab4e34e505185f1c77d847357cdJulia Reynolds        }
3190c5a401a58b30041d5fe712d78fccbd47f5b744dMaggie Benthall
320fe7655914de3852093c35600df996824d03ddf63Jim Miller        // Advanced Security features
321fe7655914de3852093c35600df996824d03ddf63Jim Miller        PreferenceGroup advancedCategory =
322fe7655914de3852093c35600df996824d03ddf63Jim Miller                (PreferenceGroup)root.findPreference(KEY_ADVANCED_SECURITY);
3239d25005e7ab39bcc14722f7bdb977ceccb6107dcJim Miller        if (advancedCategory != null) {
324fe7655914de3852093c35600df996824d03ddf63Jim Miller            Preference manageAgents = advancedCategory.findPreference(KEY_MANAGE_TRUST_AGENTS);
3255437588abd69bf3eff59684528aaf50e09b6cffcAdrian Roos            if (manageAgents != null && !mLockPatternUtils.isSecure(MY_USER_ID)) {
3269d25005e7ab39bcc14722f7bdb977ceccb6107dcJim Miller                manageAgents.setEnabled(false);
3279d25005e7ab39bcc14722f7bdb977ceccb6107dcJim Miller                manageAgents.setSummary(R.string.disabled_because_no_backup_security);
3289d25005e7ab39bcc14722f7bdb977ceccb6107dcJim Miller            }
329fe7655914de3852093c35600df996824d03ddf63Jim Miller        }
330fe7655914de3852093c35600df996824d03ddf63Jim Miller
331fe7655914de3852093c35600df996824d03ddf63Jim Miller        // The above preferences come and go based on security state, so we need to update
332fe7655914de3852093c35600df996824d03ddf63Jim Miller        // the index. This call is expected to be fairly cheap, but we may want to do something
333fe7655914de3852093c35600df996824d03ddf63Jim Miller        // smarter in the future.
334fe7655914de3852093c35600df996824d03ddf63Jim Miller        Index.getInstance(getActivity())
335fe7655914de3852093c35600df996824d03ddf63Jim Miller                .updateFromClassNameResource(SecuritySettings.class.getName(), true, true);
336fe7655914de3852093c35600df996824d03ddf63Jim Miller
3374a9065e6547c24c751b0aecbc151e57bcbcf92eaJim Miller        for (int i = 0; i < SWITCH_PREFERENCE_KEYS.length; i++) {
3384a9065e6547c24c751b0aecbc151e57bcbcf92eaJim Miller            final Preference pref = findPreference(SWITCH_PREFERENCE_KEYS[i]);
3394a9065e6547c24c751b0aecbc151e57bcbcf92eaJim Miller            if (pref != null) pref.setOnPreferenceChangeListener(this);
3404a9065e6547c24c751b0aecbc151e57bcbcf92eaJim Miller        }
341afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        return root;
342afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    }
343afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project
34494d4bd9390b9b9003788a2e779e2b4b256d07c3bJim Miller    private void maybeAddFingerprintPreference(PreferenceGroup securityCategory) {
34594d4bd9390b9b9003788a2e779e2b4b256d07c3bJim Miller        FingerprintManager fpm = (FingerprintManager) getActivity().getSystemService(
34694d4bd9390b9b9003788a2e779e2b4b256d07c3bJim Miller                Context.FINGERPRINT_SERVICE);
34794d4bd9390b9b9003788a2e779e2b4b256d07c3bJim Miller        if (!fpm.isHardwareDetected()) {
34894d4bd9390b9b9003788a2e779e2b4b256d07c3bJim Miller            Log.v(TAG, "No fingerprint hardware detected!!");
34994d4bd9390b9b9003788a2e779e2b4b256d07c3bJim Miller            return;
35094d4bd9390b9b9003788a2e779e2b4b256d07c3bJim Miller        }
35194d4bd9390b9b9003788a2e779e2b4b256d07c3bJim Miller        Preference fingerprintPreference = new Preference(securityCategory.getContext());
3529218687e4baaf0226ef16b58f78123d979711e5dJim Miller        fingerprintPreference.setKey(KEY_FINGERPRINT_SETTINGS);
35394d4bd9390b9b9003788a2e779e2b4b256d07c3bJim Miller        fingerprintPreference.setTitle(R.string.security_settings_fingerprint_preference_title);
35494d4bd9390b9b9003788a2e779e2b4b256d07c3bJim Miller        Intent intent = new Intent();
355d16c9b7c3100b7ba653dcd58db036500dd220896Jim Miller        final List<Fingerprint> items = fpm.getEnrolledFingerprints();
356d16c9b7c3100b7ba653dcd58db036500dd220896Jim Miller        final int fingerprintCount = items != null ? items.size() : 0;
3579218687e4baaf0226ef16b58f78123d979711e5dJim Miller        final String clazz;
35894d4bd9390b9b9003788a2e779e2b4b256d07c3bJim Miller        if (fingerprintCount > 0) {
35994d4bd9390b9b9003788a2e779e2b4b256d07c3bJim Miller            fingerprintPreference.setSummary(getResources().getQuantityString(
36094d4bd9390b9b9003788a2e779e2b4b256d07c3bJim Miller                    R.plurals.security_settings_fingerprint_preference_summary,
36194d4bd9390b9b9003788a2e779e2b4b256d07c3bJim Miller                    fingerprintCount, fingerprintCount));
3629218687e4baaf0226ef16b58f78123d979711e5dJim Miller            clazz = FingerprintSettings.class.getName();
36394d4bd9390b9b9003788a2e779e2b4b256d07c3bJim Miller        } else {
364d433359684386fc238bd4a2b45b67d991af07debJim Miller            fingerprintPreference.setSummary(
365d433359684386fc238bd4a2b45b67d991af07debJim Miller                    R.string.security_settings_fingerprint_preference_summary_none);
366ec8500438cf897098ff4a22d81de34f4214f44d8Selim Cinek            clazz = FingerprintEnrollIntroduction.class.getName();
36794d4bd9390b9b9003788a2e779e2b4b256d07c3bJim Miller        }
3689218687e4baaf0226ef16b58f78123d979711e5dJim Miller        intent.setClassName("com.android.settings", clazz);
36994d4bd9390b9b9003788a2e779e2b4b256d07c3bJim Miller        fingerprintPreference.setIntent(intent);
37094d4bd9390b9b9003788a2e779e2b4b256d07c3bJim Miller        securityCategory.addPreference(fingerprintPreference);
37194d4bd9390b9b9003788a2e779e2b4b256d07c3bJim Miller    }
37294d4bd9390b9b9003788a2e779e2b4b256d07c3bJim Miller
37394d4bd9390b9b9003788a2e779e2b4b256d07c3bJim Miller    private void addTrustAgentSettings(PreferenceGroup securityCategory) {
3745437588abd69bf3eff59684528aaf50e09b6cffcAdrian Roos        final boolean hasSecurity = mLockPatternUtils.isSecure(MY_USER_ID);
37594d4bd9390b9b9003788a2e779e2b4b256d07c3bJim Miller        ArrayList<TrustAgentComponentInfo> agents =
376088de47aa8595f9e4142ce0fe6885840b83edc93Adrian Roos                getActiveTrustAgents(getPackageManager(), mLockPatternUtils, mDPM);
37794d4bd9390b9b9003788a2e779e2b4b256d07c3bJim Miller        for (int i = 0; i < agents.size(); i++) {
37894d4bd9390b9b9003788a2e779e2b4b256d07c3bJim Miller            final TrustAgentComponentInfo agent = agents.get(i);
37994d4bd9390b9b9003788a2e779e2b4b256d07c3bJim Miller            Preference trustAgentPreference =
38094d4bd9390b9b9003788a2e779e2b4b256d07c3bJim Miller                    new Preference(securityCategory.getContext());
38194d4bd9390b9b9003788a2e779e2b4b256d07c3bJim Miller            trustAgentPreference.setKey(KEY_TRUST_AGENT);
38294d4bd9390b9b9003788a2e779e2b4b256d07c3bJim Miller            trustAgentPreference.setTitle(agent.title);
38394d4bd9390b9b9003788a2e779e2b4b256d07c3bJim Miller            trustAgentPreference.setSummary(agent.summary);
38494d4bd9390b9b9003788a2e779e2b4b256d07c3bJim Miller            // Create intent for this preference.
38594d4bd9390b9b9003788a2e779e2b4b256d07c3bJim Miller            Intent intent = new Intent();
38694d4bd9390b9b9003788a2e779e2b4b256d07c3bJim Miller            intent.setComponent(agent.componentName);
38794d4bd9390b9b9003788a2e779e2b4b256d07c3bJim Miller            intent.setAction(Intent.ACTION_MAIN);
38894d4bd9390b9b9003788a2e779e2b4b256d07c3bJim Miller            trustAgentPreference.setIntent(intent);
38994d4bd9390b9b9003788a2e779e2b4b256d07c3bJim Miller            // Add preference to the settings menu.
39094d4bd9390b9b9003788a2e779e2b4b256d07c3bJim Miller            securityCategory.addPreference(trustAgentPreference);
391088de47aa8595f9e4142ce0fe6885840b83edc93Adrian Roos
392088de47aa8595f9e4142ce0fe6885840b83edc93Adrian Roos            if (agent.disabledByAdministrator) {
393088de47aa8595f9e4142ce0fe6885840b83edc93Adrian Roos                trustAgentPreference.setEnabled(false);
394088de47aa8595f9e4142ce0fe6885840b83edc93Adrian Roos                trustAgentPreference.setSummary(R.string.trust_agent_disabled_device_admin);
395088de47aa8595f9e4142ce0fe6885840b83edc93Adrian Roos            } else if (!hasSecurity) {
39694d4bd9390b9b9003788a2e779e2b4b256d07c3bJim Miller                trustAgentPreference.setEnabled(false);
39794d4bd9390b9b9003788a2e779e2b4b256d07c3bJim Miller                trustAgentPreference.setSummary(R.string.disabled_because_no_backup_security);
39894d4bd9390b9b9003788a2e779e2b4b256d07c3bJim Miller            }
39994d4bd9390b9b9003788a2e779e2b4b256d07c3bJim Miller        }
40094d4bd9390b9b9003788a2e779e2b4b256d07c3bJim Miller    }
40194d4bd9390b9b9003788a2e779e2b4b256d07c3bJim Miller
4020b8788aae47932db56bf71869c5d58e0338420a4PauloftheWest    /* Return true if a there is a Slot that has Icc.
4030b8788aae47932db56bf71869c5d58e0338420a4PauloftheWest     */
4040b8788aae47932db56bf71869c5d58e0338420a4PauloftheWest    private boolean isSimIccReady() {
4050b8788aae47932db56bf71869c5d58e0338420a4PauloftheWest        TelephonyManager tm = TelephonyManager.getDefault();
4060b8788aae47932db56bf71869c5d58e0338420a4PauloftheWest        final List<SubscriptionInfo> subInfoList =
4070b8788aae47932db56bf71869c5d58e0338420a4PauloftheWest                mSubscriptionManager.getActiveSubscriptionInfoList();
4080b8788aae47932db56bf71869c5d58e0338420a4PauloftheWest
4090b8788aae47932db56bf71869c5d58e0338420a4PauloftheWest        if (subInfoList != null) {
4100b8788aae47932db56bf71869c5d58e0338420a4PauloftheWest            for (SubscriptionInfo subInfo : subInfoList) {
411247842017e2a3b330b13b469ac8f9f3d3fa19a0eSanket Padawe                if (tm.hasIccCard(subInfo.getSimSlotIndex())) {
4120b8788aae47932db56bf71869c5d58e0338420a4PauloftheWest                    return true;
4130b8788aae47932db56bf71869c5d58e0338420a4PauloftheWest                }
4140b8788aae47932db56bf71869c5d58e0338420a4PauloftheWest            }
4150b8788aae47932db56bf71869c5d58e0338420a4PauloftheWest        }
4160b8788aae47932db56bf71869c5d58e0338420a4PauloftheWest
4170b8788aae47932db56bf71869c5d58e0338420a4PauloftheWest        return false;
4180b8788aae47932db56bf71869c5d58e0338420a4PauloftheWest    }
4190b8788aae47932db56bf71869c5d58e0338420a4PauloftheWest
42050e6ecacf99c30bf239e68bd54895e3632315063PauloftheWest    /* Return true if a SIM is ready for locking.
42150e6ecacf99c30bf239e68bd54895e3632315063PauloftheWest     * TODO: consider adding to TelephonyManager or SubscritpionManasger.
42250e6ecacf99c30bf239e68bd54895e3632315063PauloftheWest     */
4230183fb5394f105d544472a52b2b4ba628e01da92Wink Saville    private boolean isSimReady() {
42450e6ecacf99c30bf239e68bd54895e3632315063PauloftheWest        int simState = TelephonyManager.SIM_STATE_UNKNOWN;
4250183fb5394f105d544472a52b2b4ba628e01da92Wink Saville        final List<SubscriptionInfo> subInfoList =
4260183fb5394f105d544472a52b2b4ba628e01da92Wink Saville                mSubscriptionManager.getActiveSubscriptionInfoList();
42750e6ecacf99c30bf239e68bd54895e3632315063PauloftheWest        if (subInfoList != null) {
428ca7566125e00ed267aba831bd1d6d1ad78d90c17Wink Saville            for (SubscriptionInfo subInfo : subInfoList) {
4293ada2ecbe02c98da529aa106e538a4439219cb62Stuart Scott                simState = TelephonyManager.getDefault().getSimState(subInfo.getSimSlotIndex());
43050e6ecacf99c30bf239e68bd54895e3632315063PauloftheWest                if((simState != TelephonyManager.SIM_STATE_ABSENT) &&
43150e6ecacf99c30bf239e68bd54895e3632315063PauloftheWest                            (simState != TelephonyManager.SIM_STATE_UNKNOWN)){
43250e6ecacf99c30bf239e68bd54895e3632315063PauloftheWest                    return true;
43350e6ecacf99c30bf239e68bd54895e3632315063PauloftheWest                }
43450e6ecacf99c30bf239e68bd54895e3632315063PauloftheWest            }
43550e6ecacf99c30bf239e68bd54895e3632315063PauloftheWest        }
43650e6ecacf99c30bf239e68bd54895e3632315063PauloftheWest        return false;
43750e6ecacf99c30bf239e68bd54895e3632315063PauloftheWest    }
43850e6ecacf99c30bf239e68bd54895e3632315063PauloftheWest
439fe7655914de3852093c35600df996824d03ddf63Jim Miller    private static ArrayList<TrustAgentComponentInfo> getActiveTrustAgents(
440088de47aa8595f9e4142ce0fe6885840b83edc93Adrian Roos            PackageManager pm, LockPatternUtils utils, DevicePolicyManager dpm) {
441fe7655914de3852093c35600df996824d03ddf63Jim Miller        ArrayList<TrustAgentComponentInfo> result = new ArrayList<TrustAgentComponentInfo>();
442fe7655914de3852093c35600df996824d03ddf63Jim Miller        List<ResolveInfo> resolveInfos = pm.queryIntentServices(TRUST_AGENT_INTENT,
443fe7655914de3852093c35600df996824d03ddf63Jim Miller                PackageManager.GET_META_DATA);
4445437588abd69bf3eff59684528aaf50e09b6cffcAdrian Roos        List<ComponentName> enabledTrustAgents = utils.getEnabledTrustAgents(MY_USER_ID);
445088de47aa8595f9e4142ce0fe6885840b83edc93Adrian Roos
446088de47aa8595f9e4142ce0fe6885840b83edc93Adrian Roos        boolean disableTrustAgents = (dpm.getKeyguardDisabledFeatures(null)
447088de47aa8595f9e4142ce0fe6885840b83edc93Adrian Roos                & DevicePolicyManager.KEYGUARD_DISABLE_TRUST_AGENTS) != 0;
448088de47aa8595f9e4142ce0fe6885840b83edc93Adrian Roos
449fe7655914de3852093c35600df996824d03ddf63Jim Miller        if (enabledTrustAgents != null && !enabledTrustAgents.isEmpty()) {
450fe7655914de3852093c35600df996824d03ddf63Jim Miller            for (int i = 0; i < resolveInfos.size(); i++) {
451fe7655914de3852093c35600df996824d03ddf63Jim Miller                ResolveInfo resolveInfo = resolveInfos.get(i);
452fe7655914de3852093c35600df996824d03ddf63Jim Miller                if (resolveInfo.serviceInfo == null) continue;
453fe7655914de3852093c35600df996824d03ddf63Jim Miller                if (!TrustAgentUtils.checkProvidePermission(resolveInfo, pm)) continue;
454fe7655914de3852093c35600df996824d03ddf63Jim Miller                TrustAgentComponentInfo trustAgentComponentInfo =
455fe7655914de3852093c35600df996824d03ddf63Jim Miller                        TrustAgentUtils.getSettingsComponent(pm, resolveInfo);
456fe7655914de3852093c35600df996824d03ddf63Jim Miller                if (trustAgentComponentInfo.componentName == null ||
457fe7655914de3852093c35600df996824d03ddf63Jim Miller                        !enabledTrustAgents.contains(
458fe7655914de3852093c35600df996824d03ddf63Jim Miller                                TrustAgentUtils.getComponentName(resolveInfo)) ||
459fe7655914de3852093c35600df996824d03ddf63Jim Miller                        TextUtils.isEmpty(trustAgentComponentInfo.title)) continue;
460088de47aa8595f9e4142ce0fe6885840b83edc93Adrian Roos                if (disableTrustAgents && dpm.getTrustAgentConfiguration(
461088de47aa8595f9e4142ce0fe6885840b83edc93Adrian Roos                        null, TrustAgentUtils.getComponentName(resolveInfo)) == null) {
462088de47aa8595f9e4142ce0fe6885840b83edc93Adrian Roos                    trustAgentComponentInfo.disabledByAdministrator = true;
463088de47aa8595f9e4142ce0fe6885840b83edc93Adrian Roos                }
464fe7655914de3852093c35600df996824d03ddf63Jim Miller                result.add(trustAgentComponentInfo);
465fe7655914de3852093c35600df996824d03ddf63Jim Miller                if (ONLY_ONE_TRUST_AGENT) break;
466fe7655914de3852093c35600df996824d03ddf63Jim Miller            }
467fe7655914de3852093c35600df996824d03ddf63Jim Miller        }
468fe7655914de3852093c35600df996824d03ddf63Jim Miller        return result;
469fe7655914de3852093c35600df996824d03ddf63Jim Miller    }
470fe7655914de3852093c35600df996824d03ddf63Jim Miller
4712c88a877f88b142fa8fd36c1773ab484b5fd7338Gilles Debunne    private boolean isNonMarketAppsAllowed() {
4725a64c739bfbf644dabd11d5330f577e94d478326Christopher Tate        return Settings.Global.getInt(getContentResolver(),
4735a64c739bfbf644dabd11d5330f577e94d478326Christopher Tate                                      Settings.Global.INSTALL_NON_MARKET_APPS, 0) > 0;
4742c88a877f88b142fa8fd36c1773ab484b5fd7338Gilles Debunne    }
4752c88a877f88b142fa8fd36c1773ab484b5fd7338Gilles Debunne
4762c88a877f88b142fa8fd36c1773ab484b5fd7338Gilles Debunne    private void setNonMarketAppsAllowed(boolean enabled) {
4770c5a401a58b30041d5fe712d78fccbd47f5b744dMaggie Benthall        final UserManager um = (UserManager) getActivity().getSystemService(Context.USER_SERVICE);
4780c5a401a58b30041d5fe712d78fccbd47f5b744dMaggie Benthall        if (um.hasUserRestriction(UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES)) {
4790c5a401a58b30041d5fe712d78fccbd47f5b744dMaggie Benthall            return;
4800c5a401a58b30041d5fe712d78fccbd47f5b744dMaggie Benthall        }
4812c88a877f88b142fa8fd36c1773ab484b5fd7338Gilles Debunne        // Change the system setting
4825a64c739bfbf644dabd11d5330f577e94d478326Christopher Tate        Settings.Global.putInt(getContentResolver(), Settings.Global.INSTALL_NON_MARKET_APPS,
4832c88a877f88b142fa8fd36c1773ab484b5fd7338Gilles Debunne                                enabled ? 1 : 0);
4842c88a877f88b142fa8fd36c1773ab484b5fd7338Gilles Debunne    }
4852c88a877f88b142fa8fd36c1773ab484b5fd7338Gilles Debunne
4862c88a877f88b142fa8fd36c1773ab484b5fd7338Gilles Debunne    private void warnAppInstallation() {
4872c88a877f88b142fa8fd36c1773ab484b5fd7338Gilles Debunne        // TODO: DialogFragment?
4882c88a877f88b142fa8fd36c1773ab484b5fd7338Gilles Debunne        mWarnInstallApps = new AlertDialog.Builder(getActivity()).setTitle(
4892c88a877f88b142fa8fd36c1773ab484b5fd7338Gilles Debunne                getResources().getString(R.string.error_title))
4902c88a877f88b142fa8fd36c1773ab484b5fd7338Gilles Debunne                .setIcon(com.android.internal.R.drawable.ic_dialog_alert)
4912c88a877f88b142fa8fd36c1773ab484b5fd7338Gilles Debunne                .setMessage(getResources().getString(R.string.install_all_warning))
4922c88a877f88b142fa8fd36c1773ab484b5fd7338Gilles Debunne                .setPositiveButton(android.R.string.yes, this)
49317224826f896278de0805e6a2fec321701bbb83darete                .setNegativeButton(android.R.string.no, this)
4942c88a877f88b142fa8fd36c1773ab484b5fd7338Gilles Debunne                .show();
4952c88a877f88b142fa8fd36c1773ab484b5fd7338Gilles Debunne    }
4962c88a877f88b142fa8fd36c1773ab484b5fd7338Gilles Debunne
4970c5a401a58b30041d5fe712d78fccbd47f5b744dMaggie Benthall    @Override
4982c88a877f88b142fa8fd36c1773ab484b5fd7338Gilles Debunne    public void onClick(DialogInterface dialog, int which) {
49917224826f896278de0805e6a2fec321701bbb83darete        if (dialog == mWarnInstallApps) {
50017224826f896278de0805e6a2fec321701bbb83darete            boolean turnOn = which == DialogInterface.BUTTON_POSITIVE;
50117224826f896278de0805e6a2fec321701bbb83darete            setNonMarketAppsAllowed(turnOn);
502b810a0ddf9dd93a08789aff38901eeab9bfaf812Amith Yamasani            if (mToggleAppInstallation != null) {
50317224826f896278de0805e6a2fec321701bbb83darete                mToggleAppInstallation.setChecked(turnOn);
504b810a0ddf9dd93a08789aff38901eeab9bfaf812Amith Yamasani            }
5052c88a877f88b142fa8fd36c1773ab484b5fd7338Gilles Debunne        }
5062c88a877f88b142fa8fd36c1773ab484b5fd7338Gilles Debunne    }
5072c88a877f88b142fa8fd36c1773ab484b5fd7338Gilles Debunne
5082c88a877f88b142fa8fd36c1773ab484b5fd7338Gilles Debunne    @Override
5092c88a877f88b142fa8fd36c1773ab484b5fd7338Gilles Debunne    public void onDestroy() {
5102c88a877f88b142fa8fd36c1773ab484b5fd7338Gilles Debunne        super.onDestroy();
5112c88a877f88b142fa8fd36c1773ab484b5fd7338Gilles Debunne        if (mWarnInstallApps != null) {
5122c88a877f88b142fa8fd36c1773ab484b5fd7338Gilles Debunne            mWarnInstallApps.dismiss();
5132c88a877f88b142fa8fd36c1773ab484b5fd7338Gilles Debunne        }
5142c88a877f88b142fa8fd36c1773ab484b5fd7338Gilles Debunne    }
5152c88a877f88b142fa8fd36c1773ab484b5fd7338Gilles Debunne
51691d65a20af2d1bfb06f32d0bd821e8558afe939dChia-chi Yeh    private void setupLockAfterPreference() {
51791d65a20af2d1bfb06f32d0bd821e8558afe939dChia-chi Yeh        // Compatible with pre-Froyo
51891d65a20af2d1bfb06f32d0bd821e8558afe939dChia-chi Yeh        long currentTimeout = Settings.Secure.getLong(getContentResolver(),
51991d65a20af2d1bfb06f32d0bd821e8558afe939dChia-chi Yeh                Settings.Secure.LOCK_SCREEN_LOCK_AFTER_TIMEOUT, 5000);
52091d65a20af2d1bfb06f32d0bd821e8558afe939dChia-chi Yeh        mLockAfter.setValue(String.valueOf(currentTimeout));
52191d65a20af2d1bfb06f32d0bd821e8558afe939dChia-chi Yeh        mLockAfter.setOnPreferenceChangeListener(this);
52291d65a20af2d1bfb06f32d0bd821e8558afe939dChia-chi Yeh        final long adminTimeout = (mDPM != null ? mDPM.getMaximumTimeToLock(null) : 0);
52391d65a20af2d1bfb06f32d0bd821e8558afe939dChia-chi Yeh        final long displayTimeout = Math.max(0,
52491d65a20af2d1bfb06f32d0bd821e8558afe939dChia-chi Yeh                Settings.System.getInt(getContentResolver(), SCREEN_OFF_TIMEOUT, 0));
52591d65a20af2d1bfb06f32d0bd821e8558afe939dChia-chi Yeh        if (adminTimeout > 0) {
52691d65a20af2d1bfb06f32d0bd821e8558afe939dChia-chi Yeh            // This setting is a slave to display timeout when a device policy is enforced.
52791d65a20af2d1bfb06f32d0bd821e8558afe939dChia-chi Yeh            // As such, maxLockTimeout = adminTimeout - displayTimeout.
52891d65a20af2d1bfb06f32d0bd821e8558afe939dChia-chi Yeh            // If there isn't enough time, shows "immediately" setting.
52991d65a20af2d1bfb06f32d0bd821e8558afe939dChia-chi Yeh            disableUnusableTimeouts(Math.max(0, adminTimeout - displayTimeout));
53086624a21540e2cd336573a55adb5d3997e4a717dJim Miller        }
53186624a21540e2cd336573a55adb5d3997e4a717dJim Miller    }
53286624a21540e2cd336573a55adb5d3997e4a717dJim Miller
533c9787070bab332b80676b745a44dd8ee4d72073dJim Miller    private void updateLockAfterPreferenceSummary() {
534c9787070bab332b80676b745a44dd8ee4d72073dJim Miller        // Update summary message with current value
535c9787070bab332b80676b745a44dd8ee4d72073dJim Miller        long currentTimeout = Settings.Secure.getLong(getContentResolver(),
53658bca8f15fbdc9e621b3c259a50ecf41c00e64c3Amith Yamasani                Settings.Secure.LOCK_SCREEN_LOCK_AFTER_TIMEOUT, 5000);
537c9787070bab332b80676b745a44dd8ee4d72073dJim Miller        final CharSequence[] entries = mLockAfter.getEntries();
538c9787070bab332b80676b745a44dd8ee4d72073dJim Miller        final CharSequence[] values = mLockAfter.getEntryValues();
539c9787070bab332b80676b745a44dd8ee4d72073dJim Miller        int best = 0;
540c9787070bab332b80676b745a44dd8ee4d72073dJim Miller        for (int i = 0; i < values.length; i++) {
541c9787070bab332b80676b745a44dd8ee4d72073dJim Miller            long timeout = Long.valueOf(values[i].toString());
542c9787070bab332b80676b745a44dd8ee4d72073dJim Miller            if (currentTimeout >= timeout) {
543c9787070bab332b80676b745a44dd8ee4d72073dJim Miller                best = i;
544c9787070bab332b80676b745a44dd8ee4d72073dJim Miller            }
545c9787070bab332b80676b745a44dd8ee4d72073dJim Miller        }
5461682a0d1be53562a8ca2d0eb8fcd41b07af2f144Ido Ofir
5471682a0d1be53562a8ca2d0eb8fcd41b07af2f144Ido Ofir        Preference preference = getPreferenceScreen().findPreference(KEY_TRUST_AGENT);
5481682a0d1be53562a8ca2d0eb8fcd41b07af2f144Ido Ofir        if (preference != null && preference.getTitle().length() > 0) {
54950d7d19a7377df9421b6a0af2d8ecce2dfd32796Jim Miller            if (Long.valueOf(values[best].toString()) == 0) {
55050d7d19a7377df9421b6a0af2d8ecce2dfd32796Jim Miller                mLockAfter.setSummary(getString(R.string.lock_immediately_summary_with_exception,
55150d7d19a7377df9421b6a0af2d8ecce2dfd32796Jim Miller                        preference.getTitle()));
55250d7d19a7377df9421b6a0af2d8ecce2dfd32796Jim Miller            } else {
55350d7d19a7377df9421b6a0af2d8ecce2dfd32796Jim Miller                mLockAfter.setSummary(getString(R.string.lock_after_timeout_summary_with_exception,
55450d7d19a7377df9421b6a0af2d8ecce2dfd32796Jim Miller                        entries[best], preference.getTitle()));
55550d7d19a7377df9421b6a0af2d8ecce2dfd32796Jim Miller            }
5561682a0d1be53562a8ca2d0eb8fcd41b07af2f144Ido Ofir        } else {
5571682a0d1be53562a8ca2d0eb8fcd41b07af2f144Ido Ofir            mLockAfter.setSummary(getString(R.string.lock_after_timeout_summary, entries[best]));
5581682a0d1be53562a8ca2d0eb8fcd41b07af2f144Ido Ofir        }
559c9787070bab332b80676b745a44dd8ee4d72073dJim Miller    }
560c9787070bab332b80676b745a44dd8ee4d72073dJim Miller
56191d65a20af2d1bfb06f32d0bd821e8558afe939dChia-chi Yeh    private void disableUnusableTimeouts(long maxTimeout) {
56291d65a20af2d1bfb06f32d0bd821e8558afe939dChia-chi Yeh        final CharSequence[] entries = mLockAfter.getEntries();
56391d65a20af2d1bfb06f32d0bd821e8558afe939dChia-chi Yeh        final CharSequence[] values = mLockAfter.getEntryValues();
56486624a21540e2cd336573a55adb5d3997e4a717dJim Miller        ArrayList<CharSequence> revisedEntries = new ArrayList<CharSequence>();
56586624a21540e2cd336573a55adb5d3997e4a717dJim Miller        ArrayList<CharSequence> revisedValues = new ArrayList<CharSequence>();
56686624a21540e2cd336573a55adb5d3997e4a717dJim Miller        for (int i = 0; i < values.length; i++) {
56786624a21540e2cd336573a55adb5d3997e4a717dJim Miller            long timeout = Long.valueOf(values[i].toString());
56886624a21540e2cd336573a55adb5d3997e4a717dJim Miller            if (timeout <= maxTimeout) {
56986624a21540e2cd336573a55adb5d3997e4a717dJim Miller                revisedEntries.add(entries[i]);
57086624a21540e2cd336573a55adb5d3997e4a717dJim Miller                revisedValues.add(values[i]);
57186624a21540e2cd336573a55adb5d3997e4a717dJim Miller            }
57286624a21540e2cd336573a55adb5d3997e4a717dJim Miller        }
57386624a21540e2cd336573a55adb5d3997e4a717dJim Miller        if (revisedEntries.size() != entries.length || revisedValues.size() != values.length) {
57491d65a20af2d1bfb06f32d0bd821e8558afe939dChia-chi Yeh            mLockAfter.setEntries(
57586624a21540e2cd336573a55adb5d3997e4a717dJim Miller                    revisedEntries.toArray(new CharSequence[revisedEntries.size()]));
57691d65a20af2d1bfb06f32d0bd821e8558afe939dChia-chi Yeh            mLockAfter.setEntryValues(
57786624a21540e2cd336573a55adb5d3997e4a717dJim Miller                    revisedValues.toArray(new CharSequence[revisedValues.size()]));
57891d65a20af2d1bfb06f32d0bd821e8558afe939dChia-chi Yeh            final int userPreference = Integer.valueOf(mLockAfter.getValue());
57986624a21540e2cd336573a55adb5d3997e4a717dJim Miller            if (userPreference <= maxTimeout) {
58091d65a20af2d1bfb06f32d0bd821e8558afe939dChia-chi Yeh                mLockAfter.setValue(String.valueOf(userPreference));
58186624a21540e2cd336573a55adb5d3997e4a717dJim Miller            } else {
58286624a21540e2cd336573a55adb5d3997e4a717dJim Miller                // There will be no highlighted selection since nothing in the list matches
58386624a21540e2cd336573a55adb5d3997e4a717dJim Miller                // maxTimeout. The user can still select anything less than maxTimeout.
58486624a21540e2cd336573a55adb5d3997e4a717dJim Miller                // TODO: maybe append maxTimeout to the list and mark selected.
58586624a21540e2cd336573a55adb5d3997e4a717dJim Miller            }
58686624a21540e2cd336573a55adb5d3997e4a717dJim Miller        }
58791d65a20af2d1bfb06f32d0bd821e8558afe939dChia-chi Yeh        mLockAfter.setEnabled(revisedEntries.size() > 0);
58886624a21540e2cd336573a55adb5d3997e4a717dJim Miller    }
58986624a21540e2cd336573a55adb5d3997e4a717dJim Miller
590afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    @Override
591250b9bee685de8f4060dd410f0894c22de1909cfJim Miller    public void onSaveInstanceState(Bundle outState) {
592250b9bee685de8f4060dd410f0894c22de1909cfJim Miller        super.onSaveInstanceState(outState);
593250b9bee685de8f4060dd410f0894c22de1909cfJim Miller        if (mTrustAgentClickIntent != null) {
594250b9bee685de8f4060dd410f0894c22de1909cfJim Miller            outState.putParcelable(TRUST_AGENT_CLICK_INTENT, mTrustAgentClickIntent);
595250b9bee685de8f4060dd410f0894c22de1909cfJim Miller        }
596250b9bee685de8f4060dd410f0894c22de1909cfJim Miller    }
597250b9bee685de8f4060dd410f0894c22de1909cfJim Miller
598250b9bee685de8f4060dd410f0894c22de1909cfJim Miller    @Override
599d79934731c8d33f6fc63b21c120b9ffba5d06f54Amith Yamasani    public void onResume() {
600afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        super.onResume();
601afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project
60286624a21540e2cd336573a55adb5d3997e4a717dJim Miller        // Make sure we reload the preference hierarchy since some of these settings
60386624a21540e2cd336573a55adb5d3997e4a717dJim Miller        // depend on others...
60486624a21540e2cd336573a55adb5d3997e4a717dJim Miller        createPreferenceHierarchy();
60586624a21540e2cd336573a55adb5d3997e4a717dJim Miller
60600d2476d8fc122b4eef59d6be6f7aa41e9c56fdeJim Miller        final LockPatternUtils lockPatternUtils = mChooseLockSettingsHelper.utils();
607122b6c85a9bc7076eacc388142f8c4fccc5462c9Jim Miller        if (mVisiblePattern != null) {
6085437588abd69bf3eff59684528aaf50e09b6cffcAdrian Roos            mVisiblePattern.setChecked(lockPatternUtils.isVisiblePatternEnabled(
6095437588abd69bf3eff59684528aaf50e09b6cffcAdrian Roos                    MY_USER_ID));
610122b6c85a9bc7076eacc388142f8c4fccc5462c9Jim Miller        }
611071742d838f9b0c312af309c87eaf2c444aeeab8Jim Miller        if (mPowerButtonInstantlyLocks != null) {
6125437588abd69bf3eff59684528aaf50e09b6cffcAdrian Roos            mPowerButtonInstantlyLocks.setChecked(lockPatternUtils.getPowerButtonInstantlyLocks(
6135437588abd69bf3eff59684528aaf50e09b6cffcAdrian Roos                    MY_USER_ID));
614071742d838f9b0c312af309c87eaf2c444aeeab8Jim Miller        }
6157031ab0d756327496ac3ce1e082b53c94be2f3b1Hung-ying Tyan
616b810a0ddf9dd93a08789aff38901eeab9bfaf812Amith Yamasani        if (mShowPassword != null) {
617b810a0ddf9dd93a08789aff38901eeab9bfaf812Amith Yamasani            mShowPassword.setChecked(Settings.System.getInt(getContentResolver(),
618b810a0ddf9dd93a08789aff38901eeab9bfaf812Amith Yamasani                    Settings.System.TEXT_SHOW_PASSWORD, 1) != 0);
619b810a0ddf9dd93a08789aff38901eeab9bfaf812Amith Yamasani        }
620b0883cb384fc7ad46e011a72355c9fda924e5b28Hung-ying Tyan
621b810a0ddf9dd93a08789aff38901eeab9bfaf812Amith Yamasani        if (mResetCredentials != null) {
622c4372c3e3fd2d0be54341973bd72ec618931057cKenny Root            mResetCredentials.setEnabled(!mKeyStore.isEmpty());
623b810a0ddf9dd93a08789aff38901eeab9bfaf812Amith Yamasani        }
6243bcd76ce63c77b229e2d25a0191ccb568824f5c4Jason Monk
6253bcd76ce63c77b229e2d25a0191ccb568824f5c4Jason Monk        updateOwnerInfo();
6263bcd76ce63c77b229e2d25a0191ccb568824f5c4Jason Monk    }
6273bcd76ce63c77b229e2d25a0191ccb568824f5c4Jason Monk
6283bcd76ce63c77b229e2d25a0191ccb568824f5c4Jason Monk    public void updateOwnerInfo() {
6293bcd76ce63c77b229e2d25a0191ccb568824f5c4Jason Monk        if (mOwnerInfoPref != null) {
6305437588abd69bf3eff59684528aaf50e09b6cffcAdrian Roos            mOwnerInfoPref.setSummary(mLockPatternUtils.isOwnerInfoEnabled(MY_USER_ID)
6315437588abd69bf3eff59684528aaf50e09b6cffcAdrian Roos                    ? mLockPatternUtils.getOwnerInfo(MY_USER_ID)
6323bcd76ce63c77b229e2d25a0191ccb568824f5c4Jason Monk                    : getString(R.string.owner_info_settings_summary));
6333bcd76ce63c77b229e2d25a0191ccb568824f5c4Jason Monk        }
634afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    }
635afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project
636afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    @Override
637a6a8a1479b970d8a84395453703348fe42d17438Gilles Debunne    public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
638afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        final String key = preference.getKey();
639bbb4afa19f75694c585506b0c091372d60e07ca7Jim Miller        if (KEY_UNLOCK_SET_OR_CHANGE.equals(key)) {
64017e9e1933055b207473b488595b2887bfcaea32eJim Miller            startFragment(this, "com.android.settings.ChooseLockGeneric$ChooseLockGenericFragment",
6415bdf0423f9dbc3cf5970045aacdfef79daacc430Fabrice Di Meglio                    R.string.lock_settings_picker_title, SET_OR_CHANGE_LOCK_METHOD_REQUEST, null);
6426f482447b05492906755d91ac4c29ec505a8a544Ido Ofir        } else if (KEY_TRUST_AGENT.equals(key)) {
6436f482447b05492906755d91ac4c29ec505a8a544Ido Ofir            ChooseLockSettingsHelper helper =
6446f482447b05492906755d91ac4c29ec505a8a544Ido Ofir                    new ChooseLockSettingsHelper(this.getActivity(), this);
645250b9bee685de8f4060dd410f0894c22de1909cfJim Miller            mTrustAgentClickIntent = preference.getIntent();
6468a09b619aeb233e2aab1919301f162d8acf1f0f0Jorim Jaggi            boolean confirmationLaunched = helper.launchConfirmationActivity(
6478a09b619aeb233e2aab1919301f162d8acf1f0f0Jorim Jaggi                    CHANGE_TRUST_AGENT_SETTINGS, preference.getTitle());
6488a09b619aeb233e2aab1919301f162d8acf1f0f0Jorim Jaggi            if (!confirmationLaunched&&  mTrustAgentClickIntent != null) {
6496f482447b05492906755d91ac4c29ec505a8a544Ido Ofir                // If this returns false, it means no password confirmation is required.
650250b9bee685de8f4060dd410f0894c22de1909cfJim Miller                startActivity(mTrustAgentClickIntent);
651250b9bee685de8f4060dd410f0894c22de1909cfJim Miller                mTrustAgentClickIntent = null;
6526f482447b05492906755d91ac4c29ec505a8a544Ido Ofir            }
65336972bb7e952ca8a7bd8e57e32246d60873d7479Jim Miller        } else {
65436972bb7e952ca8a7bd8e57e32246d60873d7479Jim Miller            // If we didn't handle it, let preferences handle it.
65536972bb7e952ca8a7bd8e57e32246d60873d7479Jim Miller            return super.onPreferenceTreeClick(preferenceScreen, preference);
656afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        }
65736972bb7e952ca8a7bd8e57e32246d60873d7479Jim Miller        return true;
658afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    }
659afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project
660afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    /**
661a6a8a1479b970d8a84395453703348fe42d17438Gilles Debunne     * see confirmPatternThenDisableAndClear
662afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project     */
663afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    @Override
664d79934731c8d33f6fc63b21c120b9ffba5d06f54Amith Yamasani    public void onActivityResult(int requestCode, int resultCode, Intent data) {
665afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        super.onActivityResult(requestCode, resultCode, data);
666f788718f2142e5546bbf1b6847793d59ffea898bAdrian Roos        if (requestCode == CHANGE_TRUST_AGENT_SETTINGS && resultCode == Activity.RESULT_OK) {
667250b9bee685de8f4060dd410f0894c22de1909cfJim Miller            if (mTrustAgentClickIntent != null) {
668250b9bee685de8f4060dd410f0894c22de1909cfJim Miller                startActivity(mTrustAgentClickIntent);
669250b9bee685de8f4060dd410f0894c22de1909cfJim Miller                mTrustAgentClickIntent = null;
6706f482447b05492906755d91ac4c29ec505a8a544Ido Ofir            }
671250b9bee685de8f4060dd410f0894c22de1909cfJim Miller            return;
672487b16f3400b62b79f8108ca669cc3e937c69defDanielle Millett        }
673122b6c85a9bc7076eacc388142f8c4fccc5462c9Jim Miller        createPreferenceHierarchy();
674afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    }
675f06d8698a4a2de1973a25df0c6563bdfcba4eb03Amith Yamasani
6760c5a401a58b30041d5fe712d78fccbd47f5b744dMaggie Benthall    @Override
67786624a21540e2cd336573a55adb5d3997e4a717dJim Miller    public boolean onPreferenceChange(Preference preference, Object value) {
678300e920f66c03b390ce738ccf4d4646ffa039d40arete        boolean result = true;
6794a9065e6547c24c751b0aecbc151e57bcbcf92eaJim Miller        final String key = preference.getKey();
6804a9065e6547c24c751b0aecbc151e57bcbcf92eaJim Miller        final LockPatternUtils lockPatternUtils = mChooseLockSettingsHelper.utils();
6814a9065e6547c24c751b0aecbc151e57bcbcf92eaJim Miller        if (KEY_LOCK_AFTER_TIMEOUT.equals(key)) {
68291d65a20af2d1bfb06f32d0bd821e8558afe939dChia-chi Yeh            int timeout = Integer.parseInt((String) value);
68386624a21540e2cd336573a55adb5d3997e4a717dJim Miller            try {
68486624a21540e2cd336573a55adb5d3997e4a717dJim Miller                Settings.Secure.putInt(getContentResolver(),
68591d65a20af2d1bfb06f32d0bd821e8558afe939dChia-chi Yeh                        Settings.Secure.LOCK_SCREEN_LOCK_AFTER_TIMEOUT, timeout);
68686624a21540e2cd336573a55adb5d3997e4a717dJim Miller            } catch (NumberFormatException e) {
68786624a21540e2cd336573a55adb5d3997e4a717dJim Miller                Log.e("SecuritySettings", "could not persist lockAfter timeout setting", e);
68886624a21540e2cd336573a55adb5d3997e4a717dJim Miller            }
689c9787070bab332b80676b745a44dd8ee4d72073dJim Miller            updateLockAfterPreferenceSummary();
6904a9065e6547c24c751b0aecbc151e57bcbcf92eaJim Miller        } else if (KEY_VISIBLE_PATTERN.equals(key)) {
6915437588abd69bf3eff59684528aaf50e09b6cffcAdrian Roos            lockPatternUtils.setVisiblePatternEnabled((Boolean) value, MY_USER_ID);
6924a9065e6547c24c751b0aecbc151e57bcbcf92eaJim Miller        } else if (KEY_POWER_INSTANTLY_LOCKS.equals(key)) {
6935437588abd69bf3eff59684528aaf50e09b6cffcAdrian Roos            mLockPatternUtils.setPowerButtonInstantlyLocks((Boolean) value, MY_USER_ID);
6944a9065e6547c24c751b0aecbc151e57bcbcf92eaJim Miller        } else if (KEY_SHOW_PASSWORD.equals(key)) {
6954a9065e6547c24c751b0aecbc151e57bcbcf92eaJim Miller            Settings.System.putInt(getContentResolver(), Settings.System.TEXT_SHOW_PASSWORD,
6964a9065e6547c24c751b0aecbc151e57bcbcf92eaJim Miller                    ((Boolean) value) ? 1 : 0);
697c04420ce2581627478b59cc5d8b453f1fa329e02Paul Lawrence            lockPatternUtils.setVisiblePasswordEnabled((Boolean) value, MY_USER_ID);
6984a9065e6547c24c751b0aecbc151e57bcbcf92eaJim Miller        } else if (KEY_TOGGLE_INSTALL_APPLICATIONS.equals(key)) {
6994a9065e6547c24c751b0aecbc151e57bcbcf92eaJim Miller            if ((Boolean) value) {
7004a9065e6547c24c751b0aecbc151e57bcbcf92eaJim Miller                mToggleAppInstallation.setChecked(false);
7014a9065e6547c24c751b0aecbc151e57bcbcf92eaJim Miller                warnAppInstallation();
702300e920f66c03b390ce738ccf4d4646ffa039d40arete                // Don't change Switch status until user makes choice in dialog, so return false.
703300e920f66c03b390ce738ccf4d4646ffa039d40arete                result = false;
7044a9065e6547c24c751b0aecbc151e57bcbcf92eaJim Miller            } else {
7054a9065e6547c24c751b0aecbc151e57bcbcf92eaJim Miller                setNonMarketAppsAllowed(false);
7064a9065e6547c24c751b0aecbc151e57bcbcf92eaJim Miller            }
70786624a21540e2cd336573a55adb5d3997e4a717dJim Miller        }
708300e920f66c03b390ce738ccf4d4646ffa039d40arete        return result;
70986624a21540e2cd336573a55adb5d3997e4a717dJim Miller    }
710487b16f3400b62b79f8108ca669cc3e937c69defDanielle Millett
71193c0ee52cc5709ec7353fee5a01e4dd9000e42d1rich cannings    @Override
71293c0ee52cc5709ec7353fee5a01e4dd9000e42d1rich cannings    protected int getHelpResource() {
71393c0ee52cc5709ec7353fee5a01e4dd9000e42d1rich cannings        return R.string.help_url_security;
71493c0ee52cc5709ec7353fee5a01e4dd9000e42d1rich cannings    }
71593c0ee52cc5709ec7353fee5a01e4dd9000e42d1rich cannings
716d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio    /**
717d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio     * For Search. Please keep it in sync when updating "createPreferenceHierarchy()"
718d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio     */
719d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio    public static final SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
720d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio            new SecuritySearchIndexProvider();
721d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio
722fe7655914de3852093c35600df996824d03ddf63Jim Miller    private static class SecuritySearchIndexProvider extends BaseSearchIndexProvider {
723d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio
724d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio        boolean mIsPrimary;
725d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio
726d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio        public SecuritySearchIndexProvider() {
727d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio            super();
728d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio
7295437588abd69bf3eff59684528aaf50e09b6cffcAdrian Roos            mIsPrimary = MY_USER_ID == UserHandle.USER_OWNER;
730d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio        }
731d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio
732d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio        @Override
733d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio        public List<SearchIndexableResource> getXmlResourcesToIndex(
734d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio                Context context, boolean enabled) {
735d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio
736d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio            List<SearchIndexableResource> result = new ArrayList<SearchIndexableResource>();
737d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio
738d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio            LockPatternUtils lockPatternUtils = new LockPatternUtils(context);
739d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio            // Add options for lock/unlock screen
740d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio            int resId = getResIdForLockUnlockScreen(context, lockPatternUtils);
741d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio
742d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio            SearchIndexableResource sir = new SearchIndexableResource(context);
743d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio            sir.xmlResId = resId;
744d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio            result.add(sir);
745d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio
746d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio            if (mIsPrimary) {
747d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio                DevicePolicyManager dpm = (DevicePolicyManager)
748d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio                        context.getSystemService(Context.DEVICE_POLICY_SERVICE);
749d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio
750d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio                switch (dpm.getStorageEncryptionStatus()) {
751d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio                    case DevicePolicyManager.ENCRYPTION_STATUS_ACTIVE:
752d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio                        // The device is currently encrypted.
753d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio                        resId = R.xml.security_settings_encrypted;
754d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio                        break;
755d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio                    case DevicePolicyManager.ENCRYPTION_STATUS_INACTIVE:
756d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio                        // This device supports encryption but isn't encrypted.
757d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio                        resId = R.xml.security_settings_unencrypted;
758d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio                        break;
759d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio                }
760d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio
761d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio                sir = new SearchIndexableResource(context);
762d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio                sir.xmlResId = resId;
763d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio                result.add(sir);
764d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio            }
765d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio
766d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio            // Append the rest of the settings
767d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio            sir = new SearchIndexableResource(context);
768d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio            sir.xmlResId = R.xml.security_settings_misc;
769d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio            result.add(sir);
770d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio
771d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio            return result;
772d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio        }
773d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio
774d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio        @Override
775d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio        public List<SearchIndexableRaw> getRawDataToIndex(Context context, boolean enabled) {
776d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio            final List<SearchIndexableRaw> result = new ArrayList<SearchIndexableRaw>();
777d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio            final Resources res = context.getResources();
778d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio
779d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio            final String screenTitle = res.getString(R.string.security_settings_title);
780d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio
781d729029b904d43c5c6d06f1d95004fe35445873dFabrice Di Meglio            SearchIndexableRaw data = new SearchIndexableRaw(context);
782d729029b904d43c5c6d06f1d95004fe35445873dFabrice Di Meglio            data.title = screenTitle;
783d729029b904d43c5c6d06f1d95004fe35445873dFabrice Di Meglio            data.screenTitle = screenTitle;
784d729029b904d43c5c6d06f1d95004fe35445873dFabrice Di Meglio            result.add(data);
785d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio
786d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio            if (!mIsPrimary) {
787d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio                int resId = (UserManager.get(context).isLinkedUser()) ?
788d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio                        R.string.profile_info_settings_title : R.string.user_info_settings_title;
789d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio
790d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio                data = new SearchIndexableRaw(context);
791d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio                data.title = res.getString(resId);
792d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio                data.screenTitle = screenTitle;
793d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio                result.add(data);
794d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio            }
795d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio
796f516e7dde0074416dbf51650cbfb9cc763027b27Julia Reynolds            // Fingerprint
797f516e7dde0074416dbf51650cbfb9cc763027b27Julia Reynolds            FingerprintManager fpm =
798f516e7dde0074416dbf51650cbfb9cc763027b27Julia Reynolds                    (FingerprintManager) context.getSystemService(Context.FINGERPRINT_SERVICE);
799f516e7dde0074416dbf51650cbfb9cc763027b27Julia Reynolds            if (fpm.isHardwareDetected()) {
800c5fa6e26dffd43d8fab12117d7994d8ab6466048Jim Miller                // This catches the title which can be overloaded in an overlay
801f516e7dde0074416dbf51650cbfb9cc763027b27Julia Reynolds                data = new SearchIndexableRaw(context);
802f516e7dde0074416dbf51650cbfb9cc763027b27Julia Reynolds                data.title = res.getString(R.string.security_settings_fingerprint_preference_title);
803f516e7dde0074416dbf51650cbfb9cc763027b27Julia Reynolds                data.screenTitle = screenTitle;
804f516e7dde0074416dbf51650cbfb9cc763027b27Julia Reynolds                result.add(data);
805c5fa6e26dffd43d8fab12117d7994d8ab6466048Jim Miller                // Fallback for when the above doesn't contain "fingerprint"
806c5fa6e26dffd43d8fab12117d7994d8ab6466048Jim Miller                data = new SearchIndexableRaw(context);
807c5fa6e26dffd43d8fab12117d7994d8ab6466048Jim Miller                data.title = res.getString(R.string.fingerprint_manage_category_title);
808c5fa6e26dffd43d8fab12117d7994d8ab6466048Jim Miller                data.screenTitle = screenTitle;
809c5fa6e26dffd43d8fab12117d7994d8ab6466048Jim Miller                result.add(data);
810f516e7dde0074416dbf51650cbfb9cc763027b27Julia Reynolds            }
811f516e7dde0074416dbf51650cbfb9cc763027b27Julia Reynolds
812d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio            // Credential storage
813d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio            final UserManager um = (UserManager) context.getSystemService(Context.USER_SERVICE);
814d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio
815d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio            if (!um.hasUserRestriction(UserManager.DISALLOW_CONFIG_CREDENTIALS)) {
816d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio                KeyStore keyStore = KeyStore.getInstance();
817d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio
818d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio                final int storageSummaryRes = keyStore.isHardwareBacked() ?
819d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio                        R.string.credential_storage_type_hardware :
820d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio                        R.string.credential_storage_type_software;
821d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio
822d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio                data = new SearchIndexableRaw(context);
823d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio                data.title = res.getString(storageSummaryRes);
824d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio                data.screenTitle = screenTitle;
825d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio                result.add(data);
826d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio            }
827d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio
828fe7655914de3852093c35600df996824d03ddf63Jim Miller            // Advanced
829fe7655914de3852093c35600df996824d03ddf63Jim Miller            final LockPatternUtils lockPatternUtils = new LockPatternUtils(context);
8305437588abd69bf3eff59684528aaf50e09b6cffcAdrian Roos            if (lockPatternUtils.isSecure(MY_USER_ID)) {
831fe7655914de3852093c35600df996824d03ddf63Jim Miller                ArrayList<TrustAgentComponentInfo> agents =
832088de47aa8595f9e4142ce0fe6885840b83edc93Adrian Roos                        getActiveTrustAgents(context.getPackageManager(), lockPatternUtils,
833088de47aa8595f9e4142ce0fe6885840b83edc93Adrian Roos                                context.getSystemService(DevicePolicyManager.class));
834fe7655914de3852093c35600df996824d03ddf63Jim Miller                for (int i = 0; i < agents.size(); i++) {
835fe7655914de3852093c35600df996824d03ddf63Jim Miller                    final TrustAgentComponentInfo agent = agents.get(i);
836fe7655914de3852093c35600df996824d03ddf63Jim Miller                    data = new SearchIndexableRaw(context);
837fe7655914de3852093c35600df996824d03ddf63Jim Miller                    data.title = agent.title;
838fe7655914de3852093c35600df996824d03ddf63Jim Miller                    data.screenTitle = screenTitle;
839fe7655914de3852093c35600df996824d03ddf63Jim Miller                    result.add(data);
840fe7655914de3852093c35600df996824d03ddf63Jim Miller                }
841fe7655914de3852093c35600df996824d03ddf63Jim Miller            }
842d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio            return result;
843d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio        }
844d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio
845d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio        @Override
846d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio        public List<String> getNonIndexableKeys(Context context) {
847d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio            final List<String> keys = new ArrayList<String>();
848d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio
849d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio            LockPatternUtils lockPatternUtils = new LockPatternUtils(context);
850d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio            // Add options for lock/unlock screen
851d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio            int resId = getResIdForLockUnlockScreen(context, lockPatternUtils);
852d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio
853d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio            // Do not display SIM lock for devices without an Icc card
854d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio            TelephonyManager tm = TelephonyManager.getDefault();
855d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio            if (!mIsPrimary || !tm.hasIccCard()) {
856d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio                keys.add(KEY_SIM_LOCK);
857d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio            }
858d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio
859d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio            final UserManager um = (UserManager) context.getSystemService(Context.USER_SERVICE);
860d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio            if (um.hasUserRestriction(UserManager.DISALLOW_CONFIG_CREDENTIALS)) {
861d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio                keys.add(KEY_CREDENTIALS_MANAGER);
862d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio            }
863d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio
864fe7655914de3852093c35600df996824d03ddf63Jim Miller            // TrustAgent settings disappear when the user has no primary security.
8655437588abd69bf3eff59684528aaf50e09b6cffcAdrian Roos            if (!lockPatternUtils.isSecure(MY_USER_ID)) {
866fe7655914de3852093c35600df996824d03ddf63Jim Miller                keys.add(KEY_TRUST_AGENT);
867fe7655914de3852093c35600df996824d03ddf63Jim Miller                keys.add(KEY_MANAGE_TRUST_AGENTS);
868fe7655914de3852093c35600df996824d03ddf63Jim Miller            }
869fe7655914de3852093c35600df996824d03ddf63Jim Miller
870d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio            return keys;
871d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio        }
872d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio    }
873d1d2f0222443ace71e0cd48c7ad1538627f7a286Fabrice Di Meglio
874afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project}
875