Loading src/com/android/settings/network/CarrierConfigCache.java 0 → 100644 +182 −0 Original line number Diff line number Diff line /* * Copyright (C) 2022 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.android.settings.network; import static android.telephony.CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED; import static android.telephony.SubscriptionManager.EXTRA_SUBSCRIPTION_INDEX; import static android.telephony.SubscriptionManager.INVALID_SUBSCRIPTION_ID; import android.annotation.NonNull; import android.annotation.TestApi; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.os.PersistableBundle; import android.telephony.CarrierConfigManager; import android.telephony.SubscriptionManager; import android.util.Log; import androidx.annotation.GuardedBy; import androidx.annotation.VisibleForTesting; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; /** * This is a singleton class for Carrier-Configuration cache. */ public class CarrierConfigCache { private static final String TAG = "CarrConfCache"; private static final Object sInstanceLock = new Object(); /** * A singleton {@link CarrierConfigCache} object is used to share with all sub-settings. */ @GuardedBy("sInstanceLock") private static CarrierConfigCache sInstance; @TestApi @GuardedBy("sInstanceLock") private static Map<Context, CarrierConfigCache> sTestInstances; /** * Manages mapping data from the subscription ID to the Carrier-Configuration * {@link PersistableBundle} object. * * The Carrier-Configurations are used to share with all sub-settings. */ @VisibleForTesting protected static final Map<Integer, PersistableBundle> sCarrierConfigs = new ConcurrentHashMap<>(); @VisibleForTesting protected static CarrierConfigManager sCarrierConfigManager; /** * Static method to create a singleton class for Carrier-Configuration cache. * * @param context The Context this is associated with. * @return an instance of {@link CarrierConfigCache} object. */ @NonNull public static CarrierConfigCache getInstance(@NonNull Context context) { synchronized (sInstanceLock) { if (sTestInstances != null && sTestInstances.containsKey(context)) { CarrierConfigCache testInstance = sTestInstances.get(context); Log.w(TAG, "The context owner try to use a test instance:" + testInstance); return testInstance; } if (sInstance != null) return sInstance; sInstance = new CarrierConfigCache(); final CarrierConfigChangeReceiver receiver = new CarrierConfigChangeReceiver(); final Context appContext = context.getApplicationContext(); sCarrierConfigManager = appContext.getSystemService(CarrierConfigManager.class); appContext.registerReceiver(receiver, new IntentFilter(ACTION_CARRIER_CONFIG_CHANGED)); return sInstance; } } /** * A convenience method to set pre-prepared instance or mock(CarrierConfigCache.class) for * testing. * * @param context The Context this is associated with. * @param instance of {@link CarrierConfigCache} object. * @hide */ @TestApi @VisibleForTesting public static void setTestInstance(@NonNull Context context, CarrierConfigCache instance) { synchronized (sInstanceLock) { if (sTestInstances == null) sTestInstances = new ConcurrentHashMap<>(); Log.w(TAG, "Try to set a test instance by context:" + context); sTestInstances.put(context, instance); } } /** * The constructor can only be accessed from static method inside the class itself, this is * to avoid creating a class by adding a private constructor. */ private CarrierConfigCache() { // Do nothing. } /** * Returns the boolean If the system service is successfully obtained. * * @return true value, if the system service is successfully obtained. */ public boolean hasCarrierConfigManager() { return (sCarrierConfigManager != null); } /** * Gets the Carrier-Configuration for a particular subscription, which is associated with a * specific SIM card. If an invalid subId is used, the returned config will contain default * values. * * @param subId the subscription ID, normally obtained from {@link SubscriptionManager}. * @return A {@link PersistableBundle} containing the config for the given subId, or default * values for an invalid subId. */ public PersistableBundle getConfigForSubId(int subId) { if (sCarrierConfigManager == null) return null; synchronized (sCarrierConfigs) { if (sCarrierConfigs.containsKey(subId)) { return sCarrierConfigs.get(subId); } final PersistableBundle config = sCarrierConfigManager.getConfigForSubId(subId); if (config == null) { Log.e(TAG, "Could not get carrier config, subId:" + subId); return null; } sCarrierConfigs.put(subId, config); return config; } } /** * Gets the Carrier-Configuration for the default subscription. * * @see #getConfigForSubId */ public PersistableBundle getConfig() { if (sCarrierConfigManager == null) return null; return getConfigForSubId(SubscriptionManager.getDefaultSubscriptionId()); } private static class CarrierConfigChangeReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if (!ACTION_CARRIER_CONFIG_CHANGED.equals(intent.getAction())) return; final int subId = intent.getIntExtra(EXTRA_SUBSCRIPTION_INDEX, INVALID_SUBSCRIPTION_ID); synchronized (sCarrierConfigs) { if (SubscriptionManager.isValidSubscriptionId(subId)) { sCarrierConfigs.remove(subId); } else { sCarrierConfigs.clear(); } } } } } src/com/android/settings/network/telephony/ApnPreferenceController.java +4 −3 Original line number Diff line number Diff line Loading @@ -32,6 +32,7 @@ import androidx.preference.Preference; import androidx.preference.PreferenceScreen; import com.android.settings.SettingsActivity; import com.android.settings.network.CarrierConfigCache; import com.android.settings.network.apn.ApnSettings; import com.android.settingslib.RestrictedLockUtilsInternal; import com.android.settingslib.RestrictedPreference; Loading @@ -46,19 +47,19 @@ public class ApnPreferenceController extends TelephonyBasePreferenceController i LifecycleObserver, OnStart, OnStop { @VisibleForTesting CarrierConfigManager mCarrierConfigManager; CarrierConfigCache mCarrierConfigCache; private Preference mPreference; private DpcApnEnforcedObserver mDpcApnEnforcedObserver; public ApnPreferenceController(Context context, String key) { super(context, key); mCarrierConfigManager = context.getSystemService(CarrierConfigManager.class); mCarrierConfigCache = CarrierConfigCache.getInstance(context); mDpcApnEnforcedObserver = new DpcApnEnforcedObserver(new Handler(Looper.getMainLooper())); } @Override public int getAvailabilityStatus(int subId) { final PersistableBundle carrierConfig = mCarrierConfigManager.getConfigForSubId(subId); final PersistableBundle carrierConfig = mCarrierConfigCache.getConfigForSubId(subId); final boolean isCdmaApn = MobileNetworkUtils.isCdmaOptions(mContext, subId) && carrierConfig != null && carrierConfig.getBoolean(CarrierConfigManager.KEY_SHOW_APN_SETTING_CDMA_BOOL); Loading src/com/android/settings/network/telephony/CarrierPreferenceController.java +6 −4 Original line number Diff line number Diff line Loading @@ -28,17 +28,19 @@ import android.telephony.SubscriptionManager; import androidx.annotation.VisibleForTesting; import androidx.preference.Preference; import com.android.settings.network.CarrierConfigCache; /** * Preference controller for "Carrier Settings" */ public class CarrierPreferenceController extends TelephonyBasePreferenceController { @VisibleForTesting CarrierConfigManager mCarrierConfigManager; CarrierConfigCache mCarrierConfigCache; public CarrierPreferenceController(Context context, String key) { super(context, key); mCarrierConfigManager = context.getSystemService(CarrierConfigManager.class); mCarrierConfigCache = CarrierConfigCache.getInstance(context); } public void init(int subId) { Loading @@ -47,7 +49,7 @@ public class CarrierPreferenceController extends TelephonyBasePreferenceControll @Override public int getAvailabilityStatus(int subId) { final PersistableBundle carrierConfig = mCarrierConfigManager.getConfigForSubId(subId); final PersistableBundle carrierConfig = mCarrierConfigCache.getConfigForSubId(subId); // Return available if it is in CDMA or GSM mode, and the flag is on return carrierConfig != null Loading @@ -72,7 +74,7 @@ public class CarrierPreferenceController extends TelephonyBasePreferenceControll } private Intent getCarrierSettingsActivityIntent(int subId) { final PersistableBundle config = mCarrierConfigManager.getConfigForSubId(subId); final PersistableBundle config = mCarrierConfigCache.getConfigForSubId(subId); final ComponentName cn = ComponentName.unflattenFromString( config == null ? "" : config.getString( CarrierConfigManager.KEY_CARRIER_SETTINGS_ACTIVITY_COMPONENT_NAME_STRING, Loading src/com/android/settings/network/telephony/CarrierSettingsVersionPreferenceController.java +4 −3 Original line number Diff line number Diff line Loading @@ -23,15 +23,16 @@ import android.telephony.SubscriptionManager; import android.text.TextUtils; import com.android.settings.core.BasePreferenceController; import com.android.settings.network.CarrierConfigCache; public class CarrierSettingsVersionPreferenceController extends BasePreferenceController { private int mSubscriptionId; private CarrierConfigManager mManager; private CarrierConfigCache mCarrierConfigCache; public CarrierSettingsVersionPreferenceController(Context context, String preferenceKey) { super(context, preferenceKey); mManager = context.getSystemService(CarrierConfigManager.class); mCarrierConfigCache = CarrierConfigCache.getInstance(context); mSubscriptionId = SubscriptionManager.INVALID_SUBSCRIPTION_ID; } Loading @@ -41,7 +42,7 @@ public class CarrierSettingsVersionPreferenceController extends BasePreferenceCo @Override public CharSequence getSummary() { final PersistableBundle config = mManager.getConfigForSubId(mSubscriptionId); final PersistableBundle config = mCarrierConfigCache.getConfigForSubId(mSubscriptionId); if (config == null) { return null; } Loading src/com/android/settings/network/telephony/ContactDiscoveryPreferenceController.java +4 −3 Original line number Diff line number Diff line Loading @@ -35,6 +35,7 @@ import androidx.preference.Preference; import androidx.preference.PreferenceScreen; import androidx.preference.SwitchPreference; import com.android.settings.network.CarrierConfigCache; import com.android.settings.network.SubscriptionUtil; /** Loading @@ -47,7 +48,7 @@ public class ContactDiscoveryPreferenceController extends TelephonyTogglePrefere Telephony.SimInfo.COLUMN_IMS_RCS_UCE_ENABLED); private ImsManager mImsManager; private CarrierConfigManager mCarrierConfigManager; private CarrierConfigCache mCarrierConfigCache; private ContentObserver mUceSettingObserver; private FragmentManager mFragmentManager; Loading @@ -57,7 +58,7 @@ public class ContactDiscoveryPreferenceController extends TelephonyTogglePrefere public ContactDiscoveryPreferenceController(Context context, String key) { super(context, key); mImsManager = mContext.getSystemService(ImsManager.class); mCarrierConfigManager = mContext.getSystemService(CarrierConfigManager.class); mCarrierConfigCache = CarrierConfigCache.getInstance(context); } public ContactDiscoveryPreferenceController init(FragmentManager fragmentManager, int subId, Loading Loading @@ -96,7 +97,7 @@ public class ContactDiscoveryPreferenceController extends TelephonyTogglePrefere @Override public int getAvailabilityStatus(int subId) { PersistableBundle bundle = mCarrierConfigManager.getConfigForSubId(subId); PersistableBundle bundle = mCarrierConfigCache.getConfigForSubId(subId); boolean shouldShowPresence = bundle != null && (bundle.getBoolean( CarrierConfigManager.KEY_USE_RCS_PRESENCE_BOOL, false /*default*/) Loading Loading
src/com/android/settings/network/CarrierConfigCache.java 0 → 100644 +182 −0 Original line number Diff line number Diff line /* * Copyright (C) 2022 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.android.settings.network; import static android.telephony.CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED; import static android.telephony.SubscriptionManager.EXTRA_SUBSCRIPTION_INDEX; import static android.telephony.SubscriptionManager.INVALID_SUBSCRIPTION_ID; import android.annotation.NonNull; import android.annotation.TestApi; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.os.PersistableBundle; import android.telephony.CarrierConfigManager; import android.telephony.SubscriptionManager; import android.util.Log; import androidx.annotation.GuardedBy; import androidx.annotation.VisibleForTesting; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; /** * This is a singleton class for Carrier-Configuration cache. */ public class CarrierConfigCache { private static final String TAG = "CarrConfCache"; private static final Object sInstanceLock = new Object(); /** * A singleton {@link CarrierConfigCache} object is used to share with all sub-settings. */ @GuardedBy("sInstanceLock") private static CarrierConfigCache sInstance; @TestApi @GuardedBy("sInstanceLock") private static Map<Context, CarrierConfigCache> sTestInstances; /** * Manages mapping data from the subscription ID to the Carrier-Configuration * {@link PersistableBundle} object. * * The Carrier-Configurations are used to share with all sub-settings. */ @VisibleForTesting protected static final Map<Integer, PersistableBundle> sCarrierConfigs = new ConcurrentHashMap<>(); @VisibleForTesting protected static CarrierConfigManager sCarrierConfigManager; /** * Static method to create a singleton class for Carrier-Configuration cache. * * @param context The Context this is associated with. * @return an instance of {@link CarrierConfigCache} object. */ @NonNull public static CarrierConfigCache getInstance(@NonNull Context context) { synchronized (sInstanceLock) { if (sTestInstances != null && sTestInstances.containsKey(context)) { CarrierConfigCache testInstance = sTestInstances.get(context); Log.w(TAG, "The context owner try to use a test instance:" + testInstance); return testInstance; } if (sInstance != null) return sInstance; sInstance = new CarrierConfigCache(); final CarrierConfigChangeReceiver receiver = new CarrierConfigChangeReceiver(); final Context appContext = context.getApplicationContext(); sCarrierConfigManager = appContext.getSystemService(CarrierConfigManager.class); appContext.registerReceiver(receiver, new IntentFilter(ACTION_CARRIER_CONFIG_CHANGED)); return sInstance; } } /** * A convenience method to set pre-prepared instance or mock(CarrierConfigCache.class) for * testing. * * @param context The Context this is associated with. * @param instance of {@link CarrierConfigCache} object. * @hide */ @TestApi @VisibleForTesting public static void setTestInstance(@NonNull Context context, CarrierConfigCache instance) { synchronized (sInstanceLock) { if (sTestInstances == null) sTestInstances = new ConcurrentHashMap<>(); Log.w(TAG, "Try to set a test instance by context:" + context); sTestInstances.put(context, instance); } } /** * The constructor can only be accessed from static method inside the class itself, this is * to avoid creating a class by adding a private constructor. */ private CarrierConfigCache() { // Do nothing. } /** * Returns the boolean If the system service is successfully obtained. * * @return true value, if the system service is successfully obtained. */ public boolean hasCarrierConfigManager() { return (sCarrierConfigManager != null); } /** * Gets the Carrier-Configuration for a particular subscription, which is associated with a * specific SIM card. If an invalid subId is used, the returned config will contain default * values. * * @param subId the subscription ID, normally obtained from {@link SubscriptionManager}. * @return A {@link PersistableBundle} containing the config for the given subId, or default * values for an invalid subId. */ public PersistableBundle getConfigForSubId(int subId) { if (sCarrierConfigManager == null) return null; synchronized (sCarrierConfigs) { if (sCarrierConfigs.containsKey(subId)) { return sCarrierConfigs.get(subId); } final PersistableBundle config = sCarrierConfigManager.getConfigForSubId(subId); if (config == null) { Log.e(TAG, "Could not get carrier config, subId:" + subId); return null; } sCarrierConfigs.put(subId, config); return config; } } /** * Gets the Carrier-Configuration for the default subscription. * * @see #getConfigForSubId */ public PersistableBundle getConfig() { if (sCarrierConfigManager == null) return null; return getConfigForSubId(SubscriptionManager.getDefaultSubscriptionId()); } private static class CarrierConfigChangeReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if (!ACTION_CARRIER_CONFIG_CHANGED.equals(intent.getAction())) return; final int subId = intent.getIntExtra(EXTRA_SUBSCRIPTION_INDEX, INVALID_SUBSCRIPTION_ID); synchronized (sCarrierConfigs) { if (SubscriptionManager.isValidSubscriptionId(subId)) { sCarrierConfigs.remove(subId); } else { sCarrierConfigs.clear(); } } } } }
src/com/android/settings/network/telephony/ApnPreferenceController.java +4 −3 Original line number Diff line number Diff line Loading @@ -32,6 +32,7 @@ import androidx.preference.Preference; import androidx.preference.PreferenceScreen; import com.android.settings.SettingsActivity; import com.android.settings.network.CarrierConfigCache; import com.android.settings.network.apn.ApnSettings; import com.android.settingslib.RestrictedLockUtilsInternal; import com.android.settingslib.RestrictedPreference; Loading @@ -46,19 +47,19 @@ public class ApnPreferenceController extends TelephonyBasePreferenceController i LifecycleObserver, OnStart, OnStop { @VisibleForTesting CarrierConfigManager mCarrierConfigManager; CarrierConfigCache mCarrierConfigCache; private Preference mPreference; private DpcApnEnforcedObserver mDpcApnEnforcedObserver; public ApnPreferenceController(Context context, String key) { super(context, key); mCarrierConfigManager = context.getSystemService(CarrierConfigManager.class); mCarrierConfigCache = CarrierConfigCache.getInstance(context); mDpcApnEnforcedObserver = new DpcApnEnforcedObserver(new Handler(Looper.getMainLooper())); } @Override public int getAvailabilityStatus(int subId) { final PersistableBundle carrierConfig = mCarrierConfigManager.getConfigForSubId(subId); final PersistableBundle carrierConfig = mCarrierConfigCache.getConfigForSubId(subId); final boolean isCdmaApn = MobileNetworkUtils.isCdmaOptions(mContext, subId) && carrierConfig != null && carrierConfig.getBoolean(CarrierConfigManager.KEY_SHOW_APN_SETTING_CDMA_BOOL); Loading
src/com/android/settings/network/telephony/CarrierPreferenceController.java +6 −4 Original line number Diff line number Diff line Loading @@ -28,17 +28,19 @@ import android.telephony.SubscriptionManager; import androidx.annotation.VisibleForTesting; import androidx.preference.Preference; import com.android.settings.network.CarrierConfigCache; /** * Preference controller for "Carrier Settings" */ public class CarrierPreferenceController extends TelephonyBasePreferenceController { @VisibleForTesting CarrierConfigManager mCarrierConfigManager; CarrierConfigCache mCarrierConfigCache; public CarrierPreferenceController(Context context, String key) { super(context, key); mCarrierConfigManager = context.getSystemService(CarrierConfigManager.class); mCarrierConfigCache = CarrierConfigCache.getInstance(context); } public void init(int subId) { Loading @@ -47,7 +49,7 @@ public class CarrierPreferenceController extends TelephonyBasePreferenceControll @Override public int getAvailabilityStatus(int subId) { final PersistableBundle carrierConfig = mCarrierConfigManager.getConfigForSubId(subId); final PersistableBundle carrierConfig = mCarrierConfigCache.getConfigForSubId(subId); // Return available if it is in CDMA or GSM mode, and the flag is on return carrierConfig != null Loading @@ -72,7 +74,7 @@ public class CarrierPreferenceController extends TelephonyBasePreferenceControll } private Intent getCarrierSettingsActivityIntent(int subId) { final PersistableBundle config = mCarrierConfigManager.getConfigForSubId(subId); final PersistableBundle config = mCarrierConfigCache.getConfigForSubId(subId); final ComponentName cn = ComponentName.unflattenFromString( config == null ? "" : config.getString( CarrierConfigManager.KEY_CARRIER_SETTINGS_ACTIVITY_COMPONENT_NAME_STRING, Loading
src/com/android/settings/network/telephony/CarrierSettingsVersionPreferenceController.java +4 −3 Original line number Diff line number Diff line Loading @@ -23,15 +23,16 @@ import android.telephony.SubscriptionManager; import android.text.TextUtils; import com.android.settings.core.BasePreferenceController; import com.android.settings.network.CarrierConfigCache; public class CarrierSettingsVersionPreferenceController extends BasePreferenceController { private int mSubscriptionId; private CarrierConfigManager mManager; private CarrierConfigCache mCarrierConfigCache; public CarrierSettingsVersionPreferenceController(Context context, String preferenceKey) { super(context, preferenceKey); mManager = context.getSystemService(CarrierConfigManager.class); mCarrierConfigCache = CarrierConfigCache.getInstance(context); mSubscriptionId = SubscriptionManager.INVALID_SUBSCRIPTION_ID; } Loading @@ -41,7 +42,7 @@ public class CarrierSettingsVersionPreferenceController extends BasePreferenceCo @Override public CharSequence getSummary() { final PersistableBundle config = mManager.getConfigForSubId(mSubscriptionId); final PersistableBundle config = mCarrierConfigCache.getConfigForSubId(mSubscriptionId); if (config == null) { return null; } Loading
src/com/android/settings/network/telephony/ContactDiscoveryPreferenceController.java +4 −3 Original line number Diff line number Diff line Loading @@ -35,6 +35,7 @@ import androidx.preference.Preference; import androidx.preference.PreferenceScreen; import androidx.preference.SwitchPreference; import com.android.settings.network.CarrierConfigCache; import com.android.settings.network.SubscriptionUtil; /** Loading @@ -47,7 +48,7 @@ public class ContactDiscoveryPreferenceController extends TelephonyTogglePrefere Telephony.SimInfo.COLUMN_IMS_RCS_UCE_ENABLED); private ImsManager mImsManager; private CarrierConfigManager mCarrierConfigManager; private CarrierConfigCache mCarrierConfigCache; private ContentObserver mUceSettingObserver; private FragmentManager mFragmentManager; Loading @@ -57,7 +58,7 @@ public class ContactDiscoveryPreferenceController extends TelephonyTogglePrefere public ContactDiscoveryPreferenceController(Context context, String key) { super(context, key); mImsManager = mContext.getSystemService(ImsManager.class); mCarrierConfigManager = mContext.getSystemService(CarrierConfigManager.class); mCarrierConfigCache = CarrierConfigCache.getInstance(context); } public ContactDiscoveryPreferenceController init(FragmentManager fragmentManager, int subId, Loading Loading @@ -96,7 +97,7 @@ public class ContactDiscoveryPreferenceController extends TelephonyTogglePrefere @Override public int getAvailabilityStatus(int subId) { PersistableBundle bundle = mCarrierConfigManager.getConfigForSubId(subId); PersistableBundle bundle = mCarrierConfigCache.getConfigForSubId(subId); boolean shouldShowPresence = bundle != null && (bundle.getBoolean( CarrierConfigManager.KEY_USE_RCS_PRESENCE_BOOL, false /*default*/) Loading