1 /*
2  * Copyright (C) 2022 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.settings.network;
18 
19 import static android.telephony.CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED;
20 import static android.telephony.SubscriptionManager.EXTRA_SUBSCRIPTION_INDEX;
21 import static android.telephony.SubscriptionManager.INVALID_SUBSCRIPTION_ID;
22 
23 import android.annotation.TestApi;
24 import android.content.BroadcastReceiver;
25 import android.content.Context;
26 import android.content.Intent;
27 import android.content.IntentFilter;
28 import android.os.PersistableBundle;
29 import android.telephony.CarrierConfigManager;
30 import android.telephony.SubscriptionManager;
31 import android.util.Log;
32 
33 import androidx.annotation.GuardedBy;
34 import androidx.annotation.NonNull;
35 import androidx.annotation.VisibleForTesting;
36 
37 import java.util.Map;
38 import java.util.concurrent.ConcurrentHashMap;
39 
40 /**
41  * This is a singleton class for Carrier-Configuration cache.
42  */
43 public class CarrierConfigCache {
44     private static final String TAG = "CarrConfCache";
45 
46     private static final Object sInstanceLock = new Object();
47     /**
48      * A singleton {@link CarrierConfigCache} object is used to share with all sub-settings.
49      */
50     @GuardedBy("sInstanceLock")
51     private static CarrierConfigCache sInstance;
52     @TestApi
53     @GuardedBy("sInstanceLock")
54     private static Map<Context, CarrierConfigCache> sTestInstances;
55 
56     /**
57      * Manages mapping data from the subscription ID to the Carrier-Configuration
58      * {@link PersistableBundle} object.
59      *
60      * The Carrier-Configurations are used to share with all sub-settings.
61      */
62     @VisibleForTesting
63     protected static final Map<Integer, PersistableBundle> sCarrierConfigs =
64             new ConcurrentHashMap<>();
65     @VisibleForTesting
66     protected static CarrierConfigManager sCarrierConfigManager;
67 
68     /**
69      * Static method to create a singleton class for Carrier-Configuration cache.
70      *
71      * @param context The Context this is associated with.
72      * @return an instance of {@link CarrierConfigCache} object.
73      */
74     @NonNull
getInstance(@onNull Context context)75     public static CarrierConfigCache getInstance(@NonNull Context context) {
76         synchronized (sInstanceLock) {
77             if (sTestInstances != null && sTestInstances.containsKey(context)) {
78                 CarrierConfigCache testInstance = sTestInstances.get(context);
79                 Log.w(TAG, "The context owner try to use a test instance:" + testInstance);
80                 return testInstance;
81             }
82 
83             if (sInstance != null) return sInstance;
84 
85             sInstance = new CarrierConfigCache();
86             final CarrierConfigChangeReceiver receiver = new CarrierConfigChangeReceiver();
87             final Context appContext = context.getApplicationContext();
88             sCarrierConfigManager = appContext.getSystemService(CarrierConfigManager.class);
89             appContext.registerReceiver(receiver, new IntentFilter(ACTION_CARRIER_CONFIG_CHANGED),
90                     Context.RECEIVER_EXPORTED/*UNAUDITED*/);
91             return sInstance;
92         }
93     }
94 
95     /**
96      * A convenience method to set pre-prepared instance or mock(CarrierConfigCache.class) for
97      * testing.
98      *
99      * @param context The Context this is associated with.
100      * @param instance of {@link CarrierConfigCache} object.
101      * @hide
102      */
103     @TestApi
104     @VisibleForTesting
setTestInstance(@onNull Context context, CarrierConfigCache instance)105     public static void setTestInstance(@NonNull Context context, CarrierConfigCache instance) {
106         synchronized (sInstanceLock) {
107             if (sTestInstances == null) sTestInstances = new ConcurrentHashMap<>();
108 
109             Log.w(TAG, "Try to set a test instance by context:" + context);
110             sTestInstances.put(context, instance);
111         }
112     }
113 
114     /**
115      * The constructor can only be accessed from static method inside the class itself, this is
116      * to avoid creating a class by adding a private constructor.
117      */
CarrierConfigCache()118     private CarrierConfigCache() {
119         // Do nothing.
120     }
121 
122     /**
123      * Returns the boolean If the system service is successfully obtained.
124      *
125      * @return true value, if the system service is successfully obtained.
126      */
hasCarrierConfigManager()127     public boolean hasCarrierConfigManager() {
128         return (sCarrierConfigManager != null);
129     }
130 
131     /**
132      * Gets the Carrier-Configuration for a particular subscription, which is associated with a
133      * specific SIM card. If an invalid subId is used, the returned config will contain default
134      * values.
135      *
136      * @param subId the subscription ID, normally obtained from {@link SubscriptionManager}.
137      * @return A {@link PersistableBundle} containing the config for the given subId, or default
138      * values for an invalid subId.
139      */
getConfigForSubId(int subId)140     public PersistableBundle getConfigForSubId(int subId) {
141         if (sCarrierConfigManager == null) return null;
142 
143         synchronized (sCarrierConfigs) {
144             if (sCarrierConfigs.containsKey(subId)) {
145                 return sCarrierConfigs.get(subId);
146             }
147             final PersistableBundle config = sCarrierConfigManager.getConfigForSubId(subId);
148             if (config == null) {
149                 Log.e(TAG, "Could not get carrier config, subId:" + subId);
150                 return null;
151             }
152             sCarrierConfigs.put(subId, config);
153             return config;
154         }
155     }
156 
157     /**
158      * Gets the Carrier-Configuration for the default subscription.
159      *
160      * @see #getConfigForSubId
161      */
getConfig()162     public PersistableBundle getConfig() {
163         if (sCarrierConfigManager == null) return null;
164 
165         return getConfigForSubId(SubscriptionManager.getDefaultSubscriptionId());
166     }
167 
168     private static class CarrierConfigChangeReceiver extends BroadcastReceiver {
169         @Override
onReceive(Context context, Intent intent)170         public void onReceive(Context context, Intent intent) {
171             if (!ACTION_CARRIER_CONFIG_CHANGED.equals(intent.getAction())) return;
172 
173             final int subId = intent.getIntExtra(EXTRA_SUBSCRIPTION_INDEX, INVALID_SUBSCRIPTION_ID);
174             synchronized (sCarrierConfigs) {
175                 if (SubscriptionManager.isValidSubscriptionId(subId)) {
176                     sCarrierConfigs.remove(subId);
177                 } else {
178                     sCarrierConfigs.clear();
179                 }
180             }
181         }
182     }
183 }
184