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.text.TextUtils;
23 import android.util.Log;
24 
25 import androidx.annotation.VisibleForTesting;
26 
27 import com.android.settings.R;
28 import com.android.settings.SettingsActivity;
29 import com.android.settings.Utils;
30 import com.android.settings.connecteddevice.audiosharing.AudioSharingDevicePreferenceController;
31 import com.android.settings.connecteddevice.audiosharing.AudioSharingUtils;
32 import com.android.settings.core.SettingsUIDeviceConfig;
33 import com.android.settings.dashboard.DashboardFragment;
34 import com.android.settings.overlay.FeatureFactory;
35 import com.android.settings.overlay.SurveyFeatureProvider;
36 import com.android.settings.search.BaseSearchIndexProvider;
37 import com.android.settings.slices.SlicePreferenceController;
38 import com.android.settingslib.bluetooth.HearingAidStatsLogUtils;
39 import com.android.settingslib.search.SearchIndexable;
40 
41 @SearchIndexable(forTarget = SearchIndexable.ALL & ~SearchIndexable.ARC)
42 public class ConnectedDeviceDashboardFragment extends DashboardFragment {
43 
44     private static final String TAG = "ConnectedDeviceFrag";
45     private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
46     private static final String SLICE_ACTION = "com.android.settings.SEARCH_RESULT_TRAMPOLINE";
47 
48     @VisibleForTesting static final String KEY_CONNECTED_DEVICES = "connected_device_list";
49     @VisibleForTesting static final String KEY_AVAILABLE_DEVICES = "available_device_list";
50 
51     @Override
getMetricsCategory()52     public int getMetricsCategory() {
53         return SettingsEnums.SETTINGS_CONNECTED_DEVICE_CATEGORY;
54     }
55 
56     @Override
getLogTag()57     protected String getLogTag() {
58         return TAG;
59     }
60 
61     @Override
getHelpResource()62     public int getHelpResource() {
63         return R.string.help_url_connected_devices;
64     }
65 
66     @Override
getPreferenceScreenResId()67     protected int getPreferenceScreenResId() {
68         return R.xml.connected_devices;
69     }
70 
71     @Override
onAttach(Context context)72     public void onAttach(Context context) {
73         super.onAttach(context);
74         final boolean nearbyEnabled =
75                 DeviceConfig.getBoolean(
76                         DeviceConfig.NAMESPACE_SETTINGS_UI,
77                         SettingsUIDeviceConfig.BT_NEAR_BY_SUGGESTION_ENABLED,
78                         true);
79         String callingAppPackageName =
80                 ((SettingsActivity) getActivity()).getInitialCallingPackage();
81         String action = getIntent() != null ? getIntent().getAction() : "";
82         if (DEBUG) {
83             Log.d(
84                     TAG,
85                     "onAttach() calling package name is : "
86                             + callingAppPackageName
87                             + ", action : "
88                             + action);
89         }
90         if (AudioSharingUtils.isFeatureEnabled()) {
91             use(AudioSharingDevicePreferenceController.class).init(this);
92         }
93         use(AvailableMediaDeviceGroupController.class).init(this);
94         use(ConnectedDeviceGroupController.class).init(this);
95         use(PreviouslyConnectedDevicePreferenceController.class).init(this);
96         use(SlicePreferenceController.class)
97                 .setSliceUri(
98                         nearbyEnabled
99                                 ? Uri.parse(getString(R.string.config_nearby_devices_slice_uri))
100                                 : null);
101         use(DiscoverableFooterPreferenceController.class)
102                 .setAlwaysDiscoverable(isAlwaysDiscoverable(callingAppPackageName, action));
103 
104         // Show hearing devices survey if user is categorized as one of interested category
105         final String category = HearingAidStatsLogUtils.getUserCategory(context);
106         if (category != null && !category.isEmpty()) {
107             SurveyFeatureProvider provider =
108                     FeatureFactory.getFeatureFactory().getSurveyFeatureProvider(context);
109             if (provider != null) {
110                 provider.sendActivityIfAvailable(category);
111             }
112         }
113     }
114 
115     @VisibleForTesting
isAlwaysDiscoverable(String callingAppPackageName, String action)116     boolean isAlwaysDiscoverable(String callingAppPackageName, String action) {
117         return TextUtils.equals(SLICE_ACTION, action)
118                 ? false
119                 : TextUtils.equals(Utils.SETTINGS_PACKAGE_NAME, callingAppPackageName)
120                         || TextUtils.equals(Utils.SYSTEMUI_PACKAGE_NAME, callingAppPackageName);
121     }
122 
123     /** For Search. */
124     public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
125             new BaseSearchIndexProvider(R.xml.connected_devices);
126 }
127