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

Commit 4ae59dde authored by Sundeep Ghuman's avatar Sundeep Ghuman
Browse files

Allow WifiSettings to show old APs on app resume.

MANUAL MERGE of ag/2398149 to avoid merge conflicts when trying to
submit to oc-dev.

If we do not have any recent scan results, show the previous APs from
when the app was last paused, by not triggering a force update.

Accompanying changes are also made in WifiTracker to prevent
onAccessPointsChanged callbacks from being invoked until the tracker
receives a new 'SCAN_RESULTS_AVAILABLE' broadcast (ag/2409026).

Bug: b/38212080
Test: runtest --path
packages/apps/Settings/tests/unit/src/com/android/settings/wifi/WifiSettingsUiTest.java
one minute later.

Change-Id: I4f9b2ec855e057e28235b0253ab42c6b4521bebc
parent 6f365765
Loading
Loading
Loading
Loading
+23 −5
Original line number Diff line number Diff line
@@ -43,7 +43,6 @@ import android.provider.Settings;
import android.support.annotation.VisibleForTesting;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceCategory;
import android.support.v7.preference.PreferenceManager;
import android.text.TextUtils;
import android.util.Log;
import android.view.ContextMenu;
@@ -321,7 +320,6 @@ public class WifiSettings extends RestrictedSettingsFragment

        if (intent.hasExtra(EXTRA_START_CONNECT_SSID)) {
            mOpenSsid = intent.getStringExtra(EXTRA_START_CONNECT_SSID);
            updateAccessPointsDelayed();
        }
    }

@@ -354,13 +352,28 @@ public class WifiSettings extends RestrictedSettingsFragment
        onWifiStateChanged(mWifiManager.getWifiState());
    }

    private void forceUpdateAPs() {
    /**
     * Only update the AP list if there are not any APs currently shown.
     *
     * <p>Thus forceUpdate will only be called during cold start or when toggling between wifi on
     * and off. In other use cases, the previous APs will remain until the next update is received
     * from {@link WifiTracker}.
     */
    private void conditionallyForceUpdateAPs() {
        if (mAccessPointsPreferenceCategory.getPreferenceCount() > 0
                && mAccessPointsPreferenceCategory.getPreference(0) instanceof
                        AccessPointPreference) {
            // Make sure we don't update due to callbacks initiated by sticky broadcasts in
            // WifiTracker.
            Log.d(TAG, "Did not force update APs due to existing APs displayed");
            getView().removeCallbacks(mUpdateAccessPointsRunnable);
            return;
        }
        setProgressBarVisible(true);
        mWifiTracker.forceUpdate();
        if (DEBUG) {
            Log.d(TAG, "WifiSettings force update APs: " + mWifiTracker.getAccessPoints());
        }

        getView().removeCallbacks(mUpdateAccessPointsRunnable);
        updateAccessPointPreferences();
    }
@@ -654,6 +667,7 @@ public class WifiSettings extends RestrictedSettingsFragment
     */
    @Override
    public void onAccessPointsChanged() {
        Log.d(TAG, "onAccessPointsChanged (WifiTracker) callback initiated");
        updateAccessPointsDelayed();
    }

@@ -679,7 +693,7 @@ public class WifiSettings extends RestrictedSettingsFragment
        final int wifiState = mWifiManager.getWifiState();
        switch (wifiState) {
            case WifiManager.WIFI_STATE_ENABLED:
                forceUpdateAPs();
                conditionallyForceUpdateAPs();
                break;

            case WifiManager.WIFI_STATE_ENABLING:
@@ -720,6 +734,9 @@ public class WifiSettings extends RestrictedSettingsFragment
        }
        // AccessPoints are sorted by the WifiTracker
        final List<AccessPoint> accessPoints = mWifiTracker.getAccessPoints();
        if (DEBUG) {
            Log.d(TAG, "updateAccessPoints called for: " + accessPoints);
        }

        boolean hasAvailableAccessPoints = false;
        mAccessPointsPreferenceCategory.removePreference(mStatusMessagePreference);
@@ -1015,6 +1032,7 @@ public class WifiSettings extends RestrictedSettingsFragment

    @Override
    public void onAccessPointChanged(final AccessPoint accessPoint) {
        Log.d(TAG, "onAccessPointChanged (singular) callback initiated");
        View view = getView();
        if (view != null) {
            view.post(new Runnable() {
+25 −0
Original line number Diff line number Diff line
@@ -15,6 +15,7 @@
 */
package com.android.settings.wifi;

import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
@@ -45,6 +46,7 @@ import org.mockito.MockitoAnnotations;

import java.util.List;

import static android.support.test.InstrumentationRegistry.getInstrumentation;
import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.assertion.ViewAssertions.doesNotExist;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
@@ -52,10 +54,14 @@ import static android.support.test.espresso.matcher.ViewMatchers.Visibility.VISI
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
import static android.support.test.espresso.matcher.ViewMatchers.withEffectiveVisibility;
import static android.support.test.espresso.matcher.ViewMatchers.withText;

import static com.google.common.truth.Truth.assertThat;

import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.startsWith;

import static org.mockito.Mockito.atMost;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@@ -217,4 +223,23 @@ public class WifiSettingsUiTest {

        onView(withText(CONNECTED)).check(matches(isDisplayed()));
    }

    @Test
    public void resumingAp_shouldNotForceUpdateWhenExistingAPsAreListed() {
        setWifiState(WifiManager.WIFI_STATE_ENABLED);
        setupConnectedAccessPoint();
        when(mWifiTracker.isConnected()).thenReturn(true);

        launchActivity();

        onView(withText(CONNECTED)).check(matches(isDisplayed()));
        verify(mWifiTracker).forceUpdate();

        Activity activity = mActivityRule.getActivity();
        activity.finish();
        getInstrumentation().waitForIdleSync();

        getInstrumentation().callActivityOnStart(activity);
        verify(mWifiTracker, atMost(1)).forceUpdate();
    }
}