1 /*
2  * Copyright (C) 2014 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 package com.android.settings.deviceinfo;
17 
18 import android.content.Context;
19 import android.os.Bundle;
20 import android.support.v7.preference.Preference;
21 import android.support.v7.preference.PreferenceScreen;
22 import android.telephony.SubscriptionManager;
23 import android.telephony.TelephonyManager;
24 import android.text.TextUtils;
25 
26 import com.android.internal.logging.MetricsProto.MetricsEvent;
27 import com.android.internal.telephony.Phone;
28 import com.android.internal.telephony.PhoneConstants;
29 import com.android.internal.telephony.PhoneFactory;
30 import com.android.settings.R;
31 import com.android.settings.SettingsPreferenceFragment;
32 
33 public class ImeiInformation extends SettingsPreferenceFragment {
34 
35     private static final String KEY_PRL_VERSION = "prl_version";
36     private static final String KEY_MIN_NUMBER = "min_number";
37     private static final String KEY_MEID_NUMBER = "meid_number";
38     private static final String KEY_ICC_ID = "icc_id";
39     private static final String KEY_IMEI = "imei";
40     private static final String KEY_IMEI_SV = "imei_sv";
41 
42     private SubscriptionManager mSubscriptionManager;
43     private boolean isMultiSIM = false;
44 
45     @Override
onCreate(Bundle savedInstanceState)46     public void onCreate(Bundle savedInstanceState) {
47         super.onCreate(savedInstanceState);
48         mSubscriptionManager = SubscriptionManager.from(getContext());
49         final TelephonyManager telephonyManager =
50             (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
51         initPreferenceScreen(telephonyManager.getSimCount());
52     }
53 
54     // Since there are multiple phone for dsds, therefore need to show information for different
55     // phones.
initPreferenceScreen(int slotCount)56     private void initPreferenceScreen(int slotCount) {
57         isMultiSIM = (slotCount > 1);
58         for (int slotId = 0; slotId < slotCount; slotId ++) {
59             addPreferencesFromResource(R.xml.device_info_phone_status);
60             setPreferenceValue(slotId);
61             setNewKey(slotId);
62         }
63     }
64 
setPreferenceValue(int phoneId)65     private void setPreferenceValue(int phoneId) {
66         final Phone phone = PhoneFactory.getPhone(phoneId);
67 
68         if (phone != null) {
69             if (phone.getPhoneType() == TelephonyManager.PHONE_TYPE_CDMA) {
70                 setSummaryText(KEY_MEID_NUMBER, phone.getMeid());
71                 setSummaryText(KEY_MIN_NUMBER, phone.getCdmaMin());
72 
73                 if (getResources().getBoolean(R.bool.config_msid_enable)) {
74                     findPreference(KEY_MIN_NUMBER).setTitle(R.string.status_msid_number);
75                 }
76 
77                 setSummaryText(KEY_PRL_VERSION, phone.getCdmaPrlVersion());
78                 removePreferenceFromScreen(KEY_IMEI_SV);
79 
80                 if (phone.getLteOnCdmaMode() == PhoneConstants.LTE_ON_CDMA_TRUE) {
81                     // Show ICC ID and IMEI for LTE device
82                     setSummaryText(KEY_ICC_ID, phone.getIccSerialNumber());
83                     setSummaryText(KEY_IMEI, phone.getImei());
84                 } else {
85                     // device is not GSM/UMTS, do not display GSM/UMTS features
86                     // check Null in case no specified preference in overlay xml
87                     removePreferenceFromScreen(KEY_IMEI);
88                     removePreferenceFromScreen(KEY_ICC_ID);
89                 }
90             } else {
91                 setSummaryText(KEY_IMEI, phone.getImei());
92                 setSummaryText(KEY_IMEI_SV, phone.getDeviceSvn());
93                 // device is not CDMA, do not display CDMA features
94                 // check Null in case no specified preference in overlay xml
95                 removePreferenceFromScreen(KEY_PRL_VERSION);
96                 removePreferenceFromScreen(KEY_MEID_NUMBER);
97                 removePreferenceFromScreen(KEY_MIN_NUMBER);
98                 removePreferenceFromScreen(KEY_ICC_ID);
99             }
100         }
101     }
102 
103     // Modify the preference key with prefix "_", so new added information preference can be set
104     // related phone information.
setNewKey(int slotId)105     private void setNewKey(int slotId) {
106         final PreferenceScreen prefScreen = getPreferenceScreen();
107         final int count = prefScreen.getPreferenceCount();
108         for (int i = 0; i < count; i++) {
109             Preference pref = prefScreen.getPreference(i);
110             String key = pref.getKey();
111             if (!key.startsWith("_")){
112                 key = "_" + key + String.valueOf(slotId);
113                 pref.setKey(key);
114                 updateTitle(pref, slotId);
115             }
116         }
117     }
118 
updateTitle(Preference pref, int slotId)119     private void updateTitle(Preference pref, int slotId) {
120         if (pref != null) {
121             String title = pref.getTitle().toString();
122             if (isMultiSIM) {
123                 // Slot starts from 1, slotId starts from 0 so plus 1
124                 title += " " + getResources().getString(R.string.slot_number, slotId + 1);
125             }
126             pref.setTitle(title);
127         }
128     }
129 
setSummaryText(String key, String text)130     private void setSummaryText(String key, String text) {
131         final Preference preference = findPreference(key);
132 
133         if (TextUtils.isEmpty(text)) {
134             text = getResources().getString(R.string.device_info_default);
135         }
136 
137         if (preference != null) {
138             preference.setSummary(text);
139         }
140     }
141 
142     /**
143      * Removes the specified preference, if it exists.
144      * @param key the key for the Preference item
145      */
removePreferenceFromScreen(String key)146     private void removePreferenceFromScreen(String key) {
147         final Preference preference = findPreference(key);
148         if (preference != null) {
149             getPreferenceScreen().removePreference(preference);
150         }
151     }
152 
153     @Override
getMetricsCategory()154     protected int getMetricsCategory() {
155         return MetricsEvent.DEVICEINFO_IMEI_INFORMATION;
156     }
157 }
158