1 /*
2  * Copyright (C) 2016 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 package com.android.settings.system;
17 
18 import android.app.settings.SettingsEnums;
19 import android.content.Context;
20 import android.os.Bundle;
21 import android.provider.SearchIndexableResource;
22 
23 import androidx.annotation.VisibleForTesting;
24 import androidx.preference.Preference;
25 import androidx.preference.PreferenceGroup;
26 import androidx.preference.PreferenceScreen;
27 
28 import com.android.settings.R;
29 import com.android.settings.dashboard.DashboardFragment;
30 import com.android.settings.overlay.FeatureFactory;
31 import com.android.settings.search.BaseSearchIndexProvider;
32 import com.android.settings.search.Indexable;
33 import com.android.settingslib.search.SearchIndexable;
34 
35 import java.util.Arrays;
36 import java.util.List;
37 
38 @SearchIndexable
39 public class SystemDashboardFragment extends DashboardFragment {
40 
41     private static final String TAG = "SystemDashboardFrag";
42 
43     private static final String KEY_RESET = "reset_dashboard";
44 
45     public static final String EXTRA_SHOW_AWARE_DISABLED = "show_aware_dialog_disabled";
46 
47     @Override
onCreate(Bundle icicle)48     public void onCreate(Bundle icicle) {
49         super.onCreate(icicle);
50 
51         final PreferenceScreen screen = getPreferenceScreen();
52         // We do not want to display an advanced button if only one setting is hidden
53         if (getVisiblePreferenceCount(screen) == screen.getInitialExpandedChildrenCount() + 1) {
54             screen.setInitialExpandedChildrenCount(Integer.MAX_VALUE);
55         }
56 
57         showRestrictionDialog();
58     }
59 
60     @VisibleForTesting
showRestrictionDialog()61     public void showRestrictionDialog() {
62         final Bundle args = getArguments();
63         if (args != null && args.getBoolean(EXTRA_SHOW_AWARE_DISABLED, false)) {
64             FeatureFactory.getFactory(getContext()).getAwareFeatureProvider()
65                     .showRestrictionDialog(this);
66         }
67     }
68 
69     @Override
getMetricsCategory()70     public int getMetricsCategory() {
71         return SettingsEnums.SETTINGS_SYSTEM_CATEGORY;
72     }
73 
74     @Override
getLogTag()75     protected String getLogTag() {
76         return TAG;
77     }
78 
79     @Override
getPreferenceScreenResId()80     protected int getPreferenceScreenResId() {
81         return R.xml.system_dashboard_fragment;
82     }
83 
84     @Override
getHelpResource()85     public int getHelpResource() {
86         return R.string.help_url_system_dashboard;
87     }
88 
getVisiblePreferenceCount(PreferenceGroup group)89     private int getVisiblePreferenceCount(PreferenceGroup group) {
90         int visibleCount = 0;
91         for (int i = 0; i < group.getPreferenceCount(); i++) {
92             final Preference preference = group.getPreference(i);
93             if (preference instanceof PreferenceGroup) {
94                 visibleCount += getVisiblePreferenceCount((PreferenceGroup) preference);
95             } else if (preference.isVisible()) {
96                 visibleCount++;
97             }
98         }
99         return visibleCount;
100     }
101 
102     /**
103      * For Search.
104      */
105     public static final Indexable.SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
106             new BaseSearchIndexProvider() {
107                 @Override
108                 public List<SearchIndexableResource> getXmlResourcesToIndex(
109                         Context context, boolean enabled) {
110                     final SearchIndexableResource sir = new SearchIndexableResource(context);
111                     sir.xmlResId = R.xml.system_dashboard_fragment;
112                     return Arrays.asList(sir);
113                 }
114             };
115 }