1 /*
2  * Copyright (C) 2021 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.applications;
18 
19 import android.app.settings.SettingsEnums;
20 import android.content.Context;
21 import android.provider.SearchIndexableResource;
22 
23 import com.android.internal.annotations.VisibleForTesting;
24 import com.android.settings.R;
25 import com.android.settings.applications.appcompat.UserAspectRatioAppsPreferenceController;
26 import com.android.settings.dashboard.DashboardFragment;
27 import com.android.settings.search.BaseSearchIndexProvider;
28 import com.android.settings.widget.PreferenceCategoryController;
29 import com.android.settingslib.core.AbstractPreferenceController;
30 import com.android.settingslib.search.SearchIndexable;
31 
32 import java.util.ArrayList;
33 import java.util.Arrays;
34 import java.util.List;
35 
36 /** Settings page for apps. */
37 @SearchIndexable
38 public class AppDashboardFragment extends DashboardFragment {
39 
40     private static final String TAG = "AppDashboardFragment";
41     private static final String ADVANCED_CATEGORY_KEY = "advanced_category";
42     private static final String ASPECT_RATIO_PREF_KEY = "aspect_ratio_apps";
43     private AppsPreferenceController mAppsPreferenceController;
44 
buildPreferenceControllers(Context context)45     private static List<AbstractPreferenceController> buildPreferenceControllers(Context context) {
46         final List<AbstractPreferenceController> controllers = new ArrayList<>();
47         controllers.add(new AppsPreferenceController(context));
48 
49         final UserAspectRatioAppsPreferenceController aspectRatioAppsPreferenceController =
50                 new UserAspectRatioAppsPreferenceController(context, ASPECT_RATIO_PREF_KEY);
51         final AdvancedAppsPreferenceCategoryController advancedCategoryController =
52                 new AdvancedAppsPreferenceCategoryController(context, ADVANCED_CATEGORY_KEY);
53         advancedCategoryController.setChildren(List.of(aspectRatioAppsPreferenceController));
54         controllers.add(advancedCategoryController);
55 
56         return controllers;
57     }
58 
59     @Override
getMetricsCategory()60     public int getMetricsCategory() {
61         return SettingsEnums.MANAGE_APPLICATIONS;
62     }
63 
64     @Override
getLogTag()65     protected String getLogTag() {
66         return TAG;
67     }
68 
69     @Override
getHelpResource()70     public int getHelpResource() {
71         return R.string.help_url_apps_and_notifications;
72     }
73 
74     @Override
getPreferenceScreenResId()75     protected int getPreferenceScreenResId() {
76         return R.xml.apps;
77     }
78 
79     @Override
onAttach(Context context)80     public void onAttach(Context context) {
81         super.onAttach(context);
82         mAppsPreferenceController = use(AppsPreferenceController.class);
83         mAppsPreferenceController.setFragment(this /* fragment */);
84         getSettingsLifecycle().addObserver(mAppsPreferenceController);
85 
86         final HibernatedAppsPreferenceController hibernatedAppsPreferenceController =
87                 use(HibernatedAppsPreferenceController.class);
88         getSettingsLifecycle().addObserver(hibernatedAppsPreferenceController);
89     }
90 
91     @VisibleForTesting
getAdvancedAppsPreferenceCategoryController()92     PreferenceCategoryController getAdvancedAppsPreferenceCategoryController() {
93         return use(AdvancedAppsPreferenceCategoryController.class);
94     }
95 
96     @Override
createPreferenceControllers(Context context)97     protected List<AbstractPreferenceController> createPreferenceControllers(Context context) {
98         return buildPreferenceControllers(context);
99     }
100 
101     public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
102             new BaseSearchIndexProvider() {
103                 @Override
104                 public List<SearchIndexableResource> getXmlResourcesToIndex(
105                         Context context, boolean enabled) {
106                     final SearchIndexableResource sir = new SearchIndexableResource(context);
107                     sir.xmlResId = R.xml.apps;
108                     return Arrays.asList(sir);
109                 }
110 
111                 @Override
112                 public List<AbstractPreferenceController> createPreferenceControllers(
113                         Context context) {
114                     return buildPreferenceControllers(context);
115                 }
116             };
117 }
118