1 /*
2  * Copyright (C) 2019 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.car.developeroptions.connecteddevice;
17 
18 import android.app.settings.SettingsEnums;
19 import android.bluetooth.BluetoothAdapter;
20 import android.bluetooth.BluetoothManager;
21 import android.content.Context;
22 import android.os.Bundle;
23 
24 import com.android.car.developeroptions.R;
25 import com.android.car.developeroptions.SettingsActivity;
26 import com.android.car.developeroptions.bluetooth.BluetoothDeviceRenamePreferenceController;
27 import com.android.car.developeroptions.bluetooth.BluetoothSwitchPreferenceController;
28 import com.android.car.developeroptions.core.TogglePreferenceController;
29 import com.android.car.developeroptions.dashboard.DashboardFragment;
30 import com.android.car.developeroptions.search.BaseSearchIndexProvider;
31 import com.android.car.developeroptions.widget.SwitchBar;
32 import com.android.car.developeroptions.widget.SwitchBarController;
33 import com.android.settingslib.core.lifecycle.Lifecycle;
34 import com.android.settingslib.search.SearchIndexable;
35 import com.android.settingslib.search.SearchIndexableRaw;
36 import com.android.settingslib.widget.FooterPreference;
37 
38 import java.util.ArrayList;
39 import java.util.List;
40 
41 /**
42  * Dedicated screen for allowing the user to toggle bluetooth which displays relevant information to
43  * the user based on related settings such as bluetooth scanning.
44  */
45 @SearchIndexable(forTarget = SearchIndexable.ALL)
46 public class BluetoothDashboardFragment extends DashboardFragment {
47 
48     private static final String TAG = "BluetoothDashboardFrag";
49     private static final String KEY_BLUETOOTH_SCREEN_FOOTER = "bluetooth_screen_footer";
50     public static final String KEY_BLUETOOTH_SCREEN = "bluetooth_switchbar_screen";
51 
52     private FooterPreference mFooterPreference;
53     private SwitchBar mSwitchBar;
54     private BluetoothSwitchPreferenceController mController;
55 
56     @Override
getMetricsCategory()57     public int getMetricsCategory() {
58         return SettingsEnums.BLUETOOTH_FRAGMENT;
59     }
60 
61     @Override
getLogTag()62     protected String getLogTag() {
63         return TAG;
64     }
65 
66     @Override
getHelpResource()67     public int getHelpResource() {
68         return R.string.help_uri_bluetooth_screen;
69     }
70 
71     @Override
getPreferenceScreenResId()72     protected int getPreferenceScreenResId() {
73         return R.xml.bluetooth_screen;
74     }
75 
76     @Override
onCreate(Bundle icicle)77     public void onCreate(Bundle icicle) {
78         super.onCreate(icicle);
79         mFooterPreference = findPreference(KEY_BLUETOOTH_SCREEN_FOOTER);
80     }
81 
82     @Override
onAttach(Context context)83     public void onAttach(Context context) {
84         super.onAttach(context);
85         use(BluetoothDeviceRenamePreferenceController.class).setFragment(this);
86     }
87 
88     @Override
onActivityCreated(Bundle savedInstanceState)89     public void onActivityCreated(Bundle savedInstanceState) {
90         super.onActivityCreated(savedInstanceState);
91 
92         SettingsActivity activity = (SettingsActivity) getActivity();
93         mSwitchBar = activity.getSwitchBar();
94         mController = new BluetoothSwitchPreferenceController(activity,
95                 new SwitchBarController(mSwitchBar), mFooterPreference);
96         Lifecycle lifecycle = getSettingsLifecycle();
97         if (lifecycle != null) {
98             lifecycle.addObserver(mController);
99         }
100     }
101     /**
102      * For Search.
103      */
104     public static final SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
105             new BaseSearchIndexProvider() {
106                 @Override
107                 public List<SearchIndexableRaw> getRawDataToIndex(Context context,
108                         boolean enabled) {
109                     final List<SearchIndexableRaw> result = new ArrayList<>();
110 
111                     // Add the activity title
112                     final SearchIndexableRaw data = new SearchIndexableRaw(context);
113                     data.title = context.getString(R.string.bluetooth_settings_title);
114                     data.screenTitle = context.getString(R.string.bluetooth_settings_title);
115                     data.keywords = context.getString(R.string.keywords_bluetooth_settings);
116                     data.key = KEY_BLUETOOTH_SCREEN;
117                     result.add(data);
118 
119                     return result;
120                 }
121 
122                 @Override
123                 public List<String> getNonIndexableKeys(Context context) {
124                     final List<String> keys = super.getNonIndexableKeys(context);
125                     BluetoothManager manager =
126                             (BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE);
127                     if (manager != null) {
128                         BluetoothAdapter adapter = manager.getAdapter();
129                         final int status = adapter != null
130                                 ? TogglePreferenceController.AVAILABLE
131                                 : TogglePreferenceController.UNSUPPORTED_ON_DEVICE;
132                         if (status != TogglePreferenceController.AVAILABLE) {
133                             keys.add(KEY_BLUETOOTH_SCREEN);
134                         }
135                     }
136 
137                     return keys;
138                 }
139             };
140 }
141