1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.settings.security.screenlock;
18 
19 import android.app.Activity;
20 import android.app.settings.SettingsEnums;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.os.UserHandle;
24 
25 import androidx.annotation.Nullable;
26 
27 import com.android.internal.widget.LockPatternUtils;
28 import com.android.settings.R;
29 import com.android.settings.dashboard.DashboardFragment;
30 import com.android.settings.search.BaseSearchIndexProvider;
31 import com.android.settings.security.OwnerInfoPreferenceController;
32 import com.android.settingslib.core.AbstractPreferenceController;
33 import com.android.settingslib.search.SearchIndexable;
34 
35 import java.util.ArrayList;
36 import java.util.List;
37 
38 @SearchIndexable
39 public class ScreenLockSettings extends DashboardFragment
40         implements OwnerInfoPreferenceController.OwnerInfoCallback {
41 
42     private static final String TAG = "ScreenLockSettings";
43 
44     private static final int MY_USER_ID = UserHandle.myUserId();
45 
46     static final int AUTO_PIN_SETTING_ENABLING_REQUEST_CODE = 111;
47     static final int AUTO_PIN_SETTING_DISABLING_REQUEST_CODE = 112;
48 
49     private LockPatternUtils mLockPatternUtils;
50 
51     @Override
getMetricsCategory()52     public int getMetricsCategory() {
53         return SettingsEnums.SCREEN_LOCK_SETTINGS;
54     }
55 
56     @Override
getPreferenceScreenResId()57     protected int getPreferenceScreenResId() {
58         return R.xml.screen_lock_settings;
59     }
60 
61     @Override
getLogTag()62     protected String getLogTag() {
63         return TAG;
64     }
65 
66     @Override
createPreferenceControllers(Context context)67     protected List<AbstractPreferenceController> createPreferenceControllers(Context context) {
68         mLockPatternUtils = new LockPatternUtils(context);
69         return buildPreferenceControllers(context, this /* parent */, mLockPatternUtils);
70     }
71 
72     @Override
onOwnerInfoUpdated()73     public void onOwnerInfoUpdated() {
74         use(OwnerInfoPreferenceController.class).updateSummary();
75     }
76 
buildPreferenceControllers(Context context, DashboardFragment parent, LockPatternUtils lockPatternUtils)77     private static List<AbstractPreferenceController> buildPreferenceControllers(Context context,
78             DashboardFragment parent, LockPatternUtils lockPatternUtils) {
79 
80         final List<AbstractPreferenceController> controllers = new ArrayList<>();
81         controllers.add(new PatternVisiblePreferenceController(
82                 context, MY_USER_ID, lockPatternUtils));
83         controllers.add(new PinPrivacyPreferenceController(
84                 context, MY_USER_ID, lockPatternUtils));
85         controllers.add(new PowerButtonInstantLockPreferenceController(
86                 context, MY_USER_ID, lockPatternUtils));
87         controllers.add(new LockAfterTimeoutPreferenceController(
88                 context, MY_USER_ID, lockPatternUtils));
89         controllers.add(new AutoPinConfirmPreferenceController(
90                 context, MY_USER_ID, lockPatternUtils, parent));
91         controllers.add(new OwnerInfoPreferenceController(context, parent));
92         return controllers;
93     }
94 
95     public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
96             new BaseSearchIndexProvider(R.xml.screen_lock_settings) {
97 
98                 @Override
99                 public List<AbstractPreferenceController> createPreferenceControllers(
100                         Context context) {
101                     return buildPreferenceControllers(context, null /* parent */,
102                             new LockPatternUtils(context));
103                 }
104             };
105 
106     @Override
onActivityResult(int requestCode, int resultCode, @Nullable Intent data)107     public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
108         if (requestCode == AUTO_PIN_SETTING_ENABLING_REQUEST_CODE) {
109             if (resultCode == Activity.RESULT_OK) {
110                 onAutoPinConfirmSettingChange(/* newState= */ true);
111             }
112         } else if (requestCode == AUTO_PIN_SETTING_DISABLING_REQUEST_CODE) {
113             if (resultCode == Activity.RESULT_OK) {
114                 onAutoPinConfirmSettingChange(/* newState= */ false);
115             }
116         }
117     }
118 
onAutoPinConfirmSettingChange(boolean newState)119     private void onAutoPinConfirmSettingChange(boolean newState) {
120         // update the auto pin confirm setting.
121         mLockPatternUtils.setAutoPinConfirm(newState, MY_USER_ID);
122         // store the pin length info to disk; If it fails, reset the setting to prev state.
123         if (!mLockPatternUtils.refreshStoredPinLength(MY_USER_ID)) {
124             mLockPatternUtils.setAutoPinConfirm(!newState, MY_USER_ID);
125         }
126     }
127 }
128