1/*
2 * Copyright (C) 2017 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.wifi;
18
19import android.content.Context;
20import android.provider.Settings;
21import android.support.v14.preference.SwitchPreference;
22import android.support.v7.preference.Preference;
23import android.text.TextUtils;
24
25import com.android.settings.core.PreferenceController;
26
27/**
28 * {@link PreferenceController} that controls whether we should fall back to celluar when wifi is
29 * bad.
30 */
31public class CellularFallbackPreferenceController extends PreferenceController {
32
33    private static final String KEY_CELLULAR_FALLBACK = "wifi_cellular_data_fallback";
34
35
36    public CellularFallbackPreferenceController(Context context) {
37        super(context);
38    }
39
40    @Override
41    public boolean isAvailable() {
42        return !avoidBadWifiConfig();
43    }
44
45    @Override
46    public String getPreferenceKey() {
47        return KEY_CELLULAR_FALLBACK;
48    }
49
50    @Override
51    public boolean handlePreferenceTreeClick(Preference preference) {
52        if (!TextUtils.equals(preference.getKey(), KEY_CELLULAR_FALLBACK)) {
53            return false;
54        }
55        if (!(preference instanceof SwitchPreference)) {
56            return false;
57        }
58        // On: avoid bad wifi. Off: prompt.
59        String settingName = Settings.Global.NETWORK_AVOID_BAD_WIFI;
60        Settings.Global.putString(mContext.getContentResolver(), settingName,
61                ((SwitchPreference) preference).isChecked() ? "1" : null);
62        return true;
63    }
64
65    @Override
66    public void updateState(Preference preference) {
67        final boolean currentSetting = avoidBadWifiCurrentSettings();
68        // TODO: can this ever be null? The return value of avoidBadWifiConfig() can only
69        // change if the resources change, but if that happens the activity will be recreated...
70        if (preference != null) {
71            SwitchPreference pref = (SwitchPreference) preference;
72            pref.setChecked(currentSetting);
73        }
74    }
75
76    private boolean avoidBadWifiConfig() {
77        return mContext.getResources().getInteger(
78                com.android.internal.R.integer.config_networkAvoidBadWifi) == 1;
79    }
80
81    private boolean avoidBadWifiCurrentSettings() {
82        return "1".equals(Settings.Global.getString(mContext.getContentResolver(),
83                Settings.Global.NETWORK_AVOID_BAD_WIFI));
84    }
85}
86