• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.TogglePreferenceController;
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     /**
69      * Get carrier config based on specific subscription id.
70      *
71      * @param subId is the subscription id
72      * @return {@link PersistableBundle} of carrier config, or {@code null} when carrier config
73      * is not available.
74      */
getCarrierConfigForSubId(int subId)75     public PersistableBundle getCarrierConfigForSubId(int subId) {
76         if (!SubscriptionManager.isValidSubscriptionId(subId)) {
77             return null;
78         }
79         final CarrierConfigManager carrierConfigMgr =
80                 mContext.getSystemService(CarrierConfigManager.class);
81         return carrierConfigMgr.getConfigForSubId(subId);
82     }
83 
84     /**
85      * Returns the resources associated with Subscription.
86      *
87      * @return Resources associated with Subscription.
88      */
getResourcesForSubId()89     public Resources getResourcesForSubId() {
90         return SubscriptionManager.getResourcesForSubId(mContext, mSubId);
91     }
92 }
93