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

Commit 67007052 authored by Weng Su's avatar Weng Su Committed by Android (Google) Code Review
Browse files

Merge "Avoid unnecessary stopTethering when startTethering fails" into tm-dev

parents bbeed148 b1795081
Loading
Loading
Loading
Loading
+1 −0
Original line number Original line Diff line number Diff line
@@ -256,6 +256,7 @@ public class TetherService extends Service {
    }
    }


    private void disableTethering(final int tetheringType) {
    private void disableTethering(final int tetheringType) {
        Log.w(TAG, "Disable tethering, type:" + tetheringType);
        final TetheringManager tm = (TetheringManager) getSystemService(Context.TETHERING_SERVICE);
        final TetheringManager tm = (TetheringManager) getSystemService(Context.TETHERING_SERVICE);
        tm.stopTethering(tetheringType);
        tm.stopTethering(tetheringType);
    }
    }
+17 −2
Original line number Original line Diff line number Diff line
@@ -31,6 +31,7 @@ import android.net.ConnectivityManager;
import android.net.wifi.WifiManager;
import android.net.wifi.WifiManager;
import android.os.Handler;
import android.os.Handler;
import android.os.Looper;
import android.os.Looper;
import android.util.Log;
import android.widget.Switch;
import android.widget.Switch;


import androidx.annotation.VisibleForTesting;
import androidx.annotation.VisibleForTesting;
@@ -47,6 +48,8 @@ import com.android.settingslib.widget.OnMainSwitchChangeListener;
 */
 */
public class WifiTetherSwitchBarController implements
public class WifiTetherSwitchBarController implements
        LifecycleObserver, OnStart, OnStop, DataSaverBackend.Listener, OnMainSwitchChangeListener {
        LifecycleObserver, OnStart, OnStop, DataSaverBackend.Listener, OnMainSwitchChangeListener {

    private static final String TAG = "WifiTetherSBC";
    private static final IntentFilter WIFI_INTENT_FILTER;
    private static final IntentFilter WIFI_INTENT_FILTER;


    private final Context mContext;
    private final Context mContext;
@@ -63,8 +66,8 @@ public class WifiTetherSwitchBarController implements
                @Override
                @Override
                public void onTetheringFailed() {
                public void onTetheringFailed() {
                    super.onTetheringFailed();
                    super.onTetheringFailed();
                    mSwitchBar.setChecked(false);
                    Log.e(TAG, "Failed to start Wi-Fi Tethering.");
                    updateWifiSwitch();
                    handleWifiApStateChanged(mWifiManager.getWifiApState());
                }
                }
            };
            };


@@ -111,16 +114,28 @@ public class WifiTetherSwitchBarController implements
    }
    }


    void stopTether() {
    void stopTether() {
        if (!isWifiApActivated()) return;

        mSwitchBar.setEnabled(false);
        mSwitchBar.setEnabled(false);
        mConnectivityManager.stopTethering(TETHERING_WIFI);
        mConnectivityManager.stopTethering(TETHERING_WIFI);
    }
    }


    void startTether() {
    void startTether() {
        if (isWifiApActivated()) return;

        mSwitchBar.setEnabled(false);
        mSwitchBar.setEnabled(false);
        mConnectivityManager.startTethering(TETHERING_WIFI, true /* showProvisioningUi */,
        mConnectivityManager.startTethering(TETHERING_WIFI, true /* showProvisioningUi */,
                mOnStartTetheringCallback, new Handler(Looper.getMainLooper()));
                mOnStartTetheringCallback, new Handler(Looper.getMainLooper()));
    }
    }


    private boolean isWifiApActivated() {
        final int wifiApState = mWifiManager.getWifiApState();
        if (wifiApState == WIFI_AP_STATE_ENABLED || wifiApState == WIFI_AP_STATE_ENABLING) {
            return true;
        }
        return false;
    }

    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        @Override
        public void onReceive(Context context, Intent intent) {
        public void onReceive(Context context, Intent intent) {
+38 −0
Original line number Original line Diff line number Diff line
@@ -83,8 +83,45 @@ public class WifiTetherSwitchBarControllerTest {
        mController.mDataSaverBackend = mDataSaverBackend;
        mController.mDataSaverBackend = mDataSaverBackend;
    }
    }


    @Test
    public void startTether_wifiApIsActivated_doNothing() {
        when(mWifiManager.getWifiApState()).thenReturn(WIFI_AP_STATE_ENABLED);

        mController.startTether();

        verify(mConnectivityManager, never()).startTethering(anyInt(), anyBoolean(), any(), any());
    }

    @Test
    public void startTether_wifiApNotActivated_startTethering() {
        when(mWifiManager.getWifiApState()).thenReturn(WIFI_AP_STATE_DISABLED);

        mController.startTether();

        verify(mConnectivityManager).startTethering(anyInt(), anyBoolean(), any(), any());
    }

    @Test
    public void stopTether_wifiApIsActivated_stopTethering() {
        when(mWifiManager.getWifiApState()).thenReturn(WIFI_AP_STATE_ENABLED);

        mController.stopTether();

        verify(mConnectivityManager).stopTethering(anyInt());
    }

    @Test
    public void stopTether_wifiApNotActivated_doNothing() {
        when(mWifiManager.getWifiApState()).thenReturn(WIFI_AP_STATE_DISABLED);

        mController.stopTether();

        verify(mConnectivityManager, never()).stopTethering(anyInt());
    }

    @Test
    @Test
    public void startTether_fail_resetSwitchBar() {
    public void startTether_fail_resetSwitchBar() {
        when(mWifiManager.getWifiApState()).thenReturn(WIFI_AP_STATE_DISABLED);
        when(mDataSaverBackend.isDataSaverEnabled()).thenReturn(false);
        when(mDataSaverBackend.isDataSaverEnabled()).thenReturn(false);


        mController.startTether();
        mController.startTether();
@@ -130,6 +167,7 @@ public class WifiTetherSwitchBarControllerTest {


    @Test
    @Test
    public void onSwitchChanged_isNotChecked_stopTethering() {
    public void onSwitchChanged_isNotChecked_stopTethering() {
        when(mWifiManager.getWifiApState()).thenReturn(WIFI_AP_STATE_ENABLED);
        when(mSwitch.isChecked()).thenReturn(false);
        when(mSwitch.isChecked()).thenReturn(false);


        mController.onSwitchChanged(mSwitch, mSwitch.isChecked());
        mController.onSwitchChanged(mSwitch, mSwitch.isChecked());