1 /*
2  * Copyright (C) 2019 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.car.settings.wifi.preferences;
18 
19 import android.car.drivingstate.CarUxRestrictions;
20 import android.content.Context;
21 import android.provider.Settings;
22 import android.text.TextUtils;
23 
24 import androidx.preference.TwoStatePreference;
25 
26 import com.android.car.settings.R;
27 import com.android.car.settings.common.FragmentController;
28 import com.android.car.settings.common.PreferenceController;
29 
30 /** Business logic to enable falling back to cellular data when wifi is poor. */
31 public class CellularFallbackTogglePreferenceController extends
32         PreferenceController<TwoStatePreference> {
33 
34     private static final String ENABLED = "1";
35     private static final String DISABLED = null;
36 
CellularFallbackTogglePreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)37     public CellularFallbackTogglePreferenceController(Context context, String preferenceKey,
38             FragmentController fragmentController, CarUxRestrictions uxRestrictions) {
39         super(context, preferenceKey, fragmentController, uxRestrictions);
40     }
41 
42     @Override
getPreferenceType()43     protected Class<TwoStatePreference> getPreferenceType() {
44         return TwoStatePreference.class;
45     }
46 
47     @Override
getAvailabilityStatus()48     protected int getAvailabilityStatus() {
49         return avoidBadWifiConfigEnabled() ? UNSUPPORTED_ON_DEVICE : AVAILABLE;
50     }
51 
52     @Override
updateState(TwoStatePreference preference)53     protected void updateState(TwoStatePreference preference) {
54         preference.setChecked(cellularFallbackEnabled());
55     }
56 
57     @Override
handlePreferenceChanged(TwoStatePreference preference, Object newValue)58     protected boolean handlePreferenceChanged(TwoStatePreference preference, Object newValue) {
59         boolean enableCellularFallback = (Boolean) newValue;
60         Settings.Global.putString(getContext().getContentResolver(),
61                 Settings.Global.NETWORK_AVOID_BAD_WIFI,
62                 enableCellularFallback ? ENABLED : DISABLED);
63         return true;
64     }
65 
66     /**
67      * See {@link Settings.Global#NETWORK_AVOID_BAD_WIFI} for description of this configuration. In
68      * short, if the device is configured to avoid bad wifi, the option to fallback to cellular is
69      * not shown.
70      */
avoidBadWifiConfigEnabled()71     private boolean avoidBadWifiConfigEnabled() {
72         return getContext().getResources().getInteger(R.integer.config_networkAvoidBadWifi) == 1;
73     }
74 
cellularFallbackEnabled()75     private boolean cellularFallbackEnabled() {
76         return TextUtils.equals(ENABLED,
77                 Settings.Global.getString(getContext().getContentResolver(),
78                         Settings.Global.NETWORK_AVOID_BAD_WIFI));
79     }
80 }
81