diff options
3 files changed, 118 insertions, 57 deletions
diff --git a/packages/SettingsLib/src/com/android/settingslib/TetherUtil.java b/packages/SettingsLib/src/com/android/settingslib/TetherUtil.java new file mode 100644 index 0000000..58e5e29 --- /dev/null +++ b/packages/SettingsLib/src/com/android/settingslib/TetherUtil.java @@ -0,0 +1,106 @@ +/* + * Copyright (C) 2015 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.android.settingslib; + +import android.app.ActivityManager; +import android.content.ComponentName; +import android.content.ContentResolver; +import android.content.Context; +import android.content.res.Resources; +import android.net.ConnectivityManager; +import android.net.wifi.WifiManager; +import android.os.SystemProperties; +import android.os.UserHandle; +import android.provider.Settings; + +public class TetherUtil { + + // Types of tethering. + public static final int TETHERING_INVALID = -1; + public static final int TETHERING_WIFI = 0; + public static final int TETHERING_USB = 1; + public static final int TETHERING_BLUETOOTH = 2; + + // Extras used for communicating with the TetherService. + public static final String EXTRA_ADD_TETHER_TYPE = "extraAddTetherType"; + public static final String EXTRA_REM_TETHER_TYPE = "extraRemTetherType"; + public static final String EXTRA_SET_ALARM = "extraSetAlarm"; + /** + * Tells the service to run a provision check now. + */ + public static final String EXTRA_RUN_PROVISION = "extraRunProvision"; + /** + * Enables wifi tethering if the provision check is successful. Used by + * QS to enable tethering. + */ + public static final String EXTRA_ENABLE_WIFI_TETHER = "extraEnableWifiTether"; + + public static ComponentName TETHER_SERVICE = ComponentName.unflattenFromString(Resources + .getSystem().getString(com.android.internal.R.string.config_wifi_tether_enable)); + + public static boolean setWifiTethering(boolean enable, Context context) { + final WifiManager wifiManager = + (WifiManager) context.getSystemService(Context.WIFI_SERVICE); + final ContentResolver cr = context.getContentResolver(); + /** + * Disable Wifi if enabling tethering + */ + int wifiState = wifiManager.getWifiState(); + if (enable && ((wifiState == WifiManager.WIFI_STATE_ENABLING) || + (wifiState == WifiManager.WIFI_STATE_ENABLED))) { + wifiManager.setWifiEnabled(false); + Settings.Global.putInt(cr, Settings.Global.WIFI_SAVED_STATE, 1); + } + + boolean success = wifiManager.setWifiApEnabled(null, enable); + /** + * If needed, restore Wifi on tether disable + */ + if (!enable) { + int wifiSavedState = Settings.Global.getInt(cr, Settings.Global.WIFI_SAVED_STATE, 0); + if (wifiSavedState == 1) { + wifiManager.setWifiEnabled(true); + Settings.Global.putInt(cr, Settings.Global.WIFI_SAVED_STATE, 0); + } + } + return success; + } + + public static boolean isWifiTetherEnabled(Context context) { + WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); + return wifiManager.getWifiApState() == WifiManager.WIFI_AP_STATE_ENABLED; + } + + public static boolean isProvisioningNeeded(Context context) { + // Keep in sync with other usage of config_mobile_hotspot_provision_app. + // ConnectivityManager#enforceTetherChangePermission + String[] provisionApp = context.getResources().getStringArray( + com.android.internal.R.array.config_mobile_hotspot_provision_app); + if (SystemProperties.getBoolean("net.tethering.noprovisioning", false) + || provisionApp == null) { + return false; + } + return (provisionApp.length == 2); + } + + public static boolean isTetheringSupported(Context context) { + final ConnectivityManager cm = + (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); + final boolean isSecondaryUser = ActivityManager.getCurrentUser() != UserHandle.USER_OWNER; + return !isSecondaryUser && cm.isTetheringSupported(); + } + +} diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/policy/HotspotController.java b/packages/SystemUI/src/com/android/systemui/statusbar/policy/HotspotController.java index 0863c86..7ca91a5 100644 --- a/packages/SystemUI/src/com/android/systemui/statusbar/policy/HotspotController.java +++ b/packages/SystemUI/src/com/android/systemui/statusbar/policy/HotspotController.java @@ -22,7 +22,6 @@ public interface HotspotController { boolean isHotspotEnabled(); boolean isHotspotSupported(); void setHotspotEnabled(boolean enabled); - boolean isProvisioningNeeded(); public interface Callback { void onHotspotChanged(boolean enabled); diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/policy/HotspotControllerImpl.java b/packages/SystemUI/src/com/android/systemui/statusbar/policy/HotspotControllerImpl.java index 5eff5a6..4bfd528 100644 --- a/packages/SystemUI/src/com/android/systemui/statusbar/policy/HotspotControllerImpl.java +++ b/packages/SystemUI/src/com/android/systemui/statusbar/policy/HotspotControllerImpl.java @@ -16,45 +16,38 @@ package com.android.systemui.statusbar.policy; -import android.app.ActivityManager; import android.content.BroadcastReceiver; -import android.content.ComponentName; import android.content.ContentResolver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; -import android.net.ConnectivityManager; import android.net.wifi.WifiManager; -import android.os.SystemProperties; import android.os.UserHandle; -import android.provider.Settings; import android.util.Log; +import com.android.settingslib.TetherUtil; + import java.util.ArrayList; public class HotspotControllerImpl implements HotspotController { private static final String TAG = "HotspotController"; private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG); - // Keep these in sync with Settings TetherService.java - public static final String EXTRA_ADD_TETHER_TYPE = "extraAddTetherType"; - public static final String EXTRA_SET_ALARM = "extraSetAlarm"; - public static final String EXTRA_RUN_PROVISION = "extraRunProvision"; - public static final String EXTRA_ENABLE_WIFI_TETHER = "extraEnableWifiTether"; - // Keep this in sync with Settings TetherSettings.java - public static final int WIFI_TETHERING = 0; + private static final Intent TETHER_SERVICE_INTENT = new Intent() + .putExtra(TetherUtil.EXTRA_ADD_TETHER_TYPE, TetherUtil.TETHERING_WIFI) + .putExtra(TetherUtil.EXTRA_SET_ALARM, true) + .putExtra(TetherUtil.EXTRA_RUN_PROVISION, true) + .putExtra(TetherUtil.EXTRA_ENABLE_WIFI_TETHER, true) + .setComponent(TetherUtil.TETHER_SERVICE); private final ArrayList<Callback> mCallbacks = new ArrayList<Callback>(); private final Receiver mReceiver = new Receiver(); private final Context mContext; private final WifiManager mWifiManager; - private final ConnectivityManager mConnectivityManager; public HotspotControllerImpl(Context context) { mContext = context; mWifiManager = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE); - mConnectivityManager = (ConnectivityManager) - mContext.getSystemService(Context.CONNECTIVITY_SERVICE); } public void addCallback(Callback callback) { @@ -78,54 +71,17 @@ public class HotspotControllerImpl implements HotspotController { @Override public boolean isHotspotSupported() { - final boolean isSecondaryUser = ActivityManager.getCurrentUser() != UserHandle.USER_OWNER; - return !isSecondaryUser && mConnectivityManager.isTetheringSupported(); - } - - @Override - public boolean isProvisioningNeeded() { - // Keep in sync with other usage of config_mobile_hotspot_provision_app. - // TetherSettings#isProvisioningNeeded and - // ConnectivityManager#enforceTetherChangePermission - String[] provisionApp = mContext.getResources().getStringArray( - com.android.internal.R.array.config_mobile_hotspot_provision_app); - if (SystemProperties.getBoolean("net.tethering.noprovisioning", false) - || provisionApp == null) { - return false; - } - return (provisionApp.length == 2); + return TetherUtil.isTetheringSupported(mContext); } @Override public void setHotspotEnabled(boolean enabled) { final ContentResolver cr = mContext.getContentResolver(); // Call provisioning app which is called when enabling Tethering from Settings - if (enabled) { - if (isProvisioningNeeded()) { - String tetherEnable = mContext.getResources().getString( - com.android.internal.R.string.config_wifi_tether_enable); - Intent intent = new Intent(); - intent.putExtra(EXTRA_ADD_TETHER_TYPE, WIFI_TETHERING); - intent.putExtra(EXTRA_SET_ALARM, true); - intent.putExtra(EXTRA_RUN_PROVISION, true); - intent.putExtra(EXTRA_ENABLE_WIFI_TETHER, true); - intent.setComponent(ComponentName.unflattenFromString(tetherEnable)); - mContext.startServiceAsUser(intent, UserHandle.CURRENT); - } else { - int wifiState = mWifiManager.getWifiState(); - if ((wifiState == WifiManager.WIFI_STATE_ENABLING) || - (wifiState == WifiManager.WIFI_STATE_ENABLED)) { - mWifiManager.setWifiEnabled(false); - Settings.Global.putInt(cr, Settings.Global.WIFI_SAVED_STATE, 1); - } - mWifiManager.setWifiApEnabled(null, true); - } + if (enabled && TetherUtil.isProvisioningNeeded(mContext)) { + mContext.startServiceAsUser(TETHER_SERVICE_INTENT, UserHandle.CURRENT); } else { - mWifiManager.setWifiApEnabled(null, false); - if (Settings.Global.getInt(cr, Settings.Global.WIFI_SAVED_STATE, 0) == 1) { - mWifiManager.setWifiEnabled(true); - Settings.Global.putInt(cr, Settings.Global.WIFI_SAVED_STATE, 0); - } + TetherUtil.setWifiTethering(enabled, mContext); } } |