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.simstatus; 18 19 import android.content.Context; 20 import android.telephony.SubscriptionInfo; 21 import android.telephony.SubscriptionManager; 22 import android.telephony.TelephonyManager; 23 24 import androidx.annotation.VisibleForTesting; 25 import androidx.fragment.app.Fragment; 26 import androidx.preference.Preference; 27 import androidx.preference.PreferenceScreen; 28 29 import com.android.settings.R; 30 import com.android.settings.core.PreferenceControllerMixin; 31 import com.android.settingslib.deviceinfo.AbstractSimStatusImeiInfoPreferenceController; 32 33 import java.util.ArrayList; 34 import java.util.List; 35 36 public class SimStatusPreferenceController extends 37 AbstractSimStatusImeiInfoPreferenceController implements PreferenceControllerMixin { 38 39 private static final String KEY_SIM_STATUS = "sim_status"; 40 41 private final TelephonyManager mTelephonyManager; 42 private final SubscriptionManager mSubscriptionManager; 43 private final Fragment mFragment; 44 private final List<Preference> mPreferenceList = new ArrayList<>(); 45 SimStatusPreferenceController(Context context, Fragment fragment)46 public SimStatusPreferenceController(Context context, Fragment fragment) { 47 super(context); 48 49 mTelephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); 50 mSubscriptionManager = (SubscriptionManager) context.getSystemService( 51 Context.TELEPHONY_SUBSCRIPTION_SERVICE); 52 mFragment = fragment; 53 } 54 55 @Override getPreferenceKey()56 public String getPreferenceKey() { 57 return KEY_SIM_STATUS; 58 } 59 60 @Override displayPreference(PreferenceScreen screen)61 public void displayPreference(PreferenceScreen screen) { 62 super.displayPreference(screen); 63 final Preference preference = screen.findPreference(getPreferenceKey()); 64 if (!isAvailable() || preference == null || !preference.isVisible()) { 65 return; 66 } 67 68 mPreferenceList.add(preference); 69 70 final int simStatusOrder = preference.getOrder(); 71 // Add additional preferences for each sim in the device 72 for (int simSlotNumber = 1; simSlotNumber < mTelephonyManager.getPhoneCount(); 73 simSlotNumber++) { 74 final Preference multiSimPreference = createNewPreference(screen.getContext()); 75 multiSimPreference.setOrder(simStatusOrder + simSlotNumber); 76 multiSimPreference.setKey(KEY_SIM_STATUS + simSlotNumber); 77 screen.addPreference(multiSimPreference); 78 mPreferenceList.add(multiSimPreference); 79 } 80 } 81 82 @Override updateState(Preference preference)83 public void updateState(Preference preference) { 84 for (int simSlotNumber = 0; simSlotNumber < mPreferenceList.size(); simSlotNumber++) { 85 final Preference simStatusPreference = mPreferenceList.get(simSlotNumber); 86 simStatusPreference.setTitle(getPreferenceTitle(simSlotNumber /* sim slot */)); 87 simStatusPreference.setSummary(getCarrierName(simSlotNumber /* sim slot */)); 88 } 89 } 90 91 @Override handlePreferenceTreeClick(Preference preference)92 public boolean handlePreferenceTreeClick(Preference preference) { 93 final int simSlot = mPreferenceList.indexOf(preference); 94 if (simSlot == -1) { 95 return false; 96 } 97 98 SimStatusDialogFragment.show(mFragment, simSlot, getPreferenceTitle(simSlot)); 99 return true; 100 } 101 getPreferenceTitle(int simSlot)102 private String getPreferenceTitle(int simSlot) { 103 return mTelephonyManager.getPhoneCount() > 1 ? mContext.getString( 104 R.string.sim_status_title_sim_slot, simSlot + 1) : mContext.getString( 105 R.string.sim_status_title); 106 } 107 getCarrierName(int simSlot)108 private CharSequence getCarrierName(int simSlot) { 109 final List<SubscriptionInfo> subscriptionInfoList = 110 mSubscriptionManager.getActiveSubscriptionInfoList(true); 111 if (subscriptionInfoList != null) { 112 for (SubscriptionInfo info : subscriptionInfoList) { 113 if (info.getSimSlotIndex() == simSlot) { 114 return info.getCarrierName(); 115 } 116 } 117 } 118 return mContext.getText(R.string.device_info_not_available); 119 } 120 121 @VisibleForTesting createNewPreference(Context context)122 Preference createNewPreference(Context context) { 123 return new Preference(context); 124 } 125 } 126