1 /* 2 * Copyright (C) 2018 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.details; 18 19 import android.app.backup.BackupManager; 20 import android.content.Context; 21 import android.net.wifi.WifiConfiguration; 22 import android.net.wifi.WifiManager; 23 import android.provider.Settings; 24 import android.support.annotation.VisibleForTesting; 25 import android.support.v14.preference.SwitchPreference; 26 import android.support.v7.preference.DropDownPreference; 27 import android.support.v7.preference.Preference; 28 import android.support.v7.preference.PreferenceScreen; 29 import android.text.TextUtils; 30 31 import com.android.settings.core.BasePreferenceController; 32 import com.android.settings.core.PreferenceControllerMixin; 33 import com.android.settingslib.core.AbstractPreferenceController; 34 35 /** 36 * {@link AbstractPreferenceController} that controls whether the wifi network is metered or not 37 */ 38 public class WifiMeteredPreferenceController extends BasePreferenceController implements 39 Preference.OnPreferenceChangeListener { 40 41 private static final String KEY_WIFI_METERED = "metered"; 42 private WifiConfiguration mWifiConfiguration; 43 private WifiManager mWifiManager; 44 WifiMeteredPreferenceController(Context context, WifiConfiguration wifiConfiguration)45 public WifiMeteredPreferenceController(Context context, WifiConfiguration wifiConfiguration) { 46 super(context, KEY_WIFI_METERED); 47 mWifiConfiguration = wifiConfiguration; 48 mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); 49 } 50 51 @Override updateState(Preference preference)52 public void updateState(Preference preference) { 53 final DropDownPreference dropDownPreference = (DropDownPreference) preference; 54 final int meteredOverride = getMeteredOverride(); 55 dropDownPreference.setValue(Integer.toString(meteredOverride)); 56 updateSummary(dropDownPreference, meteredOverride); 57 } 58 59 @Override getAvailabilityStatus()60 public int getAvailabilityStatus() { 61 return AVAILABLE; 62 } 63 64 @Override onPreferenceChange(Preference preference, Object newValue)65 public boolean onPreferenceChange(Preference preference, Object newValue) { 66 if (mWifiConfiguration != null) { 67 mWifiConfiguration.meteredOverride = Integer.parseInt((String) newValue); 68 } 69 mWifiManager.updateNetwork(mWifiConfiguration); 70 // Stage the backup of the SettingsProvider package which backs this up 71 BackupManager.dataChanged("com.android.providers.settings"); 72 updateSummary((DropDownPreference) preference, getMeteredOverride()); 73 return true; 74 } 75 76 @VisibleForTesting getMeteredOverride()77 int getMeteredOverride() { 78 if (mWifiConfiguration != null) { 79 // Wrap the meteredOverride since robolectric cannot recognize it 80 return mWifiConfiguration.meteredOverride; 81 } 82 return WifiConfiguration.METERED_OVERRIDE_NONE; 83 } 84 updateSummary(DropDownPreference preference, int meteredOverride)85 private void updateSummary(DropDownPreference preference, int meteredOverride) { 86 preference.setSummary(preference.getEntries()[meteredOverride]); 87 } 88 } 89