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