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.settings.R;
35 
36 public class ImeiInfoDialogController {
37 
38     private static final String TAG = "ImeiInfoDialog";
39 
40     @VisibleForTesting
41     static final int ID_PRL_VERSION_VALUE = R.id.prl_version_value;
42     private static final int ID_MIN_NUMBER_LABEL = R.id.min_number_label;
43     @VisibleForTesting
44     static final int ID_MIN_NUMBER_VALUE = R.id.min_number_value;
45     @VisibleForTesting
46     static final int ID_MEID_NUMBER_VALUE = R.id.meid_number_value;
47     @VisibleForTesting
48     static final int ID_IMEI_VALUE = R.id.imei_value;
49     @VisibleForTesting
50     static final int ID_IMEI_SV_VALUE = R.id.imei_sv_value;
51     @VisibleForTesting
52     static final int ID_CDMA_SETTINGS = R.id.cdma_settings;
53     @VisibleForTesting
54     static final int ID_GSM_SETTINGS = R.id.gsm_settings;
55 
getTextAsDigits(CharSequence text)56     private static CharSequence getTextAsDigits(CharSequence text) {
57         if (TextUtils.isEmpty(text)) {
58             return "";
59         }
60         if (TextUtils.isDigitsOnly(text)) {
61             final Spannable spannable = new SpannableStringBuilder(text);
62             final TtsSpan span = new TtsSpan.DigitsBuilder(text.toString()).build();
63             spannable.setSpan(span, 0, spannable.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
64             text = spannable;
65         }
66         return text;
67     }
68 
69     private final ImeiInfoDialogFragment mDialog;
70     private final TelephonyManager mTelephonyManager;
71     private final SubscriptionInfo mSubscriptionInfo;
72     private final int mSlotId;
73 
ImeiInfoDialogController(@onNull ImeiInfoDialogFragment dialog, int slotId)74     public ImeiInfoDialogController(@NonNull ImeiInfoDialogFragment dialog, int slotId) {
75         mDialog = dialog;
76         mSlotId = slotId;
77         final Context context = dialog.getContext();
78         mSubscriptionInfo = context.getSystemService(SubscriptionManager.class)
79                 .getActiveSubscriptionInfoForSimSlotIndex(slotId);
80         TelephonyManager tm = context.getSystemService(TelephonyManager.class);
81         if (mSubscriptionInfo != null) {
82             mTelephonyManager = context.getSystemService(TelephonyManager.class)
83                     .createForSubscriptionId(mSubscriptionInfo.getSubscriptionId());
84         } else if(isValidSlotIndex(slotId, tm)) {
85             mTelephonyManager = tm;
86         } else {
87             mTelephonyManager = null;
88         }
89     }
90 
91     /**
92      * Sets IMEI/MEID information based on whether the device is CDMA or GSM.
93      */
populateImeiInfo()94     public void populateImeiInfo() {
95         if (mTelephonyManager == null) {
96             Log.w(TAG, "TelephonyManager for this slot is null. Invalid slot? id=" + mSlotId);
97             return;
98         }
99         if (mTelephonyManager.getPhoneType() == TelephonyManager.PHONE_TYPE_CDMA) {
100             updateDialogForCdmaPhone();
101         } else {
102             updateDialogForGsmPhone();
103         }
104     }
105 
updateDialogForCdmaPhone()106     private void updateDialogForCdmaPhone() {
107         final Resources res = mDialog.getContext().getResources();
108         mDialog.setText(ID_MEID_NUMBER_VALUE, getMeid());
109         // MIN needs to read from SIM. So if no SIM, we should not show MIN on UI
110         mDialog.setText(ID_MIN_NUMBER_VALUE, mSubscriptionInfo != null
111                 ? mTelephonyManager.getCdmaMin(mSubscriptionInfo.getSubscriptionId())
112                 : "");
113 
114         if (res.getBoolean(R.bool.config_msid_enable)) {
115             mDialog.setText(ID_MIN_NUMBER_LABEL,
116                     res.getString(R.string.status_msid_number));
117         }
118 
119         mDialog.setText(ID_PRL_VERSION_VALUE, getCdmaPrlVersion());
120 
121         if ((mSubscriptionInfo != null && isCdmaLteEnabled()) ||
122                     (mSubscriptionInfo == null && isSimPresent(mSlotId))) {
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.isLteCdmaEvdoGsmWcdmaEnabled();
151     }
152 
isSimPresent(int slotId)153     boolean isSimPresent(int slotId) {
154         final int simState = mTelephonyManager.getSimState(slotId);
155         if ((simState != TelephonyManager.SIM_STATE_ABSENT) &&
156                 (simState != TelephonyManager.SIM_STATE_UNKNOWN)) {
157             return true;
158         }
159         return false;
160     }
161 
162     @VisibleForTesting
getMeid()163     String getMeid() {
164         return mTelephonyManager.getMeid(mSlotId);
165     }
166 
167     @VisibleForTesting
isValidSlotIndex(int slotIndex, TelephonyManager telephonyManager)168     private boolean isValidSlotIndex(int slotIndex, TelephonyManager telephonyManager) {
169         return slotIndex >= 0 && slotIndex < telephonyManager.getPhoneCount();
170     }
171 }
172