From 4753adfb5275705a11a97ea7549dec12c9eda7d0 Mon Sep 17 00:00:00 2001 From: Jason Chiu Date: Tue, 8 Sep 2020 12:17:03 +0800 Subject: [PATCH] [DO NOT MERGE] Fix the blank space on contextual card loading timeout Root cause: Sometimes loading contextual cards exceeds 1 second and a timeout expires. In the past, we used the timeout in order not to update homepage UI and to avoid screen scrolling. But we've introduced a mechanism of card space pre-allocation to avoid flickering, so when the timeout expires, the pre-allocated space will be always blank. Solution: Display a card on timeout if the one-card space is pre-allocated. Fixes: 165886791 Test: robotest Change-Id: I79b29c5fd6d9c4fe6b53dd4f5eab4cd3a606d76d --- .../ContextualCardManager.java | 13 +++++++-- .../ContextualCardManagerTest.java | 29 +++++++++++++++++-- 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/src/com/android/settings/homepage/contextualcards/ContextualCardManager.java b/src/com/android/settings/homepage/contextualcards/ContextualCardManager.java index 00755296d0a..fc39b59e33e 100644 --- a/src/com/android/settings/homepage/contextualcards/ContextualCardManager.java +++ b/src/com/android/settings/homepage/contextualcards/ContextualCardManager.java @@ -243,7 +243,7 @@ public class ContextualCardManager implements ContextualCardLoader.CardContentLo final MetricsFeatureProvider metricsFeatureProvider = FeatureFactory.getFactory(mContext).getMetricsFeatureProvider(); - //navigate back to the homepage, screen rotate or after card dismissal + // navigate back to the homepage, screen rotate or after card dismissal if (!mIsFirstLaunch) { onContextualCardUpdated(cardsToKeep.stream() .collect(groupingBy(ContextualCard::getCardType))); @@ -266,8 +266,17 @@ public class ContextualCardManager implements ContextualCardLoader.CardContentLo SettingsEnums.ACTION_CONTEXTUAL_CARD_LOAD_TIMEOUT, SettingsEnums.SETTINGS_HOMEPAGE, null /* key */, (int) loadTime /* value */); + + // display a card on timeout if the one-card space is pre-allocated + if (!cards.isEmpty() && ContextualCardLoader.getCardCount(mContext) == 1) { + onContextualCardUpdated(cards.stream() + .collect(groupingBy(ContextualCard::getCardType))); + metricsFeatureProvider.action(mContext, + SettingsEnums.ACTION_CONTEXTUAL_CARD_SHOW, + ContextualCardLogUtils.buildCardListLog(cards)); + } } - //only log homepage display upon a fresh launch + // only log homepage display upon a fresh launch final long totalTime = System.currentTimeMillis() - mStartTime; metricsFeatureProvider.action(mContext, SettingsEnums.ACTION_CONTEXTUAL_HOME_SHOW, (int) totalTime); diff --git a/tests/robotests/src/com/android/settings/homepage/contextualcards/ContextualCardManagerTest.java b/tests/robotests/src/com/android/settings/homepage/contextualcards/ContextualCardManagerTest.java index 69333d7ad9d..96d0c3be4db 100644 --- a/tests/robotests/src/com/android/settings/homepage/contextualcards/ContextualCardManagerTest.java +++ b/tests/robotests/src/com/android/settings/homepage/contextualcards/ContextualCardManagerTest.java @@ -16,6 +16,7 @@ package com.android.settings.homepage.contextualcards; +import static com.android.settings.homepage.contextualcards.ContextualCardLoader.CONTEXTUAL_CARD_COUNT; import static com.android.settings.homepage.contextualcards.ContextualCardManager.KEY_CONTEXTUAL_CARDS; import static com.android.settings.homepage.contextualcards.slices.SliceContextualCardRenderer.VIEW_TYPE_FULL_WIDTH; import static com.android.settings.homepage.contextualcards.slices.SliceContextualCardRenderer.VIEW_TYPE_HALF_WIDTH; @@ -307,7 +308,7 @@ public class ContextualCardManagerTest { } @Test - public void onFinishCardLoading_fastLoad_shouldCallOnContextualCardUpdated() { + public void onFinishCardLoading_fastLoad_shouldUpdateContextualCard() { mManager.mStartTime = System.currentTimeMillis(); final ContextualCardManager manager = spy(mManager); doNothing().when(manager).onContextualCardUpdated(anyMap()); @@ -318,7 +319,7 @@ public class ContextualCardManagerTest { } @Test - public void onFinishCardLoading_slowLoad_shouldSkipOnContextualCardUpdated() { + public void onFinishCardLoading_slowLoadAndNoCard_shouldNotUpdateContextualCard() { mManager.mStartTime = 0; final ContextualCardManager manager = spy(mManager); doNothing().when(manager).onContextualCardUpdated(anyMap()); @@ -328,6 +329,30 @@ public class ContextualCardManagerTest { verify(manager, never()).onContextualCardUpdated(anyMap()); } + @Test + public void onFinishCardLoading_slowLoadAndNotPreAllocateSpace_shouldNotUpdateContextualCard() { + mManager.mStartTime = 0; + Settings.Global.putInt(mContext.getContentResolver(), CONTEXTUAL_CARD_COUNT, 3); + final ContextualCardManager manager = spy(mManager); + doNothing().when(manager).onContextualCardUpdated(anyMap()); + + manager.onFinishCardLoading(Arrays.asList(buildContextualCard(TEST_SLICE_URI))); + + verify(manager, never()).onContextualCardUpdated(anyMap()); + } + + @Test + public void onFinishCardLoading_slowLoadAndPreAllocateSpace_shouldUpdateContextualCard() { + mManager.mStartTime = 0; + Settings.Global.putInt(mContext.getContentResolver(), CONTEXTUAL_CARD_COUNT, 1); + final ContextualCardManager manager = spy(mManager); + doNothing().when(manager).onContextualCardUpdated(anyMap()); + + manager.onFinishCardLoading(Arrays.asList(buildContextualCard(TEST_SLICE_URI))); + + verify(manager).onContextualCardUpdated(anyMap()); + } + @Test public void onFinishCardLoading_newLaunch_twoLoadedCards_shouldShowTwoCards() { mManager.mStartTime = System.currentTimeMillis();