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 displayPreference(PreferenceScreen screen)59 public void displayPreference(PreferenceScreen screen) { 60 super.displayPreference(screen); 61 if (!SubscriptionUtil.isSimHardwareVisible(mContext)) { 62 return; 63 } 64 final Preference preference = screen.findPreference(getPreferenceKey()); 65 final PreferenceCategory category = screen.findPreference(KEY_PREFERENCE_CATEGORY); 66 mPreferenceList.add(preference); 67 68 final int phonePreferenceOrder = preference.getOrder(); 69 // Add additional preferences for each sim in the device 70 for (int simSlotNumber = 1; simSlotNumber < mTelephonyManager.getPhoneCount(); 71 simSlotNumber++) { 72 final Preference multiSimPreference = createNewPreference(screen.getContext()); 73 multiSimPreference.setSelectable(false); 74 multiSimPreference.setCopyingEnabled(true); 75 multiSimPreference.setOrder(phonePreferenceOrder + simSlotNumber); 76 multiSimPreference.setKey(KEY_PHONE_NUMBER + simSlotNumber); 77 category.addPreference(multiSimPreference); 78 mPreferenceList.add(multiSimPreference); 79 } 80 } 81 82 @Override updateState(Preference preference)83 public void updateState(Preference preference) { 84 for (int simSlotNumber = 0; simSlotNumber < mPreferenceList.size(); simSlotNumber++) { 85 final Preference simStatusPreference = mPreferenceList.get(simSlotNumber); 86 simStatusPreference.setTitle(getPreferenceTitle(simSlotNumber)); 87 simStatusPreference.setSummary(getPhoneNumber(simSlotNumber)); 88 } 89 } 90 91 @Override useDynamicSliceSummary()92 public boolean useDynamicSliceSummary() { 93 return true; 94 } 95 getFirstPhoneNumber()96 private CharSequence getFirstPhoneNumber() { 97 final List<SubscriptionInfo> subscriptionInfoList = 98 mSubscriptionManager.getActiveSubscriptionInfoList(); 99 if (subscriptionInfoList == null || subscriptionInfoList.isEmpty()) { 100 return mContext.getText(R.string.device_info_default); 101 } 102 103 // For now, We only return first result for slice view. 104 return getFormattedPhoneNumber(subscriptionInfoList.get(0)); 105 } 106 getPhoneNumber(int simSlot)107 private CharSequence getPhoneNumber(int simSlot) { 108 final SubscriptionInfo subscriptionInfo = getSubscriptionInfo(simSlot); 109 if (subscriptionInfo == null) { 110 return mContext.getText(R.string.device_info_default); 111 } 112 113 return getFormattedPhoneNumber(subscriptionInfo); 114 } 115 getPreferenceTitle(int simSlot)116 private CharSequence getPreferenceTitle(int simSlot) { 117 return mTelephonyManager.getPhoneCount() > 1 ? mContext.getString( 118 R.string.status_number_sim_slot, simSlot + 1) : mContext.getString( 119 R.string.status_number); 120 } 121 122 @VisibleForTesting getSubscriptionInfo(int simSlot)123 protected SubscriptionInfo getSubscriptionInfo(int simSlot) { 124 final List<SubscriptionInfo> subscriptionInfoList = 125 mSubscriptionManager.getActiveSubscriptionInfoList(); 126 if (subscriptionInfoList != null) { 127 for (SubscriptionInfo info : subscriptionInfoList) { 128 if (info.getSimSlotIndex() == simSlot) { 129 return info; 130 } 131 } 132 } 133 return null; 134 } 135 136 @VisibleForTesting getFormattedPhoneNumber(SubscriptionInfo subscriptionInfo)137 protected String getFormattedPhoneNumber(SubscriptionInfo subscriptionInfo) { 138 final String phoneNumber = SubscriptionUtil.getBidiFormattedPhoneNumber(mContext, 139 subscriptionInfo); 140 return TextUtils.isEmpty(phoneNumber) ? mContext.getString(R.string.device_info_default) 141 : phoneNumber; 142 } 143 144 @VisibleForTesting createNewPreference(Context context)145 protected Preference createNewPreference(Context context) { 146 return new Preference(context); 147 } 148 } 149