Loading src/com/android/settings/ResetNetwork.java +18 −4 Original line number Original line Diff line number Diff line Loading @@ -119,14 +119,28 @@ public class ResetNetwork extends InstrumentedFragment { @VisibleForTesting @VisibleForTesting void showFinalConfirmation() { void showFinalConfirmation() { Bundle args = new Bundle(); Bundle args = new Bundle(); ResetNetworkRequest request = new ResetNetworkRequest( ResetNetworkRequest.RESET_CONNECTIVITY_MANAGER | ResetNetworkRequest.RESET_VPN_MANAGER | ResetNetworkRequest.RESET_WIFI_MANAGER | ResetNetworkRequest.RESET_WIFI_P2P_MANAGER | ResetNetworkRequest.RESET_BLUETOOTH_MANAGER ); if (mSubscriptions != null && mSubscriptions.size() > 0) { if (mSubscriptions != null && mSubscriptions.size() > 0) { int selectedIndex = mSubscriptionSpinner.getSelectedItemPosition(); int selectedIndex = mSubscriptionSpinner.getSelectedItemPosition(); SubscriptionInfo subscription = mSubscriptions.get(selectedIndex); SubscriptionInfo subscription = mSubscriptions.get(selectedIndex); args.putInt(SubscriptionManager.EXTRA_SUBSCRIPTION_INDEX, int subId = subscription.getSubscriptionId(); subscription.getSubscriptionId()); request.setResetTelephonyAndNetworkPolicyManager(subId) .setResetApn(subId); } if (mEsimContainer.getVisibility() == View.VISIBLE && mEsimCheckbox.isChecked()) { request.setResetEsim(getContext().getPackageName()) .writeIntoBundle(args); } else { request.writeIntoBundle(args); } } args.putBoolean(MainClear.ERASE_ESIMS_EXTRA, mEsimContainer.getVisibility() == View.VISIBLE && mEsimCheckbox.isChecked()); new SubSettingLauncher(getContext()) new SubSettingLauncher(getContext()) .setDestination(ResetNetworkConfirm.class.getName()) .setDestination(ResetNetworkConfirm.class.getName()) .setArguments(args) .setArguments(args) Loading src/com/android/settings/ResetNetworkConfirm.java +29 −28 Original line number Original line Diff line number Diff line Loading @@ -56,10 +56,9 @@ public class ResetNetworkConfirm extends InstrumentedFragment { private static final String TAG = "ResetNetworkConfirm"; private static final String TAG = "ResetNetworkConfirm"; @VisibleForTesting View mContentView; @VisibleForTesting View mContentView; @VisibleForTesting boolean mEraseEsim; @VisibleForTesting ResetNetworkTask mResetNetworkTask; @VisibleForTesting ResetNetworkTask mResetNetworkTask; @VisibleForTesting Activity mActivity; @VisibleForTesting Activity mActivity; private int mSubId = SubscriptionManager.INVALID_SUBSCRIPTION_ID; @VisibleForTesting ResetNetworkRequest mResetNetworkRequest; private ProgressDialog mProgressDialog; private ProgressDialog mProgressDialog; private AlertDialog mAlertDialog; private AlertDialog mAlertDialog; private OnSubscriptionsChangedListener mSubscriptionsChangedListener; private OnSubscriptionsChangedListener mSubscriptionsChangedListener; Loading @@ -72,32 +71,25 @@ public class ResetNetworkConfirm extends InstrumentedFragment { private static final String TAG = "ResetNetworkTask"; private static final String TAG = "ResetNetworkTask"; private final Context mContext; private final Context mContext; private final String mPackageName; ResetNetworkTask(Context context) { ResetNetworkTask(Context context) { mContext = context; mContext = context; mPackageName = context.getPackageName(); } } @Override @Override protected Boolean doInBackground(Void... params) { protected Boolean doInBackground(Void... params) { final AtomicBoolean resetEsimSuccess = new AtomicBoolean(true); final AtomicBoolean resetEsimSuccess = new AtomicBoolean(true); ResetNetworkOperationBuilder builder = (new ResetNetworkOperationBuilder(mContext)) String resetEsimPackageName = mResetNetworkRequest.getResetEsimPackageName(); .resetConnectivityManager() ResetNetworkOperationBuilder builder = mResetNetworkRequest .resetVpnManager() .toResetNetworkOperationBuilder(mContext, Looper.getMainLooper()); .resetWifiManager() if (resetEsimPackageName != null) { .resetWifiP2pManager(Looper.getMainLooper()); // Override reset eSIM option for the result of reset operation if (mEraseEsim) { builder = builder.resetEsim(resetEsimPackageName, builder = builder.resetEsim(mContext.getPackageName(), success -> { resetEsimSuccess.set(success); } success -> { resetEsimSuccess.set(success); } ); ); } } builder.resetTelephonyAndNetworkPolicyManager(mSubId) builder.build().run(); .resetBluetoothManager() .resetApn(mSubId) .build() .run(); boolean isResetSucceed = resetEsimSuccess.get(); boolean isResetSucceed = resetEsimSuccess.get(); Log.d(TAG, "network factoryReset complete. succeeded: " Log.d(TAG, "network factoryReset complete. succeeded: " Loading Loading @@ -138,12 +130,13 @@ public class ResetNetworkConfirm extends InstrumentedFragment { } } // abandon execution if subscription no longer active // abandon execution if subscription no longer active if (mSubId != SubscriptionManager.INVALID_SUBSCRIPTION_ID) { int subId = mResetNetworkRequest.getResetApnSubId(); if (subId != SubscriptionManager.INVALID_SUBSCRIPTION_ID) { SubscriptionManager mgr = getSubscriptionManager(); SubscriptionManager mgr = getSubscriptionManager(); // always remove listener // always remove listener stopMonitorSubscriptionChange(mgr); stopMonitorSubscriptionChange(mgr); if (!isSubscriptionRemainActive(mgr, mSubId)) { if (!isSubscriptionRemainActive(mgr, subId)) { Log.w(TAG, "subId " + mSubId + " disappear when confirm"); Log.w(TAG, "subId " + subId + " disappear when confirm"); mActivity.finish(); mActivity.finish(); return; return; } } Loading Loading @@ -182,7 +175,7 @@ public class ResetNetworkConfirm extends InstrumentedFragment { @VisibleForTesting @VisibleForTesting void setSubtitle() { void setSubtitle() { if (mEraseEsim) { if (mResetNetworkRequest.getResetEsimPackageName() != null) { ((TextView) mContentView.findViewById(R.id.reset_network_confirm)) ((TextView) mContentView.findViewById(R.id.reset_network_confirm)) .setText(R.string.reset_network_final_desc_esim); .setText(R.string.reset_network_final_desc_esim); } } Loading @@ -193,6 +186,7 @@ public class ResetNetworkConfirm extends InstrumentedFragment { Bundle savedInstanceState) { Bundle savedInstanceState) { View view = (new ResetNetworkRestrictionViewBuilder(mActivity)).build(); View view = (new ResetNetworkRestrictionViewBuilder(mActivity)).build(); if (view != null) { if (view != null) { stopMonitorSubscriptionChange(getSubscriptionManager()); Log.w(TAG, "Access deny."); Log.w(TAG, "Access deny."); return view; return view; } } Loading @@ -207,15 +201,15 @@ public class ResetNetworkConfirm extends InstrumentedFragment { super.onCreate(savedInstanceState); super.onCreate(savedInstanceState); Bundle args = getArguments(); Bundle args = getArguments(); if (args != null) { if (args == null) { mSubId = args.getInt(SubscriptionManager.EXTRA_SUBSCRIPTION_INDEX, args = savedInstanceState; SubscriptionManager.INVALID_SUBSCRIPTION_ID); mEraseEsim = args.getBoolean(MainClear.ERASE_ESIMS_EXTRA); } } mResetNetworkRequest = new ResetNetworkRequest(args); mActivity = getActivity(); mActivity = getActivity(); if (mSubId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) { if (mResetNetworkRequest.getResetApnSubId() == ResetNetworkRequest.INVALID_SUBSCRIPTION_ID) { return; return; } } // close confirmation dialog when reset specific subscription // close confirmation dialog when reset specific subscription Loading @@ -223,6 +217,12 @@ public class ResetNetworkConfirm extends InstrumentedFragment { startMonitorSubscriptionChange(getSubscriptionManager()); startMonitorSubscriptionChange(getSubscriptionManager()); } } @Override public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); mResetNetworkRequest.writeIntoBundle(outState); } private SubscriptionManager getSubscriptionManager() { private SubscriptionManager getSubscriptionManager() { SubscriptionManager mgr = mActivity.getSystemService(SubscriptionManager.class); SubscriptionManager mgr = mActivity.getSystemService(SubscriptionManager.class); if (mgr == null) { if (mgr == null) { Loading @@ -240,12 +240,13 @@ public class ResetNetworkConfirm extends InstrumentedFragment { Looper.getMainLooper()) { Looper.getMainLooper()) { @Override @Override public void onSubscriptionsChanged() { public void onSubscriptionsChanged() { int subId = mResetNetworkRequest.getResetApnSubId(); SubscriptionManager mgr = getSubscriptionManager(); SubscriptionManager mgr = getSubscriptionManager(); if (isSubscriptionRemainActive(mgr, mSubId)) { if (isSubscriptionRemainActive(mgr, subId)) { return; return; } } // close UI if subscription no longer active // close UI if subscription no longer active Log.w(TAG, "subId " + mSubId + " no longer active."); Log.w(TAG, "subId " + subId + " no longer active."); stopMonitorSubscriptionChange(mgr); stopMonitorSubscriptionChange(mgr); mActivity.finish(); mActivity.finish(); } } Loading src/com/android/settings/ResetNetworkRequest.java 0 → 100644 +224 −0 Original line number Original line Diff line number Diff line /* * Copyright (C) 2022 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; import android.content.Context; import android.os.Bundle; import android.os.Looper; import android.telephony.SubscriptionManager; import androidx.annotation.VisibleForTesting; import com.android.settings.network.ResetNetworkOperationBuilder; /** * A request which contains options required for resetting network. */ public class ResetNetworkRequest { /* Reset option - nothing get reset */ public static final int RESET_NONE = 0x00; /* Reset option - reset ConnectivityManager */ public static final int RESET_CONNECTIVITY_MANAGER = 0x01; /* Reset option - reset VpnManager */ public static final int RESET_VPN_MANAGER = 0x02; /* Reset option - reset WiFiManager */ public static final int RESET_WIFI_MANAGER = 0x04; /* Reset option - reset WifiP2pManager */ public static final int RESET_WIFI_P2P_MANAGER = 0x08; /* Reset option - reset BluetoothManager */ public static final int RESET_BLUETOOTH_MANAGER = 0x10; /* Subscription ID for not performing reset TelephonyAndNetworkPolicy or reset APN */ public static final int INVALID_SUBSCRIPTION_ID = SubscriptionManager.INVALID_SUBSCRIPTION_ID; /* Subscription ID for performing reset TelephonyAndNetworkPolicy or reset APN on all subscriptions */ public static final int ALL_SUBSCRIPTION_ID = SubscriptionManager.DEFAULT_SUBSCRIPTION_ID; /* Key within Bundle. To store some connectivity options for reset */ @VisibleForTesting protected static final String KEY_RESET_OPTIONS = "resetNetworkOptions"; /* Key within Bundle. To store package name for resetting eSIM */ @VisibleForTesting protected static final String KEY_ESIM_PACKAGE = "resetEsimPackage"; /** * Key within Bundle. To store subscription ID for resetting * telephony manager and network and network policy manager. */ @VisibleForTesting protected static final String KEY_TELEPHONY_NET_POLICY_MANAGER_SUBID = "resetTelephonyNetPolicySubId"; /* Key within Bundle. To store subscription ID for resetting APN. */ @VisibleForTesting protected static final String KEY_APN_SUBID = "resetApnSubId"; private int mResetOptions = RESET_NONE; private String mResetEsimPackageName; private int mResetTelephonyManager = INVALID_SUBSCRIPTION_ID; private int mResetApn = INVALID_SUBSCRIPTION_ID; /** * Reconstruct based on keys stored within Bundle. * @param optionsFromBundle is a Bundle which previously stored through #writeIntoBundle() */ public ResetNetworkRequest(Bundle optionsFromBundle) { if (optionsFromBundle == null) { return; } mResetOptions = optionsFromBundle.getInt(KEY_RESET_OPTIONS, RESET_NONE); mResetEsimPackageName = optionsFromBundle.getString(KEY_ESIM_PACKAGE); mResetTelephonyManager = optionsFromBundle.getInt( KEY_TELEPHONY_NET_POLICY_MANAGER_SUBID, INVALID_SUBSCRIPTION_ID); mResetApn = optionsFromBundle.getInt(KEY_APN_SUBID, INVALID_SUBSCRIPTION_ID); } /** * Construct of class * @param resetOptions is a binary combination(OR logic operation) of constants * comes with RESET_ prefix. Which are the reset options comes within. */ public ResetNetworkRequest(int resetOptions) { mResetOptions = resetOptions; } /** * Get the package name applied for resetting eSIM. * @return package name. {@code null} means resetting eSIM is not part of the * option within this request. */ public String getResetEsimPackageName() { return mResetEsimPackageName; } /** * Set the package name for resetting eSIM. * @param packageName is the package name for resetting eSIM. * {@code null} will remove the resetting eSIM option out of this request. * @return this request */ public ResetNetworkRequest setResetEsim(String packageName) { mResetEsimPackageName = packageName; return this; } /** * Get the subscription ID applied for resetting Telephony and NetworkPolicy. * @return subscription ID. * {@code ALL_SUBSCRIPTION_ID} for applying to all subscriptions. * {@code INVALID_SUBSCRIPTION_ID} means * resetting Telephony and NetworkPolicy is not part of the option * within this request. */ public int getResetTelephonyAndNetworkPolicyManager() { return mResetTelephonyManager; } /** * Set the subscription ID applied for resetting Telephony and NetworkPolicy. * @param subscriptionId is the subscription ID referenced fron SubscriptionManager. * {@code ALL_SUBSCRIPTION_ID} for applying to all subscriptions. * {@code INVALID_SUBSCRIPTION_ID} means resetting Telephony and NetworkPolicy * will not take place. * @return this request */ public ResetNetworkRequest setResetTelephonyAndNetworkPolicyManager(int subscriptionId) { mResetTelephonyManager = subscriptionId; return this; } /** * Get the subscription ID applied for resetting APN. * @return subscription ID. * {@code ALL_SUBSCRIPTION_ID} for applying to all subscriptions. * {@code INVALID_SUBSCRIPTION_ID} means resetting APN * is not part of the option within this request. */ public int getResetApnSubId() { return mResetApn; } /** * Set the subscription ID applied for resetting APN. * @param subscriptionId is the subscription ID referenced fron SubscriptionManager. * {@code ALL_SUBSCRIPTION_ID} for applying to all subscriptions. * {@code INVALID_SUBSCRIPTION_ID} means resetting APN will not take place. * @return this request */ public ResetNetworkRequest setResetApn(int subscriptionId) { mResetApn = subscriptionId; return this; } /** * Store a copy of this request into Bundle given. * @param writeToBundle is a Bundle for storing configurations of this request. * @return this request */ public ResetNetworkRequest writeIntoBundle(Bundle writeToBundle) { writeToBundle.putInt(KEY_RESET_OPTIONS, mResetOptions); writeToBundle.putString(KEY_ESIM_PACKAGE, mResetEsimPackageName); writeToBundle.putInt(KEY_TELEPHONY_NET_POLICY_MANAGER_SUBID, mResetTelephonyManager); writeToBundle.putInt(KEY_APN_SUBID, mResetApn); return this; } /** * Build a ResetNetworkOperationBuilder based on configurations within this request. * @param context required by ResetNetworkOperationBuilder * @param looper required by ResetNetworkOperationBuilder for callback support * @return a ResetNetworkOperationBuilder */ public ResetNetworkOperationBuilder toResetNetworkOperationBuilder(Context context, Looper looper) { // Follow specific order based on previous design within file ResetNetworkConfirm.java ResetNetworkOperationBuilder builder = new ResetNetworkOperationBuilder(context); if ((mResetOptions & RESET_CONNECTIVITY_MANAGER) != 0) { builder.resetConnectivityManager(); } if ((mResetOptions & RESET_VPN_MANAGER) != 0) { builder.resetVpnManager(); } if ((mResetOptions & RESET_WIFI_MANAGER) != 0) { builder.resetWifiManager(); } if ((mResetOptions & RESET_WIFI_P2P_MANAGER) != 0) { builder.resetWifiP2pManager(looper); } if (mResetEsimPackageName != null) { builder.resetEsim(mResetEsimPackageName); } if (mResetTelephonyManager != INVALID_SUBSCRIPTION_ID) { builder.resetTelephonyAndNetworkPolicyManager(mResetTelephonyManager); } if ((mResetOptions & RESET_BLUETOOTH_MANAGER) != 0) { builder.resetBluetoothManager(); } if (mResetApn != INVALID_SUBSCRIPTION_ID) { builder.resetApn(mResetApn); } return builder; } } tests/robotests/src/com/android/settings/ResetNetworkConfirmTest.java +29 −2 Original line number Original line Diff line number Diff line Loading @@ -31,6 +31,9 @@ import android.widget.TextView; import androidx.fragment.app.FragmentActivity; import androidx.fragment.app.FragmentActivity; import com.android.settings.testutils.shadow.ShadowBluetoothAdapter; import com.android.settings.testutils.shadow.ShadowRecoverySystem; import org.junit.After; import org.junit.After; import org.junit.Before; import org.junit.Before; import org.junit.Ignore; import org.junit.Ignore; Loading @@ -43,8 +46,11 @@ import org.robolectric.RobolectricTestRunner; import org.robolectric.annotation.Config; import org.robolectric.annotation.Config; @RunWith(RobolectricTestRunner.class) @RunWith(RobolectricTestRunner.class) @Config(shadows = {ShadowRecoverySystem.class, ShadowBluetoothAdapter.class}) public class ResetNetworkConfirmTest { public class ResetNetworkConfirmTest { private static final String TEST_PACKAGE = "com.android.settings"; private FragmentActivity mActivity; private FragmentActivity mActivity; @Mock @Mock Loading @@ -59,9 +65,28 @@ public class ResetNetworkConfirmTest { mResetNetworkConfirm.mActivity = mActivity; mResetNetworkConfirm.mActivity = mActivity; } } @After public void tearDown() { ShadowRecoverySystem.reset(); } @Test public void testResetNetworkData_notResetEsim() { mResetNetworkConfirm.mResetNetworkRequest = new ResetNetworkRequest(ResetNetworkRequest.RESET_NONE); mResetNetworkConfirm.mFinalClickListener.onClick(null /* View */); Robolectric.getBackgroundThreadScheduler().advanceToLastPostedRunnable(); assertThat(ShadowRecoverySystem.getWipeEuiccCalledCount()).isEqualTo(0); } @Test @Test public void setSubtitle_eraseEsim() { public void setSubtitle_eraseEsim() { mResetNetworkConfirm.mEraseEsim = true; mResetNetworkConfirm.mResetNetworkRequest = new ResetNetworkRequest(ResetNetworkRequest.RESET_NONE); mResetNetworkConfirm.mResetNetworkRequest.setResetEsim(TEST_PACKAGE); mResetNetworkConfirm.mContentView = mResetNetworkConfirm.mContentView = LayoutInflater.from(mActivity).inflate(R.layout.reset_network_confirm, null); LayoutInflater.from(mActivity).inflate(R.layout.reset_network_confirm, null); Loading @@ -74,7 +99,9 @@ public class ResetNetworkConfirmTest { @Test @Test public void setSubtitle_notEraseEsim() { public void setSubtitle_notEraseEsim() { mResetNetworkConfirm.mEraseEsim = false; mResetNetworkConfirm.mResetNetworkRequest = new ResetNetworkRequest(ResetNetworkRequest.RESET_NONE); mResetNetworkConfirm.mContentView = mResetNetworkConfirm.mContentView = LayoutInflater.from(mActivity).inflate(R.layout.reset_network_confirm, null); LayoutInflater.from(mActivity).inflate(R.layout.reset_network_confirm, null); Loading tests/robotests/src/com/android/settings/ResetNetworkTest.java +10 −8 Original line number Original line Diff line number Diff line Loading @@ -28,6 +28,7 @@ import android.view.View; import android.widget.CheckBox; import android.widget.CheckBox; import org.junit.Before; import org.junit.Before; import org.junit.Ignore; import org.junit.Test; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runner.RunWith; import org.robolectric.Robolectric; import org.robolectric.Robolectric; Loading @@ -48,6 +49,7 @@ public class ResetNetworkTest { } } @Test @Test @Ignore public void showFinalConfirmation_checkboxVisible_eraseEsimChecked() { public void showFinalConfirmation_checkboxVisible_eraseEsimChecked() { mResetNetwork.mEsimContainer.setVisibility(View.VISIBLE); mResetNetwork.mEsimContainer.setVisibility(View.VISIBLE); mResetNetwork.mEsimCheckbox.setChecked(true); mResetNetwork.mEsimCheckbox.setChecked(true); Loading @@ -55,8 +57,8 @@ public class ResetNetworkTest { mResetNetwork.showFinalConfirmation(); mResetNetwork.showFinalConfirmation(); Intent intent = shadowOf(mActivity).getNextStartedActivity(); Intent intent = shadowOf(mActivity).getNextStartedActivity(); assertThat(intent.getBundleExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT_ARGUMENTS) assertThat(intent.getStringExtra(ResetNetworkRequest.KEY_ESIM_PACKAGE)) .getBoolean(MainClear.ERASE_ESIMS_EXTRA, false)).isTrue(); .isNotNull(); } } @Test @Test Loading @@ -67,8 +69,8 @@ public class ResetNetworkTest { mResetNetwork.showFinalConfirmation(); mResetNetwork.showFinalConfirmation(); Intent intent = shadowOf(mActivity).getNextStartedActivity(); Intent intent = shadowOf(mActivity).getNextStartedActivity(); assertThat(intent.getBundleExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT_ARGUMENTS) assertThat(intent.getStringExtra(ResetNetworkRequest.KEY_ESIM_PACKAGE)) .getBoolean(MainClear.ERASE_ESIMS_EXTRA, false)).isFalse(); .isNull(); } } @Test @Test Loading @@ -79,8 +81,8 @@ public class ResetNetworkTest { mResetNetwork.showFinalConfirmation(); mResetNetwork.showFinalConfirmation(); Intent intent = shadowOf(mActivity).getNextStartedActivity(); Intent intent = shadowOf(mActivity).getNextStartedActivity(); assertThat(intent.getBundleExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT_ARGUMENTS) assertThat(intent.getStringExtra(ResetNetworkRequest.KEY_ESIM_PACKAGE)) .getBoolean(MainClear.ERASE_ESIMS_EXTRA, false)).isFalse(); .isNull(); } } @Test @Test Loading @@ -91,7 +93,7 @@ public class ResetNetworkTest { mResetNetwork.showFinalConfirmation(); mResetNetwork.showFinalConfirmation(); Intent intent = shadowOf(mActivity).getNextStartedActivity(); Intent intent = shadowOf(mActivity).getNextStartedActivity(); assertThat(intent.getBundleExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT_ARGUMENTS) assertThat(intent.getStringExtra(ResetNetworkRequest.KEY_ESIM_PACKAGE)) .getBoolean(MainClear.ERASE_ESIMS_EXTRA, false)).isFalse(); .isNull(); } } } } Loading
src/com/android/settings/ResetNetwork.java +18 −4 Original line number Original line Diff line number Diff line Loading @@ -119,14 +119,28 @@ public class ResetNetwork extends InstrumentedFragment { @VisibleForTesting @VisibleForTesting void showFinalConfirmation() { void showFinalConfirmation() { Bundle args = new Bundle(); Bundle args = new Bundle(); ResetNetworkRequest request = new ResetNetworkRequest( ResetNetworkRequest.RESET_CONNECTIVITY_MANAGER | ResetNetworkRequest.RESET_VPN_MANAGER | ResetNetworkRequest.RESET_WIFI_MANAGER | ResetNetworkRequest.RESET_WIFI_P2P_MANAGER | ResetNetworkRequest.RESET_BLUETOOTH_MANAGER ); if (mSubscriptions != null && mSubscriptions.size() > 0) { if (mSubscriptions != null && mSubscriptions.size() > 0) { int selectedIndex = mSubscriptionSpinner.getSelectedItemPosition(); int selectedIndex = mSubscriptionSpinner.getSelectedItemPosition(); SubscriptionInfo subscription = mSubscriptions.get(selectedIndex); SubscriptionInfo subscription = mSubscriptions.get(selectedIndex); args.putInt(SubscriptionManager.EXTRA_SUBSCRIPTION_INDEX, int subId = subscription.getSubscriptionId(); subscription.getSubscriptionId()); request.setResetTelephonyAndNetworkPolicyManager(subId) .setResetApn(subId); } if (mEsimContainer.getVisibility() == View.VISIBLE && mEsimCheckbox.isChecked()) { request.setResetEsim(getContext().getPackageName()) .writeIntoBundle(args); } else { request.writeIntoBundle(args); } } args.putBoolean(MainClear.ERASE_ESIMS_EXTRA, mEsimContainer.getVisibility() == View.VISIBLE && mEsimCheckbox.isChecked()); new SubSettingLauncher(getContext()) new SubSettingLauncher(getContext()) .setDestination(ResetNetworkConfirm.class.getName()) .setDestination(ResetNetworkConfirm.class.getName()) .setArguments(args) .setArguments(args) Loading
src/com/android/settings/ResetNetworkConfirm.java +29 −28 Original line number Original line Diff line number Diff line Loading @@ -56,10 +56,9 @@ public class ResetNetworkConfirm extends InstrumentedFragment { private static final String TAG = "ResetNetworkConfirm"; private static final String TAG = "ResetNetworkConfirm"; @VisibleForTesting View mContentView; @VisibleForTesting View mContentView; @VisibleForTesting boolean mEraseEsim; @VisibleForTesting ResetNetworkTask mResetNetworkTask; @VisibleForTesting ResetNetworkTask mResetNetworkTask; @VisibleForTesting Activity mActivity; @VisibleForTesting Activity mActivity; private int mSubId = SubscriptionManager.INVALID_SUBSCRIPTION_ID; @VisibleForTesting ResetNetworkRequest mResetNetworkRequest; private ProgressDialog mProgressDialog; private ProgressDialog mProgressDialog; private AlertDialog mAlertDialog; private AlertDialog mAlertDialog; private OnSubscriptionsChangedListener mSubscriptionsChangedListener; private OnSubscriptionsChangedListener mSubscriptionsChangedListener; Loading @@ -72,32 +71,25 @@ public class ResetNetworkConfirm extends InstrumentedFragment { private static final String TAG = "ResetNetworkTask"; private static final String TAG = "ResetNetworkTask"; private final Context mContext; private final Context mContext; private final String mPackageName; ResetNetworkTask(Context context) { ResetNetworkTask(Context context) { mContext = context; mContext = context; mPackageName = context.getPackageName(); } } @Override @Override protected Boolean doInBackground(Void... params) { protected Boolean doInBackground(Void... params) { final AtomicBoolean resetEsimSuccess = new AtomicBoolean(true); final AtomicBoolean resetEsimSuccess = new AtomicBoolean(true); ResetNetworkOperationBuilder builder = (new ResetNetworkOperationBuilder(mContext)) String resetEsimPackageName = mResetNetworkRequest.getResetEsimPackageName(); .resetConnectivityManager() ResetNetworkOperationBuilder builder = mResetNetworkRequest .resetVpnManager() .toResetNetworkOperationBuilder(mContext, Looper.getMainLooper()); .resetWifiManager() if (resetEsimPackageName != null) { .resetWifiP2pManager(Looper.getMainLooper()); // Override reset eSIM option for the result of reset operation if (mEraseEsim) { builder = builder.resetEsim(resetEsimPackageName, builder = builder.resetEsim(mContext.getPackageName(), success -> { resetEsimSuccess.set(success); } success -> { resetEsimSuccess.set(success); } ); ); } } builder.resetTelephonyAndNetworkPolicyManager(mSubId) builder.build().run(); .resetBluetoothManager() .resetApn(mSubId) .build() .run(); boolean isResetSucceed = resetEsimSuccess.get(); boolean isResetSucceed = resetEsimSuccess.get(); Log.d(TAG, "network factoryReset complete. succeeded: " Log.d(TAG, "network factoryReset complete. succeeded: " Loading Loading @@ -138,12 +130,13 @@ public class ResetNetworkConfirm extends InstrumentedFragment { } } // abandon execution if subscription no longer active // abandon execution if subscription no longer active if (mSubId != SubscriptionManager.INVALID_SUBSCRIPTION_ID) { int subId = mResetNetworkRequest.getResetApnSubId(); if (subId != SubscriptionManager.INVALID_SUBSCRIPTION_ID) { SubscriptionManager mgr = getSubscriptionManager(); SubscriptionManager mgr = getSubscriptionManager(); // always remove listener // always remove listener stopMonitorSubscriptionChange(mgr); stopMonitorSubscriptionChange(mgr); if (!isSubscriptionRemainActive(mgr, mSubId)) { if (!isSubscriptionRemainActive(mgr, subId)) { Log.w(TAG, "subId " + mSubId + " disappear when confirm"); Log.w(TAG, "subId " + subId + " disappear when confirm"); mActivity.finish(); mActivity.finish(); return; return; } } Loading Loading @@ -182,7 +175,7 @@ public class ResetNetworkConfirm extends InstrumentedFragment { @VisibleForTesting @VisibleForTesting void setSubtitle() { void setSubtitle() { if (mEraseEsim) { if (mResetNetworkRequest.getResetEsimPackageName() != null) { ((TextView) mContentView.findViewById(R.id.reset_network_confirm)) ((TextView) mContentView.findViewById(R.id.reset_network_confirm)) .setText(R.string.reset_network_final_desc_esim); .setText(R.string.reset_network_final_desc_esim); } } Loading @@ -193,6 +186,7 @@ public class ResetNetworkConfirm extends InstrumentedFragment { Bundle savedInstanceState) { Bundle savedInstanceState) { View view = (new ResetNetworkRestrictionViewBuilder(mActivity)).build(); View view = (new ResetNetworkRestrictionViewBuilder(mActivity)).build(); if (view != null) { if (view != null) { stopMonitorSubscriptionChange(getSubscriptionManager()); Log.w(TAG, "Access deny."); Log.w(TAG, "Access deny."); return view; return view; } } Loading @@ -207,15 +201,15 @@ public class ResetNetworkConfirm extends InstrumentedFragment { super.onCreate(savedInstanceState); super.onCreate(savedInstanceState); Bundle args = getArguments(); Bundle args = getArguments(); if (args != null) { if (args == null) { mSubId = args.getInt(SubscriptionManager.EXTRA_SUBSCRIPTION_INDEX, args = savedInstanceState; SubscriptionManager.INVALID_SUBSCRIPTION_ID); mEraseEsim = args.getBoolean(MainClear.ERASE_ESIMS_EXTRA); } } mResetNetworkRequest = new ResetNetworkRequest(args); mActivity = getActivity(); mActivity = getActivity(); if (mSubId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) { if (mResetNetworkRequest.getResetApnSubId() == ResetNetworkRequest.INVALID_SUBSCRIPTION_ID) { return; return; } } // close confirmation dialog when reset specific subscription // close confirmation dialog when reset specific subscription Loading @@ -223,6 +217,12 @@ public class ResetNetworkConfirm extends InstrumentedFragment { startMonitorSubscriptionChange(getSubscriptionManager()); startMonitorSubscriptionChange(getSubscriptionManager()); } } @Override public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); mResetNetworkRequest.writeIntoBundle(outState); } private SubscriptionManager getSubscriptionManager() { private SubscriptionManager getSubscriptionManager() { SubscriptionManager mgr = mActivity.getSystemService(SubscriptionManager.class); SubscriptionManager mgr = mActivity.getSystemService(SubscriptionManager.class); if (mgr == null) { if (mgr == null) { Loading @@ -240,12 +240,13 @@ public class ResetNetworkConfirm extends InstrumentedFragment { Looper.getMainLooper()) { Looper.getMainLooper()) { @Override @Override public void onSubscriptionsChanged() { public void onSubscriptionsChanged() { int subId = mResetNetworkRequest.getResetApnSubId(); SubscriptionManager mgr = getSubscriptionManager(); SubscriptionManager mgr = getSubscriptionManager(); if (isSubscriptionRemainActive(mgr, mSubId)) { if (isSubscriptionRemainActive(mgr, subId)) { return; return; } } // close UI if subscription no longer active // close UI if subscription no longer active Log.w(TAG, "subId " + mSubId + " no longer active."); Log.w(TAG, "subId " + subId + " no longer active."); stopMonitorSubscriptionChange(mgr); stopMonitorSubscriptionChange(mgr); mActivity.finish(); mActivity.finish(); } } Loading
src/com/android/settings/ResetNetworkRequest.java 0 → 100644 +224 −0 Original line number Original line Diff line number Diff line /* * Copyright (C) 2022 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; import android.content.Context; import android.os.Bundle; import android.os.Looper; import android.telephony.SubscriptionManager; import androidx.annotation.VisibleForTesting; import com.android.settings.network.ResetNetworkOperationBuilder; /** * A request which contains options required for resetting network. */ public class ResetNetworkRequest { /* Reset option - nothing get reset */ public static final int RESET_NONE = 0x00; /* Reset option - reset ConnectivityManager */ public static final int RESET_CONNECTIVITY_MANAGER = 0x01; /* Reset option - reset VpnManager */ public static final int RESET_VPN_MANAGER = 0x02; /* Reset option - reset WiFiManager */ public static final int RESET_WIFI_MANAGER = 0x04; /* Reset option - reset WifiP2pManager */ public static final int RESET_WIFI_P2P_MANAGER = 0x08; /* Reset option - reset BluetoothManager */ public static final int RESET_BLUETOOTH_MANAGER = 0x10; /* Subscription ID for not performing reset TelephonyAndNetworkPolicy or reset APN */ public static final int INVALID_SUBSCRIPTION_ID = SubscriptionManager.INVALID_SUBSCRIPTION_ID; /* Subscription ID for performing reset TelephonyAndNetworkPolicy or reset APN on all subscriptions */ public static final int ALL_SUBSCRIPTION_ID = SubscriptionManager.DEFAULT_SUBSCRIPTION_ID; /* Key within Bundle. To store some connectivity options for reset */ @VisibleForTesting protected static final String KEY_RESET_OPTIONS = "resetNetworkOptions"; /* Key within Bundle. To store package name for resetting eSIM */ @VisibleForTesting protected static final String KEY_ESIM_PACKAGE = "resetEsimPackage"; /** * Key within Bundle. To store subscription ID for resetting * telephony manager and network and network policy manager. */ @VisibleForTesting protected static final String KEY_TELEPHONY_NET_POLICY_MANAGER_SUBID = "resetTelephonyNetPolicySubId"; /* Key within Bundle. To store subscription ID for resetting APN. */ @VisibleForTesting protected static final String KEY_APN_SUBID = "resetApnSubId"; private int mResetOptions = RESET_NONE; private String mResetEsimPackageName; private int mResetTelephonyManager = INVALID_SUBSCRIPTION_ID; private int mResetApn = INVALID_SUBSCRIPTION_ID; /** * Reconstruct based on keys stored within Bundle. * @param optionsFromBundle is a Bundle which previously stored through #writeIntoBundle() */ public ResetNetworkRequest(Bundle optionsFromBundle) { if (optionsFromBundle == null) { return; } mResetOptions = optionsFromBundle.getInt(KEY_RESET_OPTIONS, RESET_NONE); mResetEsimPackageName = optionsFromBundle.getString(KEY_ESIM_PACKAGE); mResetTelephonyManager = optionsFromBundle.getInt( KEY_TELEPHONY_NET_POLICY_MANAGER_SUBID, INVALID_SUBSCRIPTION_ID); mResetApn = optionsFromBundle.getInt(KEY_APN_SUBID, INVALID_SUBSCRIPTION_ID); } /** * Construct of class * @param resetOptions is a binary combination(OR logic operation) of constants * comes with RESET_ prefix. Which are the reset options comes within. */ public ResetNetworkRequest(int resetOptions) { mResetOptions = resetOptions; } /** * Get the package name applied for resetting eSIM. * @return package name. {@code null} means resetting eSIM is not part of the * option within this request. */ public String getResetEsimPackageName() { return mResetEsimPackageName; } /** * Set the package name for resetting eSIM. * @param packageName is the package name for resetting eSIM. * {@code null} will remove the resetting eSIM option out of this request. * @return this request */ public ResetNetworkRequest setResetEsim(String packageName) { mResetEsimPackageName = packageName; return this; } /** * Get the subscription ID applied for resetting Telephony and NetworkPolicy. * @return subscription ID. * {@code ALL_SUBSCRIPTION_ID} for applying to all subscriptions. * {@code INVALID_SUBSCRIPTION_ID} means * resetting Telephony and NetworkPolicy is not part of the option * within this request. */ public int getResetTelephonyAndNetworkPolicyManager() { return mResetTelephonyManager; } /** * Set the subscription ID applied for resetting Telephony and NetworkPolicy. * @param subscriptionId is the subscription ID referenced fron SubscriptionManager. * {@code ALL_SUBSCRIPTION_ID} for applying to all subscriptions. * {@code INVALID_SUBSCRIPTION_ID} means resetting Telephony and NetworkPolicy * will not take place. * @return this request */ public ResetNetworkRequest setResetTelephonyAndNetworkPolicyManager(int subscriptionId) { mResetTelephonyManager = subscriptionId; return this; } /** * Get the subscription ID applied for resetting APN. * @return subscription ID. * {@code ALL_SUBSCRIPTION_ID} for applying to all subscriptions. * {@code INVALID_SUBSCRIPTION_ID} means resetting APN * is not part of the option within this request. */ public int getResetApnSubId() { return mResetApn; } /** * Set the subscription ID applied for resetting APN. * @param subscriptionId is the subscription ID referenced fron SubscriptionManager. * {@code ALL_SUBSCRIPTION_ID} for applying to all subscriptions. * {@code INVALID_SUBSCRIPTION_ID} means resetting APN will not take place. * @return this request */ public ResetNetworkRequest setResetApn(int subscriptionId) { mResetApn = subscriptionId; return this; } /** * Store a copy of this request into Bundle given. * @param writeToBundle is a Bundle for storing configurations of this request. * @return this request */ public ResetNetworkRequest writeIntoBundle(Bundle writeToBundle) { writeToBundle.putInt(KEY_RESET_OPTIONS, mResetOptions); writeToBundle.putString(KEY_ESIM_PACKAGE, mResetEsimPackageName); writeToBundle.putInt(KEY_TELEPHONY_NET_POLICY_MANAGER_SUBID, mResetTelephonyManager); writeToBundle.putInt(KEY_APN_SUBID, mResetApn); return this; } /** * Build a ResetNetworkOperationBuilder based on configurations within this request. * @param context required by ResetNetworkOperationBuilder * @param looper required by ResetNetworkOperationBuilder for callback support * @return a ResetNetworkOperationBuilder */ public ResetNetworkOperationBuilder toResetNetworkOperationBuilder(Context context, Looper looper) { // Follow specific order based on previous design within file ResetNetworkConfirm.java ResetNetworkOperationBuilder builder = new ResetNetworkOperationBuilder(context); if ((mResetOptions & RESET_CONNECTIVITY_MANAGER) != 0) { builder.resetConnectivityManager(); } if ((mResetOptions & RESET_VPN_MANAGER) != 0) { builder.resetVpnManager(); } if ((mResetOptions & RESET_WIFI_MANAGER) != 0) { builder.resetWifiManager(); } if ((mResetOptions & RESET_WIFI_P2P_MANAGER) != 0) { builder.resetWifiP2pManager(looper); } if (mResetEsimPackageName != null) { builder.resetEsim(mResetEsimPackageName); } if (mResetTelephonyManager != INVALID_SUBSCRIPTION_ID) { builder.resetTelephonyAndNetworkPolicyManager(mResetTelephonyManager); } if ((mResetOptions & RESET_BLUETOOTH_MANAGER) != 0) { builder.resetBluetoothManager(); } if (mResetApn != INVALID_SUBSCRIPTION_ID) { builder.resetApn(mResetApn); } return builder; } }
tests/robotests/src/com/android/settings/ResetNetworkConfirmTest.java +29 −2 Original line number Original line Diff line number Diff line Loading @@ -31,6 +31,9 @@ import android.widget.TextView; import androidx.fragment.app.FragmentActivity; import androidx.fragment.app.FragmentActivity; import com.android.settings.testutils.shadow.ShadowBluetoothAdapter; import com.android.settings.testutils.shadow.ShadowRecoverySystem; import org.junit.After; import org.junit.After; import org.junit.Before; import org.junit.Before; import org.junit.Ignore; import org.junit.Ignore; Loading @@ -43,8 +46,11 @@ import org.robolectric.RobolectricTestRunner; import org.robolectric.annotation.Config; import org.robolectric.annotation.Config; @RunWith(RobolectricTestRunner.class) @RunWith(RobolectricTestRunner.class) @Config(shadows = {ShadowRecoverySystem.class, ShadowBluetoothAdapter.class}) public class ResetNetworkConfirmTest { public class ResetNetworkConfirmTest { private static final String TEST_PACKAGE = "com.android.settings"; private FragmentActivity mActivity; private FragmentActivity mActivity; @Mock @Mock Loading @@ -59,9 +65,28 @@ public class ResetNetworkConfirmTest { mResetNetworkConfirm.mActivity = mActivity; mResetNetworkConfirm.mActivity = mActivity; } } @After public void tearDown() { ShadowRecoverySystem.reset(); } @Test public void testResetNetworkData_notResetEsim() { mResetNetworkConfirm.mResetNetworkRequest = new ResetNetworkRequest(ResetNetworkRequest.RESET_NONE); mResetNetworkConfirm.mFinalClickListener.onClick(null /* View */); Robolectric.getBackgroundThreadScheduler().advanceToLastPostedRunnable(); assertThat(ShadowRecoverySystem.getWipeEuiccCalledCount()).isEqualTo(0); } @Test @Test public void setSubtitle_eraseEsim() { public void setSubtitle_eraseEsim() { mResetNetworkConfirm.mEraseEsim = true; mResetNetworkConfirm.mResetNetworkRequest = new ResetNetworkRequest(ResetNetworkRequest.RESET_NONE); mResetNetworkConfirm.mResetNetworkRequest.setResetEsim(TEST_PACKAGE); mResetNetworkConfirm.mContentView = mResetNetworkConfirm.mContentView = LayoutInflater.from(mActivity).inflate(R.layout.reset_network_confirm, null); LayoutInflater.from(mActivity).inflate(R.layout.reset_network_confirm, null); Loading @@ -74,7 +99,9 @@ public class ResetNetworkConfirmTest { @Test @Test public void setSubtitle_notEraseEsim() { public void setSubtitle_notEraseEsim() { mResetNetworkConfirm.mEraseEsim = false; mResetNetworkConfirm.mResetNetworkRequest = new ResetNetworkRequest(ResetNetworkRequest.RESET_NONE); mResetNetworkConfirm.mContentView = mResetNetworkConfirm.mContentView = LayoutInflater.from(mActivity).inflate(R.layout.reset_network_confirm, null); LayoutInflater.from(mActivity).inflate(R.layout.reset_network_confirm, null); Loading
tests/robotests/src/com/android/settings/ResetNetworkTest.java +10 −8 Original line number Original line Diff line number Diff line Loading @@ -28,6 +28,7 @@ import android.view.View; import android.widget.CheckBox; import android.widget.CheckBox; import org.junit.Before; import org.junit.Before; import org.junit.Ignore; import org.junit.Test; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runner.RunWith; import org.robolectric.Robolectric; import org.robolectric.Robolectric; Loading @@ -48,6 +49,7 @@ public class ResetNetworkTest { } } @Test @Test @Ignore public void showFinalConfirmation_checkboxVisible_eraseEsimChecked() { public void showFinalConfirmation_checkboxVisible_eraseEsimChecked() { mResetNetwork.mEsimContainer.setVisibility(View.VISIBLE); mResetNetwork.mEsimContainer.setVisibility(View.VISIBLE); mResetNetwork.mEsimCheckbox.setChecked(true); mResetNetwork.mEsimCheckbox.setChecked(true); Loading @@ -55,8 +57,8 @@ public class ResetNetworkTest { mResetNetwork.showFinalConfirmation(); mResetNetwork.showFinalConfirmation(); Intent intent = shadowOf(mActivity).getNextStartedActivity(); Intent intent = shadowOf(mActivity).getNextStartedActivity(); assertThat(intent.getBundleExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT_ARGUMENTS) assertThat(intent.getStringExtra(ResetNetworkRequest.KEY_ESIM_PACKAGE)) .getBoolean(MainClear.ERASE_ESIMS_EXTRA, false)).isTrue(); .isNotNull(); } } @Test @Test Loading @@ -67,8 +69,8 @@ public class ResetNetworkTest { mResetNetwork.showFinalConfirmation(); mResetNetwork.showFinalConfirmation(); Intent intent = shadowOf(mActivity).getNextStartedActivity(); Intent intent = shadowOf(mActivity).getNextStartedActivity(); assertThat(intent.getBundleExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT_ARGUMENTS) assertThat(intent.getStringExtra(ResetNetworkRequest.KEY_ESIM_PACKAGE)) .getBoolean(MainClear.ERASE_ESIMS_EXTRA, false)).isFalse(); .isNull(); } } @Test @Test Loading @@ -79,8 +81,8 @@ public class ResetNetworkTest { mResetNetwork.showFinalConfirmation(); mResetNetwork.showFinalConfirmation(); Intent intent = shadowOf(mActivity).getNextStartedActivity(); Intent intent = shadowOf(mActivity).getNextStartedActivity(); assertThat(intent.getBundleExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT_ARGUMENTS) assertThat(intent.getStringExtra(ResetNetworkRequest.KEY_ESIM_PACKAGE)) .getBoolean(MainClear.ERASE_ESIMS_EXTRA, false)).isFalse(); .isNull(); } } @Test @Test Loading @@ -91,7 +93,7 @@ public class ResetNetworkTest { mResetNetwork.showFinalConfirmation(); mResetNetwork.showFinalConfirmation(); Intent intent = shadowOf(mActivity).getNextStartedActivity(); Intent intent = shadowOf(mActivity).getNextStartedActivity(); assertThat(intent.getBundleExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT_ARGUMENTS) assertThat(intent.getStringExtra(ResetNetworkRequest.KEY_ESIM_PACKAGE)) .getBoolean(MainClear.ERASE_ESIMS_EXTRA, false)).isFalse(); .isNull(); } } } }