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

Commit acd25f92 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Automerger Merge Worker
Browse files

Merge "Use Wi-Fi state cache in Internet Panel" into tm-dev am: ac3033fd

parents 2a3371e2 ac3033fd
Loading
Loading
Loading
Loading
+8 −11
Original line number Diff line number Diff line
@@ -24,7 +24,6 @@ import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.net.Network;
import android.net.NetworkCapabilities;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.Handler;
import android.telephony.ServiceState;
@@ -90,8 +89,6 @@ public class InternetDialog extends SystemUIDialog implements
    @VisibleForTesting
    protected InternetAdapter mAdapter;
    @VisibleForTesting
    protected WifiManager mWifiManager;
    @VisibleForTesting
    protected View mDialogView;
    @VisibleForTesting
    protected boolean mCanConfigWifi;
@@ -179,7 +176,6 @@ public class InternetDialog extends SystemUIDialog implements
        mSubscriptionManager = mInternetDialogController.getSubscriptionManager();
        mDefaultDataSubId = mInternetDialogController.getDefaultDataSubscriptionId();
        mTelephonyManager = mInternetDialogController.getTelephonyManager();
        mWifiManager = mInternetDialogController.getWifiManager();
        mCanConfigMobileData = canConfigMobileData;
        mCanConfigWifi = canConfigWifi;
        mCanChangeWifiState = WifiEnterpriseRestrictionUtils.isChangeWifiStateAllowed(context);
@@ -332,7 +328,7 @@ public class InternetDialog extends SystemUIDialog implements

        showProgressBar();
        final boolean isDeviceLocked = mInternetDialogController.isDeviceLocked();
        final boolean isWifiEnabled = mWifiManager != null && mWifiManager.isWifiEnabled();
        final boolean isWifiEnabled = mInternetDialogController.isWifiEnabled();
        final boolean isWifiScanEnabled = mInternetDialogController.isWifiScanEnabled();
        updateWifiToggle(isWifiEnabled, isDeviceLocked);
        updateConnectedWifi(isWifiEnabled, isDeviceLocked);
@@ -362,9 +358,8 @@ public class InternetDialog extends SystemUIDialog implements
        mSeeAllLayout.setOnClickListener(this::onClickSeeMoreButton);
        mWiFiToggle.setOnCheckedChangeListener(
                (buttonView, isChecked) -> {
                    if (mWifiManager == null) return;
                    buttonView.setChecked(isChecked);
                    mWifiManager.setWifiEnabled(isChecked);
                    if (mInternetDialogController.isWifiEnabled() == isChecked) return;
                    mInternetDialogController.setWifiEnabled(isChecked);
                });
        mDoneButton.setOnClickListener(v -> dismiss());
        mAirplaneModeButton.setOnClickListener(v -> {
@@ -388,7 +383,7 @@ public class InternetDialog extends SystemUIDialog implements
            Log.d(TAG, "setMobileDataLayout, isCarrierNetworkActive = " + isCarrierNetworkActive);
        }

        boolean isWifiEnabled = mWifiManager != null && mWifiManager.isWifiEnabled();
        boolean isWifiEnabled = mInternetDialogController.isWifiEnabled();
        if (!mInternetDialogController.hasActiveSubId()
                && (!isWifiEnabled || !isCarrierNetworkActive)) {
            mMobileNetworkLayout.setVisibility(View.GONE);
@@ -444,7 +439,9 @@ public class InternetDialog extends SystemUIDialog implements

    @MainThread
    private void updateWifiToggle(boolean isWifiEnabled, boolean isDeviceLocked) {
        if (mWiFiToggle.isChecked() != isWifiEnabled) {
            mWiFiToggle.setChecked(isWifiEnabled);
        }
        if (isDeviceLocked) {
            mWifiToggleTitleText.setTextAppearance((mConnectedWifiEntry != null)
                    ? R.style.TextAppearance_InternetDialog_Active
@@ -572,7 +569,7 @@ public class InternetDialog extends SystemUIDialog implements
    }

    protected void showProgressBar() {
        if (mWifiManager == null || !mWifiManager.isWifiEnabled()
        if (!mInternetDialogController.isWifiEnabled()
                || mInternetDialogController.isDeviceLocked()) {
            setProgressBarVisible(false);
            return;
+28 −2
Original line number Diff line number Diff line
@@ -22,6 +22,7 @@ import static com.android.wifitrackerlib.WifiEntry.CONNECTED_STATE_CONNECTED;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.annotation.AnyThread;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
@@ -157,6 +158,7 @@ public class InternetDialogController implements AccessPointController.AccessPoi
    private LocationController mLocationController;
    private DialogLaunchAnimator mDialogLaunchAnimator;
    private boolean mHasWifiEntries;
    private WifiStateWorker mWifiStateWorker;

    @VisibleForTesting
    static final float TOAST_PARAMS_HORIZONTAL_WEIGHT = 1.0f;
@@ -210,7 +212,9 @@ public class InternetDialogController implements AccessPointController.AccessPoi
            @Background Handler workerHandler,
            CarrierConfigTracker carrierConfigTracker,
            LocationController locationController,
            DialogLaunchAnimator dialogLaunchAnimator) {
            DialogLaunchAnimator dialogLaunchAnimator,
            WifiStateWorker wifiStateWorker
    ) {
        if (DEBUG) {
            Log.d(TAG, "Init InternetDialogController");
        }
@@ -241,6 +245,7 @@ public class InternetDialogController implements AccessPointController.AccessPoi
        mLocationController = locationController;
        mDialogLaunchAnimator = dialogLaunchAnimator;
        mConnectedWifiInternetMonitor = new ConnectedWifiInternetMonitor();
        mWifiStateWorker = wifiStateWorker;
    }

    void onStart(@NonNull InternetDialogCallback callback, boolean canConfigWifi) {
@@ -323,7 +328,7 @@ public class InternetDialogController implements AccessPointController.AccessPoi

    @Nullable
    CharSequence getSubtitleText(boolean isProgressBarVisible) {
        if (mCanConfigWifi && !mWifiManager.isWifiEnabled()) {
        if (mCanConfigWifi && !isWifiEnabled()) {
            // When Wi-Fi is disabled.
            //   Sub-Title: Wi-Fi is off
            if (DEBUG) {
@@ -648,6 +653,27 @@ public class InternetDialogController implements AccessPointController.AccessPoi
        startActivity(intent, view);
    }

    /**
     * Enable or disable Wi-Fi.
     *
     * @param enabled {@code true} to enable, {@code false} to disable.
     */
    @AnyThread
    public void setWifiEnabled(boolean enabled) {
        mWifiStateWorker.setWifiEnabled(enabled);
    }

    /**
     * Return whether Wi-Fi is enabled or disabled.
     *
     * @return {@code true} if Wi-Fi is enabled or enabling
     * @see WifiManager#getWifiState()
     */
    @AnyThread
    public boolean isWifiEnabled() {
        return mWifiStateWorker.isWifiEnabled();
    }

    void connectCarrierNetwork() {
        final MergedCarrierEntry mergedCarrierEntry =
                mAccessPointController.getMergedCarrierEntry();
+124 −0
Original line number Diff line number Diff line
/*
 * 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.systemui.qs.tiles.dialog;

import static android.net.wifi.WifiManager.EXTRA_WIFI_STATE;
import static android.net.wifi.WifiManager.WIFI_STATE_CHANGED_ACTION;
import static android.net.wifi.WifiManager.WIFI_STATE_DISABLED;
import static android.net.wifi.WifiManager.WIFI_STATE_DISABLING;
import static android.net.wifi.WifiManager.WIFI_STATE_ENABLED;
import static android.net.wifi.WifiManager.WIFI_STATE_ENABLING;
import static android.net.wifi.WifiManager.WIFI_STATE_UNKNOWN;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.wifi.WifiManager;
import android.util.Log;

import androidx.annotation.AnyThread;
import androidx.annotation.Nullable;

import com.android.systemui.broadcast.BroadcastDispatcher;
import com.android.systemui.dagger.SysUISingleton;
import com.android.systemui.dagger.qualifiers.Background;
import com.android.systemui.util.concurrency.DelayableExecutor;

import javax.inject.Inject;

/**
 * Worker for the Wi-Fi enabled state cache.
 */
@SysUISingleton
public class WifiStateWorker extends BroadcastReceiver {

    private static final String TAG = "WifiStateWorker";

    private DelayableExecutor mBackgroundExecutor;
    private WifiManager mWifiManager;
    private int mWifiState = WIFI_STATE_DISABLED;

    @Inject
    public WifiStateWorker(
            BroadcastDispatcher broadcastDispatcher,
            @Background DelayableExecutor backgroundExecutor,
            @Nullable WifiManager wifiManager) {
        mWifiManager = wifiManager;
        mBackgroundExecutor = backgroundExecutor;

        broadcastDispatcher.registerReceiver(this, new IntentFilter(WIFI_STATE_CHANGED_ACTION));
        mBackgroundExecutor.execute(() -> {
            if (mWifiManager == null) return;

            mWifiState = mWifiManager.getWifiState();
            Log.i(TAG, "WifiManager.getWifiState():" + mWifiState);
        });
    }

    /**
     * Enable or disable Wi-Fi.
     *
     * @param enabled {@code true} to enable, {@code false} to disable.
     */
    @AnyThread
    public void setWifiEnabled(boolean enabled) {
        mBackgroundExecutor.execute(() -> {
            if (mWifiManager == null) return;

            mWifiState = (enabled) ? WIFI_STATE_ENABLING : WIFI_STATE_DISABLING;
            if (!mWifiManager.setWifiEnabled(enabled)) {
                Log.e(TAG, "Failed to WifiManager.setWifiEnabled(" + enabled + ");");
            }
        });
    }

    /**
     * Gets the Wi-Fi enabled state.
     *
     * @return One of {@link WifiManager#WIFI_STATE_DISABLED},
     *         {@link WifiManager#WIFI_STATE_DISABLING}, {@link WifiManager#WIFI_STATE_ENABLED},
     *         {@link WifiManager#WIFI_STATE_ENABLING}
     */
    @AnyThread
    public int getWifiState() {
        return mWifiState;
    }

    /**
     * Return whether Wi-Fi is enabled or disabled.
     *
     * @return {@code true} if Wi-Fi is enabled or enabling
     * @see WifiManager#getWifiState()
     */
    @AnyThread
    public boolean isWifiEnabled() {
        return (mWifiState == WIFI_STATE_ENABLED || mWifiState == WIFI_STATE_ENABLING);
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent == null) return;

        if (WIFI_STATE_CHANGED_ACTION.equals(intent.getAction())) {
            final int wifiState = intent.getIntExtra(EXTRA_WIFI_STATE, WIFI_STATE_DISABLED);
            if (wifiState == WIFI_STATE_UNKNOWN) return;

            mWifiState = wifiState;
        }
    }
}
+12 −9
Original line number Diff line number Diff line
@@ -138,6 +138,8 @@ public class InternetDialogControllerTest extends SysuiTestCase {
    private DialogLaunchAnimator mDialogLaunchAnimator;
    @Mock
    private View mDialogLaunchView;
    @Mock
    private WifiStateWorker mWifiStateWorker;

    private TestableResources mTestableResources;
    private InternetDialogController mInternetDialogController;
@@ -166,6 +168,7 @@ public class InternetDialogControllerTest extends SysuiTestCase {
        when(mSystemUIToast.getView()).thenReturn(mToastView);
        when(mSystemUIToast.getGravity()).thenReturn(GRAVITY_FLAGS);
        when(mSystemUIToast.getInAnimation()).thenReturn(mAnimator);
        when(mWifiStateWorker.isWifiEnabled()).thenReturn(true);

        mInternetDialogController = new InternetDialogController(mContext,
                mock(UiEventLogger.class), mock(ActivityStarter.class), mAccessPointController,
@@ -173,7 +176,7 @@ public class InternetDialogControllerTest extends SysuiTestCase {
                mock(ConnectivityManager.class), mHandler, mExecutor, mBroadcastDispatcher,
                mock(KeyguardUpdateMonitor.class), mGlobalSettings, mKeyguardStateController,
                mWindowManager, mToastFactory, mWorkerHandler, mCarrierConfigTracker,
                mLocationController, mDialogLaunchAnimator);
                mLocationController, mDialogLaunchAnimator, mWifiStateWorker);
        mSubscriptionManager.addOnSubscriptionsChangedListener(mExecutor,
                mInternetDialogController.mOnSubscriptionsChangedListener);
        mInternetDialogController.onStart(mInternetDialogCallback, true);
@@ -239,7 +242,7 @@ public class InternetDialogControllerTest extends SysuiTestCase {
    @Test
    public void getSubtitleText_withApmOnAndWifiOff_returnWifiIsOff() {
        fakeAirplaneModeEnabled(true);
        when(mWifiManager.isWifiEnabled()).thenReturn(false);
        when(mWifiStateWorker.isWifiEnabled()).thenReturn(false);

        assertThat(mInternetDialogController.getSubtitleText(false))
                .isEqualTo(getResourcesString("wifi_is_off"));
@@ -254,7 +257,7 @@ public class InternetDialogControllerTest extends SysuiTestCase {
    @Test
    public void getSubtitleText_withWifiOff_returnWifiIsOff() {
        fakeAirplaneModeEnabled(false);
        when(mWifiManager.isWifiEnabled()).thenReturn(false);
        when(mWifiStateWorker.isWifiEnabled()).thenReturn(false);

        assertThat(mInternetDialogController.getSubtitleText(false))
                .isEqualTo(getResourcesString("wifi_is_off"));
@@ -269,7 +272,7 @@ public class InternetDialogControllerTest extends SysuiTestCase {
    @Test
    public void getSubtitleText_withNoWifiEntry_returnSearchWifi() {
        fakeAirplaneModeEnabled(false);
        when(mWifiManager.isWifiEnabled()).thenReturn(true);
        when(mWifiStateWorker.isWifiEnabled()).thenReturn(true);
        mInternetDialogController.onAccessPointsChanged(null /* accessPoints */);

        assertThat(mInternetDialogController.getSubtitleText(true))
@@ -286,7 +289,7 @@ public class InternetDialogControllerTest extends SysuiTestCase {
    public void getSubtitleText_withWifiEntry_returnTapToConnect() {
        // The preconditions WiFi Entries is already in setUp()
        fakeAirplaneModeEnabled(false);
        when(mWifiManager.isWifiEnabled()).thenReturn(true);
        when(mWifiStateWorker.isWifiEnabled()).thenReturn(true);

        assertThat(mInternetDialogController.getSubtitleText(false))
                .isEqualTo(getResourcesString("tap_a_network_to_connect"));
@@ -301,7 +304,7 @@ public class InternetDialogControllerTest extends SysuiTestCase {
    @Test
    public void getSubtitleText_deviceLockedWithWifiOn_returnUnlockToViewNetworks() {
        fakeAirplaneModeEnabled(false);
        when(mWifiManager.isWifiEnabled()).thenReturn(true);
        when(mWifiStateWorker.isWifiEnabled()).thenReturn(true);
        when(mKeyguardStateController.isUnlocked()).thenReturn(false);

        assertTrue(TextUtils.equals(mInternetDialogController.getSubtitleText(false),
@@ -311,7 +314,7 @@ public class InternetDialogControllerTest extends SysuiTestCase {
    @Test
    public void getSubtitleText_withNoService_returnNoNetworksAvailable() {
        fakeAirplaneModeEnabled(false);
        when(mWifiManager.isWifiEnabled()).thenReturn(true);
        when(mWifiStateWorker.isWifiEnabled()).thenReturn(true);
        mInternetDialogController.onAccessPointsChanged(null /* accessPoints */);

        doReturn(ServiceState.STATE_OUT_OF_SERVICE).when(mServiceState).getState();
@@ -325,7 +328,7 @@ public class InternetDialogControllerTest extends SysuiTestCase {
    @Test
    public void getSubtitleText_withMobileDataDisabled_returnNoOtherAvailable() {
        fakeAirplaneModeEnabled(false);
        when(mWifiManager.isWifiEnabled()).thenReturn(true);
        when(mWifiStateWorker.isWifiEnabled()).thenReturn(true);
        mInternetDialogController.onAccessPointsChanged(null /* accessPoints */);

        doReturn(ServiceState.STATE_IN_SERVICE).when(mServiceState).getState();
@@ -346,7 +349,7 @@ public class InternetDialogControllerTest extends SysuiTestCase {
    @Test
    public void getSubtitleText_withCarrierNetworkActiveOnly_returnNoOtherAvailable() {
        fakeAirplaneModeEnabled(false);
        when(mWifiManager.isWifiEnabled()).thenReturn(true);
        when(mWifiStateWorker.isWifiEnabled()).thenReturn(true);
        mInternetDialogController.onAccessPointsChanged(null /* accessPoints */);
        when(mMergedCarrierEntry.isDefaultNetwork()).thenReturn(true);

+29 −13
Original line number Diff line number Diff line
@@ -14,7 +14,6 @@ import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import android.net.wifi.WifiManager;
import android.os.Handler;
import android.telephony.TelephonyManager;
import android.testing.AndroidTestingRunner;
@@ -64,8 +63,6 @@ public class InternetDialogTest extends SysuiTestCase {
    @Mock
    private TelephonyManager mTelephonyManager;
    @Mock
    private WifiManager mWifiManager;
    @Mock
    private WifiEntry mInternetWifiEntry;
    @Mock
    private List<WifiEntry> mWifiEntries;
@@ -97,7 +94,6 @@ public class InternetDialogTest extends SysuiTestCase {
    public void setUp() {
        MockitoAnnotations.initMocks(this);
        doReturn(mTelephonyManager).when(mTelephonyManager).createForSubscriptionId(anyInt());
        when(mWifiManager.isWifiEnabled()).thenReturn(true);
        when(mInternetWifiEntry.getTitle()).thenReturn(WIFI_TITLE);
        when(mInternetWifiEntry.getSummary(false)).thenReturn(WIFI_SUMMARY);
        when(mInternetWifiEntry.isDefaultNetwork()).thenReturn(true);
@@ -107,7 +103,7 @@ public class InternetDialogTest extends SysuiTestCase {
        when(mInternetDialogController.getMobileNetworkTitle()).thenReturn(MOBILE_NETWORK_TITLE);
        when(mInternetDialogController.getMobileNetworkSummary())
                .thenReturn(MOBILE_NETWORK_SUMMARY);
        when(mInternetDialogController.getWifiManager()).thenReturn(mWifiManager);
        when(mInternetDialogController.isWifiEnabled()).thenReturn(true);

        mMockitoSession = ExtendedMockito.mockitoSession()
                .spyStatic(WifiEnterpriseRestrictionUtils.class)
@@ -232,7 +228,7 @@ public class InternetDialogTest extends SysuiTestCase {
        // Carrier network should be gone if airplane mode ON and Wi-Fi is off.
        when(mInternetDialogController.isCarrierNetworkActive()).thenReturn(true);
        when(mInternetDialogController.isAirplaneModeEnabled()).thenReturn(true);
        when(mWifiManager.isWifiEnabled()).thenReturn(false);
        when(mInternetDialogController.isWifiEnabled()).thenReturn(false);

        mInternetDialog.updateDialog(true);

@@ -241,7 +237,7 @@ public class InternetDialogTest extends SysuiTestCase {
        // Carrier network should be visible if airplane mode ON and Wi-Fi is ON.
        when(mInternetDialogController.isCarrierNetworkActive()).thenReturn(true);
        when(mInternetDialogController.isAirplaneModeEnabled()).thenReturn(true);
        when(mWifiManager.isWifiEnabled()).thenReturn(true);
        when(mInternetDialogController.isWifiEnabled()).thenReturn(true);

        mInternetDialog.updateDialog(true);

@@ -468,7 +464,7 @@ public class InternetDialogTest extends SysuiTestCase {

    @Test
    public void updateDialog_wifiOffAndWifiScanOff_hideWifiScanNotify() {
        when(mWifiManager.isWifiEnabled()).thenReturn(false);
        when(mInternetDialogController.isWifiEnabled()).thenReturn(false);
        when(mInternetDialogController.isWifiScanEnabled()).thenReturn(false);

        mInternetDialog.updateDialog(false);
@@ -478,7 +474,7 @@ public class InternetDialogTest extends SysuiTestCase {

    @Test
    public void updateDialog_wifiOffAndWifiScanOnAndDeviceLocked_hideWifiScanNotify() {
        when(mWifiManager.isWifiEnabled()).thenReturn(false);
        when(mInternetDialogController.isWifiEnabled()).thenReturn(false);
        when(mInternetDialogController.isWifiScanEnabled()).thenReturn(true);
        when(mInternetDialogController.isDeviceLocked()).thenReturn(true);

@@ -489,7 +485,7 @@ public class InternetDialogTest extends SysuiTestCase {

    @Test
    public void updateDialog_wifiOffAndWifiScanOnAndDeviceUnlocked_showWifiScanNotify() {
        when(mWifiManager.isWifiEnabled()).thenReturn(false);
        when(mInternetDialogController.isWifiEnabled()).thenReturn(false);
        when(mInternetDialogController.isWifiScanEnabled()).thenReturn(true);
        when(mInternetDialogController.isDeviceLocked()).thenReturn(false);

@@ -501,6 +497,26 @@ public class InternetDialogTest extends SysuiTestCase {
        assertThat(wifiScanNotifyText.getMovementMethod()).isNotNull();
    }

    @Test
    public void updateDialog_wifiIsDisabled_uncheckWifiSwitch() {
        when(mInternetDialogController.isWifiEnabled()).thenReturn(false);
        mWifiToggleSwitch.setChecked(true);

        mInternetDialog.updateDialog(false);

        assertThat(mWifiToggleSwitch.isChecked()).isFalse();
    }

    @Test
    public void updateDialog_wifiIsEnabled_checkWifiSwitch() {
        when(mInternetDialogController.isWifiEnabled()).thenReturn(true);
        mWifiToggleSwitch.setChecked(false);

        mInternetDialog.updateDialog(false);

        assertThat(mWifiToggleSwitch.isChecked()).isTrue();
    }

    @Test
    public void onClickSeeMoreButton_clickSeeAll_verifyLaunchNetworkSetting() {
        mSeeAll.performClick();
@@ -512,7 +528,7 @@ public class InternetDialogTest extends SysuiTestCase {
    @Test
    public void showProgressBar_wifiDisabled_hideProgressBar() {
        Mockito.reset(mHandler);
        when(mWifiManager.isWifiEnabled()).thenReturn(false);
        when(mInternetDialogController.isWifiEnabled()).thenReturn(false);

        mInternetDialog.showProgressBar();

@@ -534,7 +550,7 @@ public class InternetDialogTest extends SysuiTestCase {
    @Test
    public void showProgressBar_wifiEnabledWithWifiEntry_showProgressBarThenHide() {
        Mockito.reset(mHandler);
        when(mWifiManager.isWifiEnabled()).thenReturn(true);
        when(mInternetDialogController.isWifiEnabled()).thenReturn(true);

        mInternetDialog.showProgressBar();

@@ -553,7 +569,7 @@ public class InternetDialogTest extends SysuiTestCase {
    @Test
    public void showProgressBar_wifiEnabledWithoutWifiEntries_showProgressBarThenHideSearch() {
        Mockito.reset(mHandler);
        when(mWifiManager.isWifiEnabled()).thenReturn(true);
        when(mInternetDialogController.isWifiEnabled()).thenReturn(true);
        mInternetDialog.mConnectedWifiEntry = null;
        mInternetDialog.mWifiEntriesCount = 0;

Loading