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