• 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.content.res.Resources;
21 import android.provider.Settings;
22 import android.telephony.SubscriptionManager;
23 
24 import com.android.internal.annotations.VisibleForTesting;
25 import com.android.settings.core.TogglePreferenceController;
26 
27 /**
28  * CellularFallbackPreferenceController controls whether we should fall back to celluar when
29  * wifi is bad.
30  */
31 public class CellularFallbackPreferenceController extends TogglePreferenceController {
32 
CellularFallbackPreferenceController(Context context, String key)33     public CellularFallbackPreferenceController(Context context, String key) {
34         super(context, key);
35     }
36 
37     @Override
getAvailabilityStatus()38     public int getAvailabilityStatus() {
39         return avoidBadWifiConfig() ? UNSUPPORTED_ON_DEVICE : AVAILABLE;
40     }
41 
42     @Override
isChecked()43     public boolean isChecked() {
44         return avoidBadWifiCurrentSettings();
45     }
46 
47     @Override
setChecked(boolean isChecked)48     public boolean setChecked(boolean isChecked) {
49         // On: avoid bad wifi. Off: prompt.
50         return Settings.Global.putString(mContext.getContentResolver(),
51                 Settings.Global.NETWORK_AVOID_BAD_WIFI, isChecked ? "1" : null);
52     }
53 
avoidBadWifiConfig()54     private boolean avoidBadWifiConfig() {
55         final int activeDataSubscriptionId = getActiveDataSubscriptionId();
56         if (activeDataSubscriptionId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
57             return true;
58         }
59 
60         final Resources resources = getResourcesForSubId(activeDataSubscriptionId);
61         return resources.getInteger(com.android.internal.R.integer.config_networkAvoidBadWifi) == 1;
62     }
63 
64     @VisibleForTesting
getActiveDataSubscriptionId()65     int getActiveDataSubscriptionId() {
66         return SubscriptionManager.getActiveDataSubscriptionId();
67     }
68 
69     @VisibleForTesting
getResourcesForSubId(int subscriptionId)70     Resources getResourcesForSubId(int subscriptionId) {
71         return SubscriptionManager.getResourcesForSubId(mContext, subscriptionId);
72     }
73 
avoidBadWifiCurrentSettings()74     private boolean avoidBadWifiCurrentSettings() {
75         return "1".equals(Settings.Global.getString(mContext.getContentResolver(),
76                 Settings.Global.NETWORK_AVOID_BAD_WIFI));
77     }
78 }