diff --git a/res/values/strings.xml b/res/values/strings.xml index 0e64431a288..0f383ffb27a 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -2697,6 +2697,16 @@ This will reset all network settings, including:\n\n
  • Wi\u2011Fi
  • \n
  • Mobile data
  • \n
  • Bluetooth
  • "
    + + + Reset Bluetooth & Wi\u2011Fi + + This will reset all Wi\u2011Fi & Bluetooth settings. You can\u2019t undo this action. + + Reset + + Bluetooth & Wi\u2011Fi have been reset + Erase diff --git a/res/xml/reset_dashboard_fragment.xml b/res/xml/reset_dashboard_fragment.xml index 25161a5a66d..d2c8ca0d1bf 100644 --- a/res/xml/reset_dashboard_fragment.xml +++ b/res/xml/reset_dashboard_fragment.xml @@ -28,6 +28,14 @@ settings:useAdminDisabledSummary="true" android:fragment="com.android.settings.ResetNetwork" /> + + + { + final AtomicReference exceptionDuringReset = + new AtomicReference(); + try { + resetOperation(); + } catch (Exception exception) { + exceptionDuringReset.set(exception); + } + mContext.getMainExecutor().execute(() -> endOfReset(exceptionDuringReset.get())); + }); + } + + @VisibleForTesting + protected ProgressDialog getProgressDialog(Context context) { + final ProgressDialog progressDialog = new ProgressDialog(context); + progressDialog.setIndeterminate(true); + progressDialog.setCancelable(false); + progressDialog.setMessage( + context.getString(R.string.main_clear_progress_text)); + return progressDialog; + } + + @VisibleForTesting + protected void resetOperation() throws Exception { + new ResetNetworkRequest( + ResetNetworkRequest.RESET_WIFI_MANAGER | + ResetNetworkRequest.RESET_WIFI_P2P_MANAGER | + ResetNetworkRequest.RESET_BLUETOOTH_MANAGER + ).toResetNetworkOperationBuilder(mContext, Looper.getMainLooper()) + .build().run(); + } + + @VisibleForTesting + protected void endOfReset(Exception exceptionDuringReset) { + if (mExecutorService != null) { + mExecutorService.shutdown(); + mExecutorService = null; + } + if (mProgressDialog != null) { + mProgressDialog.dismiss(); + mProgressDialog = null; + } + if (exceptionDuringReset == null) { + Toast.makeText(mContext, R.string.reset_bluetooth_wifi_complete_toast, + Toast.LENGTH_SHORT).show(); + } else { + Log.e(TAG, "Exception during reset", exceptionDuringReset); + } + } +} diff --git a/tests/robotests/src/com/android/settings/network/BluetoothWiFiResetPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/network/BluetoothWiFiResetPreferenceControllerTest.java new file mode 100644 index 00000000000..4714afee82a --- /dev/null +++ b/tests/robotests/src/com/android/settings/network/BluetoothWiFiResetPreferenceControllerTest.java @@ -0,0 +1,58 @@ +/* + * 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.network; + +import static com.google.common.truth.Truth.assertThat; +import static org.mockito.Mockito.spy; + +import android.content.Context; + +import com.android.settings.R; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.MockitoAnnotations; +import org.robolectric.RobolectricTestRunner; +import org.robolectric.RuntimeEnvironment; +import org.robolectric.shadows.ShadowToast; + +@RunWith(RobolectricTestRunner.class) +public class BluetoothWiFiResetPreferenceControllerTest { + + private static final String PREFERENCE_KEY = "network_reset_bluetooth_wifi_pref"; + + private Context mContext; + private BluetoothWiFiResetPreferenceController mTarget; + + @Before + public void setUp() { + MockitoAnnotations.initMocks(this); + mContext = spy(RuntimeEnvironment.application); + + mTarget = spy(new BluetoothWiFiResetPreferenceController(mContext, PREFERENCE_KEY)); + } + + @Test + public void endOfReset_toastMessage_whenSuccess() { + mTarget.endOfReset(null); + + assertThat(ShadowToast.getTextOfLatestToast()).isEqualTo( + mContext.getString(R.string.reset_bluetooth_wifi_complete_toast)); + } + +}