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