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 */
16package com.android.settings.connecteddevice;
17
18import android.bluetooth.BluetoothAdapter;
19import android.bluetooth.BluetoothManager;
20import android.content.Context;
21import android.os.Bundle;
22import android.support.annotation.VisibleForTesting;
23
24import com.android.internal.logging.nano.MetricsProto;
25import com.android.settings.R;
26import com.android.settings.SettingsActivity;
27import com.android.settings.bluetooth.BluetoothDeviceRenamePreferenceController;
28import com.android.settings.bluetooth.BluetoothSwitchPreferenceController;
29import com.android.settings.core.SubSettingLauncher;
30import com.android.settings.core.TogglePreferenceController;
31import com.android.settings.dashboard.DashboardFragment;
32import com.android.settings.location.ScanningSettings;
33import com.android.settings.search.BaseSearchIndexProvider;
34import com.android.settings.search.SearchIndexableRaw;
35import com.android.settings.widget.SwitchBar;
36import com.android.settings.widget.SwitchBarController;
37import com.android.settingslib.core.lifecycle.Lifecycle;
38import com.android.settingslib.widget.FooterPreference;
39
40import java.util.ArrayList;
41import java.util.List;
42
43/**
44 * Dedicated screen for allowing the user to toggle bluetooth which displays relevant information to
45 * the user based on related settings such as bluetooth scanning.
46 */
47public class BluetoothDashboardFragment extends DashboardFragment {
48
49    private static final String TAG = "BluetoothDashboardFrag";
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
57    public int getMetricsCategory() {
58        return MetricsProto.MetricsEvent.BLUETOOTH_FRAGMENT;
59    }
60
61    @Override
62    protected String getLogTag() {
63        return TAG;
64    }
65
66    @Override
67    public int getHelpResource() {
68        return R.string.help_uri_bluetooth_screen;
69    }
70
71    @Override
72    protected int getPreferenceScreenResId() {
73        return R.xml.bluetooth_screen;
74    }
75
76    @Override
77    public void onCreate(Bundle icicle) {
78        super.onCreate(icicle);
79        mFooterPreference = mFooterPreferenceMixin.createFooterPreference();
80    }
81
82    @Override
83    public void onAttach(Context context) {
84        super.onAttach(context);
85        use(BluetoothDeviceRenamePreferenceController.class).setFragment(this);
86    }
87
88    @Override
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 = getLifecycle();
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