• 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.car.settings.system;
18 
19 import android.car.drivingstate.CarUxRestrictions;
20 import android.content.Context;
21 import android.content.pm.PackageManager;
22 import android.telephony.SubscriptionInfo;
23 import android.telephony.SubscriptionManager;
24 import android.text.TextUtils;
25 
26 import androidx.preference.ListPreference;
27 
28 import com.android.car.settings.R;
29 import com.android.car.settings.common.FragmentController;
30 import com.android.car.settings.common.PreferenceController;
31 
32 import java.util.ArrayList;
33 import java.util.List;
34 
35 /**
36  * Controller which determines if a network selection option is visible. On devices with multiple
37  * network subscriptions, a user may select the network to reset.
38  */
39 public class ResetNetworkSubscriptionPreferenceController extends
40         PreferenceController<ListPreference> {
41 
42     private final SubscriptionManager mSubscriptionManager;
43 
ResetNetworkSubscriptionPreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)44     public ResetNetworkSubscriptionPreferenceController(Context context, String preferenceKey,
45             FragmentController fragmentController, CarUxRestrictions uxRestrictions) {
46         super(context, preferenceKey, fragmentController, uxRestrictions);
47         mSubscriptionManager = context.getSystemService(SubscriptionManager.class);
48     }
49 
50     @Override
getPreferenceType()51     protected Class<ListPreference> getPreferenceType() {
52         return ListPreference.class;
53     }
54 
55     @Override
getAvailabilityStatus()56     public int getAvailabilityStatus() {
57         return getContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_TELEPHONY)
58                 ? AVAILABLE : UNSUPPORTED_ON_DEVICE;
59     }
60 
61     @Override
updateState(ListPreference preference)62     protected void updateState(ListPreference preference) {
63         List<SubscriptionInfo> subscriptions = mSubscriptionManager.getActiveSubscriptionInfoList();
64         if (subscriptions == null || subscriptions.isEmpty()) {
65             // No subscriptions to reset.
66             preference.setValue(String.valueOf(SubscriptionManager.INVALID_SUBSCRIPTION_ID));
67             preference.setVisible(false);
68             return;
69         }
70         if (subscriptions.size() == 1) {
71             // Only one subscription, so nothing else to select. Use it and hide the preference.
72             preference.setValue(String.valueOf(subscriptions.get(0).getSubscriptionId()));
73             preference.setVisible(false);
74             return;
75         }
76 
77         int defaultSubscriptionId = getDefaultSubscriptionId();
78         int selectedIndex = 0;
79         int size = subscriptions.size();
80         List<String> subscriptionNames = new ArrayList<>(size);
81         List<String> subscriptionIds = new ArrayList<>(size);
82         for (int i = 0; i < size; i++) {
83             SubscriptionInfo subscription = subscriptions.get(i);
84             int subscriptionId = subscription.getSubscriptionId();
85             if (subscriptionId == defaultSubscriptionId) {
86                 // Set the default as the first selected value.
87                 selectedIndex = i;
88             }
89             subscriptionNames.add(getSubscriptionName(subscription));
90             subscriptionIds.add(String.valueOf(subscriptionId));
91         }
92 
93         preference.setEntries(toCharSequenceArray(subscriptionNames));
94         preference.setEntryValues(toCharSequenceArray(subscriptionIds));
95         preference.setTitle(subscriptionNames.get(selectedIndex));
96         preference.setValueIndex(selectedIndex);
97     }
98 
99     @Override
handlePreferenceChanged(ListPreference preference, Object newValue)100     protected boolean handlePreferenceChanged(ListPreference preference, Object newValue) {
101         String subscriptionIdStr = (String) newValue;
102         int index = preference.findIndexOfValue(subscriptionIdStr);
103         CharSequence subscriptionName = preference.getEntries()[index];
104         preference.setTitle(subscriptionName);
105         return true;
106     }
107 
108     /**
109      * Returns the default subscription id in the order of data, voice, sms, system subscription.
110      */
getDefaultSubscriptionId()111     private int getDefaultSubscriptionId() {
112         int defaultSubscriptionId = SubscriptionManager.getDefaultDataSubscriptionId();
113         if (!SubscriptionManager.isUsableSubIdValue(defaultSubscriptionId)) {
114             defaultSubscriptionId = SubscriptionManager.getDefaultVoiceSubscriptionId();
115         }
116         if (!SubscriptionManager.isUsableSubIdValue(defaultSubscriptionId)) {
117             defaultSubscriptionId = SubscriptionManager.getDefaultSmsSubscriptionId();
118         }
119         if (!SubscriptionManager.isUsableSubIdValue(defaultSubscriptionId)) {
120             defaultSubscriptionId = SubscriptionManager.getDefaultSubscriptionId();
121         }
122         return defaultSubscriptionId;
123     }
124 
125     /**
126      * Returns the subscription display name falling back to the number, the carrier, and then
127      * network id codes.
128      */
getSubscriptionName(SubscriptionInfo subscription)129     private String getSubscriptionName(SubscriptionInfo subscription) {
130         String name = subscription.getDisplayName().toString();
131         if (TextUtils.isEmpty(name)) {
132             name = subscription.getNumber();
133         }
134         if (TextUtils.isEmpty(name)) {
135             name = subscription.getCarrierName().toString();
136         }
137         if (TextUtils.isEmpty(name)) {
138             name = getContext().getString(R.string.reset_network_fallback_subscription_name,
139                     subscription.getMcc(), subscription.getMnc(), subscription.getSimSlotIndex(),
140                     subscription.getSubscriptionId());
141         }
142         return name;
143     }
144 
toCharSequenceArray(List<String> list)145     private CharSequence[] toCharSequenceArray(List<String> list) {
146         CharSequence[] array = new CharSequence[list.size()];
147         list.toArray(array);
148         return array;
149     }
150 }
151