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 */
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;
28import android.telephony.CarrierConfigManager;
29
30public 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);
58        return wifiManager.setWifiApEnabled(null, enable);
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
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
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        }
82        // Check carrier config for entitlement checks
83        if (isEntitlementCheckRequired(context) == false) {
84            return false;
85        }
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}
97