Loading packages/SystemUI/src/com/android/systemui/qs/tiles/dialog/InternetDialog.java +8 −11 Original line number Diff line number Diff line Loading @@ -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; Loading Loading @@ -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; Loading Loading @@ -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); Loading Loading @@ -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); Loading Loading @@ -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 -> { Loading @@ -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); Loading Loading @@ -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 Loading Loading @@ -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; Loading packages/SystemUI/src/com/android/systemui/qs/tiles/dialog/InternetDialogController.java +28 −2 Original line number Diff line number Diff line Loading @@ -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; Loading Loading @@ -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; Loading Loading @@ -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"); } Loading Loading @@ -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) { Loading Loading @@ -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) { Loading Loading @@ -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(); Loading packages/SystemUI/src/com/android/systemui/qs/tiles/dialog/WifiStateWorker.java 0 → 100644 +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; } } } packages/SystemUI/tests/src/com/android/systemui/qs/tiles/dialog/InternetDialogControllerTest.java +12 −9 Original line number Diff line number Diff line Loading @@ -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; Loading Loading @@ -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, Loading @@ -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); Loading Loading @@ -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")); Loading @@ -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")); Loading @@ -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)) Loading @@ -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")); Loading @@ -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), Loading @@ -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(); Loading @@ -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(); Loading @@ -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); Loading packages/SystemUI/tests/src/com/android/systemui/qs/tiles/dialog/InternetDialogTest.java +29 −13 Original line number Diff line number Diff line Loading @@ -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; Loading Loading @@ -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; Loading Loading @@ -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); Loading @@ -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) Loading Loading @@ -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); Loading @@ -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); Loading Loading @@ -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); Loading @@ -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); Loading @@ -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); Loading @@ -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(); Loading @@ -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(); Loading @@ -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(); Loading @@ -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 Loading
packages/SystemUI/src/com/android/systemui/qs/tiles/dialog/InternetDialog.java +8 −11 Original line number Diff line number Diff line Loading @@ -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; Loading Loading @@ -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; Loading Loading @@ -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); Loading Loading @@ -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); Loading Loading @@ -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 -> { Loading @@ -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); Loading Loading @@ -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 Loading Loading @@ -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; Loading
packages/SystemUI/src/com/android/systemui/qs/tiles/dialog/InternetDialogController.java +28 −2 Original line number Diff line number Diff line Loading @@ -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; Loading Loading @@ -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; Loading Loading @@ -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"); } Loading Loading @@ -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) { Loading Loading @@ -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) { Loading Loading @@ -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(); Loading
packages/SystemUI/src/com/android/systemui/qs/tiles/dialog/WifiStateWorker.java 0 → 100644 +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; } } }
packages/SystemUI/tests/src/com/android/systemui/qs/tiles/dialog/InternetDialogControllerTest.java +12 −9 Original line number Diff line number Diff line Loading @@ -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; Loading Loading @@ -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, Loading @@ -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); Loading Loading @@ -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")); Loading @@ -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")); Loading @@ -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)) Loading @@ -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")); Loading @@ -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), Loading @@ -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(); Loading @@ -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(); Loading @@ -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); Loading
packages/SystemUI/tests/src/com/android/systemui/qs/tiles/dialog/InternetDialogTest.java +29 −13 Original line number Diff line number Diff line Loading @@ -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; Loading Loading @@ -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; Loading Loading @@ -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); Loading @@ -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) Loading Loading @@ -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); Loading @@ -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); Loading Loading @@ -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); Loading @@ -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); Loading @@ -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); Loading @@ -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(); Loading @@ -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(); Loading @@ -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(); Loading @@ -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