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.tether; 18 19 import static android.net.ConnectivityManager.TETHERING_WIFI; 20 21 import android.content.BroadcastReceiver; 22 import android.content.Context; 23 import android.content.Intent; 24 import android.net.ConnectivityManager; 25 import android.net.wifi.WifiManager; 26 import android.os.Handler; 27 import android.os.Looper; 28 import android.provider.Settings; 29 30 import com.android.settings.datausage.DataSaverBackend; 31 import com.android.settings.widget.SwitchWidgetController; 32 import com.android.settingslib.core.lifecycle.LifecycleObserver; 33 import com.android.settingslib.core.lifecycle.events.OnStart; 34 import com.android.settingslib.core.lifecycle.events.OnStop; 35 36 public class WifiTetherSwitchBarController implements SwitchWidgetController.OnSwitchChangeListener, 37 LifecycleObserver, OnStart, OnStop { 38 39 private final Context mContext; 40 private final SwitchWidgetController mSwitchBar; 41 private final ConnectivityManager mConnectivityManager; 42 private final DataSaverBackend mDataSaverBackend; 43 private final WifiManager mWifiManager; 44 45 WifiTetherSwitchBarController(Context context, SwitchWidgetController switchBar) { 46 mContext = context; 47 mSwitchBar = switchBar; 48 mDataSaverBackend = new DataSaverBackend(context); 49 mConnectivityManager = 50 (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); 51 mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); 52 mSwitchBar.setChecked(mWifiManager.getWifiApState() == WifiManager.WIFI_AP_STATE_ENABLED); 53 mSwitchBar.setListener(this); 54 } 55 56 @Override 57 public void onStart() { 58 mSwitchBar.startListening(); 59 mContext.registerReceiver(mReceiver, 60 WifiTetherPreferenceController.WIFI_TETHER_INTENT_FILTER); 61 } 62 63 @Override 64 public void onStop() { 65 mSwitchBar.stopListening(); 66 mContext.unregisterReceiver(mReceiver); 67 } 68 69 @Override 70 public boolean onSwitchToggled(boolean isChecked) { 71 if (isChecked) { 72 startTether(); 73 } else { 74 stopTether(); 75 } 76 return true; 77 } 78 79 void stopTether() { 80 mSwitchBar.setEnabled(false); 81 mConnectivityManager.stopTethering(TETHERING_WIFI); 82 } 83 84 void startTether() { 85 mSwitchBar.setEnabled(false); 86 mConnectivityManager.startTethering(TETHERING_WIFI, true /* showProvisioningUi */, 87 NoOpOnStartTetheringCallback.newInstance(), new Handler(Looper.getMainLooper())); 88 } 89 90 private final BroadcastReceiver mReceiver = new BroadcastReceiver() { 91 @Override 92 public void onReceive(Context context, Intent intent) { 93 String action = intent.getAction(); 94 if (WifiManager.WIFI_AP_STATE_CHANGED_ACTION.equals(action)) { 95 final int state = intent.getIntExtra( 96 WifiManager.EXTRA_WIFI_AP_STATE, WifiManager.WIFI_AP_STATE_FAILED); 97 handleWifiApStateChanged(state); 98 } else if (Intent.ACTION_AIRPLANE_MODE_CHANGED.equals(action)) { 99 enableWifiSwitch(); 100 } 101 } 102 }; 103 104 private void handleWifiApStateChanged(int state) { 105 switch (state) { 106 case WifiManager.WIFI_AP_STATE_ENABLING: 107 mSwitchBar.setEnabled(false); 108 break; 109 case WifiManager.WIFI_AP_STATE_ENABLED: 110 if (!mSwitchBar.isChecked()) { 111 mSwitchBar.setChecked(true); 112 } 113 enableWifiSwitch(); 114 break; 115 case WifiManager.WIFI_AP_STATE_DISABLING: 116 if (mSwitchBar.isChecked()) { 117 mSwitchBar.setChecked(false); 118 } 119 mSwitchBar.setEnabled(false); 120 break; 121 case WifiManager.WIFI_AP_STATE_DISABLED: 122 mSwitchBar.setChecked(false); 123 enableWifiSwitch(); 124 break; 125 default: 126 mSwitchBar.setChecked(false); 127 enableWifiSwitch(); 128 break; 129 } 130 } 131 132 private void enableWifiSwitch() { 133 boolean isAirplaneMode = Settings.Global.getInt(mContext.getContentResolver(), 134 Settings.Global.AIRPLANE_MODE_ON, 0) != 0; 135 if (!isAirplaneMode) { 136 mSwitchBar.setEnabled(!mDataSaverBackend.isDataSaverEnabled()); 137 } else { 138 mSwitchBar.setEnabled(false); 139 } 140 } 141 } 142