summaryrefslogtreecommitdiffstats
path: root/src/com/android/settings/sim/SimSettings.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/com/android/settings/sim/SimSettings.java')
-rw-r--r--src/com/android/settings/sim/SimSettings.java393
1 files changed, 393 insertions, 0 deletions
diff --git a/src/com/android/settings/sim/SimSettings.java b/src/com/android/settings/sim/SimSettings.java
new file mode 100644
index 0000000..836ea24
--- /dev/null
+++ b/src/com/android/settings/sim/SimSettings.java
@@ -0,0 +1,393 @@
+/*
+ * Copyright (C) 2014 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.settings.sim;
+
+import android.provider.SearchIndexableResource;
+import com.android.settings.R;
+
+import android.app.AlertDialog;
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.Intent;
+import android.content.IntentFilter;
+import android.content.DialogInterface;
+import android.content.res.Resources;
+import android.os.Bundle;
+import android.os.IBinder;
+import android.os.RemoteException;
+import android.os.UserHandle;
+import android.preference.ListPreference;
+import android.preference.Preference;
+import android.preference.PreferenceCategory;
+import android.preference.Preference.OnPreferenceChangeListener;
+import android.preference.PreferenceScreen;
+import android.telephony.SubInfoRecord;
+import android.telephony.SubscriptionManager;
+import android.telephony.TelephonyManager;
+import android.telecom.PhoneAccount;
+import android.telephony.CellInfo;
+import android.text.TextUtils;
+import android.util.Log;
+import android.view.LayoutInflater;
+import android.view.View;
+import android.widget.ArrayAdapter;
+import android.widget.EditText;
+import android.widget.FrameLayout;
+import android.widget.ImageView;
+import android.widget.ListView;
+import android.widget.Spinner;
+import android.widget.TextView;
+
+import com.android.internal.telephony.PhoneConstants;
+import com.android.internal.telephony.TelephonyIntents;
+import com.android.settings.RestrictedSettingsFragment;
+import com.android.settings.SettingsPreferenceFragment;
+import com.android.settings.Utils;
+import com.android.settings.notification.DropDownPreference;
+import com.android.settings.search.BaseSearchIndexProvider;
+import com.android.settings.search.Indexable;
+import com.android.settings.search.Indexable.SearchIndexProvider;
+import com.android.settings.search.SearchIndexableRaw;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class SimSettings extends RestrictedSettingsFragment implements Indexable {
+ private static final String TAG = "SimSettings";
+
+ private static final String DISALLOW_CONFIG_SIM = "no_config_sim";
+ private static final String SIM_CARD_CATEGORY = "sim_cards";
+ private static final String KEY_CELLULAR_DATA = "sim_cellular_data";
+ private static final String KEY_CALLS = "sim_calls";
+ private static final String KEY_SMS = "sim_sms";
+ private static final String KEY_ACTIVITIES = "activities";
+
+ /**
+ * By UX design we have use only one Subscription Information(SubInfo) record per SIM slot.
+ * mAvalableSubInfos is the list of SubInfos we present to the user.
+ * mSubInfoList is the list of all SubInfos.
+ */
+ private List<SubInfoRecord> mAvailableSubInfos = null;
+ private List<SubInfoRecord> mSubInfoList = null;
+
+ private SubInfoRecord mCellularData = null;
+ private SubInfoRecord mCalls = null;
+ private SubInfoRecord mSMS = null;
+
+ private int mNumSims;
+
+ public SimSettings() {
+ super(DISALLOW_CONFIG_SIM);
+ }
+
+ @Override
+ public void onCreate(final Bundle bundle) {
+ super.onCreate(bundle);
+
+ if (mSubInfoList == null) {
+ mSubInfoList = SubscriptionManager.getActiveSubInfoList();
+ }
+
+ createPreferences();
+ updateAllOptions();
+ }
+
+ private void createPreferences() {
+ final TelephonyManager tm =
+ (TelephonyManager) getActivity().getSystemService(Context.TELEPHONY_SERVICE);
+
+ addPreferencesFromResource(R.xml.sim_settings);
+
+ final PreferenceCategory simCards = (PreferenceCategory)findPreference(SIM_CARD_CATEGORY);
+
+ final int numSlots = tm.getSimCount();
+ mAvailableSubInfos = new ArrayList<SubInfoRecord>(numSlots);
+ mNumSims = 0;
+ for (int i = 0; i < numSlots; ++i) {
+ final SubInfoRecord sir = findRecordBySlotId(i);
+ simCards.addPreference(new SimPreference(getActivity(), sir, i));
+ mAvailableSubInfos.add(sir);
+ if (sir != null) {
+ mNumSims++;
+ }
+ }
+
+ updateActivitesCategory();
+ }
+
+ private void updateAllOptions() {
+ updateSimSlotValues();
+ updateActivitesCategory();
+ }
+
+ private void updateSimSlotValues() {
+ SubscriptionManager.getAllSubInfoList();
+ final PreferenceCategory simCards = (PreferenceCategory)findPreference(SIM_CARD_CATEGORY);
+ final PreferenceScreen prefScreen = getPreferenceScreen();
+
+ final int prefSize = prefScreen.getPreferenceCount();
+ for (int i = 0; i < prefSize; ++i) {
+ Preference pref = prefScreen.getPreference(i);
+ if (pref instanceof SimPreference) {
+ ((SimPreference)pref).update();
+ }
+ }
+ }
+
+ private void updateActivitesCategory() {
+ createDropDown((DropDownPreference) findPreference(KEY_CELLULAR_DATA));
+ createDropDown((DropDownPreference) findPreference(KEY_CALLS));
+ createDropDown((DropDownPreference) findPreference(KEY_SMS));
+
+ updateCellularDataValues();
+ updateCallValues();
+ updateSmsValues();
+ }
+
+ /**
+ * finds a record with subId.
+ * Since the number of SIMs are few, an array is fine.
+ */
+ private SubInfoRecord findRecordBySubId(final long subId) {
+ final int availableSubInfoLength = mAvailableSubInfos.size();
+
+ for (int i = 0; i < availableSubInfoLength; ++i) {
+ final SubInfoRecord sir = mAvailableSubInfos.get(i);
+ if (sir != null && sir.subId == subId) {
+ return sir;
+ }
+ }
+
+ return null;
+ }
+
+ /**
+ * finds a record with slotId.
+ * Since the number of SIMs are few, an array is fine.
+ */
+ private SubInfoRecord findRecordBySlotId(final int slotId) {
+ if (mSubInfoList != null){
+ final int availableSubInfoLength = mSubInfoList.size();
+
+ for (int i = 0; i < availableSubInfoLength; ++i) {
+ final SubInfoRecord sir = mSubInfoList.get(i);
+ if (sir.slotId == slotId) {
+ //Right now we take the first subscription on a SIM.
+ return sir;
+ }
+ }
+ }
+
+ return null;
+ }
+
+ private void updateSmsValues() {
+ final DropDownPreference simPref = (DropDownPreference) findPreference(KEY_SMS);
+ final SubInfoRecord sir = findRecordBySubId(SubscriptionManager.getDefaultSmsSubId());
+ if (sir != null) {
+ simPref.setSelectedItem(sir.slotId + 1);
+ }
+ simPref.setEnabled(mNumSims > 1);
+ }
+
+ private void updateCellularDataValues() {
+ final DropDownPreference simPref = (DropDownPreference) findPreference(KEY_CELLULAR_DATA);
+ final SubInfoRecord sir = findRecordBySubId(SubscriptionManager.getDefaultDataSubId());
+ if (sir != null) {
+ simPref.setSelectedItem(sir.slotId);
+ }
+ simPref.setEnabled(mNumSims > 1);
+ }
+
+ private void updateCallValues() {
+ final DropDownPreference simPref = (DropDownPreference) findPreference(KEY_CALLS);
+ final SubInfoRecord sir = findRecordBySubId(SubscriptionManager.getDefaultVoiceSubId());
+ if (sir != null) {
+ simPref.setSelectedItem(sir.slotId + 1);
+ }
+ simPref.setEnabled(mNumSims > 1);
+ }
+
+ @Override
+ public void onResume() {
+ super.onResume();
+ updateAllOptions();
+ }
+
+ @Override
+ public boolean onPreferenceTreeClick(final PreferenceScreen preferenceScreen,
+ final Preference preference) {
+ if (preference instanceof SimPreference) {
+ ((SimPreference)preference).createEditDialog((SimPreference)preference);
+ }
+
+ return true;
+ }
+
+ public void createDropDown(DropDownPreference preference) {
+ final DropDownPreference simPref = preference;
+ final String keyPref = simPref.getKey();
+ final boolean askFirst = keyPref.equals(KEY_CALLS) || keyPref.equals(KEY_SMS);
+
+ simPref.clearItems();
+
+ if (askFirst) {
+ simPref.addItem(getResources().getString(
+ R.string.sim_calls_ask_first_prefs_title), null);
+ }
+
+ final int subAvailableSize = mAvailableSubInfos.size();
+ for (int i = 0; i < subAvailableSize; ++i) {
+ final SubInfoRecord sir = mAvailableSubInfos.get(i);
+ if(sir != null){
+ simPref.addItem(sir.displayName, sir);
+ }
+ }
+
+ simPref.setCallback(new DropDownPreference.Callback() {
+ @Override
+ public boolean onItemSelected(int pos, Object value) {
+ final long subId = value == null ? 0 : ((SubInfoRecord)value).subId;
+
+ if (simPref.getKey().equals(KEY_CELLULAR_DATA)) {
+ SubscriptionManager.setDefaultDataSubId(subId);
+ } else if (simPref.getKey().equals(KEY_CALLS)) {
+ SubscriptionManager.setDefaultVoiceSubId(subId);
+ } else if (simPref.getKey().equals(KEY_SMS)) {
+ // TODO: uncomment once implemented. Bug: 16520931
+ // SubscriptionManager.setDefaultSMSSubId(subId);
+ }
+
+ return true;
+ }
+ });
+ }
+
+ private void setActivity(Preference preference, SubInfoRecord sir) {
+ final String key = preference.getKey();
+
+ if (key.equals(KEY_CELLULAR_DATA)) {
+ mCellularData = sir;
+ } else if (key.equals(KEY_CALLS)) {
+ mCalls = sir;
+ } else if (key.equals(KEY_SMS)) {
+ mSMS = sir;
+ }
+
+ updateActivitesCategory();
+ }
+
+ private class SimPreference extends Preference{
+ private SubInfoRecord mSubInfoRecord;
+ private int mSlotId;
+
+ public SimPreference(Context context, SubInfoRecord subInfoRecord, int slotId) {
+ super(context);
+
+ mSubInfoRecord = subInfoRecord;
+ mSlotId = slotId;
+ setKey("sim" + mSlotId);
+ update();
+ }
+
+ public void update() {
+ final Resources res = getResources();
+
+ setTitle(res.getString(R.string.sim_card_number_title, mSlotId + 1));
+ if (mSubInfoRecord != null) {
+ setSummary(res.getString(R.string.sim_settings_summary,
+ mSubInfoRecord.displayName, mSubInfoRecord.number));
+ setEnabled(true);
+ } else {
+ setSummary(R.string.sim_slot_empty);
+ setFragment(null);
+ setEnabled(false);
+ }
+ }
+
+ public void createEditDialog(SimPreference simPref) {
+ AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
+
+ final View dialogLayout = getActivity().getLayoutInflater().inflate(
+ R.layout.multi_sim_dialog, null);
+ builder.setView(dialogLayout);
+
+ EditText nameText = (EditText)dialogLayout.findViewById(R.id.sim_name);
+ nameText.setText(mSubInfoRecord.displayName);
+
+ TextView numberView = (TextView)dialogLayout.findViewById(R.id.number);
+ numberView.setText(mSubInfoRecord.number);
+
+ TextView carrierView = (TextView)dialogLayout.findViewById(R.id.carrier);
+ carrierView.setText(mSubInfoRecord.displayName);
+
+ builder.setTitle(R.string.sim_editor_title);
+
+ builder.setPositiveButton(R.string.okay, new DialogInterface.OnClickListener() {
+ @Override
+ public void onClick(DialogInterface dialog, int whichButton) {
+ final EditText nameText = (EditText)dialogLayout.findViewById(R.id.sim_name);
+ final Spinner displayNumbers =
+ (Spinner)dialogLayout.findViewById(R.id.display_numbers);
+
+ SubscriptionManager.setDisplayNumberFormat(
+ displayNumbers.getSelectedItemPosition() == 0
+ ? SubscriptionManager.DISPLAY_NUMBER_LAST
+ : SubscriptionManager.DISPLAY_NUMBER_FIRST, mSubInfoRecord.subId);
+
+ mSubInfoRecord.displayName = nameText.getText().toString();
+ SubscriptionManager.setDisplayName(mSubInfoRecord.displayName,
+ mSubInfoRecord.subId);
+
+ updateAllOptions();
+ update();
+ }
+ });
+
+ builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
+ @Override
+ public void onClick(DialogInterface dialog, int whichButton) {
+ dialog.dismiss();
+ }
+ });
+
+ builder.create().show();
+ }
+ }
+
+ /**
+ * For search
+ */
+ public static final SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
+ new BaseSearchIndexProvider() {
+ @Override
+ public List<SearchIndexableResource> getXmlResourcesToIndex(Context context,
+ boolean enabled) {
+ ArrayList<SearchIndexableResource> result =
+ new ArrayList<SearchIndexableResource>();
+
+ if (Utils.showSimCardTile(context)) {
+ SearchIndexableResource sir = new SearchIndexableResource(context);
+ sir.xmlResId = R.xml.sim_settings;
+ result.add(sir);
+ }
+
+ return result;
+ }
+ };
+
+}