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 static android.os.UserManager.DISALLOW_CONFIG_WIFI; 20 21 import android.content.Context; 22 import android.os.Bundle; 23 import android.os.UserManager; 24 import android.util.SparseArray; 25 26 import androidx.annotation.NonNull; 27 import androidx.annotation.VisibleForTesting; 28 29 import java.util.HashMap; 30 import java.util.Map; 31 32 /** 33 * This is a singleton class for Wi-Fi restrictions caching. 34 */ 35 public class WifiRestrictionsCache { 36 private static final String TAG = "WifiResCache"; 37 38 /** 39 * Manages mapping between user ID and corresponding singleton {@link WifiRestrictionsCache} 40 * object. 41 */ 42 @VisibleForTesting 43 protected static final SparseArray<WifiRestrictionsCache> sInstances = new SparseArray<>(); 44 45 @VisibleForTesting 46 protected UserManager mUserManager; 47 @VisibleForTesting 48 protected Bundle mUserRestrictions; 49 @VisibleForTesting 50 protected final Map<String, Boolean> mRestrictions = new HashMap<>(); 51 52 /** 53 * @return an instance of {@link WifiRestrictionsCache} object. 54 */ 55 @NonNull getInstance(@onNull Context context)56 public static WifiRestrictionsCache getInstance(@NonNull Context context) { 57 final int requestUserId = context.getUserId(); 58 WifiRestrictionsCache cache; 59 synchronized (sInstances) { 60 // We have same user context as request. 61 if (sInstances.indexOfKey(requestUserId) >= 0) { 62 return sInstances.get(requestUserId); 63 } 64 // Request by a new user context. 65 cache = new WifiRestrictionsCache(context); 66 sInstances.put(context.getUserId(), cache); 67 } 68 return cache; 69 } 70 71 /** 72 * Removes all the instances. 73 */ clearInstance()74 public static void clearInstance() { 75 synchronized (sInstances) { 76 for (int i = 0; i < sInstances.size(); i++) { 77 int key = sInstances.keyAt(i); 78 WifiRestrictionsCache cache = sInstances.get(key); 79 cache.clearRestrictions(); 80 sInstances.remove(key); 81 } 82 sInstances.clear(); 83 } 84 } 85 86 /** 87 * Constructor to create a singleton class for Wi-Fi restrictions cache. 88 * 89 * @param context The Context this is associated with. 90 */ WifiRestrictionsCache(@onNull Context context)91 protected WifiRestrictionsCache(@NonNull Context context) { 92 mUserManager = context.getSystemService(UserManager.class); 93 if (mUserManager != null) { 94 mUserRestrictions = mUserManager.getUserRestrictions(); 95 } 96 } 97 98 /** 99 * @return the boolean value of the restrictions 100 */ getRestriction(String key)101 public Boolean getRestriction(String key) { 102 if (mUserRestrictions == null) { 103 return false; 104 } 105 Boolean restriction; 106 synchronized (mRestrictions) { 107 if (mRestrictions.containsKey(key)) { 108 return mRestrictions.get(key); 109 } 110 restriction = mUserRestrictions.getBoolean(key); 111 mRestrictions.put(key, restriction); 112 } 113 return restriction; 114 } 115 116 /** 117 * Removes all the restrictions. 118 */ clearRestrictions()119 public void clearRestrictions() { 120 synchronized (mRestrictions) { 121 mRestrictions.clear(); 122 } 123 } 124 125 /** 126 * @return Whether the user is allowed to config Wi-Fi. 127 */ isConfigWifiAllowed()128 public Boolean isConfigWifiAllowed() { 129 return !getRestriction(DISALLOW_CONFIG_WIFI); 130 } 131 } 132