1 /* 2 * Copyright (C) 2010 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.BroadcastReceiver; 20 import android.content.Context; 21 import android.content.Intent; 22 import android.content.IntentFilter; 23 import android.net.ConnectivityManager; 24 import android.net.wifi.WifiConfiguration; 25 import android.net.wifi.WifiManager; 26 import android.provider.Settings; 27 import android.support.v14.preference.SwitchPreference; 28 29 import com.android.settings.R; 30 import com.android.settings.datausage.DataSaverBackend; 31 32 import java.util.ArrayList; 33 34 public class WifiApEnabler { 35 private final Context mContext; 36 private final SwitchPreference mSwitch; 37 private final CharSequence mOriginalSummary; 38 private final DataSaverBackend mDataSaverBackend; 39 40 private WifiManager mWifiManager; 41 private final IntentFilter mIntentFilter; 42 43 ConnectivityManager mCm; 44 private String[] mWifiRegexs; 45 46 private final BroadcastReceiver mReceiver = new BroadcastReceiver() { 47 @Override 48 public void onReceive(Context context, Intent intent) { 49 String action = intent.getAction(); 50 if (WifiManager.WIFI_AP_STATE_CHANGED_ACTION.equals(action)) { 51 int state = intent.getIntExtra( 52 WifiManager.EXTRA_WIFI_AP_STATE, WifiManager.WIFI_AP_STATE_FAILED); 53 if (state == WifiManager.WIFI_AP_STATE_FAILED) { 54 int reason = intent.getIntExtra(WifiManager.EXTRA_WIFI_AP_FAILURE_REASON, 55 WifiManager.SAP_START_FAILURE_GENERAL); 56 handleWifiApStateChanged(state, reason); 57 } else { 58 handleWifiApStateChanged(state, WifiManager.SAP_START_FAILURE_GENERAL); 59 } 60 } else if (ConnectivityManager.ACTION_TETHER_STATE_CHANGED.equals(action)) { 61 ArrayList<String> available = intent.getStringArrayListExtra( 62 ConnectivityManager.EXTRA_AVAILABLE_TETHER); 63 ArrayList<String> active = intent.getStringArrayListExtra( 64 ConnectivityManager.EXTRA_ACTIVE_TETHER); 65 ArrayList<String> errored = intent.getStringArrayListExtra( 66 ConnectivityManager.EXTRA_ERRORED_TETHER); 67 updateTetherState(available.toArray(), active.toArray(), errored.toArray()); 68 } else if (Intent.ACTION_AIRPLANE_MODE_CHANGED.equals(action)) { 69 enableWifiSwitch(); 70 } 71 } 72 }; 73 WifiApEnabler(Context context, DataSaverBackend dataSaverBackend, SwitchPreference switchPreference)74 public WifiApEnabler(Context context, DataSaverBackend dataSaverBackend, 75 SwitchPreference switchPreference) { 76 mContext = context; 77 mDataSaverBackend = dataSaverBackend; 78 mSwitch = switchPreference; 79 mOriginalSummary = switchPreference.getSummary(); 80 switchPreference.setPersistent(false); 81 82 mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); 83 mCm = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE); 84 85 mWifiRegexs = mCm.getTetherableWifiRegexs(); 86 87 mIntentFilter = new IntentFilter(WifiManager.WIFI_AP_STATE_CHANGED_ACTION); 88 mIntentFilter.addAction(ConnectivityManager.ACTION_TETHER_STATE_CHANGED); 89 mIntentFilter.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED); 90 } 91 resume()92 public void resume() { 93 mContext.registerReceiver(mReceiver, mIntentFilter); 94 enableWifiSwitch(); 95 } 96 pause()97 public void pause() { 98 mContext.unregisterReceiver(mReceiver); 99 } 100 enableWifiSwitch()101 private void enableWifiSwitch() { 102 boolean isAirplaneMode = Settings.Global.getInt(mContext.getContentResolver(), 103 Settings.Global.AIRPLANE_MODE_ON, 0) != 0; 104 if(!isAirplaneMode) { 105 mSwitch.setEnabled(!mDataSaverBackend.isDataSaverEnabled()); 106 } else { 107 mSwitch.setSummary(mOriginalSummary); 108 mSwitch.setEnabled(false); 109 } 110 } 111 updateConfigSummary(WifiConfiguration wifiConfig)112 public void updateConfigSummary(WifiConfiguration wifiConfig) { 113 String s = mContext.getString( 114 com.android.internal.R.string.wifi_tether_configure_ssid_default); 115 mSwitch.setSummary(String.format( 116 mContext.getString(R.string.wifi_tether_enabled_subtext), 117 (wifiConfig == null) ? s : wifiConfig.SSID)); 118 } 119 updateTetherState(Object[] available, Object[] tethered, Object[] errored)120 private void updateTetherState(Object[] available, Object[] tethered, Object[] errored) { 121 boolean wifiTethered = false; 122 boolean wifiErrored = false; 123 124 for (Object o : tethered) { 125 String s = (String)o; 126 for (String regex : mWifiRegexs) { 127 if (s.matches(regex)) wifiTethered = true; 128 } 129 } 130 for (Object o: errored) { 131 String s = (String)o; 132 for (String regex : mWifiRegexs) { 133 if (s.matches(regex)) wifiErrored = true; 134 } 135 } 136 137 if (wifiTethered) { 138 WifiConfiguration wifiConfig = mWifiManager.getWifiApConfiguration(); 139 updateConfigSummary(wifiConfig); 140 } else if (wifiErrored) { 141 mSwitch.setSummary(R.string.wifi_error); 142 } else { 143 mSwitch.setSummary(R.string.wifi_hotspot_off_subtext); 144 } 145 } 146 handleWifiApStateChanged(int state, int reason)147 private void handleWifiApStateChanged(int state, int reason) { 148 switch (state) { 149 case WifiManager.WIFI_AP_STATE_ENABLING: 150 mSwitch.setSummary(R.string.wifi_tether_starting); 151 mSwitch.setEnabled(false); 152 break; 153 case WifiManager.WIFI_AP_STATE_ENABLED: 154 /** 155 * Summary on enable is handled by tether 156 * broadcast notice 157 */ 158 mSwitch.setChecked(true); 159 /* Doesnt need the airplane check */ 160 mSwitch.setEnabled(!mDataSaverBackend.isDataSaverEnabled()); 161 break; 162 case WifiManager.WIFI_AP_STATE_DISABLING: 163 mSwitch.setSummary(R.string.wifi_tether_stopping); 164 mSwitch.setChecked(false); 165 mSwitch.setEnabled(false); 166 break; 167 case WifiManager.WIFI_AP_STATE_DISABLED: 168 mSwitch.setChecked(false); 169 mSwitch.setSummary(mOriginalSummary); 170 enableWifiSwitch(); 171 break; 172 default: 173 mSwitch.setChecked(false); 174 if (reason == WifiManager.SAP_START_FAILURE_NO_CHANNEL) { 175 mSwitch.setSummary(R.string.wifi_sap_no_channel_error); 176 } else { 177 mSwitch.setSummary(R.string.wifi_error); 178 } 179 enableWifiSwitch(); 180 } 181 } 182 } 183