Loading src/com/android/settings/network/telephony/gsm/AutoSelectPreferenceController.java +26 −21 Original line number Diff line number Diff line Loading @@ -51,6 +51,7 @@ import com.android.settingslib.utils.ThreadUtils; import java.util.ArrayList; import java.util.List; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; /** Loading Loading @@ -147,10 +148,27 @@ public class AutoSelectPreferenceController extends TelephonyTogglePreferenceCon @Override public boolean setChecked(boolean isChecked) { if (isChecked) { setAutomaticSelectionMode(); return false; } else { final Bundle bundle = new Bundle(); bundle.putInt(Settings.EXTRA_SUB_ID, mSubId); new SubSettingLauncher(mContext) .setDestination(NetworkSelectSettings.class.getName()) .setSourceMetricsCategory(SettingsEnums.MOBILE_NETWORK_SELECT) .setTitleRes(R.string.choose_network_title) .setArguments(bundle) .launch(); return false; } } @VisibleForTesting Future setAutomaticSelectionMode() { final long startMillis = SystemClock.elapsedRealtime(); showAutoSelectProgressBar(); mSwitchPreference.setEnabled(false); ThreadUtils.postOnBackgroundThread(() -> { return ThreadUtils.postOnBackgroundThread(() -> { // set network selection mode in background mTelephonyManager.setNetworkSelectionModeAutomatic(); final int mode = mTelephonyManager.getNetworkSelectionMode(); Loading @@ -165,21 +183,8 @@ public class AutoSelectPreferenceController extends TelephonyTogglePreferenceCon lsn.onNetworkSelectModeChanged(); } dismissProgressBar(); }, Math.max(MINIMUM_DIALOG_TIME_MILLIS - durationMillis, 0)); }, Math.max(MINIMUM_DIALOG_TIME_MILLIS - durationMillis, 0)); }); return false; } else { final Bundle bundle = new Bundle(); bundle.putInt(Settings.EXTRA_SUB_ID, mSubId); new SubSettingLauncher(mContext) .setDestination(NetworkSelectSettings.class.getName()) .setSourceMetricsCategory(SettingsEnums.MOBILE_NETWORK_SELECT) .setTitleRes(R.string.choose_network_title) .setArguments(bundle) .launch(); return false; } } public AutoSelectPreferenceController init(Lifecycle lifecycle, int subId) { Loading tests/unit/src/com/android/settings/network/telephony/gsm/AutoSelectPreferenceControllerTest.java +13 −2 Original line number Diff line number Diff line Loading @@ -18,6 +18,7 @@ package com.android.settings.network.telephony.gsm; import static com.google.common.truth.Truth.assertThat; import static org.junit.Assert.fail; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.verify; Loading @@ -42,6 +43,9 @@ import org.mockito.Answers; import org.mockito.Mock; import org.mockito.MockitoAnnotations; import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; @RunWith(AndroidJUnit4.class) public class AutoSelectPreferenceControllerTest { private static final int SUB_ID = 2; Loading Loading @@ -93,7 +97,14 @@ public class AutoSelectPreferenceControllerTest { when(mTelephonyManager.getNetworkSelectionMode()).thenReturn( TelephonyManager.NETWORK_SELECTION_MODE_AUTO); assertThat(mController.setChecked(true)).isFalse(); // Wait for asynchronous thread to finish, otherwise test will flake. Future thread = mController.setAutomaticSelectionMode(); try { thread.get(); } catch (ExecutionException | InterruptedException e) { e.printStackTrace(); fail("Exception during automatic selection"); } verify(mProgressDialog).show(); verify(mTelephonyManager).setNetworkSelectionModeAutomatic(); Loading Loading
src/com/android/settings/network/telephony/gsm/AutoSelectPreferenceController.java +26 −21 Original line number Diff line number Diff line Loading @@ -51,6 +51,7 @@ import com.android.settingslib.utils.ThreadUtils; import java.util.ArrayList; import java.util.List; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; /** Loading Loading @@ -147,10 +148,27 @@ public class AutoSelectPreferenceController extends TelephonyTogglePreferenceCon @Override public boolean setChecked(boolean isChecked) { if (isChecked) { setAutomaticSelectionMode(); return false; } else { final Bundle bundle = new Bundle(); bundle.putInt(Settings.EXTRA_SUB_ID, mSubId); new SubSettingLauncher(mContext) .setDestination(NetworkSelectSettings.class.getName()) .setSourceMetricsCategory(SettingsEnums.MOBILE_NETWORK_SELECT) .setTitleRes(R.string.choose_network_title) .setArguments(bundle) .launch(); return false; } } @VisibleForTesting Future setAutomaticSelectionMode() { final long startMillis = SystemClock.elapsedRealtime(); showAutoSelectProgressBar(); mSwitchPreference.setEnabled(false); ThreadUtils.postOnBackgroundThread(() -> { return ThreadUtils.postOnBackgroundThread(() -> { // set network selection mode in background mTelephonyManager.setNetworkSelectionModeAutomatic(); final int mode = mTelephonyManager.getNetworkSelectionMode(); Loading @@ -165,21 +183,8 @@ public class AutoSelectPreferenceController extends TelephonyTogglePreferenceCon lsn.onNetworkSelectModeChanged(); } dismissProgressBar(); }, Math.max(MINIMUM_DIALOG_TIME_MILLIS - durationMillis, 0)); }, Math.max(MINIMUM_DIALOG_TIME_MILLIS - durationMillis, 0)); }); return false; } else { final Bundle bundle = new Bundle(); bundle.putInt(Settings.EXTRA_SUB_ID, mSubId); new SubSettingLauncher(mContext) .setDestination(NetworkSelectSettings.class.getName()) .setSourceMetricsCategory(SettingsEnums.MOBILE_NETWORK_SELECT) .setTitleRes(R.string.choose_network_title) .setArguments(bundle) .launch(); return false; } } public AutoSelectPreferenceController init(Lifecycle lifecycle, int subId) { Loading
tests/unit/src/com/android/settings/network/telephony/gsm/AutoSelectPreferenceControllerTest.java +13 −2 Original line number Diff line number Diff line Loading @@ -18,6 +18,7 @@ package com.android.settings.network.telephony.gsm; import static com.google.common.truth.Truth.assertThat; import static org.junit.Assert.fail; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.verify; Loading @@ -42,6 +43,9 @@ import org.mockito.Answers; import org.mockito.Mock; import org.mockito.MockitoAnnotations; import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; @RunWith(AndroidJUnit4.class) public class AutoSelectPreferenceControllerTest { private static final int SUB_ID = 2; Loading Loading @@ -93,7 +97,14 @@ public class AutoSelectPreferenceControllerTest { when(mTelephonyManager.getNetworkSelectionMode()).thenReturn( TelephonyManager.NETWORK_SELECTION_MODE_AUTO); assertThat(mController.setChecked(true)).isFalse(); // Wait for asynchronous thread to finish, otherwise test will flake. Future thread = mController.setAutomaticSelectionMode(); try { thread.get(); } catch (ExecutionException | InterruptedException e) { e.printStackTrace(); fail("Exception during automatic selection"); } verify(mProgressDialog).show(); verify(mTelephonyManager).setNetworkSelectionModeAutomatic(); Loading