1 /* 2 * Copyright (C) 2019 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.telephony; 18 19 import android.content.Context; 20 import android.content.res.Resources; 21 import android.os.PersistableBundle; 22 import android.telephony.CarrierConfigManager; 23 import android.telephony.SubscriptionManager; 24 25 import com.android.settings.core.BasePreferenceController; 26 27 import java.util.concurrent.atomic.AtomicInteger; 28 29 /** 30 * {@link BasePreferenceController} that used by all preferences that requires subscription id. 31 */ 32 public abstract class TelephonyBasePreferenceController extends BasePreferenceController 33 implements TelephonyAvailabilityCallback, TelephonyAvailabilityHandler { 34 protected int mSubId; 35 private AtomicInteger mAvailabilityStatus = new AtomicInteger(0); 36 private AtomicInteger mSetSessionCount = new AtomicInteger(0); 37 TelephonyBasePreferenceController(Context context, String preferenceKey)38 public TelephonyBasePreferenceController(Context context, String preferenceKey) { 39 super(context, preferenceKey); 40 mSubId = SubscriptionManager.INVALID_SUBSCRIPTION_ID; 41 } 42 43 @Override getAvailabilityStatus()44 public int getAvailabilityStatus() { 45 if (mSetSessionCount.get() <= 0) { 46 mAvailabilityStatus.set(MobileNetworkUtils 47 .getAvailability(mContext, mSubId, this::getAvailabilityStatus)); 48 } 49 return mAvailabilityStatus.get(); 50 } 51 52 @Override setAvailabilityStatus(int status)53 public void setAvailabilityStatus(int status) { 54 mAvailabilityStatus.set(status); 55 mSetSessionCount.getAndIncrement(); 56 } 57 58 @Override unsetAvailabilityStatus()59 public void unsetAvailabilityStatus() { 60 mSetSessionCount.getAndDecrement(); 61 } 62 63 /** 64 * Get carrier config based on specific subscription id. 65 * 66 * @param subId is the subscription id 67 * @return {@link PersistableBundle} of carrier config, or {@code null} when carrier config 68 * is not available. 69 */ getCarrierConfigForSubId(int subId)70 public PersistableBundle getCarrierConfigForSubId(int subId) { 71 if (!SubscriptionManager.isValidSubscriptionId(subId)) { 72 return null; 73 } 74 final CarrierConfigManager carrierConfigMgr = 75 mContext.getSystemService(CarrierConfigManager.class); 76 return carrierConfigMgr.getConfigForSubId(subId); 77 } 78 79 /** 80 * Returns the resources associated with Subscription. 81 * 82 * @return Resources associated with Subscription. 83 */ getResourcesForSubId()84 public Resources getResourcesForSubId() { 85 return SubscriptionManager.getResourcesForSubId(mContext, mSubId); 86 } 87 } 88