Jason Monk | 95f03c4 | 2015-02-11 13:28:26 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 | package com.android.settingslib; |
| 17 | |
| 18 | import android.app.ActivityManager; |
| 19 | import android.content.ComponentName; |
| 20 | import android.content.ContentResolver; |
| 21 | import android.content.Context; |
| 22 | import android.content.res.Resources; |
| 23 | import android.net.ConnectivityManager; |
| 24 | import android.net.wifi.WifiManager; |
| 25 | import android.os.SystemProperties; |
| 26 | import android.os.UserHandle; |
| 27 | import android.provider.Settings; |
Chris Manton | 1ba0fa0 | 2015-09-01 13:56:46 -0700 | [diff] [blame^] | 28 | import android.telephony.CarrierConfigManager; |
Jason Monk | 95f03c4 | 2015-02-11 13:28:26 -0500 | [diff] [blame] | 29 | |
| 30 | public class TetherUtil { |
| 31 | |
| 32 | // Types of tethering. |
| 33 | public static final int TETHERING_INVALID = -1; |
| 34 | public static final int TETHERING_WIFI = 0; |
| 35 | public static final int TETHERING_USB = 1; |
| 36 | public static final int TETHERING_BLUETOOTH = 2; |
| 37 | |
| 38 | // Extras used for communicating with the TetherService. |
| 39 | public static final String EXTRA_ADD_TETHER_TYPE = "extraAddTetherType"; |
| 40 | public static final String EXTRA_REM_TETHER_TYPE = "extraRemTetherType"; |
| 41 | public static final String EXTRA_SET_ALARM = "extraSetAlarm"; |
| 42 | /** |
| 43 | * Tells the service to run a provision check now. |
| 44 | */ |
| 45 | public static final String EXTRA_RUN_PROVISION = "extraRunProvision"; |
| 46 | /** |
| 47 | * Enables wifi tethering if the provision check is successful. Used by |
| 48 | * QS to enable tethering. |
| 49 | */ |
| 50 | public static final String EXTRA_ENABLE_WIFI_TETHER = "extraEnableWifiTether"; |
| 51 | |
| 52 | public static ComponentName TETHER_SERVICE = ComponentName.unflattenFromString(Resources |
| 53 | .getSystem().getString(com.android.internal.R.string.config_wifi_tether_enable)); |
| 54 | |
| 55 | public static boolean setWifiTethering(boolean enable, Context context) { |
| 56 | final WifiManager wifiManager = |
| 57 | (WifiManager) context.getSystemService(Context.WIFI_SERVICE); |
Mitchell Wills | 8ba94ba | 2015-08-11 17:59:45 -0700 | [diff] [blame] | 58 | return wifiManager.setWifiApEnabled(null, enable); |
Jason Monk | 95f03c4 | 2015-02-11 13:28:26 -0500 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | public static boolean isWifiTetherEnabled(Context context) { |
| 62 | WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); |
| 63 | return wifiManager.getWifiApState() == WifiManager.WIFI_AP_STATE_ENABLED; |
| 64 | } |
| 65 | |
Chris Manton | 1ba0fa0 | 2015-09-01 13:56:46 -0700 | [diff] [blame^] | 66 | private static boolean isEntitlementCheckRequired(Context context) { |
| 67 | final CarrierConfigManager configManager = (CarrierConfigManager) context |
| 68 | .getSystemService(Context.CARRIER_CONFIG_SERVICE); |
| 69 | return configManager.getConfig().getBoolean(CarrierConfigManager |
| 70 | .KEY_REQUIRE_ENTITLEMENT_CHECKS_BOOL); |
| 71 | } |
| 72 | |
Jason Monk | 95f03c4 | 2015-02-11 13:28:26 -0500 | [diff] [blame] | 73 | public static boolean isProvisioningNeeded(Context context) { |
| 74 | // Keep in sync with other usage of config_mobile_hotspot_provision_app. |
| 75 | // ConnectivityManager#enforceTetherChangePermission |
| 76 | String[] provisionApp = context.getResources().getStringArray( |
| 77 | com.android.internal.R.array.config_mobile_hotspot_provision_app); |
| 78 | if (SystemProperties.getBoolean("net.tethering.noprovisioning", false) |
| 79 | || provisionApp == null) { |
| 80 | return false; |
| 81 | } |
Chris Manton | 1ba0fa0 | 2015-09-01 13:56:46 -0700 | [diff] [blame^] | 82 | // Check carrier config for entitlement checks |
| 83 | if (isEntitlementCheckRequired(context) == false) { |
| 84 | return false; |
| 85 | } |
Jason Monk | 95f03c4 | 2015-02-11 13:28:26 -0500 | [diff] [blame] | 86 | return (provisionApp.length == 2); |
| 87 | } |
| 88 | |
| 89 | public static boolean isTetheringSupported(Context context) { |
| 90 | final ConnectivityManager cm = |
| 91 | (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); |
| 92 | final boolean isSecondaryUser = ActivityManager.getCurrentUser() != UserHandle.USER_OWNER; |
| 93 | return !isSecondaryUser && cm.isTetheringSupported(); |
| 94 | } |
| 95 | |
| 96 | } |