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.connecteddevice;
17 
18 import android.app.settings.SettingsEnums;
19 import android.content.Context;
20 import android.net.Uri;
21 import android.provider.DeviceConfig;
22 import android.provider.SearchIndexableResource;
23 
24 import androidx.annotation.VisibleForTesting;
25 
26 import com.android.settings.R;
27 import com.android.settings.core.SettingsUIDeviceConfig;
28 import com.android.settings.dashboard.DashboardFragment;
29 import com.android.settings.search.BaseSearchIndexProvider;
30 import com.android.settings.slices.SlicePreferenceController;
31 import com.android.settingslib.core.AbstractPreferenceController;
32 import com.android.settingslib.core.lifecycle.Lifecycle;
33 import com.android.settingslib.search.SearchIndexable;
34 
35 import java.util.ArrayList;
36 import java.util.Arrays;
37 import java.util.List;
38 
39 @SearchIndexable(forTarget = SearchIndexable.ALL & ~SearchIndexable.ARC)
40 public class ConnectedDeviceDashboardFragment extends DashboardFragment {
41 
42     private static final String TAG = "ConnectedDeviceFrag";
43 
44     @VisibleForTesting
45     static final String KEY_CONNECTED_DEVICES = "connected_device_list";
46     @VisibleForTesting
47     static final String KEY_AVAILABLE_DEVICES = "available_device_list";
48 
49     @Override
getMetricsCategory()50     public int getMetricsCategory() {
51         return SettingsEnums.SETTINGS_CONNECTED_DEVICE_CATEGORY;
52     }
53 
54     @Override
getLogTag()55     protected String getLogTag() {
56         return TAG;
57     }
58 
59     @Override
getHelpResource()60     public int getHelpResource() {
61         return R.string.help_url_connected_devices;
62     }
63 
64     @Override
getPreferenceScreenResId()65     protected int getPreferenceScreenResId() {
66         return R.xml.connected_devices;
67     }
68 
69     @Override
createPreferenceControllers(Context context)70     protected List<AbstractPreferenceController> createPreferenceControllers(Context context) {
71         return buildPreferenceControllers(context, getSettingsLifecycle());
72     }
73 
buildPreferenceControllers(Context context, Lifecycle lifecycle)74     private static List<AbstractPreferenceController> buildPreferenceControllers(Context context,
75             Lifecycle lifecycle) {
76         final List<AbstractPreferenceController> controllers = new ArrayList<>();
77         final DiscoverableFooterPreferenceController discoverableFooterPreferenceController =
78                 new DiscoverableFooterPreferenceController(context);
79         controllers.add(discoverableFooterPreferenceController);
80 
81         if (lifecycle != null) {
82             lifecycle.addObserver(discoverableFooterPreferenceController);
83         }
84 
85         return controllers;
86     }
87 
88     @Override
onAttach(Context context)89     public void onAttach(Context context) {
90         super.onAttach(context);
91         final boolean nearbyEnabled = DeviceConfig.getBoolean(DeviceConfig.NAMESPACE_SETTINGS_UI,
92                 SettingsUIDeviceConfig.BT_NEAR_BY_SUGGESTION_ENABLED, true);
93         use(AvailableMediaDeviceGroupController.class).init(this);
94         use(ConnectedDeviceGroupController.class).init(this);
95         use(PreviouslyConnectedDevicePreferenceController.class).init(this);
96         use(DiscoverableFooterPreferenceController.class).init(this);
97         use(SlicePreferenceController.class).setSliceUri(nearbyEnabled
98                 ? Uri.parse(getString(R.string.config_nearby_devices_slice_uri))
99                 : null);
100     }
101 
102     /**
103      * For Search.
104      */
105     public static final 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.connected_devices;
112                     return Arrays.asList(sir);
113                 }
114 
115                 @Override
116                 public List<AbstractPreferenceController> createPreferenceControllers(Context
117                         context) {
118                     return buildPreferenceControllers(context, null /* lifecycle */);
119                 }
120             };
121 }
122