packages/apps/Settings
修訂 | 60b814ba434f346779635d8fe1a91296856e474f (tree) |
---|---|
時間 | 2017-06-15 11:07:05 |
作者 | Sundeep Ghuman <sghuman@goog...> |
Commiter | Sundeep Ghuman |
Allow WifiSettings to show old APs on app resume.
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
Merged-In: I4f9b2ec855e057e28235b0253ab42c6b4521bebc
@@ -43,7 +43,6 @@ import android.provider.Settings; | ||
43 | 43 | import android.support.annotation.VisibleForTesting; |
44 | 44 | import android.support.v7.preference.Preference; |
45 | 45 | import android.support.v7.preference.PreferenceCategory; |
46 | -import android.support.v7.preference.PreferenceManager; | |
47 | 46 | import android.text.TextUtils; |
48 | 47 | import android.util.Log; |
49 | 48 | import android.view.ContextMenu; |
@@ -321,7 +320,6 @@ public class WifiSettings extends RestrictedSettingsFragment | ||
321 | 320 | |
322 | 321 | if (intent.hasExtra(EXTRA_START_CONNECT_SSID)) { |
323 | 322 | mOpenSsid = intent.getStringExtra(EXTRA_START_CONNECT_SSID); |
324 | - updateAccessPointsDelayed(); | |
325 | 323 | } |
326 | 324 | } |
327 | 325 |
@@ -354,13 +352,28 @@ public class WifiSettings extends RestrictedSettingsFragment | ||
354 | 352 | onWifiStateChanged(mWifiManager.getWifiState()); |
355 | 353 | } |
356 | 354 | |
357 | - private void forceUpdateAPs() { | |
355 | + /** | |
356 | + * Only update the AP list if there are not any APs currently shown. | |
357 | + * | |
358 | + * <p>Thus forceUpdate will only be called during cold start or when toggling between wifi on | |
359 | + * and off. In other use cases, the previous APs will remain until the next update is received | |
360 | + * from {@link WifiTracker}. | |
361 | + */ | |
362 | + private void conditionallyForceUpdateAPs() { | |
363 | + if (mAccessPointsPreferenceCategory.getPreferenceCount() > 0 | |
364 | + && mAccessPointsPreferenceCategory.getPreference(0) instanceof | |
365 | + AccessPointPreference) { | |
366 | + // Make sure we don't update due to callbacks initiated by sticky broadcasts in | |
367 | + // WifiTracker. | |
368 | + Log.d(TAG, "Did not force update APs due to existing APs displayed"); | |
369 | + getView().removeCallbacks(mUpdateAccessPointsRunnable); | |
370 | + return; | |
371 | + } | |
358 | 372 | setProgressBarVisible(true); |
359 | 373 | mWifiTracker.forceUpdate(); |
360 | 374 | if (DEBUG) { |
361 | 375 | Log.d(TAG, "WifiSettings force update APs: " + mWifiTracker.getAccessPoints()); |
362 | 376 | } |
363 | - | |
364 | 377 | getView().removeCallbacks(mUpdateAccessPointsRunnable); |
365 | 378 | updateAccessPointPreferences(); |
366 | 379 | } |
@@ -654,6 +667,7 @@ public class WifiSettings extends RestrictedSettingsFragment | ||
654 | 667 | */ |
655 | 668 | @Override |
656 | 669 | public void onAccessPointsChanged() { |
670 | + Log.d(TAG, "onAccessPointsChanged (WifiTracker) callback initiated"); | |
657 | 671 | updateAccessPointsDelayed(); |
658 | 672 | } |
659 | 673 |
@@ -679,7 +693,7 @@ public class WifiSettings extends RestrictedSettingsFragment | ||
679 | 693 | final int wifiState = mWifiManager.getWifiState(); |
680 | 694 | switch (wifiState) { |
681 | 695 | case WifiManager.WIFI_STATE_ENABLED: |
682 | - forceUpdateAPs(); | |
696 | + conditionallyForceUpdateAPs(); | |
683 | 697 | break; |
684 | 698 | |
685 | 699 | case WifiManager.WIFI_STATE_ENABLING: |
@@ -719,6 +733,9 @@ public class WifiSettings extends RestrictedSettingsFragment | ||
719 | 733 | } |
720 | 734 | // AccessPoints are sorted by the WifiTracker |
721 | 735 | final List<AccessPoint> accessPoints = mWifiTracker.getAccessPoints(); |
736 | + if (DEBUG) { | |
737 | + Log.d(TAG, "updateAccessPoints called for: " + accessPoints); | |
738 | + } | |
722 | 739 | |
723 | 740 | boolean hasAvailableAccessPoints = false; |
724 | 741 | mAccessPointsPreferenceCategory.removePreference(mStatusMessagePreference); |
@@ -1014,6 +1031,7 @@ public class WifiSettings extends RestrictedSettingsFragment | ||
1014 | 1031 | |
1015 | 1032 | @Override |
1016 | 1033 | public void onAccessPointChanged(final AccessPoint accessPoint) { |
1034 | + Log.d(TAG, "onAccessPointChanged (singular) callback initiated"); | |
1017 | 1035 | View view = getView(); |
1018 | 1036 | if (view != null) { |
1019 | 1037 | view.post(new Runnable() { |
@@ -15,6 +15,7 @@ | ||
15 | 15 | */ |
16 | 16 | package com.android.settings.wifi; |
17 | 17 | |
18 | +import static android.support.test.InstrumentationRegistry.getInstrumentation; | |
18 | 19 | import static android.support.test.espresso.Espresso.onView; |
19 | 20 | import static android.support.test.espresso.assertion.ViewAssertions.doesNotExist; |
20 | 21 | import static android.support.test.espresso.assertion.ViewAssertions.matches; |
@@ -22,13 +23,17 @@ import static android.support.test.espresso.matcher.ViewMatchers.Visibility.VISI | ||
22 | 23 | import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed; |
23 | 24 | import static android.support.test.espresso.matcher.ViewMatchers.withEffectiveVisibility; |
24 | 25 | import static android.support.test.espresso.matcher.ViewMatchers.withText; |
26 | + | |
25 | 27 | import static com.google.common.truth.Truth.assertThat; |
28 | + | |
26 | 29 | import static org.hamcrest.Matchers.allOf; |
27 | 30 | import static org.hamcrest.Matchers.not; |
28 | 31 | import static org.hamcrest.Matchers.startsWith; |
32 | +import static org.mockito.Mockito.atMost; | |
29 | 33 | import static org.mockito.Mockito.verify; |
30 | 34 | import static org.mockito.Mockito.when; |
31 | 35 | |
36 | +import android.app.Activity; | |
32 | 37 | import android.app.Fragment; |
33 | 38 | import android.content.Context; |
34 | 39 | import android.content.Intent; |
@@ -41,12 +46,15 @@ import android.net.wifi.WifiSsid; | ||
41 | 46 | import android.support.test.InstrumentationRegistry; |
42 | 47 | import android.support.test.rule.ActivityTestRule; |
43 | 48 | import android.support.test.runner.AndroidJUnit4; |
49 | + | |
44 | 50 | import com.android.settings.Settings.WifiSettingsActivity; |
45 | 51 | import com.android.settingslib.wifi.AccessPoint; |
46 | 52 | import com.android.settingslib.wifi.WifiTracker; |
47 | 53 | import com.android.settingslib.wifi.WifiTracker.WifiListener; |
48 | 54 | import com.android.settingslib.wifi.WifiTrackerFactory; |
55 | + | |
49 | 56 | import com.google.common.collect.Lists; |
57 | + | |
50 | 58 | import org.junit.Before; |
51 | 59 | import org.junit.Rule; |
52 | 60 | import org.junit.Test; |
@@ -202,4 +210,23 @@ public class WifiSettingsUiTest { | ||
202 | 210 | |
203 | 211 | onView(withText(CONNECTED)).check(matches(isDisplayed())); |
204 | 212 | } |
213 | + | |
214 | + @Test | |
215 | + public void resumingAp_shouldNotForceUpdateWhenExistingAPsAreListed() { | |
216 | + setWifiState(WifiManager.WIFI_STATE_ENABLED); | |
217 | + setupConnectedAccessPoint(); | |
218 | + when(mWifiTracker.isConnected()).thenReturn(true); | |
219 | + | |
220 | + launchActivity(); | |
221 | + | |
222 | + onView(withText(CONNECTED)).check(matches(isDisplayed())); | |
223 | + verify(mWifiTracker).forceUpdate(); | |
224 | + | |
225 | + Activity activity = mActivityRule.getActivity(); | |
226 | + activity.finish(); | |
227 | + getInstrumentation().waitForIdleSync(); | |
228 | + | |
229 | + getInstrumentation().callActivityOnStart(activity); | |
230 | + verify(mWifiTracker, atMost(1)).forceUpdate(); | |
231 | + } | |
205 | 232 | } |