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.settings.wifi.details2;
18 
19 import android.app.backup.BackupManager;
20 import android.content.Context;
21 
22 import androidx.annotation.NonNull;
23 import androidx.annotation.VisibleForTesting;
24 import androidx.preference.ListPreference;
25 import androidx.preference.Preference;
26 
27 import com.android.settings.core.BasePreferenceController;
28 import com.android.wifitrackerlib.WifiEntry;
29 
30 /**
31  * A controller that controls whether the Wi-Fi network is metered or not.
32  */
33 public class WifiMeteredPreferenceController2 extends BasePreferenceController implements
34         Preference.OnPreferenceChangeListener {
35 
36     private static final String KEY_WIFI_METERED = "metered";
37     private final WifiEntry mWifiEntry;
38 
WifiMeteredPreferenceController2(Context context, WifiEntry wifiEntry)39     public WifiMeteredPreferenceController2(Context context, WifiEntry wifiEntry) {
40         super(context, KEY_WIFI_METERED);
41         mWifiEntry = wifiEntry;
42     }
43 
44     @Override
updateState(Preference preference)45     public void updateState(Preference preference) {
46         final ListPreference listPreference = (ListPreference) preference;
47         final int meteredOverride = getMeteredOverride();
48         preference.setSelectable(mWifiEntry.canSetMeteredChoice());
49         listPreference.setValue(Integer.toString(meteredOverride));
50         updateSummary(listPreference, meteredOverride);
51     }
52 
53     @Override
getAvailabilityStatus()54     public int getAvailabilityStatus() {
55         return AVAILABLE;
56     }
57 
58     @Override
onPreferenceChange(@onNull Preference preference, Object newValue)59     public boolean onPreferenceChange(@NonNull Preference preference, Object newValue) {
60         if (mWifiEntry.isSaved() || mWifiEntry.isSubscription()) {
61             mWifiEntry.setMeteredChoice(Integer.parseInt((String) newValue));
62         }
63 
64         // Stage the backup of the SettingsProvider package which backs this up
65         BackupManager.dataChanged("com.android.providers.settings");
66         updateSummary((ListPreference) preference, getMeteredOverride());
67         return true;
68     }
69 
70     @VisibleForTesting
getMeteredOverride()71     int getMeteredOverride() {
72         if (mWifiEntry.isSaved() || mWifiEntry.isSubscription()) {
73             // Wrap the meteredOverride since robolectric cannot recognize it
74             return mWifiEntry.getMeteredChoice();
75         }
76         return WifiEntry.METERED_CHOICE_AUTO;
77     }
78 
updateSummary(ListPreference preference, int meteredOverride)79     private void updateSummary(ListPreference preference, int meteredOverride) {
80         preference.setSummary(preference.getEntries()[meteredOverride]);
81     }
82 }
83