1/*
2 * Copyright (C) 2015 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
17package com.android.settings.location;
18
19import android.provider.Settings.Global;
20import android.support.v14.preference.SwitchPreference;
21import android.support.v7.preference.Preference;
22import android.support.v7.preference.PreferenceScreen;
23
24import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
25import com.android.settings.R;
26import com.android.settings.SettingsPreferenceFragment;
27
28/**
29 * A page that configures the background scanning settings for Wi-Fi and Bluetooth.
30 */
31public class ScanningSettings extends SettingsPreferenceFragment {
32    private static final String KEY_WIFI_SCAN_ALWAYS_AVAILABLE = "wifi_always_scanning";
33    private static final String KEY_BLUETOOTH_SCAN_ALWAYS_AVAILABLE = "bluetooth_always_scanning";
34
35    @Override
36    public int getMetricsCategory() {
37        return MetricsEvent.LOCATION_SCANNING;
38    }
39
40    @Override
41    public void onResume() {
42        super.onResume();
43        createPreferenceHierarchy();
44    }
45
46    private PreferenceScreen createPreferenceHierarchy() {
47        PreferenceScreen root = getPreferenceScreen();
48        if (root != null) {
49            root.removeAll();
50        }
51        addPreferencesFromResource(R.xml.location_scanning);
52        root = getPreferenceScreen();
53        initPreferences();
54        return root;
55    }
56
57    private void initPreferences() {
58        final SwitchPreference wifiScanAlwaysAvailable =
59            (SwitchPreference) findPreference(KEY_WIFI_SCAN_ALWAYS_AVAILABLE);
60        wifiScanAlwaysAvailable.setChecked(Global.getInt(getContentResolver(),
61                    Global.WIFI_SCAN_ALWAYS_AVAILABLE, 0) == 1);
62        final SwitchPreference bleScanAlwaysAvailable =
63            (SwitchPreference) findPreference(KEY_BLUETOOTH_SCAN_ALWAYS_AVAILABLE);
64        bleScanAlwaysAvailable.setChecked(Global.getInt(getContentResolver(),
65                    Global.BLE_SCAN_ALWAYS_AVAILABLE, 0) == 1);
66    }
67
68    @Override
69    public boolean onPreferenceTreeClick(Preference preference) {
70        String key = preference.getKey();
71        if (KEY_WIFI_SCAN_ALWAYS_AVAILABLE.equals(key)) {
72            Global.putInt(getContentResolver(),
73                    Global.WIFI_SCAN_ALWAYS_AVAILABLE,
74                    ((SwitchPreference) preference).isChecked() ? 1 : 0);
75        } else if (KEY_BLUETOOTH_SCAN_ALWAYS_AVAILABLE.equals(key)) {
76            Global.putInt(getContentResolver(),
77                    Global.BLE_SCAN_ALWAYS_AVAILABLE,
78                    ((SwitchPreference) preference).isChecked() ? 1 : 0);
79        } else {
80            return super.onPreferenceTreeClick(preference);
81        }
82        return true;
83    }
84}
85