Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit e4f64e5c authored by Lei Yu's avatar Lei Yu Committed by Android (Google) Code Review
Browse files

Merge "Move telephony method to worker thread"

parents 5a9f4473 6db1440a
Loading
Loading
Loading
Loading
+3 −4
Original line number Diff line number Diff line
@@ -96,11 +96,10 @@ public class NetworkSelectSettings extends DashboardFragment {
                com.android.internal.R.bool.config_enableNewAutoSelectNetworkUI);
        mSubId = getArguments().getInt(Settings.EXTRA_SUB_ID);

        mConnectedPreferenceCategory =
                (PreferenceCategory) findPreference(PREF_KEY_CONNECTED_NETWORK_OPERATOR);
        mPreferenceCategory =
                (PreferenceCategory) findPreference(PREF_KEY_NETWORK_OPERATORS);
        mConnectedPreferenceCategory = findPreference(PREF_KEY_CONNECTED_NETWORK_OPERATOR);
        mPreferenceCategory = findPreference(PREF_KEY_NETWORK_OPERATORS);
        mStatusMessagePreference = new Preference(getContext());
        mStatusMessagePreference.setSelectable(false);
        mSelectedPreference = null;
        mTelephonyManager = TelephonyManager.from(getContext()).createForSubscriptionId(mSubId);
        mNetworkScanHelper = new NetworkScanHelper(
+62 −8
Original line number Diff line number Diff line
@@ -16,41 +16,57 @@

package com.android.settings.network.telephony.gsm;

import android.app.ProgressDialog;
import android.app.settings.SettingsEnums;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.PersistableBundle;
import android.os.SystemClock;
import android.provider.Settings;
import android.telephony.CarrierConfigManager;
import android.telephony.SubscriptionManager;
import android.telephony.TelephonyManager;

import androidx.annotation.VisibleForTesting;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
import androidx.preference.SwitchPreference;

import com.android.settings.R;
import com.android.settings.core.SubSettingLauncher;
import com.android.settings.network.telephony.MobileNetworkUtils;
import com.android.settings.network.telephony.NetworkSelectSettings;
import com.android.settings.network.telephony.TelephonyTogglePreferenceController;
import com.android.settingslib.utils.ThreadUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * Preference controller for "Auto Select Network"
 */
public class AutoSelectPreferenceController extends TelephonyTogglePreferenceController {
    private static final long MINIMUM_DIALOG_TIME_MILLIS = TimeUnit.SECONDS.toMillis(1);

    private final Handler mUiHandler;
    private int mSubId;
    private TelephonyManager mTelephonyManager;
    private boolean mOnlyAutoSelectInHome;
    private List<OnNetworkSelectModeListener> mListeners;
    @VisibleForTesting
    ProgressDialog mProgressDialog;
    @VisibleForTesting
    SwitchPreference mSwitchPreference;

    public AutoSelectPreferenceController(Context context, String key) {
        super(context, key);
        mTelephonyManager = context.getSystemService(TelephonyManager.class);
        mSubId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
        mListeners = new ArrayList<>();
        mUiHandler = new Handler(Looper.getMainLooper());
    }

    @Override
@@ -60,6 +76,12 @@ public class AutoSelectPreferenceController extends TelephonyTogglePreferenceCon
                : CONDITIONALLY_UNAVAILABLE;
    }

    @Override
    public void displayPreference(PreferenceScreen screen) {
        super.displayPreference(screen);
        mSwitchPreference = screen.findPreference(getPreferenceKey());
    }

    @Override
    public boolean isChecked() {
        return mTelephonyManager.getNetworkSelectionMode()
@@ -86,14 +108,28 @@ public class AutoSelectPreferenceController extends TelephonyTogglePreferenceCon
    @Override
    public boolean setChecked(boolean isChecked) {
        if (isChecked) {
            final long startMillis = SystemClock.elapsedRealtime();
            showAutoSelectProgressBar();
            mSwitchPreference.setEnabled(false);
            ThreadUtils.postOnBackgroundThread(() -> {
                // set network selection mode in background
                mTelephonyManager.setNetworkSelectionModeAutomatic();

                final int mode = mTelephonyManager.getNetworkSelectionMode();

                //Update UI in UI thread
                final long durationMillis = SystemClock.elapsedRealtime() - startMillis;
                mUiHandler.postDelayed(() -> {
                            mSwitchPreference.setEnabled(true);
                            mSwitchPreference.setChecked(
                                    mode == TelephonyManager.NETWORK_SELECTION_MODE_AUTO);
                            for (OnNetworkSelectModeListener lsn : mListeners) {
                                lsn.onNetworkSelectModeChanged();
                            }
            // Manually check whether it is successfully
            return mTelephonyManager.getNetworkSelectionMode()
                    == TelephonyManager.NETWORK_SELECTION_MODE_AUTO;
                            dismissProgressBar();
                        },
                        Math.max(MINIMUM_DIALOG_TIME_MILLIS - durationMillis, 0));
            });
            return false;
        } else {
            final Bundle bundle = new Bundle();
            bundle.putInt(Settings.EXTRA_SUB_ID, mSubId);
@@ -126,6 +162,24 @@ public class AutoSelectPreferenceController extends TelephonyTogglePreferenceCon
        return this;
    }

    private void showAutoSelectProgressBar() {
        if (mProgressDialog == null) {
            mProgressDialog = new ProgressDialog(mContext);
            mProgressDialog.setMessage(
                    mContext.getResources().getString(R.string.register_automatically));
            mProgressDialog.setCanceledOnTouchOutside(false);
            mProgressDialog.setCancelable(false);
            mProgressDialog.setIndeterminate(true);
        }
        mProgressDialog.show();
    }

    private void dismissProgressBar() {
        if (mProgressDialog != null && mProgressDialog.isShowing()) {
            mProgressDialog.dismiss();
        }
    }

    /**
     * Callback when network select mode is changed
     *
+18 −10
Original line number Diff line number Diff line
@@ -23,13 +23,14 @@ import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.PersistableBundle;
import android.telephony.CarrierConfigManager;
import android.telephony.SubscriptionManager;
import android.telephony.TelephonyManager;

import androidx.preference.Preference;
import androidx.preference.SwitchPreference;

import com.android.settings.R;

@@ -39,6 +40,7 @@ import org.junit.runner.RunWith;
import org.mockito.Answers;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;

@@ -53,10 +55,12 @@ public class AutoSelectPreferenceControllerTest {
    private SubscriptionManager mSubscriptionManager;
    @Mock
    private CarrierConfigManager mCarrierConfigManager;
    @Mock
    private ProgressDialog mProgressDialog;

    private PersistableBundle mCarrierConfig;
    private AutoSelectPreferenceController mController;
    private Preference mPreference;
    private SwitchPreference mSwitchPreference;
    private Context mContext;

    @Before
@@ -75,18 +79,22 @@ public class AutoSelectPreferenceControllerTest {
                true);
        when(mCarrierConfigManager.getConfigForSubId(SUB_ID)).thenReturn(mCarrierConfig);

        mPreference = new Preference(mContext);
        mSwitchPreference = new SwitchPreference(mContext);
        mController = new AutoSelectPreferenceController(mContext, "auto_select");
        mController.mProgressDialog = mProgressDialog;
        mController.mSwitchPreference = mSwitchPreference;
        mController.init(SUB_ID);
    }

    @Test
    public void setChecked_isChecked_updateValue() {
    public void setChecked_isChecked_showProgressDialog() {
        when(mTelephonyManager.getNetworkSelectionMode()).thenReturn(
                TelephonyManager.NETWORK_SELECTION_MODE_AUTO);

        assertThat(mController.setChecked(true)).isTrue();
        assertThat(mController.setChecked(true)).isFalse();
        Robolectric.flushBackgroundThreadScheduler();

        verify(mProgressDialog).show();
        verify(mTelephonyManager).setNetworkSelectionModeAutomatic();
    }

@@ -94,9 +102,9 @@ public class AutoSelectPreferenceControllerTest {
    public void updateState_isRoaming_enabled() {
        when(mTelephonyManager.getServiceState().getRoaming()).thenReturn(true);

        mController.updateState(mPreference);
        mController.updateState(mSwitchPreference);

        assertThat(mPreference.isEnabled()).isTrue();
        assertThat(mSwitchPreference.isEnabled()).isTrue();
    }

    @Test
@@ -104,10 +112,10 @@ public class AutoSelectPreferenceControllerTest {
        when(mTelephonyManager.getServiceState().getRoaming()).thenReturn(false);
        doReturn(OPERATOR_NAME).when(mTelephonyManager).getSimOperatorName();

        mController.updateState(mPreference);
        mController.updateState(mSwitchPreference);

        assertThat(mPreference.isEnabled()).isFalse();
        assertThat(mPreference.getSummary()).isEqualTo(
        assertThat(mSwitchPreference.isEnabled()).isFalse();
        assertThat(mSwitchPreference.getSummary()).isEqualTo(
                mContext.getString(R.string.manual_mode_disallowed_summary,
                        mTelephonyManager.getSimOperatorName()));
    }