• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.NonNull;
24 import android.annotation.TestApi;
25 import android.content.BroadcastReceiver;
26 import android.content.Context;
27 import android.content.Intent;
28 import android.content.IntentFilter;
29 import android.os.PersistableBundle;
30 import android.telephony.CarrierConfigManager;
31 import android.telephony.SubscriptionManager;
32 import android.util.Log;
33 
34 import androidx.annotation.GuardedBy;
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             return sInstance;
91         }
92     }
93 
94     /**
95      * A convenience method to set pre-prepared instance or mock(CarrierConfigCache.class) for
96      * testing.
97      *
98      * @param context The Context this is associated with.
99      * @param instance of {@link CarrierConfigCache} object.
100      * @hide
101      */
102     @TestApi
103     @VisibleForTesting
setTestInstance(@onNull Context context, CarrierConfigCache instance)104     public static void setTestInstance(@NonNull Context context, CarrierConfigCache instance) {
105         synchronized (sInstanceLock) {
106             if (sTestInstances == null) sTestInstances = new ConcurrentHashMap<>();
107 
108             Log.w(TAG, "Try to set a test instance by context:" + context);
109             sTestInstances.put(context, instance);
110         }
111     }
112 
113     /**
114      * The constructor can only be accessed from static method inside the class itself, this is
115      * to avoid creating a class by adding a private constructor.
116      */
CarrierConfigCache()117     private CarrierConfigCache() {
118         // Do nothing.
119     }
120 
121     /**
122      * Returns the boolean If the system service is successfully obtained.
123      *
124      * @return true value, if the system service is successfully obtained.
125      */
hasCarrierConfigManager()126     public boolean hasCarrierConfigManager() {
127         return (sCarrierConfigManager != null);
128     }
129 
130     /**
131      * Gets the Carrier-Configuration for a particular subscription, which is associated with a
132      * specific SIM card. If an invalid subId is used, the returned config will contain default
133      * values.
134      *
135      * @param subId the subscription ID, normally obtained from {@link SubscriptionManager}.
136      * @return A {@link PersistableBundle} containing the config for the given subId, or default
137      * values for an invalid subId.
138      */
getConfigForSubId(int subId)139     public PersistableBundle getConfigForSubId(int subId) {
140         if (sCarrierConfigManager == null) return null;
141 
142         synchronized (sCarrierConfigs) {
143             if (sCarrierConfigs.containsKey(subId)) {
144                 return sCarrierConfigs.get(subId);
145             }
146             final PersistableBundle config = sCarrierConfigManager.getConfigForSubId(subId);
147             if (config == null) {
148                 Log.e(TAG, "Could not get carrier config, subId:" + subId);
149                 return null;
150             }
151             sCarrierConfigs.put(subId, config);
152             return config;
153         }
154     }
155 
156     /**
157      * Gets the Carrier-Configuration for the default subscription.
158      *
159      * @see #getConfigForSubId
160      */
getConfig()161     public PersistableBundle getConfig() {
162         if (sCarrierConfigManager == null) return null;
163 
164         return getConfigForSubId(SubscriptionManager.getDefaultSubscriptionId());
165     }
166 
167     private static class CarrierConfigChangeReceiver extends BroadcastReceiver {
168         @Override
onReceive(Context context, Intent intent)169         public void onReceive(Context context, Intent intent) {
170             if (!ACTION_CARRIER_CONFIG_CHANGED.equals(intent.getAction())) return;
171 
172             final int subId = intent.getIntExtra(EXTRA_SUBSCRIPTION_INDEX, INVALID_SUBSCRIPTION_ID);
173             synchronized (sCarrierConfigs) {
174                 if (SubscriptionManager.isValidSubscriptionId(subId)) {
175                     sCarrierConfigs.remove(subId);
176                 } else {
177                     sCarrierConfigs.clear();
178                 }
179             }
180         }
181     }
182 }
183