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 static android.telephony.TelephonyManager.PHONE_TYPE_CDMA; 20 21 import android.content.Context; 22 import android.os.UserManager; 23 import android.telephony.SubscriptionInfo; 24 import android.telephony.SubscriptionManager; 25 import android.telephony.TelephonyManager; 26 27 import androidx.annotation.VisibleForTesting; 28 import androidx.fragment.app.Fragment; 29 import androidx.preference.Preference; 30 import androidx.preference.PreferenceCategory; 31 import androidx.preference.PreferenceScreen; 32 33 import com.android.settings.R; 34 import com.android.settings.core.BasePreferenceController; 35 import com.android.settings.slices.Sliceable; 36 import com.android.settingslib.Utils; 37 38 import java.util.ArrayList; 39 import java.util.List; 40 41 /** 42 * Controller that manages preference for single and multi sim devices. 43 */ 44 public class ImeiInfoPreferenceController extends BasePreferenceController { 45 46 private static final String KEY_PREFERENCE_CATEGORY = "device_detail_category"; 47 48 private final boolean mIsMultiSim; 49 private final TelephonyManager mTelephonyManager; 50 private final List<Preference> mPreferenceList = new ArrayList<>(); 51 private Fragment mFragment; 52 ImeiInfoPreferenceController(Context context, String key)53 public ImeiInfoPreferenceController(Context context, String key) { 54 super(context, key); 55 mTelephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); 56 mIsMultiSim = mTelephonyManager.getPhoneCount() > 1; 57 } 58 setHost(Fragment fragment)59 public void setHost(Fragment fragment) { 60 mFragment = fragment; 61 } 62 63 @Override displayPreference(PreferenceScreen screen)64 public void displayPreference(PreferenceScreen screen) { 65 super.displayPreference(screen); 66 final Preference preference = screen.findPreference(getPreferenceKey()); 67 final PreferenceCategory category = screen.findPreference(KEY_PREFERENCE_CATEGORY); 68 69 mPreferenceList.add(preference); 70 updatePreference(preference, 0 /* simSlot */); 71 72 final int imeiPreferenceOrder = preference.getOrder(); 73 // Add additional preferences for each sim in the device 74 for (int simSlotNumber = 1; simSlotNumber < mTelephonyManager.getPhoneCount(); 75 simSlotNumber++) { 76 final Preference multiSimPreference = createNewPreference(screen.getContext()); 77 multiSimPreference.setOrder(imeiPreferenceOrder + simSlotNumber); 78 multiSimPreference.setKey(getPreferenceKey() + simSlotNumber); 79 category.addPreference(multiSimPreference); 80 mPreferenceList.add(multiSimPreference); 81 updatePreference(multiSimPreference, simSlotNumber); 82 } 83 } 84 85 @Override updateState(Preference preference)86 public void updateState(Preference preference) { 87 if (preference == null) { 88 return; 89 } 90 int size = mPreferenceList.size(); 91 for (int i = 0; i < size; i++) { 92 Preference pref = mPreferenceList.get(i); 93 updatePreference(pref, i); 94 } 95 } 96 97 @Override getSummary()98 public CharSequence getSummary() { 99 return getSummary(0); 100 } 101 getSummary(int simSlot)102 private CharSequence getSummary(int simSlot) { 103 final int phoneType = getPhoneType(simSlot); 104 return phoneType == PHONE_TYPE_CDMA ? mTelephonyManager.getMeid(simSlot) 105 : mTelephonyManager.getImei(simSlot); 106 } 107 108 @Override handlePreferenceTreeClick(Preference preference)109 public boolean handlePreferenceTreeClick(Preference preference) { 110 final int simSlot = mPreferenceList.indexOf(preference); 111 if (simSlot == -1) { 112 return false; 113 } 114 115 ImeiInfoDialogFragment.show(mFragment, simSlot, preference.getTitle().toString()); 116 return true; 117 } 118 119 @Override getAvailabilityStatus()120 public int getAvailabilityStatus() { 121 return mContext.getSystemService(UserManager.class).isAdminUser() 122 && !Utils.isWifiOnly(mContext) ? AVAILABLE : UNSUPPORTED_ON_DEVICE; 123 } 124 125 @Override useDynamicSliceSummary()126 public boolean useDynamicSliceSummary() { 127 return true; 128 } 129 130 @Override copy()131 public void copy() { 132 Sliceable.setCopyContent(mContext, getSummary(0), getTitle(0)); 133 } 134 updatePreference(Preference preference, int simSlot)135 private void updatePreference(Preference preference, int simSlot) { 136 preference.setTitle(getTitle(simSlot)); 137 preference.setSummary(getSummary(simSlot)); 138 } 139 getTitleForGsmPhone(int simSlot)140 private CharSequence getTitleForGsmPhone(int simSlot) { 141 return mIsMultiSim ? mContext.getString(R.string.imei_multi_sim, simSlot + 1) 142 : mContext.getString(R.string.status_imei); 143 } 144 getTitleForCdmaPhone(int simSlot)145 private CharSequence getTitleForCdmaPhone(int simSlot) { 146 return mIsMultiSim ? mContext.getString(R.string.meid_multi_sim, simSlot + 1) 147 : mContext.getString(R.string.status_meid_number); 148 } 149 getTitle(int simSlot)150 private CharSequence getTitle(int simSlot) { 151 final int phoneType = getPhoneType(simSlot); 152 return phoneType == PHONE_TYPE_CDMA ? getTitleForCdmaPhone(simSlot) 153 : getTitleForGsmPhone(simSlot); 154 } 155 getPhoneType(int slotIndex)156 private int getPhoneType(int slotIndex) { 157 SubscriptionInfo subInfo = SubscriptionManager.from(mContext) 158 .getActiveSubscriptionInfoForSimSlotIndex(slotIndex); 159 return mTelephonyManager.getCurrentPhoneType(subInfo != null ? subInfo.getSubscriptionId() 160 : SubscriptionManager.DEFAULT_SUBSCRIPTION_ID); 161 } 162 163 @VisibleForTesting createNewPreference(Context context)164 Preference createNewPreference(Context context) { 165 return new Preference(context); 166 } 167 } 168