diff --git a/src/com/android/settings/ResetNetworkConfirm.java b/src/com/android/settings/ResetNetworkConfirm.java index 376b7262d09..f457bbc4615 100644 --- a/src/com/android/settings/ResetNetworkConfirm.java +++ b/src/com/android/settings/ResetNetworkConfirm.java @@ -18,6 +18,8 @@ package com.android.settings; import static com.android.settingslib.RestrictedLockUtils.EnforcedAdmin; +import android.app.Activity; +import android.app.ProgressDialog; import android.app.settings.SettingsEnums; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothManager; @@ -33,7 +35,6 @@ import android.os.Bundle; import android.os.RecoverySystem; import android.os.UserHandle; import android.os.UserManager; -import android.provider.Telephony; import android.telephony.SubscriptionManager; import android.telephony.TelephonyManager; import android.view.LayoutInflater; @@ -66,30 +67,76 @@ import com.android.settingslib.RestrictedLockUtilsInternal; public class ResetNetworkConfirm extends InstrumentedFragment { @VisibleForTesting View mContentView; - private int mSubId = SubscriptionManager.INVALID_SUBSCRIPTION_ID; @VisibleForTesting boolean mEraseEsim; - @VisibleForTesting EraseEsimAsyncTask mEraseEsimTask; + @VisibleForTesting ResetNetworkTask mResetNetworkTask; + @VisibleForTesting Activity mActivity; + private int mSubId = SubscriptionManager.INVALID_SUBSCRIPTION_ID; + private ProgressDialog mProgressDialog; /** - * Async task used to erase all the eSIM profiles from the phone. If error happens during + * Async task used to do all reset task. If error happens during * erasing eSIM profiles or timeout, an error msg is shown. */ - private static class EraseEsimAsyncTask extends AsyncTask { + private class ResetNetworkTask extends AsyncTask { private final Context mContext; private final String mPackageName; - EraseEsimAsyncTask(Context context, String packageName) { + ResetNetworkTask(Context context) { mContext = context; - mPackageName = packageName; + mPackageName = context.getPackageName(); } @Override protected Boolean doInBackground(Void... params) { - return RecoverySystem.wipeEuiccData(mContext, mPackageName); + ConnectivityManager connectivityManager = (ConnectivityManager) + mContext.getSystemService(Context.CONNECTIVITY_SERVICE); + if (connectivityManager != null) { + connectivityManager.factoryReset(); + } + + WifiManager wifiManager = (WifiManager) + mContext.getSystemService(Context.WIFI_SERVICE); + if (wifiManager != null) { + wifiManager.factoryReset(); + } + + p2pFactoryReset(mContext); + + TelephonyManager telephonyManager = (TelephonyManager) + mContext.getSystemService(Context.TELEPHONY_SERVICE); + if (telephonyManager != null) { + telephonyManager.factoryReset(mSubId); + } + + NetworkPolicyManager policyManager = (NetworkPolicyManager) + mContext.getSystemService(Context.NETWORK_POLICY_SERVICE); + if (policyManager != null) { + String subscriberId = telephonyManager.getSubscriberId(mSubId); + policyManager.factoryReset(subscriberId); + } + + BluetoothManager btManager = (BluetoothManager) + mContext.getSystemService(Context.BLUETOOTH_SERVICE); + if (btManager != null) { + BluetoothAdapter btAdapter = btManager.getAdapter(); + if (btAdapter != null) { + btAdapter.factoryReset(); + } + } + + ImsManager.getInstance(mContext, + SubscriptionManager.getPhoneId(mSubId)).factoryReset(); + restoreDefaultApn(mContext); + if (mEraseEsim) { + return RecoverySystem.wipeEuiccData(mContext, mPackageName); + } else { + return true; + } } @Override protected void onPostExecute(Boolean succeeded) { + mProgressDialog.dismiss(); if (succeeded) { Toast.makeText(mContext, R.string.reset_network_complete_toast, Toast.LENGTH_SHORT) .show(); @@ -107,71 +154,23 @@ public class ResetNetworkConfirm extends InstrumentedFragment { * The user has gone through the multiple confirmation, so now we go ahead * and reset the network settings to its factory-default state. */ - private Button.OnClickListener mFinalClickListener = new Button.OnClickListener() { + @VisibleForTesting + Button.OnClickListener mFinalClickListener = new Button.OnClickListener() { @Override public void onClick(View v) { if (Utils.isMonkeyRunning()) { return; } - // TODO maybe show a progress screen if this ends up taking a while and won't let user - // go back until the tasks finished. - Context context = getActivity(); - ConnectivityManager connectivityManager = (ConnectivityManager) - context.getSystemService(Context.CONNECTIVITY_SERVICE); - if (connectivityManager != null) { - connectivityManager.factoryReset(); - } + mProgressDialog = getProgressDialog(mActivity); + mProgressDialog.show(); - WifiManager wifiManager = (WifiManager) - context.getSystemService(Context.WIFI_SERVICE); - if (wifiManager != null) { - wifiManager.factoryReset(); - } - - p2pFactoryReset(context); - - TelephonyManager telephonyManager = (TelephonyManager) - context.getSystemService(Context.TELEPHONY_SERVICE); - if (telephonyManager != null) { - telephonyManager.factoryReset(mSubId); - } - - NetworkPolicyManager policyManager = (NetworkPolicyManager) - context.getSystemService(Context.NETWORK_POLICY_SERVICE); - if (policyManager != null) { - String subscriberId = telephonyManager.getSubscriberId(mSubId); - policyManager.factoryReset(subscriberId); - } - - BluetoothManager btManager = (BluetoothManager) - context.getSystemService(Context.BLUETOOTH_SERVICE); - if (btManager != null) { - BluetoothAdapter btAdapter = btManager.getAdapter(); - if (btAdapter != null) { - btAdapter.factoryReset(); - } - } - - ImsManager.getInstance(context, - SubscriptionManager.getPhoneId(mSubId)).factoryReset(); - restoreDefaultApn(context); - esimFactoryReset(context, context.getPackageName()); + mResetNetworkTask = new ResetNetworkTask(mActivity); + mResetNetworkTask.execute(); } }; - @VisibleForTesting - void esimFactoryReset(Context context, String packageName) { - if (mEraseEsim) { - mEraseEsimTask = new EraseEsimAsyncTask(context, packageName); - mEraseEsimTask.execute(); - } else { - Toast.makeText(context, R.string.reset_network_complete_toast, Toast.LENGTH_SHORT) - .show(); - } - } - @VisibleForTesting void p2pFactoryReset(Context context) { WifiP2pManager wifiP2pManager = (WifiP2pManager) @@ -186,6 +185,15 @@ public class ResetNetworkConfirm extends InstrumentedFragment { } } + private ProgressDialog getProgressDialog(Context context) { + final ProgressDialog progressDialog = new ProgressDialog(context); + progressDialog.setIndeterminate(true); + progressDialog.setCancelable(false); + progressDialog.setMessage( + context.getString(R.string.master_clear_progress_text)); + return progressDialog; + } + /** * Restore APN settings to default. */ @@ -220,16 +228,16 @@ public class ResetNetworkConfirm extends InstrumentedFragment { public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { final EnforcedAdmin admin = RestrictedLockUtilsInternal.checkIfRestrictionEnforced( - getActivity(), UserManager.DISALLOW_NETWORK_RESET, UserHandle.myUserId()); - if (RestrictedLockUtilsInternal.hasBaseUserRestriction(getActivity(), + mActivity, UserManager.DISALLOW_NETWORK_RESET, UserHandle.myUserId()); + if (RestrictedLockUtilsInternal.hasBaseUserRestriction(mActivity, UserManager.DISALLOW_NETWORK_RESET, UserHandle.myUserId())) { return inflater.inflate(R.layout.network_reset_disallowed_screen, null); } else if (admin != null) { - new ActionDisabledByAdminDialogHelper(getActivity()) + new ActionDisabledByAdminDialogHelper(mActivity) .prepareDialogBuilder(UserManager.DISALLOW_NETWORK_RESET, admin) - .setOnDismissListener(__ -> getActivity().finish()) + .setOnDismissListener(__ -> mActivity.finish()) .show(); - return new View(getContext()); + return new View(mActivity); } mContentView = inflater.inflate(R.layout.reset_network_confirm, null); establishFinalConfirmationState(); @@ -247,13 +255,15 @@ public class ResetNetworkConfirm extends InstrumentedFragment { SubscriptionManager.INVALID_SUBSCRIPTION_ID); mEraseEsim = args.getBoolean(MasterClear.ERASE_ESIMS_EXTRA); } + + mActivity = getActivity(); } @Override public void onDestroy() { - if (mEraseEsimTask != null) { - mEraseEsimTask.cancel(true /* mayInterruptIfRunning */); - mEraseEsimTask = null; + if (mResetNetworkTask != null) { + mResetNetworkTask.cancel(true /* mayInterruptIfRunning */); + mResetNetworkTask = null; } super.onDestroy(); } diff --git a/tests/robotests/src/com/android/settings/ResetNetworkConfirmTest.java b/tests/robotests/src/com/android/settings/ResetNetworkConfirmTest.java index 1e960bf9d2e..6bade9822df 100644 --- a/tests/robotests/src/com/android/settings/ResetNetworkConfirmTest.java +++ b/tests/robotests/src/com/android/settings/ResetNetworkConfirmTest.java @@ -18,12 +18,12 @@ package com.android.settings; import static com.google.common.truth.Truth.assertThat; -import static org.mockito.Mockito.spy; - -import android.app.Activity; import android.view.LayoutInflater; import android.widget.TextView; +import androidx.fragment.app.FragmentActivity; + +import com.android.settings.testutils.shadow.ShadowBluetoothAdapter; import com.android.settings.testutils.shadow.ShadowRecoverySystem; import com.android.settings.testutils.shadow.ShadowWifiP2pManager; @@ -38,18 +38,21 @@ import org.robolectric.RobolectricTestRunner; import org.robolectric.annotation.Config; @RunWith(RobolectricTestRunner.class) -@Config(shadows = {ShadowRecoverySystem.class, ShadowWifiP2pManager.class}) +@Config(shadows = {ShadowRecoverySystem.class, + ShadowWifiP2pManager.class, ShadowBluetoothAdapter.class +}) public class ResetNetworkConfirmTest { - private Activity mActivity; + private FragmentActivity mActivity; @Mock private ResetNetworkConfirm mResetNetworkConfirm; @Before public void setUp() { MockitoAnnotations.initMocks(this); - mResetNetworkConfirm = spy(new ResetNetworkConfirm()); - mActivity = Robolectric.setupActivity(Activity.class); + mResetNetworkConfirm = new ResetNetworkConfirm(); + mActivity = Robolectric.setupActivity(FragmentActivity.class); + mResetNetworkConfirm.mActivity = mActivity; } @After @@ -62,10 +65,9 @@ public class ResetNetworkConfirmTest { public void testResetNetworkData_resetEsim() { mResetNetworkConfirm.mEraseEsim = true; - mResetNetworkConfirm.esimFactoryReset(mActivity, "" /* packageName */); + mResetNetworkConfirm.mFinalClickListener.onClick(null /* View */); Robolectric.getBackgroundThreadScheduler().advanceToLastPostedRunnable(); - assertThat(mResetNetworkConfirm.mEraseEsimTask).isNotNull(); assertThat(ShadowRecoverySystem.getWipeEuiccCalledCount()).isEqualTo(1); } @@ -73,9 +75,9 @@ public class ResetNetworkConfirmTest { public void testResetNetworkData_notResetEsim() { mResetNetworkConfirm.mEraseEsim = false; - mResetNetworkConfirm.esimFactoryReset(mActivity, "" /* packageName */); + mResetNetworkConfirm.mFinalClickListener.onClick(null /* View */); + Robolectric.getBackgroundThreadScheduler().advanceToLastPostedRunnable(); - assertThat(mResetNetworkConfirm.mEraseEsimTask).isNull(); assertThat(ShadowRecoverySystem.getWipeEuiccCalledCount()).isEqualTo(0); } @@ -93,25 +95,25 @@ public class ResetNetworkConfirmTest { public void setSubtitle_eraseEsim() { mResetNetworkConfirm.mEraseEsim = true; mResetNetworkConfirm.mContentView = - LayoutInflater.from(mActivity).inflate(R.layout.reset_network_confirm, null); + LayoutInflater.from(mActivity).inflate(R.layout.reset_network_confirm, null); mResetNetworkConfirm.setSubtitle(); assertThat(((TextView) mResetNetworkConfirm.mContentView - .findViewById(R.id.reset_network_confirm)).getText()) - .isEqualTo(mActivity.getString(R.string.reset_network_final_desc_esim)); + .findViewById(R.id.reset_network_confirm)).getText()) + .isEqualTo(mActivity.getString(R.string.reset_network_final_desc_esim)); } @Test public void setSubtitle_notEraseEsim() { mResetNetworkConfirm.mEraseEsim = false; mResetNetworkConfirm.mContentView = - LayoutInflater.from(mActivity).inflate(R.layout.reset_network_confirm, null); + LayoutInflater.from(mActivity).inflate(R.layout.reset_network_confirm, null); mResetNetworkConfirm.setSubtitle(); assertThat(((TextView) mResetNetworkConfirm.mContentView - .findViewById(R.id.reset_network_confirm)).getText()) - .isEqualTo(mActivity.getString(R.string.reset_network_final_desc)); + .findViewById(R.id.reset_network_confirm)).getText()) + .isEqualTo(mActivity.getString(R.string.reset_network_final_desc)); } } diff --git a/tests/robotests/src/com/android/settings/testutils/shadow/ShadowBluetoothAdapter.java b/tests/robotests/src/com/android/settings/testutils/shadow/ShadowBluetoothAdapter.java index 2444df66bf0..f65729366cf 100644 --- a/tests/robotests/src/com/android/settings/testutils/shadow/ShadowBluetoothAdapter.java +++ b/tests/robotests/src/com/android/settings/testutils/shadow/ShadowBluetoothAdapter.java @@ -51,4 +51,9 @@ public class ShadowBluetoothAdapter extends org.robolectric.shadows.ShadowBlueto public void setConnectionState(int state) { mState = state; } + + @Implementation + protected boolean factoryReset() { + return true; + } }