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.SubscriptionManager; 23 24 import com.android.settings.core.TogglePreferenceController; 25 import com.android.settings.network.CarrierConfigCache; 26 27 import java.util.concurrent.atomic.AtomicInteger; 28 29 /** 30 * {@link TogglePreferenceController} that used by all preferences that requires subscription id. 31 */ 32 public abstract class TelephonyTogglePreferenceController extends TogglePreferenceController 33 implements TelephonyAvailabilityCallback, TelephonyAvailabilityHandler { 34 protected int mSubId; 35 private AtomicInteger mAvailabilityStatus = new AtomicInteger(0); 36 private AtomicInteger mSetSessionCount = new AtomicInteger(0); 37 TelephonyTogglePreferenceController(Context context, String preferenceKey)38 public TelephonyTogglePreferenceController(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 @Override isSliceable()64 public boolean isSliceable() { 65 return false; 66 } 67 68 @Override getSliceHighlightMenuRes()69 public int getSliceHighlightMenuRes() { 70 // not needed since it's not sliceable 71 return NO_RES; 72 } 73 74 /** 75 * Get carrier config based on specific subscription id. 76 * 77 * @param subId is the subscription id 78 * @return {@link PersistableBundle} of carrier config, or {@code null} when carrier config 79 * is not available. 80 */ getCarrierConfigForSubId(int subId)81 public PersistableBundle getCarrierConfigForSubId(int subId) { 82 if (!SubscriptionManager.isValidSubscriptionId(subId)) { 83 return null; 84 } 85 return CarrierConfigCache.getInstance(mContext).getConfigForSubId(subId); 86 } 87 88 /** 89 * Returns the resources associated with Subscription. 90 * 91 * @return Resources associated with Subscription. 92 */ getResourcesForSubId()93 public Resources getResourcesForSubId() { 94 return SubscriptionManager.getResourcesForSubId(mContext, mSubId); 95 } 96 } 97