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.imei; 18 19 import android.content.Context; 20 import android.content.res.Resources; 21 import android.telephony.SubscriptionInfo; 22 import android.telephony.SubscriptionManager; 23 import android.telephony.TelephonyManager; 24 import android.text.Spannable; 25 import android.text.SpannableStringBuilder; 26 import android.text.Spanned; 27 import android.text.TextUtils; 28 import android.text.style.TtsSpan; 29 import android.util.Log; 30 31 import androidx.annotation.NonNull; 32 import androidx.annotation.VisibleForTesting; 33 34 import com.android.internal.telephony.PhoneConstants; 35 import com.android.settings.R; 36 37 public class ImeiInfoDialogController { 38 39 private static final String TAG = "ImeiInfoDialog"; 40 41 @VisibleForTesting 42 static final int ID_PRL_VERSION_VALUE = R.id.prl_version_value; 43 private static final int ID_MIN_NUMBER_LABEL = R.id.min_number_label; 44 @VisibleForTesting 45 static final int ID_MIN_NUMBER_VALUE = R.id.min_number_value; 46 @VisibleForTesting 47 static final int ID_MEID_NUMBER_VALUE = R.id.meid_number_value; 48 @VisibleForTesting 49 static final int ID_IMEI_VALUE = R.id.imei_value; 50 @VisibleForTesting 51 static final int ID_IMEI_SV_VALUE = R.id.imei_sv_value; 52 @VisibleForTesting 53 static final int ID_CDMA_SETTINGS = R.id.cdma_settings; 54 @VisibleForTesting 55 static final int ID_GSM_SETTINGS = R.id.gsm_settings; 56 getTextAsDigits(CharSequence text)57 private static CharSequence getTextAsDigits(CharSequence text) { 58 if (TextUtils.isEmpty(text)) { 59 return ""; 60 } 61 if (TextUtils.isDigitsOnly(text)) { 62 final Spannable spannable = new SpannableStringBuilder(text); 63 final TtsSpan span = new TtsSpan.DigitsBuilder(text.toString()).build(); 64 spannable.setSpan(span, 0, spannable.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 65 text = spannable; 66 } 67 return text; 68 } 69 70 private final ImeiInfoDialogFragment mDialog; 71 private final TelephonyManager mTelephonyManager; 72 private final SubscriptionInfo mSubscriptionInfo; 73 private final int mSlotId; 74 ImeiInfoDialogController(@onNull ImeiInfoDialogFragment dialog, int slotId)75 public ImeiInfoDialogController(@NonNull ImeiInfoDialogFragment dialog, int slotId) { 76 mDialog = dialog; 77 mSlotId = slotId; 78 final Context context = dialog.getContext(); 79 mSubscriptionInfo = context.getSystemService(SubscriptionManager.class) 80 .getActiveSubscriptionInfoForSimSlotIndex(slotId); 81 TelephonyManager tm = context.getSystemService(TelephonyManager.class); 82 if (mSubscriptionInfo != null) { 83 mTelephonyManager = context.getSystemService(TelephonyManager.class) 84 .createForSubscriptionId(mSubscriptionInfo.getSubscriptionId()); 85 } else if(isValidSlotIndex(slotId, tm)) { 86 mTelephonyManager = tm; 87 } else { 88 mTelephonyManager = null; 89 } 90 } 91 92 /** 93 * Sets IMEI/MEID information based on whether the device is CDMA or GSM. 94 */ populateImeiInfo()95 public void populateImeiInfo() { 96 if (mTelephonyManager == null) { 97 Log.w(TAG, "TelephonyManager for this slot is null. Invalid slot? id=" + mSlotId); 98 return; 99 } 100 if (mTelephonyManager.getPhoneType() == TelephonyManager.PHONE_TYPE_CDMA) { 101 updateDialogForCdmaPhone(); 102 } else { 103 updateDialogForGsmPhone(); 104 } 105 } 106 updateDialogForCdmaPhone()107 private void updateDialogForCdmaPhone() { 108 final Resources res = mDialog.getContext().getResources(); 109 mDialog.setText(ID_MEID_NUMBER_VALUE, getMeid()); 110 // MIN needs to read from SIM. So if no SIM, we should not show MIN on UI 111 mDialog.setText(ID_MIN_NUMBER_VALUE, mSubscriptionInfo != null 112 ? mTelephonyManager.getCdmaMin(mSubscriptionInfo.getSubscriptionId()) 113 : ""); 114 115 if (res.getBoolean(R.bool.config_msid_enable)) { 116 mDialog.setText(ID_MIN_NUMBER_LABEL, 117 res.getString(R.string.status_msid_number)); 118 } 119 120 mDialog.setText(ID_PRL_VERSION_VALUE, getCdmaPrlVersion()); 121 122 if (mSubscriptionInfo != null && isCdmaLteEnabled()) { 123 // Show IMEI for LTE device 124 mDialog.setText(ID_IMEI_VALUE, 125 getTextAsDigits(mTelephonyManager.getImei(mSlotId))); 126 mDialog.setText(ID_IMEI_SV_VALUE, 127 getTextAsDigits(mTelephonyManager.getDeviceSoftwareVersion(mSlotId))); 128 } else { 129 // device is not GSM/UMTS, do not display GSM/UMTS features 130 mDialog.removeViewFromScreen(ID_GSM_SETTINGS); 131 } 132 } 133 updateDialogForGsmPhone()134 private void updateDialogForGsmPhone() { 135 mDialog.setText(ID_IMEI_VALUE, getTextAsDigits(mTelephonyManager.getImei(mSlotId))); 136 mDialog.setText(ID_IMEI_SV_VALUE, 137 getTextAsDigits(mTelephonyManager.getDeviceSoftwareVersion(mSlotId))); 138 // device is not CDMA, do not display CDMA features 139 mDialog.removeViewFromScreen(ID_CDMA_SETTINGS); 140 } 141 142 @VisibleForTesting getCdmaPrlVersion()143 String getCdmaPrlVersion() { 144 // PRL needs to read from SIM. So if no SIM, return empty 145 return mSubscriptionInfo != null ? mTelephonyManager.getCdmaPrlVersion() : ""; 146 } 147 148 @VisibleForTesting isCdmaLteEnabled()149 boolean isCdmaLteEnabled() { 150 return mTelephonyManager.getLteOnCdmaMode(mSubscriptionInfo.getSubscriptionId()) 151 == PhoneConstants.LTE_ON_CDMA_TRUE; 152 } 153 154 @VisibleForTesting getMeid()155 String getMeid() { 156 return mTelephonyManager.getMeid(mSlotId); 157 } 158 159 @VisibleForTesting isValidSlotIndex(int slotIndex, TelephonyManager telephonyManager)160 private boolean isValidSlotIndex(int slotIndex, TelephonyManager telephonyManager) { 161 return slotIndex >= 0 && slotIndex < telephonyManager.getPhoneCount(); 162 } 163 } 164