Files
app_Settings/src/com/android/settings/sim/SimSettings.java
PauloftheWest 50e6ecacf9 [DS] DSDS support for Settings
+ Rebase and modify as APIs rename.
+ Fix bugs on set sub's name as title.
+ Enabled Sim Settings.

Change-Id: Ic731c7882be95b86b6b8dcdd3f208a6125681f3e
2014-10-03 15:22:59 -07:00

525 lines
19 KiB
Java

/*
* 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.app.Fragment;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.ContentUris;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.DialogInterface;
import android.content.res.Resources;
import android.database.Cursor;
import android.net.Uri;
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.PreferenceActivity;
import android.preference.PreferenceCategory;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.PreferenceScreen;
import android.provider.Telephony;
import android.telephony.SubInfoRecord;
import android.telephony.SubscriptionManager;
import android.telephony.TelephonyManager;
import android.telephony.PhoneNumberUtils;
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.PhoneFactory;
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.Collections;
import java.util.Comparator;
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";
private static final int ID_INDEX = 0;
private static final int NAME_INDEX = 1;
private static final int APN_INDEX = 2;
private static final int PROXY_INDEX = 3;
private static final int PORT_INDEX = 4;
private static final int USER_INDEX = 5;
private static final int SERVER_INDEX = 6;
private static final int PASSWORD_INDEX = 7;
private static final int MMSC_INDEX = 8;
private static final int MCC_INDEX = 9;
private static final int MNC_INDEX = 10;
private static final int NUMERIC_INDEX = 11;
private static final int MMSPROXY_INDEX = 12;
private static final int MMSPORT_INDEX = 13;
private static final int AUTH_TYPE_INDEX = 14;
private static final int TYPE_INDEX = 15;
private static final int PROTOCOL_INDEX = 16;
private static final int CARRIER_ENABLED_INDEX = 17;
private static final int BEARER_INDEX = 18;
private static final int ROAMING_PROTOCOL_INDEX = 19;
private static final int MVNO_TYPE_INDEX = 20;
private static final int MVNO_MATCH_DATA_INDEX = 21;
/**
* By UX design we 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 PreferenceCategory mSimCards = null;
private int mNumSims;
/**
* Standard projection for the interesting columns of a normal note.
*/
private static final String[] sProjection = new String[] {
Telephony.Carriers._ID, // 0
Telephony.Carriers.NAME, // 1
Telephony.Carriers.APN, // 2
Telephony.Carriers.PROXY, // 3
Telephony.Carriers.PORT, // 4
Telephony.Carriers.USER, // 5
Telephony.Carriers.SERVER, // 6
Telephony.Carriers.PASSWORD, // 7
Telephony.Carriers.MMSC, // 8
Telephony.Carriers.MCC, // 9
Telephony.Carriers.MNC, // 10
Telephony.Carriers.NUMERIC, // 11
Telephony.Carriers.MMSPROXY,// 12
Telephony.Carriers.MMSPORT, // 13
Telephony.Carriers.AUTH_TYPE, // 14
Telephony.Carriers.TYPE, // 15
Telephony.Carriers.PROTOCOL, // 16
Telephony.Carriers.CARRIER_ENABLED, // 17
Telephony.Carriers.BEARER, // 18
Telephony.Carriers.ROAMING_PROTOCOL, // 19
Telephony.Carriers.MVNO_TYPE, // 20
Telephony.Carriers.MVNO_MATCH_DATA // 21
};
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);
mSimCards = (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);
mSimCards.addPreference(new SimPreference(getActivity(), sir, i));
mAvailableSubInfos.add(sir);
if (sir != null) {
mNumSims++;
}
}
updateActivitesCategory();
}
private void updateAvailableSubInfos(){
final TelephonyManager tm =
(TelephonyManager) getActivity().getSystemService(Context.TELEPHONY_SERVICE);
final int numSlots = tm.getSimCount();
mNumSims = 0;
mAvailableSubInfos = new ArrayList<SubInfoRecord>(numSlots);
for (int i = 0; i < numSlots; ++i) {
final SubInfoRecord sir = findRecordBySlotId(i);
mAvailableSubInfos.add(sir);
if (sir != null) {
mNumSims++;
}
}
}
private void updateAllOptions() {
updateSimSlotValues();
updateActivitesCategory();
}
private void updateSimSlotValues() {
SubscriptionManager.getAllSubInfoList();
final int prefSize = mSimCards.getPreferenceCount();
for (int i = 0; i < prefSize; ++i) {
Preference pref = mSimCards.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 (mSubInfoList.size() == 1) {
simPref.setSelectedItem(mSubInfoList.get(0).slotId + 1);
} else 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 (mSubInfoList.size() == 1) {
simPref.setSelectedItem(mSubInfoList.get(0).slotId);
} else 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 (mSubInfoList.size() == 1) {
simPref.setSelectedItem(mSubInfoList.get(0).slotId + 1);
} else if (sir != null) {
simPref.setSelectedItem(sir.slotId + 1);
}
simPref.setEnabled(mNumSims >= 1);
}
@Override
public void onResume() {
super.onResume();
mSubInfoList = SubscriptionManager.getActiveSubInfoList();
updateAvailableSubInfos();
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)) {
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 String getCarrierName() {
Uri mUri = ContentUris.withAppendedId(Telephony.Carriers.CONTENT_URI, mSubInfoRecord.subId);
Cursor mCursor = getActivity().managedQuery(mUri, sProjection, null, null);
mCursor.moveToFirst();
return mCursor.getString(1);
}
public String getFormattedPhoneNumber() {
try{
final String rawNumber = PhoneFactory.getPhone(mSlotId).getLine1Number();
String formattedNumber = null;
if (!TextUtils.isEmpty(rawNumber)) {
formattedNumber = PhoneNumberUtils.formatNumber(rawNumber);
}
return formattedNumber;
} catch (java.lang.IllegalStateException ise){
return "Unknown";
}
}
public SubInfoRecord getSubInfoRecord() {
return mSubInfoRecord;
}
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(simPref.getFormattedPhoneNumber());
TextView carrierView = (TextView)dialogLayout.findViewById(R.id.carrier);
carrierView.setText(getCarrierName());
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();
}
}
/**
* Sort Subscription List in SIM Id, Subscription Id
* @param context The Context
* @return Sorted Subscription List or NULL if no activated Subscription
*/
public static List<SubInfoRecord> getSortedSubInfoList(Context context) {
List<SubInfoRecord> infoList = SubscriptionManager.getActiveSubInfoList();
if (infoList != null) {
Collections.sort(infoList, new Comparator<SubInfoRecord>() {
@Override
public int compare(SubInfoRecord arg0, SubInfoRecord arg1) {
int flag = arg0.slotId - arg1.slotId;
if (flag == 0) {
return (int) (arg0.subId - arg1.subId);
}
return flag;
}
});
}
return infoList;
}
/**
* 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;
}
};
}