1 /*
2  * Copyright (C) 2024 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 android.net.wifi.util;
18 
19 import android.content.Context;
20 
21 import java.io.PrintWriter;
22 import java.util.HashMap;
23 import java.util.Map;
24 
25 /**
26  * A class to cache the Wifi resource value and provide override mechanism from shell
27  * @hide
28  */
29 public class WifiResourceCache {
30     private final Context mContext;
31 
32     private final Map<String, Boolean> mBooleanResourceMap;
33     private final Map<String, Integer> mIntegerResourceMap;
34 
WifiResourceCache(Context context)35     public WifiResourceCache(Context context) {
36         mContext = context;
37         mBooleanResourceMap = new HashMap<>();
38         mIntegerResourceMap = new HashMap<>();
39     }
40 
41     /**
42      * Get and cache the boolean value as {@link android.content.res.Resources#getBoolean(int)}
43      */
getBoolean(int resourceId, String resourceName)44     public boolean getBoolean(int resourceId, String resourceName) {
45         return mBooleanResourceMap.computeIfAbsent(resourceName,
46                 v -> mContext.getResources().getBoolean(resourceId));
47     }
48 
49     /**
50      * Get and cache the integer value as {@link android.content.res.Resources#getInteger(int)}
51      */
getInteger(int resourceId, String resourceName)52     public int getInteger(int resourceId, String resourceName) {
53         return mIntegerResourceMap.computeIfAbsent(resourceName,
54                 v -> mContext.getResources().getInteger(resourceId));
55     }
56 
57     /**
58      * Override the target boolean value
59      *
60      * @param resourceName the resource overlay name
61      * @param value        override to this value
62      */
overrideBooleanValue(String resourceName, boolean value)63     public void overrideBooleanValue(String resourceName, boolean value) {
64         mBooleanResourceMap.put(resourceName, value);
65     }
66 
67     /**
68      * Override the target boolean value
69      */
restoreBooleanValue(String resourceName)70     public void restoreBooleanValue(String resourceName) {
71         mBooleanResourceMap.remove(resourceName);
72     }
73 
74     /**
75      * Override the target integer value
76      * @param resourceName the resource overlay name
77      * @param value override to this value
78      */
overrideIntegerValue(String resourceName, int value)79     public void overrideIntegerValue(String resourceName, int value) {
80         mIntegerResourceMap.put(resourceName, value);
81     }
82 
83     /**
84      * Override the target integer value
85      */
restoreIntegerValue(String resourceName)86     public void restoreIntegerValue(String resourceName) {
87         mIntegerResourceMap.remove(resourceName);
88     }
89 
90     /**
91      * Dump of current resource value
92      */
dump(PrintWriter pw)93     public void dump(PrintWriter pw) {
94         pw.println("Dump of WifiResourceCache");
95         pw.println("WifiResourceCache - resource value Begin ----");
96 
97         for (Map.Entry<String, Integer> resourceEntry : mIntegerResourceMap.entrySet()) {
98             pw.println("Resource Name: " + resourceEntry.getKey()
99                     + ", value: " + resourceEntry.getValue());
100         }
101         for (Map.Entry<String, Boolean> resourceEntry : mBooleanResourceMap.entrySet()) {
102             pw.println("Resource Name: " + resourceEntry.getKey()
103                     + ", value: " + resourceEntry.getValue());
104         }
105         pw.println("WifiResourceCache - resource value End ----");
106     }
107 
108     /**
109      * Remove all override value and set to default
110      */
reset()111     public void reset() {
112         mBooleanResourceMap.clear();
113         mIntegerResourceMap.clear();
114     }
115 }
116