• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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.cdma;
18 
19 import android.content.Context;
20 import android.os.SystemProperties;
21 import android.provider.Settings;
22 import android.telephony.TelephonyManager;
23 import android.text.TextUtils;
24 
25 import androidx.annotation.VisibleForTesting;
26 import androidx.preference.ListPreference;
27 import androidx.preference.Preference;
28 
29 import com.android.settings.network.telephony.MobileNetworkUtils;
30 
31 /**
32  * Preference controller for "CDMA subscription"
33  */
34 public class CdmaSubscriptionPreferenceController extends CdmaBasePreferenceController
35         implements ListPreference.OnPreferenceChangeListener {
36     private static final String TYPE_NV = "NV";
37     private static final String TYPE_RUIM = "RUIM";
38 
39     @VisibleForTesting
40     ListPreference mPreference;
41 
CdmaSubscriptionPreferenceController(Context context, String key)42     public CdmaSubscriptionPreferenceController(Context context, String key) {
43         super(context, key);
44     }
45 
46     @Override
getAvailabilityStatus(int subId)47     public int getAvailabilityStatus(int subId) {
48         return MobileNetworkUtils.isCdmaOptions(mContext, subId) && deviceSupportsNvAndRuim()
49                 ? AVAILABLE
50                 : CONDITIONALLY_UNAVAILABLE;
51     }
52 
53     @Override
updateState(Preference preference)54     public void updateState(Preference preference) {
55         super.updateState(preference);
56         final ListPreference listPreference = (ListPreference) preference;
57         listPreference.setVisible(getAvailabilityStatus() == AVAILABLE);
58         final int mode = Settings.Global.getInt(mContext.getContentResolver(),
59                 Settings.Global.CDMA_SUBSCRIPTION_MODE,
60                 TelephonyManager.CDMA_SUBSCRIPTION_RUIM_SIM);
61         if (mode != TelephonyManager.CDMA_SUBSCRIPTION_UNKNOWN) {
62             listPreference.setValue(Integer.toString(mode));
63         }
64     }
65 
66     @Override
onPreferenceChange(Preference preference, Object object)67     public boolean onPreferenceChange(Preference preference, Object object) {
68         final int newMode = Integer.parseInt((String) object);
69         //TODO(b/117611981): only set it in one place
70         try {
71             mTelephonyManager.setCdmaSubscriptionMode(newMode);
72             Settings.Global.putInt(mContext.getContentResolver(),
73                     Settings.Global.CDMA_SUBSCRIPTION_MODE, newMode);
74             return true;
75         } catch (IllegalStateException e) {
76             return false;
77         }
78     }
79 
80     @VisibleForTesting
deviceSupportsNvAndRuim()81     boolean deviceSupportsNvAndRuim() {
82         // retrieve the list of subscription types supported by device.
83         final String subscriptionsSupported = getRilSubscriptionTypes();
84         boolean nvSupported = false;
85         boolean ruimSupported = false;
86 
87         if (!TextUtils.isEmpty(subscriptionsSupported)) {
88             // Searches through the comma-separated list for a match for "NV"
89             // and "RUIM" to update nvSupported and ruimSupported.
90             for (String subscriptionType : subscriptionsSupported.split(",")) {
91                 subscriptionType = subscriptionType.trim();
92                 if (subscriptionType.equalsIgnoreCase(TYPE_NV)) {
93                     nvSupported = true;
94                 } else if (subscriptionType.equalsIgnoreCase(TYPE_RUIM)) {
95                     ruimSupported = true;
96                 }
97             }
98         }
99 
100         return (nvSupported && ruimSupported);
101     }
102 
103     @VisibleForTesting
getRilSubscriptionTypes()104     protected String getRilSubscriptionTypes() {
105         return SystemProperties.get("ril.subscription.types");
106     }
107 }
108