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