1 /*
2  * Copyright (C) 2023 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.safetycenter;
18 
19 import android.app.settings.SettingsEnums;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.os.Bundle;
23 import android.os.UserHandle;
24 import android.os.UserManager;
25 import android.provider.SearchIndexableResource;
26 
27 import com.android.settings.R;
28 import com.android.settings.Utils;
29 import com.android.settings.dashboard.DashboardFragment;
30 import com.android.settings.search.BaseSearchIndexProvider;
31 import com.android.settings.security.LockUnificationPreferenceController;
32 import com.android.settings.security.trustagent.TrustAgentListPreferenceController;
33 import com.android.settingslib.core.AbstractPreferenceController;
34 import com.android.settingslib.core.lifecycle.Lifecycle;
35 import com.android.settingslib.drawer.CategoryKey;
36 import com.android.settingslib.search.SearchIndexable;
37 
38 import java.util.ArrayList;
39 import java.util.List;
40 
41 /**
42  * An overflow menu for {@code SecuritySettings} containing advanced security and privacy settings.
43  *
44  * <p>This also includes all work-profile related settings.
45  */
46 @SearchIndexable
47 public class MoreSecurityPrivacyFragment extends DashboardFragment {
48     private static final String TAG = "MoreSecurityPrivacyFragment";
49     private static final String KEY_NOTIFICATION_WORK_PROFILE_NOTIFICATIONS =
50             "privacy_lock_screen_work_profile_notifications";
51 
52     @Override
getMetricsCategory()53     public int getMetricsCategory() {
54         return SettingsEnums.MORE_SECURITY_PRIVACY_SETTINGS;
55     }
56 
57     @Override
getPreferenceScreenResId()58     protected int getPreferenceScreenResId() {
59         return R.xml.more_security_privacy_settings;
60     }
61 
62     @Override
getCategoryKey()63     public String getCategoryKey() {
64         return CategoryKey.CATEGORY_MORE_SECURITY_PRIVACY_SETTINGS;
65     }
66 
67     @Override
getLogTag()68     protected String getLogTag() {
69         return TAG;
70     }
71 
72     @Override
onCreate(Bundle icicle)73     public void onCreate(Bundle icicle) {
74         super.onCreate(icicle);
75         SafetyCenterUtils.replaceEnterpriseStringsForPrivacyEntries(this);
76         SafetyCenterUtils.replaceEnterpriseStringsForSecurityEntries(this);
77     }
78 
79     /** see confirmPatternThenDisableAndClear */
80     @Override
onActivityResult(int requestCode, int resultCode, Intent data)81     public void onActivityResult(int requestCode, int resultCode, Intent data) {
82         if (use(TrustAgentListPreferenceController.class)
83                 .handleActivityResult(requestCode, resultCode)) {
84             return;
85         }
86         if (use(LockUnificationPreferenceController.class)
87                 .handleActivityResult(requestCode, resultCode, data)) {
88             return;
89         }
90         super.onActivityResult(requestCode, resultCode, data);
91     }
92 
93     @Override
createPreferenceControllers(Context context)94     protected List<AbstractPreferenceController> createPreferenceControllers(Context context) {
95         return buildPreferenceControllers(context, getSettingsLifecycle(), this /* host*/);
96     }
97 
buildPreferenceControllers( Context context, Lifecycle lifecycle, DashboardFragment host)98     private static List<AbstractPreferenceController> buildPreferenceControllers(
99             Context context, Lifecycle lifecycle, DashboardFragment host) {
100         final List<AbstractPreferenceController> controllers = new ArrayList<>();
101         controllers.addAll(SafetyCenterUtils.getControllersForAdvancedPrivacy(context, lifecycle));
102         controllers.addAll(
103                 SafetyCenterUtils.getControllersForAdvancedSecurity(context, lifecycle, host));
104         return controllers;
105     }
106 
107     public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
108             new BaseSearchIndexProvider(R.xml.more_security_privacy_settings) {
109                 /**
110                  * If SafetyCenter is disabled, all of these entries will be in the More Security
111                  * Settings and the Privacy page, and we don't want to index these entries.
112                  */
113                 @Override
114                 public List<SearchIndexableResource> getXmlResourcesToIndex(
115                         Context context, boolean enabled) {
116                     // NOTE: This check likely should be moved to the super method. This is done
117                     // here to avoid potentially undesired side effects for existing implementors.
118                     if (!isPageSearchEnabled(context)) {
119                         return null;
120                     }
121                     return super.getXmlResourcesToIndex(context, enabled);
122                 }
123 
124                 @Override
125                 public List<AbstractPreferenceController> createPreferenceControllers(
126                         Context context) {
127                     return buildPreferenceControllers(context, null, null);
128                 }
129 
130                 @Override
131                 public List<String> getNonIndexableKeys(Context context) {
132                     final List<String> keys = super.getNonIndexableKeys(context);
133                     final int profileUserId =
134                             Utils.getManagedProfileId(
135                                     UserManager.get(context), UserHandle.myUserId());
136                     // If work profile is supported, we should keep the search result.
137                     if (profileUserId != UserHandle.USER_NULL) {
138                         return keys;
139                     }
140 
141                     // Otherwise, we should hide the search result.
142                     keys.add(KEY_NOTIFICATION_WORK_PROFILE_NOTIFICATIONS);
143                     return keys;
144                 }
145 
146                 @Override
147                 protected boolean isPageSearchEnabled(Context context) {
148                     return SafetyCenterManagerWrapper.get().isEnabled(context);
149                 }
150             };
151 }
152