1 /* 2 * Copyright (C) 2021 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 17 package com.android.settingslib.wifi; 18 19 import android.content.Context; 20 import android.os.Build; 21 import android.os.UserManager; 22 import android.util.Log; 23 24 import androidx.annotation.ChecksSdkIntAtLeast; 25 26 import com.android.internal.annotations.VisibleForTesting; 27 28 /* Utility class is to confirm the Wi-Fi function is available by enterprise restriction */ 29 public class WifiEnterpriseRestrictionUtils { 30 private static final String TAG = "WifiEntResUtils"; 31 32 /** 33 * Confirm Wi-Fi tethering is allowed according to whether user restriction is set 34 * 35 * @param context A context 36 * @return whether the device is permitted to use Wi-Fi Tethering 37 */ isWifiTetheringAllowed(Context context)38 public static boolean isWifiTetheringAllowed(Context context) { 39 if (!hasUserRestrictionFromT(context, UserManager.DISALLOW_WIFI_TETHERING)) return true; 40 Log.w(TAG, "Wi-Fi Tethering isn't available due to user restriction."); 41 return false; 42 } 43 44 /** 45 * Confirm Wi-Fi Direct is allowed according to whether user restriction is set 46 * 47 * @param context A context 48 * @return whether the device is permitted to use Wi-Fi Direct 49 */ isWifiDirectAllowed(Context context)50 public static boolean isWifiDirectAllowed(Context context) { 51 if (!hasUserRestrictionFromT(context, UserManager.DISALLOW_WIFI_DIRECT)) return true; 52 Log.w(TAG, "Wi-Fi Direct isn't available due to user restriction."); 53 return false; 54 } 55 56 /** 57 * Confirm Wi-Fi Config is allowed to add according to whether user restriction is set 58 * 59 * @param context A context 60 * @return whether the device is permitted to add new Wi-Fi config 61 */ isAddWifiConfigAllowed(Context context)62 public static boolean isAddWifiConfigAllowed(Context context) { 63 if (!hasUserRestrictionFromT(context, UserManager.DISALLOW_ADD_WIFI_CONFIG)) return true; 64 Log.w(TAG, "Wi-Fi Add network isn't available due to user restriction."); 65 return false; 66 } 67 68 /** 69 * Confirm Wi-Fi state is allowed to change to whether user restriction is set 70 * 71 * @param context A context 72 * @return whether the device is permitted to change Wi-Fi state 73 */ isChangeWifiStateAllowed(Context context)74 public static boolean isChangeWifiStateAllowed(Context context) { 75 if (!hasUserRestrictionFromT(context, UserManager.DISALLOW_CHANGE_WIFI_STATE)) return true; 76 Log.w(TAG, "WI-FI state isn't allowed to change due to user restriction."); 77 return false; 78 } 79 80 @VisibleForTesting hasUserRestrictionFromT(Context context, String restrictionKey)81 static boolean hasUserRestrictionFromT(Context context, String restrictionKey) { 82 if (!isAtLeastT()) return false; 83 final UserManager userManager = context.getSystemService(UserManager.class); 84 if (userManager == null) return false; 85 return userManager.hasUserRestriction(restrictionKey); 86 } 87 88 @ChecksSdkIntAtLeast(api = Build.VERSION_CODES.TIRAMISU) isAtLeastT()89 private static boolean isAtLeastT() { 90 return Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU; 91 } 92 } 93