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 static android.os.UserManager.DISALLOW_CONFIG_TETHERING;
19 
20 import android.content.Context;
21 import android.net.ConnectivityManager;
22 import android.os.SystemProperties;
23 import android.os.UserHandle;
24 import android.telephony.CarrierConfigManager;
25 
26 import androidx.annotation.VisibleForTesting;
27 
28 public class TetherUtil {
29 
30     @VisibleForTesting
isEntitlementCheckRequired(Context context)31     static boolean isEntitlementCheckRequired(Context context) {
32         final CarrierConfigManager configManager = (CarrierConfigManager) context
33              .getSystemService(Context.CARRIER_CONFIG_SERVICE);
34         if (configManager == null || configManager.getConfig() == null) {
35             // return service default
36             return true;
37         }
38         return configManager.getConfig().getBoolean(CarrierConfigManager
39              .KEY_REQUIRE_ENTITLEMENT_CHECKS_BOOL);
40     }
41 
isProvisioningNeeded(Context context)42     public static boolean isProvisioningNeeded(Context context) {
43         // Keep in sync with other usage of config_mobile_hotspot_provision_app.
44         // ConnectivityManager#enforceTetherChangePermission
45         String[] provisionApp = context.getResources().getStringArray(
46                 com.android.internal.R.array.config_mobile_hotspot_provision_app);
47         if (SystemProperties.getBoolean("net.tethering.noprovisioning", false)
48                 || provisionApp == null) {
49             return false;
50         }
51         // Check carrier config for entitlement checks
52         if (isEntitlementCheckRequired(context) == false) {
53             return false;
54         }
55         return (provisionApp.length == 2);
56     }
57 
isTetherAvailable(Context context)58     public static boolean isTetherAvailable(Context context) {
59         final ConnectivityManager cm = context.getSystemService(ConnectivityManager.class);
60         final boolean tetherConfigDisallowed = RestrictedLockUtilsInternal
61                 .checkIfRestrictionEnforced(context, DISALLOW_CONFIG_TETHERING,
62                         UserHandle.myUserId()) != null;
63         final boolean hasBaseUserRestriction = RestrictedLockUtilsInternal.hasBaseUserRestriction(
64                 context, DISALLOW_CONFIG_TETHERING, UserHandle.myUserId());
65         return (cm.isTetheringSupported() || tetherConfigDisallowed) && !hasBaseUserRestriction;
66     }
67 }
68