blob: 58e5e29adeee1ccf5279e68735c93e58aa238360 [file] [log] [blame]
Jason Monk95f03c42015-02-11 13:28:26 -05001/*
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 */
16package com.android.settingslib;
17
18import android.app.ActivityManager;
19import android.content.ComponentName;
20import android.content.ContentResolver;
21import android.content.Context;
22import android.content.res.Resources;
23import android.net.ConnectivityManager;
24import android.net.wifi.WifiManager;
25import android.os.SystemProperties;
26import android.os.UserHandle;
27import android.provider.Settings;
28
29public class TetherUtil {
30
31 // Types of tethering.
32 public static final int TETHERING_INVALID = -1;
33 public static final int TETHERING_WIFI = 0;
34 public static final int TETHERING_USB = 1;
35 public static final int TETHERING_BLUETOOTH = 2;
36
37 // Extras used for communicating with the TetherService.
38 public static final String EXTRA_ADD_TETHER_TYPE = "extraAddTetherType";
39 public static final String EXTRA_REM_TETHER_TYPE = "extraRemTetherType";
40 public static final String EXTRA_SET_ALARM = "extraSetAlarm";
41 /**
42 * Tells the service to run a provision check now.
43 */
44 public static final String EXTRA_RUN_PROVISION = "extraRunProvision";
45 /**
46 * Enables wifi tethering if the provision check is successful. Used by
47 * QS to enable tethering.
48 */
49 public static final String EXTRA_ENABLE_WIFI_TETHER = "extraEnableWifiTether";
50
51 public static ComponentName TETHER_SERVICE = ComponentName.unflattenFromString(Resources
52 .getSystem().getString(com.android.internal.R.string.config_wifi_tether_enable));
53
54 public static boolean setWifiTethering(boolean enable, Context context) {
55 final WifiManager wifiManager =
56 (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
57 final ContentResolver cr = context.getContentResolver();
58 /**
59 * Disable Wifi if enabling tethering
60 */
61 int wifiState = wifiManager.getWifiState();
62 if (enable && ((wifiState == WifiManager.WIFI_STATE_ENABLING) ||
63 (wifiState == WifiManager.WIFI_STATE_ENABLED))) {
64 wifiManager.setWifiEnabled(false);
65 Settings.Global.putInt(cr, Settings.Global.WIFI_SAVED_STATE, 1);
66 }
67
68 boolean success = wifiManager.setWifiApEnabled(null, enable);
69 /**
70 * If needed, restore Wifi on tether disable
71 */
72 if (!enable) {
73 int wifiSavedState = Settings.Global.getInt(cr, Settings.Global.WIFI_SAVED_STATE, 0);
74 if (wifiSavedState == 1) {
75 wifiManager.setWifiEnabled(true);
76 Settings.Global.putInt(cr, Settings.Global.WIFI_SAVED_STATE, 0);
77 }
78 }
79 return success;
80 }
81
82 public static boolean isWifiTetherEnabled(Context context) {
83 WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
84 return wifiManager.getWifiApState() == WifiManager.WIFI_AP_STATE_ENABLED;
85 }
86
87 public static boolean isProvisioningNeeded(Context context) {
88 // Keep in sync with other usage of config_mobile_hotspot_provision_app.
89 // ConnectivityManager#enforceTetherChangePermission
90 String[] provisionApp = context.getResources().getStringArray(
91 com.android.internal.R.array.config_mobile_hotspot_provision_app);
92 if (SystemProperties.getBoolean("net.tethering.noprovisioning", false)
93 || provisionApp == null) {
94 return false;
95 }
96 return (provisionApp.length == 2);
97 }
98
99 public static boolean isTetheringSupported(Context context) {
100 final ConnectivityManager cm =
101 (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
102 final boolean isSecondaryUser = ActivityManager.getCurrentUser() != UserHandle.USER_OWNER;
103 return !isSecondaryUser && cm.isTetheringSupported();
104 }
105
106}