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 17 package com.android.settings.sim; 18 19 import android.content.Context; 20 import android.content.Intent; 21 import android.content.res.Resources; 22 import android.graphics.drawable.BitmapDrawable; 23 import android.os.Bundle; 24 import android.os.SystemProperties; 25 import android.provider.SearchIndexableResource; 26 import android.support.v7.preference.Preference; 27 import android.support.v7.preference.PreferenceScreen; 28 import android.telecom.PhoneAccountHandle; 29 import android.telecom.TelecomManager; 30 import android.telephony.PhoneNumberUtils; 31 import android.telephony.PhoneStateListener; 32 import android.telephony.SubscriptionInfo; 33 import android.telephony.SubscriptionManager; 34 import android.telephony.TelephonyManager; 35 import android.text.TextUtils; 36 import android.util.Log; 37 38 import com.android.internal.logging.MetricsProto.MetricsEvent; 39 import com.android.internal.telephony.TelephonyProperties; 40 import com.android.settings.R; 41 import com.android.settings.RestrictedSettingsFragment; 42 import com.android.settings.Utils; 43 import com.android.settings.search.BaseSearchIndexProvider; 44 import com.android.settings.search.Indexable; 45 46 import java.util.ArrayList; 47 import java.util.List; 48 49 public class SimSettings extends RestrictedSettingsFragment implements Indexable { 50 private static final String TAG = "SimSettings"; 51 private static final boolean DBG = false; 52 53 private static final String DISALLOW_CONFIG_SIM = "no_config_sim"; 54 private static final String SIM_CARD_CATEGORY = "sim_cards"; 55 private static final String KEY_CELLULAR_DATA = "sim_cellular_data"; 56 private static final String KEY_CALLS = "sim_calls"; 57 private static final String KEY_SMS = "sim_sms"; 58 public static final String EXTRA_SLOT_ID = "slot_id"; 59 60 /** 61 * By UX design we use only one Subscription Information(SubInfo) record per SIM slot. 62 * mAvalableSubInfos is the list of SubInfos we present to the user. 63 * mSubInfoList is the list of all SubInfos. 64 * mSelectableSubInfos is the list of SubInfos that a user can select for data, calls, and SMS. 65 */ 66 private List<SubscriptionInfo> mAvailableSubInfos = null; 67 private List<SubscriptionInfo> mSubInfoList = null; 68 private List<SubscriptionInfo> mSelectableSubInfos = null; 69 private PreferenceScreen mSimCards = null; 70 private SubscriptionManager mSubscriptionManager; 71 private int mNumSlots; 72 private Context mContext; 73 74 private int mPhoneCount = TelephonyManager.getDefault().getPhoneCount(); 75 private int[] mCallState = new int[mPhoneCount]; 76 private PhoneStateListener[] mPhoneStateListener = new PhoneStateListener[mPhoneCount]; 77 78 public SimSettings() { 79 super(DISALLOW_CONFIG_SIM); 80 } 81 82 @Override 83 protected int getMetricsCategory() { 84 return MetricsEvent.SIM; 85 } 86 87 @Override 88 public void onCreate(final Bundle bundle) { 89 super.onCreate(bundle); 90 mContext = getActivity(); 91 92 mSubscriptionManager = SubscriptionManager.from(getActivity()); 93 final TelephonyManager tm = 94 (TelephonyManager) getActivity().getSystemService(Context.TELEPHONY_SERVICE); 95 addPreferencesFromResource(R.xml.sim_settings); 96 97 mNumSlots = tm.getSimCount(); 98 mSimCards = (PreferenceScreen)findPreference(SIM_CARD_CATEGORY); 99 mAvailableSubInfos = new ArrayList<SubscriptionInfo>(mNumSlots); 100 mSelectableSubInfos = new ArrayList<SubscriptionInfo>(); 101 SimSelectNotification.cancelNotification(getActivity()); 102 } 103 104 private final SubscriptionManager.OnSubscriptionsChangedListener mOnSubscriptionsChangeListener 105 = new SubscriptionManager.OnSubscriptionsChangedListener() { 106 @Override 107 public void onSubscriptionsChanged() { 108 if (DBG) log("onSubscriptionsChanged:"); 109 updateSubscriptions(); 110 } 111 }; 112 113 private void updateSubscriptions() { 114 mSubInfoList = mSubscriptionManager.getActiveSubscriptionInfoList(); 115 for (int i = 0; i < mNumSlots; ++i) { 116 Preference pref = mSimCards.findPreference("sim" + i); 117 if (pref instanceof SimPreference) { 118 mSimCards.removePreference(pref); 119 } 120 } 121 mAvailableSubInfos.clear(); 122 mSelectableSubInfos.clear(); 123 124 for (int i = 0; i < mNumSlots; ++i) { 125 final SubscriptionInfo sir = mSubscriptionManager 126 .getActiveSubscriptionInfoForSimSlotIndex(i); 127 SimPreference simPreference = new SimPreference(getPrefContext(), sir, i); 128 simPreference.setOrder(i-mNumSlots); 129 mSimCards.addPreference(simPreference); 130 mAvailableSubInfos.add(sir); 131 if (sir != null) { 132 mSelectableSubInfos.add(sir); 133 } 134 } 135 updateAllOptions(); 136 } 137 138 private void updateAllOptions() { 139 updateSimSlotValues(); 140 updateActivitesCategory(); 141 } 142 143 private void updateSimSlotValues() { 144 final int prefSize = mSimCards.getPreferenceCount(); 145 for (int i = 0; i < prefSize; ++i) { 146 Preference pref = mSimCards.getPreference(i); 147 if (pref instanceof SimPreference) { 148 ((SimPreference)pref).update(); 149 } 150 } 151 } 152 153 private void updateActivitesCategory() { 154 updateCellularDataValues(); 155 updateCallValues(); 156 updateSmsValues(); 157 } 158 159 private void updateSmsValues() { 160 final Preference simPref = findPreference(KEY_SMS); 161 final SubscriptionInfo sir = mSubscriptionManager.getDefaultSmsSubscriptionInfo(); 162 simPref.setTitle(R.string.sms_messages_title); 163 if (DBG) log("[updateSmsValues] mSubInfoList=" + mSubInfoList); 164 165 if (sir != null) { 166 simPref.setSummary(sir.getDisplayName()); 167 simPref.setEnabled(mSelectableSubInfos.size() > 1); 168 } else if (sir == null) { 169 simPref.setSummary(R.string.sim_selection_required_pref); 170 simPref.setEnabled(mSelectableSubInfos.size() >= 1); 171 } 172 } 173 174 private void updateCellularDataValues() { 175 final Preference simPref = findPreference(KEY_CELLULAR_DATA); 176 final SubscriptionInfo sir = mSubscriptionManager.getDefaultDataSubscriptionInfo(); 177 simPref.setTitle(R.string.cellular_data_title); 178 if (DBG) log("[updateCellularDataValues] mSubInfoList=" + mSubInfoList); 179 180 boolean callStateIdle = isCallStateIdle(); 181 final boolean ecbMode = SystemProperties.getBoolean( 182 TelephonyProperties.PROPERTY_INECM_MODE, false); 183 if (sir != null) { 184 simPref.setSummary(sir.getDisplayName()); 185 // Enable data preference in msim mode and call state idle 186 simPref.setEnabled((mSelectableSubInfos.size() > 1) && callStateIdle && !ecbMode); 187 } else if (sir == null) { 188 simPref.setSummary(R.string.sim_selection_required_pref); 189 // Enable data preference in msim mode and call state idle 190 simPref.setEnabled((mSelectableSubInfos.size() >= 1) && callStateIdle && !ecbMode); 191 } 192 } 193 194 private void updateCallValues() { 195 final Preference simPref = findPreference(KEY_CALLS); 196 final TelecomManager telecomManager = TelecomManager.from(mContext); 197 final PhoneAccountHandle phoneAccount = 198 telecomManager.getUserSelectedOutgoingPhoneAccount(); 199 final List<PhoneAccountHandle> allPhoneAccounts = 200 telecomManager.getCallCapablePhoneAccounts(); 201 202 simPref.setTitle(R.string.calls_title); 203 simPref.setSummary(phoneAccount == null 204 ? mContext.getResources().getString(R.string.sim_calls_ask_first_prefs_title) 205 : (String)telecomManager.getPhoneAccount(phoneAccount).getLabel()); 206 simPref.setEnabled(allPhoneAccounts.size() > 1); 207 } 208 209 @Override 210 public void onResume() { 211 super.onResume(); 212 mSubscriptionManager.addOnSubscriptionsChangedListener(mOnSubscriptionsChangeListener); 213 updateSubscriptions(); 214 final TelephonyManager tm = 215 (TelephonyManager) getActivity().getSystemService(Context.TELEPHONY_SERVICE); 216 if (mSelectableSubInfos.size() > 1) { 217 Log.d(TAG, "Register for call state change"); 218 for (int i = 0; i < mPhoneCount; i++) { 219 int subId = mSelectableSubInfos.get(i).getSubscriptionId(); 220 tm.listen(getPhoneStateListener(i, subId), 221 PhoneStateListener.LISTEN_CALL_STATE); 222 } 223 } 224 } 225 226 @Override 227 public void onPause() { 228 super.onPause(); 229 mSubscriptionManager.removeOnSubscriptionsChangedListener(mOnSubscriptionsChangeListener); 230 final TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); 231 for (int i = 0; i < mPhoneCount; i++) { 232 if (mPhoneStateListener[i] != null) { 233 tm.listen(mPhoneStateListener[i], PhoneStateListener.LISTEN_NONE); 234 mPhoneStateListener[i] = null; 235 } 236 } 237 } 238 239 private PhoneStateListener getPhoneStateListener(int phoneId, int subId) { 240 // Disable Sim selection for Data when voice call is going on as changing the default data 241 // sim causes a modem reset currently and call gets disconnected 242 // ToDo : Add subtext on disabled preference to let user know that default data sim cannot 243 // be changed while call is going on 244 final int i = phoneId; 245 mPhoneStateListener[phoneId] = new PhoneStateListener(subId) { 246 @Override 247 public void onCallStateChanged(int state, String incomingNumber) { 248 if (DBG) log("PhoneStateListener.onCallStateChanged: state=" + state); 249 mCallState[i] = state; 250 updateCellularDataValues(); 251 } 252 }; 253 return mPhoneStateListener[phoneId]; 254 } 255 256 @Override 257 public boolean onPreferenceTreeClick(final Preference preference) { 258 final Context context = mContext; 259 Intent intent = new Intent(context, SimDialogActivity.class); 260 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 261 262 if (preference instanceof SimPreference) { 263 Intent newIntent = new Intent(context, SimPreferenceDialog.class); 264 newIntent.putExtra(EXTRA_SLOT_ID, ((SimPreference)preference).getSlotId()); 265 startActivity(newIntent); 266 } else if (findPreference(KEY_CELLULAR_DATA) == preference) { 267 intent.putExtra(SimDialogActivity.DIALOG_TYPE_KEY, SimDialogActivity.DATA_PICK); 268 context.startActivity(intent); 269 } else if (findPreference(KEY_CALLS) == preference) { 270 intent.putExtra(SimDialogActivity.DIALOG_TYPE_KEY, SimDialogActivity.CALLS_PICK); 271 context.startActivity(intent); 272 } else if (findPreference(KEY_SMS) == preference) { 273 intent.putExtra(SimDialogActivity.DIALOG_TYPE_KEY, SimDialogActivity.SMS_PICK); 274 context.startActivity(intent); 275 } 276 277 return true; 278 } 279 280 private class SimPreference extends Preference { 281 private SubscriptionInfo mSubInfoRecord; 282 private int mSlotId; 283 Context mContext; 284 285 public SimPreference(Context context, SubscriptionInfo subInfoRecord, int slotId) { 286 super(context); 287 288 mContext = context; 289 mSubInfoRecord = subInfoRecord; 290 mSlotId = slotId; 291 setKey("sim" + mSlotId); 292 update(); 293 } 294 295 public void update() { 296 final Resources res = mContext.getResources(); 297 298 setTitle(String.format(mContext.getResources() 299 .getString(R.string.sim_editor_title), (mSlotId + 1))); 300 if (mSubInfoRecord != null) { 301 if (TextUtils.isEmpty(getPhoneNumber(mSubInfoRecord))) { 302 setSummary(mSubInfoRecord.getDisplayName()); 303 } else { 304 setSummary(mSubInfoRecord.getDisplayName() + " - " + 305 PhoneNumberUtils.createTtsSpannable(getPhoneNumber(mSubInfoRecord))); 306 setEnabled(true); 307 } 308 setIcon(new BitmapDrawable(res, (mSubInfoRecord.createIconBitmap(mContext)))); 309 } else { 310 setSummary(R.string.sim_slot_empty); 311 setFragment(null); 312 setEnabled(false); 313 } 314 } 315 316 private int getSlotId() { 317 return mSlotId; 318 } 319 } 320 321 // Returns the line1Number. Line1number should always be read from TelephonyManager since it can 322 // be overridden for display purposes. 323 private String getPhoneNumber(SubscriptionInfo info) { 324 final TelephonyManager tm = 325 (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE); 326 return tm.getLine1Number(info.getSubscriptionId()); 327 } 328 329 private void log(String s) { 330 Log.d(TAG, s); 331 } 332 333 /** 334 * For search 335 */ 336 public static final SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER = 337 new BaseSearchIndexProvider() { 338 @Override 339 public List<SearchIndexableResource> getXmlResourcesToIndex(Context context, 340 boolean enabled) { 341 ArrayList<SearchIndexableResource> result = 342 new ArrayList<SearchIndexableResource>(); 343 344 if (Utils.showSimCardTile(context)) { 345 SearchIndexableResource sir = new SearchIndexableResource(context); 346 sir.xmlResId = R.xml.sim_settings; 347 result.add(sir); 348 } 349 350 return result; 351 } 352 }; 353 354 private boolean isCallStateIdle() { 355 boolean callStateIdle = true; 356 for (int i = 0; i < mCallState.length; i++) { 357 if (TelephonyManager.CALL_STATE_IDLE != mCallState[i]) { 358 callStateIdle = false; 359 } 360 } 361 Log.d(TAG, "isCallStateIdle " + callStateIdle); 362 return callStateIdle; 363 } 364 } 365