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