Fix problem of multiple stacked copies of "Select SIM" dialog
The SimDialogActivity is used to ask the user questions about which SIM card to use for various services like calls, SMS, and data. In some cases of SIM changes (eg when a SIM is added or removed), the telephony stack sends a broadcast that SimSelectNotification listens for so it can pop up a general "SIM cards changed" notification, and we additionally want to bring up an interruptive dialog to ask the user a specific question. This might happen for instance when we want to ask the user's permission to turn on data on a SIM. Recent DSDS changes in the telephony stack have meant that we accidentally create several stacked copies of this dialog, because they send several broadcast updates as information about SIMs asynchronously changes. For instance, we might initially detect a SIM with a generic name of "CARD 1", and shortly after discover the actual carrier name. So what we really want is to put up the dialog, and update it as information changes. This CL makes SimDialogActivity use launchMode="singleTop" so that additional copies of the activity won't be launched. Then it internally enforces only showing one dialog per type of request (calls, SMS, data, or preferred sim). If we get a request for a dialog that already exists, we just update it instead of creating a new one for that type. So there can still be a stack of more than one dialog, but each one will be asking a different question. This also refactors the monolithic, somewhat confusing code for showing the various types of dialogs into a more clearly separated class hierarchy, and switches to using DialogFragment for the dialog. Fixes: 126596081 Test: manual (start with device in DSDS mode with 2 subs, remove SIM card and re-insert it) Change-Id: I0dbc41dc3b15015389823a24df10bbff08ec6615
This commit is contained in:
@@ -16,37 +16,30 @@
|
||||
|
||||
package com.android.settings.sim;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.app.Dialog;
|
||||
import android.content.Context;
|
||||
import android.content.DialogInterface;
|
||||
import android.content.res.Resources;
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.telecom.PhoneAccount;
|
||||
import android.telecom.PhoneAccountHandle;
|
||||
import android.telecom.TelecomManager;
|
||||
import android.telephony.SubscriptionInfo;
|
||||
import android.telephony.SubscriptionManager;
|
||||
import android.telephony.TelephonyManager;
|
||||
import android.view.KeyEvent;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.ArrayAdapter;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.ListAdapter;
|
||||
import android.widget.TextView;
|
||||
import android.util.Log;
|
||||
import android.widget.Toast;
|
||||
|
||||
import androidx.appcompat.app.AlertDialog;
|
||||
import androidx.fragment.app.Fragment;
|
||||
import androidx.fragment.app.FragmentActivity;
|
||||
import androidx.fragment.app.FragmentManager;
|
||||
|
||||
import com.android.settings.R;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
public class SimDialogActivity extends Activity {
|
||||
/**
|
||||
* This activity provides singleton semantics per dialog type for showing various kinds of
|
||||
* dialogs asking the user to make choices about which SIM to use for various services
|
||||
* (calls, SMS, and data).
|
||||
*/
|
||||
public class SimDialogActivity extends FragmentActivity {
|
||||
private static String TAG = "SimDialogActivity";
|
||||
|
||||
public static String PREFERRED_SIM = "preferred_sim";
|
||||
@@ -60,276 +53,118 @@ public class SimDialogActivity extends Activity {
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
final int dialogType = getIntent().getIntExtra(DIALOG_TYPE_KEY, INVALID_PICK);
|
||||
showOrUpdateDialog();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onNewIntent(Intent intent) {
|
||||
super.onNewIntent(intent);
|
||||
setIntent(intent);
|
||||
showOrUpdateDialog();
|
||||
}
|
||||
|
||||
private void showOrUpdateDialog() {
|
||||
final int dialogType = getIntent().getIntExtra(DIALOG_TYPE_KEY, INVALID_PICK);
|
||||
final String tag = Integer.toString(dialogType);
|
||||
final FragmentManager fragmentManager = getSupportFragmentManager();
|
||||
SimDialogFragment fragment = (SimDialogFragment) fragmentManager.findFragmentByTag(tag);
|
||||
|
||||
if (fragment == null) {
|
||||
fragment = createFragment(dialogType);
|
||||
fragment.show(fragmentManager, tag);
|
||||
} else {
|
||||
fragment.updateDialog();
|
||||
}
|
||||
}
|
||||
|
||||
private SimDialogFragment createFragment(int dialogType) {
|
||||
switch(dialogType) {
|
||||
case DATA_PICK:
|
||||
return SimListDialogFragment.newInstance(dialogType, R.string.select_sim_for_data,
|
||||
false /* includeAskEveryTime */);
|
||||
case CALLS_PICK:
|
||||
return CallsSimListDialogFragment.newInstance(dialogType, R.string.select_sim_for_calls,
|
||||
true /* includeAskEveryTime */);
|
||||
case SMS_PICK:
|
||||
return SimListDialogFragment.newInstance(dialogType, R.string.select_sim_for_sms,
|
||||
false /* includeAskEveryTime */);
|
||||
case PREFERRED_PICK:
|
||||
if (!getIntent().hasExtra(PREFERRED_SIM)) {
|
||||
throw new IllegalArgumentException("Missing required extra " + PREFERRED_SIM);
|
||||
}
|
||||
return PreferredSimDialogFragment.newInstance();
|
||||
default:
|
||||
throw new IllegalArgumentException( "Invalid dialog type " + dialogType + " sent.");
|
||||
}
|
||||
}
|
||||
|
||||
public void onSubscriptionSelected(int dialogType, int subId) {
|
||||
if (getSupportFragmentManager().findFragmentByTag(Integer.toString(dialogType)) == null) {
|
||||
Log.w(TAG, "onSubscriptionSelected ignored because stored fragment was null");
|
||||
return;
|
||||
}
|
||||
switch (dialogType) {
|
||||
case DATA_PICK:
|
||||
setDefaultDataSubId(subId);
|
||||
break;
|
||||
case CALLS_PICK:
|
||||
setDefaultCallsSubId(subId);
|
||||
break;
|
||||
case SMS_PICK:
|
||||
createDialog(this, dialogType).show();
|
||||
setDefaultSmsSubId(subId);
|
||||
break;
|
||||
case PREFERRED_PICK:
|
||||
displayPreferredDialog(getIntent().getIntExtra(PREFERRED_SIM, 0));
|
||||
setPreferredSim(subId);
|
||||
break;
|
||||
default:
|
||||
throw new IllegalArgumentException("Invalid dialog type " + dialogType + " sent.");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void displayPreferredDialog(final int slotId) {
|
||||
final Resources res = getResources();
|
||||
final Context context = getApplicationContext();
|
||||
final SubscriptionInfo sir = SubscriptionManager.from(context)
|
||||
.getActiveSubscriptionInfoForSimSlotIndex(slotId);
|
||||
|
||||
if (sir != null) {
|
||||
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
|
||||
alertDialogBuilder.setTitle(R.string.sim_preferred_title);
|
||||
alertDialogBuilder.setMessage(res.getString(
|
||||
R.string.sim_preferred_message, sir.getDisplayName()));
|
||||
|
||||
alertDialogBuilder.setPositiveButton(R.string.yes, new
|
||||
DialogInterface.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(DialogInterface dialog, int id) {
|
||||
final int subId = sir.getSubscriptionId();
|
||||
PhoneAccountHandle phoneAccountHandle =
|
||||
subscriptionIdToPhoneAccountHandle(subId);
|
||||
setDefaultDataSubId(context, subId);
|
||||
setDefaultSmsSubId(context, subId);
|
||||
setUserSelectedOutgoingPhoneAccount(phoneAccountHandle);
|
||||
finish();
|
||||
}
|
||||
});
|
||||
alertDialogBuilder.setNegativeButton(R.string.no, new
|
||||
DialogInterface.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(DialogInterface dialog,int id) {
|
||||
finish();
|
||||
}
|
||||
});
|
||||
|
||||
alertDialogBuilder.create().show();
|
||||
} else {
|
||||
finish();
|
||||
throw new IllegalArgumentException(
|
||||
"Invalid dialog type " + dialogType + " sent.");
|
||||
}
|
||||
}
|
||||
|
||||
private static void setDefaultDataSubId(final Context context, final int subId) {
|
||||
final SubscriptionManager subscriptionManager = SubscriptionManager.from(context);
|
||||
final TelephonyManager telephonyManager = TelephonyManager.from(context)
|
||||
.createForSubscriptionId(subId);
|
||||
public void onFragmentDismissed(SimDialogFragment simDialogFragment) {
|
||||
final List<Fragment> fragments = getSupportFragmentManager().getFragments();
|
||||
if (fragments.size() == 1 && fragments.get(0) == simDialogFragment) {
|
||||
finishAndRemoveTask();
|
||||
}
|
||||
}
|
||||
|
||||
private void setDefaultDataSubId(final int subId) {
|
||||
final SubscriptionManager subscriptionManager = getSystemService(SubscriptionManager.class);
|
||||
final TelephonyManager telephonyManager = getSystemService(
|
||||
TelephonyManager.class).createForSubscriptionId(subId);
|
||||
subscriptionManager.setDefaultDataSubId(subId);
|
||||
telephonyManager.setDataEnabled(true);
|
||||
Toast.makeText(context, R.string.data_switch_started, Toast.LENGTH_LONG).show();
|
||||
Toast.makeText(this, R.string.data_switch_started, Toast.LENGTH_LONG).show();
|
||||
}
|
||||
|
||||
private static void setDefaultSmsSubId(final Context context, final int subId) {
|
||||
final SubscriptionManager subscriptionManager = SubscriptionManager.from(context);
|
||||
subscriptionManager.setDefaultSmsSubId(subId);
|
||||
}
|
||||
|
||||
private void setUserSelectedOutgoingPhoneAccount(PhoneAccountHandle phoneAccount) {
|
||||
final TelecomManager telecomManager = TelecomManager.from(this);
|
||||
private void setDefaultCallsSubId(final int subId) {
|
||||
final PhoneAccountHandle phoneAccount = subscriptionIdToPhoneAccountHandle(subId);
|
||||
final TelecomManager telecomManager = getSystemService(TelecomManager.class);
|
||||
telecomManager.setUserSelectedOutgoingPhoneAccount(phoneAccount);
|
||||
}
|
||||
|
||||
private PhoneAccountHandle subscriptionIdToPhoneAccountHandle(final int subId) {
|
||||
final TelecomManager telecomManager = TelecomManager.from(this);
|
||||
final TelephonyManager telephonyManager = TelephonyManager.from(this);
|
||||
final Iterator<PhoneAccountHandle> phoneAccounts =
|
||||
telecomManager.getCallCapablePhoneAccounts().listIterator();
|
||||
private void setDefaultSmsSubId(final int subId) {
|
||||
final SubscriptionManager subscriptionManager = getSystemService(SubscriptionManager.class);
|
||||
subscriptionManager.setDefaultSmsSubId(subId);
|
||||
}
|
||||
|
||||
while (phoneAccounts.hasNext()) {
|
||||
final PhoneAccountHandle phoneAccountHandle = phoneAccounts.next();
|
||||
final PhoneAccount phoneAccount = telecomManager.getPhoneAccount(phoneAccountHandle);
|
||||
private void setPreferredSim(final int subId) {
|
||||
setDefaultDataSubId(subId);
|
||||
setDefaultSmsSubId(subId);
|
||||
setDefaultCallsSubId(subId);
|
||||
}
|
||||
|
||||
private PhoneAccountHandle subscriptionIdToPhoneAccountHandle(final int subId) {
|
||||
final TelecomManager telecomManager = getSystemService(TelecomManager.class);
|
||||
final TelephonyManager telephonyManager = getSystemService(TelephonyManager.class);
|
||||
|
||||
for (PhoneAccountHandle handle : telecomManager.getCallCapablePhoneAccounts()) {
|
||||
final PhoneAccount phoneAccount = telecomManager.getPhoneAccount(handle);
|
||||
if (subId == telephonyManager.getSubIdForPhoneAccount(phoneAccount)) {
|
||||
return phoneAccountHandle;
|
||||
return handle;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public Dialog createDialog(final Context context, final int id) {
|
||||
final ArrayList<String> list = new ArrayList<String>();
|
||||
final SubscriptionManager subscriptionManager = SubscriptionManager.from(context);
|
||||
final List<SubscriptionInfo> subInfoList =
|
||||
subscriptionManager.getActiveSubscriptionInfoList(true);
|
||||
final int selectableSubInfoLength = subInfoList == null ? 0 : subInfoList.size();
|
||||
|
||||
final DialogInterface.OnClickListener selectionListener =
|
||||
new DialogInterface.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(DialogInterface dialog, int value) {
|
||||
|
||||
final SubscriptionInfo sir;
|
||||
|
||||
switch (id) {
|
||||
case DATA_PICK:
|
||||
sir = subInfoList.get(value);
|
||||
setDefaultDataSubId(context, sir.getSubscriptionId());
|
||||
break;
|
||||
case CALLS_PICK:
|
||||
final TelecomManager telecomManager =
|
||||
TelecomManager.from(context);
|
||||
final List<PhoneAccountHandle> phoneAccountsList =
|
||||
telecomManager.getCallCapablePhoneAccounts();
|
||||
setUserSelectedOutgoingPhoneAccount(
|
||||
value < 1 ? null : phoneAccountsList.get(value - 1));
|
||||
break;
|
||||
case SMS_PICK:
|
||||
sir = subInfoList.get(value);
|
||||
setDefaultSmsSubId(context, sir.getSubscriptionId());
|
||||
break;
|
||||
default:
|
||||
throw new IllegalArgumentException("Invalid dialog type "
|
||||
+ id + " in SIM dialog.");
|
||||
}
|
||||
|
||||
finish();
|
||||
}
|
||||
};
|
||||
|
||||
Dialog.OnKeyListener keyListener = new Dialog.OnKeyListener() {
|
||||
@Override
|
||||
public boolean onKey(DialogInterface arg0, int keyCode,
|
||||
KeyEvent event) {
|
||||
if (keyCode == KeyEvent.KEYCODE_BACK) {
|
||||
finish();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
ArrayList<SubscriptionInfo> callsSubInfoList = new ArrayList<SubscriptionInfo>();
|
||||
if (id == CALLS_PICK) {
|
||||
final TelecomManager telecomManager = TelecomManager.from(context);
|
||||
final TelephonyManager telephonyManager = TelephonyManager.from(context);
|
||||
final Iterator<PhoneAccountHandle> phoneAccounts =
|
||||
telecomManager.getCallCapablePhoneAccounts().listIterator();
|
||||
|
||||
list.add(getResources().getString(R.string.sim_calls_ask_first_prefs_title));
|
||||
callsSubInfoList.add(null);
|
||||
while (phoneAccounts.hasNext()) {
|
||||
final PhoneAccount phoneAccount =
|
||||
telecomManager.getPhoneAccount(phoneAccounts.next());
|
||||
list.add((String)phoneAccount.getLabel());
|
||||
int subId = telephonyManager.getSubIdForPhoneAccount(phoneAccount);
|
||||
if (subId != SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
|
||||
final SubscriptionInfo sir = SubscriptionManager.from(context)
|
||||
.getActiveSubscriptionInfo(subId);
|
||||
callsSubInfoList.add(sir);
|
||||
} else {
|
||||
callsSubInfoList.add(null);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for (int i = 0; i < selectableSubInfoLength; ++i) {
|
||||
final SubscriptionInfo sir = subInfoList.get(i);
|
||||
CharSequence displayName = sir.getDisplayName();
|
||||
if (displayName == null) {
|
||||
displayName = "";
|
||||
}
|
||||
list.add(displayName.toString());
|
||||
}
|
||||
}
|
||||
|
||||
String[] arr = list.toArray(new String[0]);
|
||||
|
||||
AlertDialog.Builder builder = new AlertDialog.Builder(context);
|
||||
|
||||
ListAdapter adapter = new SelectAccountListAdapter(
|
||||
id == CALLS_PICK ? callsSubInfoList : subInfoList,
|
||||
builder.getContext(),
|
||||
R.layout.select_account_list_item,
|
||||
arr, id);
|
||||
|
||||
switch (id) {
|
||||
case DATA_PICK:
|
||||
builder.setTitle(R.string.select_sim_for_data);
|
||||
break;
|
||||
case CALLS_PICK:
|
||||
builder.setTitle(R.string.select_sim_for_calls);
|
||||
break;
|
||||
case SMS_PICK:
|
||||
builder.setTitle(R.string.select_sim_for_sms);
|
||||
break;
|
||||
default:
|
||||
throw new IllegalArgumentException("Invalid dialog type "
|
||||
+ id + " in SIM dialog.");
|
||||
}
|
||||
|
||||
Dialog dialog = builder.setAdapter(adapter, selectionListener).create();
|
||||
dialog.setOnKeyListener(keyListener);
|
||||
|
||||
dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
|
||||
@Override
|
||||
public void onCancel(DialogInterface dialogInterface) {
|
||||
finish();
|
||||
}
|
||||
});
|
||||
|
||||
return dialog;
|
||||
|
||||
}
|
||||
|
||||
private class SelectAccountListAdapter extends ArrayAdapter<String> {
|
||||
private Context mContext;
|
||||
private int mResId;
|
||||
private int mDialogId;
|
||||
private final float OPACITY = 0.54f;
|
||||
private List<SubscriptionInfo> mSubInfoList;
|
||||
|
||||
public SelectAccountListAdapter(List<SubscriptionInfo> subInfoList,
|
||||
Context context, int resource, String[] arr, int dialogId) {
|
||||
super(context, resource, arr);
|
||||
mContext = context;
|
||||
mResId = resource;
|
||||
mDialogId = dialogId;
|
||||
mSubInfoList = subInfoList;
|
||||
}
|
||||
|
||||
@Override
|
||||
public View getView(int position, View convertView, ViewGroup parent) {
|
||||
LayoutInflater inflater = (LayoutInflater)
|
||||
mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
|
||||
View rowView;
|
||||
final ViewHolder holder;
|
||||
|
||||
if (convertView == null) {
|
||||
// Cache views for faster scrolling
|
||||
rowView = inflater.inflate(mResId, null);
|
||||
holder = new ViewHolder();
|
||||
holder.title = (TextView) rowView.findViewById(R.id.title);
|
||||
holder.summary = (TextView) rowView.findViewById(R.id.summary);
|
||||
holder.icon = (ImageView) rowView.findViewById(R.id.icon);
|
||||
rowView.setTag(holder);
|
||||
} else {
|
||||
rowView = convertView;
|
||||
holder = (ViewHolder) rowView.getTag();
|
||||
}
|
||||
|
||||
final SubscriptionInfo sir = mSubInfoList.get(position);
|
||||
if (sir == null) {
|
||||
holder.title.setText(getItem(position));
|
||||
holder.summary.setText("");
|
||||
holder.icon.setImageDrawable(getResources()
|
||||
.getDrawable(R.drawable.ic_live_help));
|
||||
holder.icon.setAlpha(OPACITY);
|
||||
} else {
|
||||
holder.title.setText(sir.getDisplayName());
|
||||
holder.summary.setText(sir.getNumber());
|
||||
holder.icon.setImageBitmap(sir.createIconBitmap(mContext));
|
||||
}
|
||||
return rowView;
|
||||
}
|
||||
|
||||
private class ViewHolder {
|
||||
TextView title;
|
||||
TextView summary;
|
||||
ImageView icon;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user