From 0da056b2dd542f8cdca9ba05b89a67a5dad4b516 Mon Sep 17 00:00:00 2001 From: Bonian Chen Date: Tue, 15 Dec 2020 03:57:43 +0800 Subject: [PATCH] [Settings] Close reset network confirm dialog when SIM removed When SIM removed, the network reset confirm UI no longer represents the wordings which presented to the user. Proceed on resetting will only reset APN on removed SIM and lead to some side effect if user inserted a new SIM while keeping displaying the confirmation dialog. With this situation, close the confirmation dialog is current decision in design. And the UI which prior to the confirm UI also need to be updated to avoid from user accessing that removed SIM. Bug: 171070050 Test: manual Change-Id: I338835ca98593f95d98bafa70f12b177c43bf91a --- src/com/android/settings/ResetNetwork.java | 39 +++++++++-- .../android/settings/ResetNetworkConfirm.java | 68 +++++++++++++++++++ 2 files changed, 102 insertions(+), 5 deletions(-) diff --git a/src/com/android/settings/ResetNetwork.java b/src/com/android/settings/ResetNetwork.java index 5ff59b5e61a..c4fa90d3bc8 100644 --- a/src/com/android/settings/ResetNetwork.java +++ b/src/com/android/settings/ResetNetwork.java @@ -32,6 +32,7 @@ import android.telephony.SubscriptionInfo; import android.telephony.SubscriptionManager; import android.telephony.euicc.EuiccManager; import android.text.TextUtils; +import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; @@ -53,7 +54,9 @@ import com.android.settingslib.RestrictedLockUtilsInternal; import com.android.settingslib.development.DevelopmentSettingsEnabler; import java.util.ArrayList; +import java.util.Collections; import java.util.List; +import java.util.Optional; /** * Confirm and execute a reset of the device's network settings to a clean "just out of the box" @@ -112,7 +115,7 @@ public class ResetNetwork extends InstrumentedFragment { if (resultCode == Activity.RESULT_OK) { showFinalConfirmation(); } else { - establishInitialState(); + establishInitialState(getActiveSubscriptionInfoList()); } } @@ -161,14 +164,15 @@ public class ResetNetwork extends InstrumentedFragment { * inflate each view, caching all of the widget pointers we'll need at the * time, then simply reuse the inflated views directly whenever we need * to change contents. + * + * @param subscriptionsList is a list of SubscriptionInfo(s) which allow user to select from */ - private void establishInitialState() { + private void establishInitialState(List subscriptionsList) { mSubscriptionSpinner = (Spinner) mContentView.findViewById(R.id.reset_network_subscription); mEsimContainer = mContentView.findViewById(R.id.erase_esim_container); mEsimCheckbox = mContentView.findViewById(R.id.erase_esim); - mSubscriptions = SubscriptionManager.from(getActivity()) - .getActiveSubscriptionInfoList(); + mSubscriptions = subscriptionsList; if (mSubscriptions != null && mSubscriptions.size() > 0) { // Get the default subscription in the order of data, voice, sms, first up. int defaultSubscription = SubscriptionManager.getDefaultDataSubscriptionId(); @@ -231,6 +235,31 @@ public class ResetNetwork extends InstrumentedFragment { } } + private List getActiveSubscriptionInfoList() { + SubscriptionManager mgr = getActivity().getSystemService(SubscriptionManager.class); + if (mgr == null) { + Log.w(TAG, "No SubscriptionManager"); + return Collections.emptyList(); + } + return Optional.ofNullable(mgr.getActiveSubscriptionInfoList()) + .orElse(Collections.emptyList()); + } + + @Override + public void onResume() { + super.onResume(); + + // update options if subcription has been changed + List updatedSubscriptions = getActiveSubscriptionInfoList(); + if ((mSubscriptions != null) + && (mSubscriptions.size() == updatedSubscriptions.size()) + && mSubscriptions.containsAll(updatedSubscriptions)) { + return; + } + Log.d(TAG, "subcription list changed"); + establishInitialState(updatedSubscriptions); + } + private boolean showEuiccSettings(Context context) { EuiccManager euiccManager = (EuiccManager) context.getSystemService(Context.EUICC_SERVICE); @@ -261,7 +290,7 @@ public class ResetNetwork extends InstrumentedFragment { mContentView = inflater.inflate(R.layout.reset_network, null); - establishInitialState(); + establishInitialState(getActiveSubscriptionInfoList()); return mContentView; } diff --git a/src/com/android/settings/ResetNetworkConfirm.java b/src/com/android/settings/ResetNetworkConfirm.java index 97139c4f6ed..109d67afc09 100644 --- a/src/com/android/settings/ResetNetworkConfirm.java +++ b/src/com/android/settings/ResetNetworkConfirm.java @@ -32,11 +32,14 @@ import android.net.wifi.WifiManager; import android.net.wifi.p2p.WifiP2pManager; import android.os.AsyncTask; import android.os.Bundle; +import android.os.Looper; import android.os.RecoverySystem; import android.os.UserHandle; import android.os.UserManager; import android.telephony.SubscriptionManager; +import android.telephony.SubscriptionManager.OnSubscriptionsChangedListener; import android.telephony.TelephonyManager; +import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -63,6 +66,7 @@ import com.android.settingslib.RestrictedLockUtilsInternal; * This is the confirmation screen. */ public class ResetNetworkConfirm extends InstrumentedFragment { + private static final String TAG = "ResetNetworkConfirm"; @VisibleForTesting View mContentView; @VisibleForTesting boolean mEraseEsim; @@ -71,6 +75,7 @@ public class ResetNetworkConfirm extends InstrumentedFragment { private int mSubId = SubscriptionManager.INVALID_SUBSCRIPTION_ID; private ProgressDialog mProgressDialog; private AlertDialog mAlertDialog; + private OnSubscriptionsChangedListener mSubscriptionsChangedListener; /** * Async task used to do all reset task. If error happens during @@ -161,6 +166,18 @@ public class ResetNetworkConfirm extends InstrumentedFragment { return; } + // abandon execution if subscription no longer active + if (mSubId != SubscriptionManager.INVALID_SUBSCRIPTION_ID) { + SubscriptionManager mgr = getSubscriptionManager(); + // always remove listener + stopMonitorSubscriptionChange(mgr); + if (!isSubscriptionRemainActive(mgr, mSubId)) { + Log.w(TAG, "subId " + mSubId + " disappear when confirm"); + mActivity.finish(); + return; + } + } + mProgressDialog = getProgressDialog(mActivity); mProgressDialog.show(); @@ -255,6 +272,56 @@ public class ResetNetworkConfirm extends InstrumentedFragment { } mActivity = getActivity(); + + if (mSubId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) { + return; + } + // close confirmation dialog when reset specific subscription + // but removed priori to the confirmation button been pressed + startMonitorSubscriptionChange(getSubscriptionManager()); + } + + private SubscriptionManager getSubscriptionManager() { + SubscriptionManager mgr = mActivity.getSystemService(SubscriptionManager.class); + if (mgr == null) { + Log.w(TAG, "No SubscriptionManager"); + } + return mgr; + } + + private void startMonitorSubscriptionChange(SubscriptionManager mgr) { + if (mgr == null) { + return; + } + // update monitor listener + mSubscriptionsChangedListener = new OnSubscriptionsChangedListener( + Looper.getMainLooper()) { + @Override + public void onSubscriptionsChanged() { + SubscriptionManager mgr = getSubscriptionManager(); + if (isSubscriptionRemainActive(mgr, mSubId)) { + return; + } + // close UI if subscription no longer active + Log.w(TAG, "subId " + mSubId + " no longer active."); + stopMonitorSubscriptionChange(mgr); + mActivity.finish(); + } + }; + mgr.addOnSubscriptionsChangedListener( + mActivity.getMainExecutor(), mSubscriptionsChangedListener); + } + + private boolean isSubscriptionRemainActive(SubscriptionManager mgr, int subscriptionId) { + return (mgr == null) ? false : (mgr.getActiveSubscriptionInfo(subscriptionId) != null); + } + + private void stopMonitorSubscriptionChange(SubscriptionManager mgr) { + if ((mgr == null) || (mSubscriptionsChangedListener == null)) { + return; + } + mgr.removeOnSubscriptionsChangedListener(mSubscriptionsChangedListener); + mSubscriptionsChangedListener = null; } @Override @@ -269,6 +336,7 @@ public class ResetNetworkConfirm extends InstrumentedFragment { if (mAlertDialog != null) { mAlertDialog.dismiss(); } + stopMonitorSubscriptionChange(getSubscriptionManager()); super.onDestroy(); }