• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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.deviceinfo;
18 
19 import android.content.Context;
20 import android.telephony.SubscriptionInfo;
21 import android.telephony.SubscriptionManager;
22 import android.telephony.TelephonyManager;
23 import android.text.TextUtils;
24 
25 import androidx.annotation.VisibleForTesting;
26 import androidx.preference.Preference;
27 import androidx.preference.PreferenceCategory;
28 import androidx.preference.PreferenceScreen;
29 
30 import com.android.settings.R;
31 import com.android.settings.core.BasePreferenceController;
32 import com.android.settings.network.SubscriptionUtil;
33 
34 import java.util.ArrayList;
35 import java.util.List;
36 
37 public class PhoneNumberPreferenceController extends BasePreferenceController {
38 
39     private static final String KEY_PHONE_NUMBER = "phone_number";
40     private static final String KEY_PREFERENCE_CATEGORY = "basic_info_category";
41 
42     private final TelephonyManager mTelephonyManager;
43     private final SubscriptionManager mSubscriptionManager;
44     private final List<Preference> mPreferenceList = new ArrayList<>();
45 
PhoneNumberPreferenceController(Context context, String key)46     public PhoneNumberPreferenceController(Context context, String key) {
47         super(context, key);
48         mTelephonyManager = mContext.getSystemService(TelephonyManager.class);
49         mSubscriptionManager = mContext.getSystemService(SubscriptionManager.class);
50     }
51 
52     @Override
getAvailabilityStatus()53     public int getAvailabilityStatus() {
54         return SubscriptionUtil.isSimHardwareVisible(mContext) ?
55                 AVAILABLE : UNSUPPORTED_ON_DEVICE;
56     }
57 
58     @Override
getSummary()59     public CharSequence getSummary() {
60         return mContext.getString(R.string.device_info_protected_single_press);
61     }
62 
63     @Override
handlePreferenceTreeClick(Preference preference)64     public boolean handlePreferenceTreeClick(Preference preference) {
65         String prefKey = preference.getKey();
66         if (prefKey.startsWith(KEY_PHONE_NUMBER)) {
67             int simSlotNumber = 0;
68             if (!TextUtils.equals(prefKey, KEY_PHONE_NUMBER)) {
69                 // Get multisim slot number from preference key.
70                 // Multisim preference key is KEY_PHONE_NUMBER + simSlotNumber
71                 simSlotNumber = Integer.parseInt(
72                         prefKey.replaceAll("[^0-9]", ""));
73             }
74             final Preference simStatusPreference = mPreferenceList.get(simSlotNumber);
75             simStatusPreference.setSummary(getPhoneNumber(simSlotNumber));
76         }
77         return super.handlePreferenceTreeClick(preference);
78     }
79 
80     @Override
displayPreference(PreferenceScreen screen)81     public void displayPreference(PreferenceScreen screen) {
82         super.displayPreference(screen);
83         if (!SubscriptionUtil.isSimHardwareVisible(mContext)) {
84             return;
85         }
86         final Preference preference = screen.findPreference(getPreferenceKey());
87         final PreferenceCategory category = screen.findPreference(KEY_PREFERENCE_CATEGORY);
88         mPreferenceList.add(preference);
89 
90         final int phonePreferenceOrder = preference.getOrder();
91         // Add additional preferences for each sim in the device
92         for (int simSlotNumber = 1; simSlotNumber < mTelephonyManager.getPhoneCount();
93                 simSlotNumber++) {
94             final Preference multiSimPreference = createNewPreference(screen.getContext());
95             multiSimPreference.setCopyingEnabled(true);
96             multiSimPreference.setOrder(phonePreferenceOrder + simSlotNumber);
97             multiSimPreference.setKey(KEY_PHONE_NUMBER + simSlotNumber);
98             category.addPreference(multiSimPreference);
99             mPreferenceList.add(multiSimPreference);
100         }
101     }
102 
103     @Override
updateState(Preference preference)104     public void updateState(Preference preference) {
105         for (int simSlotNumber = 0; simSlotNumber < mPreferenceList.size(); simSlotNumber++) {
106             final Preference simStatusPreference = mPreferenceList.get(simSlotNumber);
107             simStatusPreference.setTitle(getPreferenceTitle(simSlotNumber));
108             simStatusPreference.setSummary(getSummary());
109         }
110     }
111 
112     @Override
useDynamicSliceSummary()113     public boolean useDynamicSliceSummary() {
114         return true;
115     }
116 
getFirstPhoneNumber()117     private CharSequence getFirstPhoneNumber() {
118         final List<SubscriptionInfo> subscriptionInfoList =
119                 mSubscriptionManager.getActiveSubscriptionInfoList();
120         if (subscriptionInfoList == null || subscriptionInfoList.isEmpty()) {
121             return mContext.getText(R.string.device_info_default);
122         }
123 
124         // For now, We only return first result for slice view.
125         return getFormattedPhoneNumber(subscriptionInfoList.get(0));
126     }
127 
getPhoneNumber(int simSlot)128     private CharSequence getPhoneNumber(int simSlot) {
129         final SubscriptionInfo subscriptionInfo = getSubscriptionInfo(simSlot);
130         if (subscriptionInfo == null) {
131             return mContext.getText(R.string.device_info_default);
132         }
133 
134         return getFormattedPhoneNumber(subscriptionInfo);
135     }
136 
getPreferenceTitle(int simSlot)137     private CharSequence getPreferenceTitle(int simSlot) {
138         return mTelephonyManager.getPhoneCount() > 1 ? mContext.getString(
139                 R.string.status_number_sim_slot, simSlot + 1) : mContext.getString(
140                 R.string.status_number);
141     }
142 
143     @VisibleForTesting
getSubscriptionInfo(int simSlot)144     protected SubscriptionInfo getSubscriptionInfo(int simSlot) {
145         final List<SubscriptionInfo> subscriptionInfoList =
146                 mSubscriptionManager.getActiveSubscriptionInfoList();
147         if (subscriptionInfoList != null) {
148             for (SubscriptionInfo info : subscriptionInfoList) {
149                 if (info.getSimSlotIndex() == simSlot) {
150                     return info;
151                 }
152             }
153         }
154         return null;
155     }
156 
157     @VisibleForTesting
getFormattedPhoneNumber(SubscriptionInfo subscriptionInfo)158     protected CharSequence getFormattedPhoneNumber(SubscriptionInfo subscriptionInfo) {
159         final String phoneNumber = SubscriptionUtil.getBidiFormattedPhoneNumber(mContext,
160                 subscriptionInfo);
161         return TextUtils.isEmpty(phoneNumber) ? mContext.getString(R.string.device_info_default)
162                 : phoneNumber;
163     }
164 
165     @VisibleForTesting
createNewPreference(Context context)166     protected Preference createNewPreference(Context context) {
167         return new PhoneNumberSummaryPreference(context);
168     }
169 }
170