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