Files
app_Settings/src/com/android/settings/sim/SimDialogActivity.java
Antony Sargent a7dc277459 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
2019-04-15 06:16:52 -07:00

171 lines
6.6 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.content.Intent;
import android.os.Bundle;
import android.telecom.PhoneAccount;
import android.telecom.PhoneAccountHandle;
import android.telecom.TelecomManager;
import android.telephony.SubscriptionManager;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Toast;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import com.android.settings.R;
import java.util.List;
/**
* 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";
public static String DIALOG_TYPE_KEY = "dialog_type";
public static final int INVALID_PICK = -1;
public static final int DATA_PICK = 0;
public static final int CALLS_PICK = 1;
public static final int SMS_PICK = 2;
public static final int PREFERRED_PICK = 3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
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:
setDefaultSmsSubId(subId);
break;
case PREFERRED_PICK:
setPreferredSim(subId);
break;
default:
throw new IllegalArgumentException(
"Invalid dialog type " + dialogType + " sent.");
}
}
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(this, R.string.data_switch_started, Toast.LENGTH_LONG).show();
}
private void setDefaultCallsSubId(final int subId) {
final PhoneAccountHandle phoneAccount = subscriptionIdToPhoneAccountHandle(subId);
final TelecomManager telecomManager = getSystemService(TelecomManager.class);
telecomManager.setUserSelectedOutgoingPhoneAccount(phoneAccount);
}
private void setDefaultSmsSubId(final int subId) {
final SubscriptionManager subscriptionManager = getSystemService(SubscriptionManager.class);
subscriptionManager.setDefaultSmsSubId(subId);
}
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 handle;
}
}
return null;
}
}