• 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.car.developeroptions.network.telephony.cdma;
18 
19 import android.content.Context;
20 import android.os.SystemProperties;
21 import android.provider.Settings;
22 import android.text.TextUtils;
23 
24 import androidx.annotation.VisibleForTesting;
25 import androidx.preference.ListPreference;
26 import androidx.preference.Preference;
27 
28 import com.android.internal.telephony.Phone;
29 import com.android.car.developeroptions.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, Phone.PREFERRED_CDMA_SUBSCRIPTION);
60         if (mode != Phone.CDMA_SUBSCRIPTION_UNKNOWN) {
61             listPreference.setValue(Integer.toString(mode));
62         }
63     }
64 
65     @Override
onPreferenceChange(Preference preference, Object object)66     public boolean onPreferenceChange(Preference preference, Object object) {
67         final int newMode = Integer.parseInt((String) object);
68         //TODO(b/117611981): only set it in one place
69         if (mTelephonyManager.setCdmaSubscriptionMode(newMode)) {
70             Settings.Global.putInt(mContext.getContentResolver(),
71                     Settings.Global.CDMA_SUBSCRIPTION_MODE, newMode);
72             return true;
73         }
74 
75         return false;
76     }
77 
78     @VisibleForTesting
deviceSupportsNvAndRuim()79     boolean deviceSupportsNvAndRuim() {
80         // retrieve the list of subscription types supported by device.
81         final String subscriptionsSupported = SystemProperties.get("ril.subscription.types");
82         boolean nvSupported = false;
83         boolean ruimSupported = false;
84 
85         if (!TextUtils.isEmpty(subscriptionsSupported)) {
86             // Searches through the comma-separated list for a match for "NV"
87             // and "RUIM" to update nvSupported and ruimSupported.
88             for (String subscriptionType : subscriptionsSupported.split(",")) {
89                 subscriptionType = subscriptionType.trim();
90                 if (subscriptionType.equalsIgnoreCase(TYPE_NV)) {
91                     nvSupported = true;
92                 } else if (subscriptionType.equalsIgnoreCase(TYPE_RUIM)) {
93                     ruimSupported = true;
94                 }
95             }
96         }
97 
98         return (nvSupported && ruimSupported);
99     }
100 }
101