• R/O
  • HTTP
  • SSH
  • HTTPS

提交

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

packages/apps/Settings


Commit MetaInfo

修订版228bc78bf9295b51bc1bb6fce46e56aaa1ca49c1 (tree)
时间2020-08-12 22:40:06
作者Yi-Ling Chuang <emilychuang@goog...>
CommiterYi-Ling Chuang

Log Message

[DO NOT MERGE] Pre-allocate height for contextual cards.

To prevent the UI jank causing by the async card loads, we pre-allocate
some space for the card to fill in. After this change, only one card can
be shown at a time.

More details:
- When the card number configuration is set to 0, don't trigger the card
loader.
- The height adjusting logic is as follows.
When Settings is opened, pre-allocate a space first.
After the RV finish laying out, reset the RV to wrap_content. So if the
card has to be expanded(eg. wifi large mode or dismissal view), then it
will adjust the height accordingly. While if a card previously shown
becomes unavailable(dismissed or conditions not meet), we also reset the
RV so the space can be gone.

Bug: 163288869
Test: robotest
Change-Id: I0dcb2dae8f0533e562ad06f664b7ae7a9afecd21

更改概述

差异

--- a/res/values/dimens.xml
+++ b/res/values/dimens.xml
@@ -348,6 +348,7 @@
348348 <dimen name="contextual_half_card_padding_top">12dp</dimen>
349349 <dimen name="contextual_half_card_padding_bottom">16dp</dimen>
350350 <dimen name="contextual_half_card_title_margin_top">12dp</dimen>
351+ <dimen name="contextual_card_preallocated_height">0dp</dimen>
351352
352353 <!-- Homepage dismissal cards size and padding -->
353354 <dimen name="contextual_card_dismissal_margin_top">12dp</dimen>
--- a/src/com/android/settings/homepage/contextualcards/ContextualCardLoader.java
+++ b/src/com/android/settings/homepage/contextualcards/ContextualCardLoader.java
@@ -50,7 +50,7 @@ import java.util.stream.Collectors;
5050 public class ContextualCardLoader extends AsyncLoaderCompat<List<ContextualCard>> {
5151
5252 @VisibleForTesting
53- static final int DEFAULT_CARD_COUNT = 3;
53+ static final int DEFAULT_CARD_COUNT = 1;
5454 @VisibleForTesting
5555 static final String CONTEXTUAL_CARD_COUNT = "contextual_card_count";
5656 static final int CARD_CONTENT_LOADER_ID = 1;
@@ -131,7 +131,7 @@ public class ContextualCardLoader extends AsyncLoaderCompat<List<ContextualCard>
131131 final List<ContextualCard> visibleCards = new ArrayList<>();
132132 final List<ContextualCard> hiddenCards = new ArrayList<>();
133133
134- final int maxCardCount = getCardCount();
134+ final int maxCardCount = getCardCount(mContext);
135135 eligibleCards.forEach(card -> {
136136 if (card.getCategory() != STICKY_VALUE) {
137137 return;
@@ -167,11 +167,10 @@ public class ContextualCardLoader extends AsyncLoaderCompat<List<ContextualCard>
167167 return visibleCards;
168168 }
169169
170- @VisibleForTesting
171- int getCardCount() {
170+ static int getCardCount(Context context) {
172171 // Return the card count if Settings.Global has KEY_CONTEXTUAL_CARD_COUNT key,
173172 // otherwise return the default one.
174- return Settings.Global.getInt(mContext.getContentResolver(),
173+ return Settings.Global.getInt(context.getContentResolver(),
175174 CONTEXTUAL_CARD_COUNT, DEFAULT_CARD_COUNT);
176175 }
177176
--- a/src/com/android/settings/homepage/contextualcards/ContextualCardManager.java
+++ b/src/com/android/settings/homepage/contextualcards/ContextualCardManager.java
@@ -122,6 +122,10 @@ public class ContextualCardManager implements ContextualCardLoader.CardContentLo
122122 Log.w(TAG, "Legacy suggestion contextual card enabled, skipping contextual cards.");
123123 return;
124124 }
125+ if (ContextualCardLoader.getCardCount(mContext) <= 0) {
126+ Log.w(TAG, "Card count is zero, skipping contextual cards.");
127+ return;
128+ }
125129 mStartTime = System.currentTimeMillis();
126130 final CardContentLoaderCallbacks cardContentLoaderCallbacks =
127131 new CardContentLoaderCallbacks(mContext);
--- a/src/com/android/settings/homepage/contextualcards/ContextualCardsAdapter.java
+++ b/src/com/android/settings/homepage/contextualcards/ContextualCardsAdapter.java
@@ -16,7 +16,10 @@
1616
1717 package com.android.settings.homepage.contextualcards;
1818
19+import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;
20+
1921 import android.content.Context;
22+import android.util.Log;
2023 import android.view.LayoutInflater;
2124 import android.view.View;
2225 import android.view.ViewGroup;
@@ -131,7 +134,22 @@ public class ContextualCardsAdapter extends RecyclerView.Adapter<RecyclerView.Vi
131134 diffResult.dispatchUpdatesTo(this);
132135 }
133136
134- if (mRecyclerView != null && previouslyEmpty && !nowEmpty) {
137+ if (mRecyclerView == null) {
138+ return;
139+ }
140+
141+ // When no card gets displayed either because a card's condition no longer meets
142+ // or when it's dismissed, the height should be rearranged.
143+ if (mContextualCards.isEmpty()) {
144+ final ViewGroup.LayoutParams params = mRecyclerView.getLayoutParams();
145+ if (params.height != WRAP_CONTENT) {
146+ Log.d(TAG, "mContextualCards is empty. Set the RV to wrap_content");
147+ params.height = WRAP_CONTENT;
148+ mRecyclerView.setLayoutParams(params);
149+ }
150+ }
151+
152+ if (previouslyEmpty && !nowEmpty) {
135153 // Adding items to empty list, should animate.
136154 mRecyclerView.scheduleLayoutAnimation();
137155 }
--- a/src/com/android/settings/homepage/contextualcards/ContextualCardsFragment.java
+++ b/src/com/android/settings/homepage/contextualcards/ContextualCardsFragment.java
@@ -16,6 +16,8 @@
1616
1717 package com.android.settings.homepage.contextualcards;
1818
19+import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;
20+
1921 import static com.android.settings.homepage.contextualcards.ContextualCardsAdapter.SPAN_COUNT;
2022
2123 import android.app.settings.SettingsEnums;
@@ -34,6 +36,7 @@ import androidx.annotation.VisibleForTesting;
3436 import androidx.loader.app.LoaderManager;
3537 import androidx.recyclerview.widget.GridLayoutManager;
3638 import androidx.recyclerview.widget.ItemTouchHelper;
39+import androidx.recyclerview.widget.RecyclerView;
3740
3841 import com.android.settings.R;
3942 import com.android.settings.core.InstrumentedFragment;
@@ -105,8 +108,20 @@ public class ContextualCardsFragment extends InstrumentedFragment implements
105108 final View rootView = inflater.inflate(R.layout.settings_homepage, container, false);
106109 mCardsContainer = rootView.findViewById(R.id.card_container);
107110 mLayoutManager = new GridLayoutManager(getActivity(), SPAN_COUNT,
108- GridLayoutManager.VERTICAL, false /* reverseLayout */);
111+ GridLayoutManager.VERTICAL, false /* reverseLayout */) {
112+ @Override
113+ public void onLayoutCompleted(RecyclerView.State state) {
114+ super.onLayoutCompleted(state);
115+ // Once cards finish laying out, make the RV back to wrap content for flexibility.
116+ final ViewGroup.LayoutParams params = mCardsContainer.getLayoutParams();
117+ if (params.height != WRAP_CONTENT) {
118+ params.height = WRAP_CONTENT;
119+ mCardsContainer.setLayoutParams(params);
120+ }
121+ }
122+ };
109123 mCardsContainer.setLayoutManager(mLayoutManager);
124+ preAllocateHeight(context);
110125 mContextualCardsAdapter = new ContextualCardsAdapter(context, this /* lifecycleOwner */,
111126 mContextualCardManager);
112127 mCardsContainer.setItemAnimator(null);
@@ -159,6 +174,25 @@ public class ContextualCardsFragment extends InstrumentedFragment implements
159174 FeatureFactory.getFactory(context).getSlicesFeatureProvider().newUiSession();
160175 }
161176
177+ private void preAllocateHeight(Context context) {
178+ final int cardCount = ContextualCardLoader.getCardCount(context);
179+ if (cardCount != 1) {
180+ // only pre-allocate space when card count is one
181+ Log.d(TAG, "Skip height pre-allocating. card count = " + cardCount);
182+ return;
183+ }
184+
185+ final int preAllocatedHeight = getResources().getDimensionPixelSize(
186+ R.dimen.contextual_card_preallocated_height);
187+ if (preAllocatedHeight == 0) {
188+ return;
189+ }
190+
191+ final ViewGroup.LayoutParams params = mCardsContainer.getLayoutParams();
192+ params.height = preAllocatedHeight;
193+ mCardsContainer.setLayoutParams(params);
194+ }
195+
162196 /**
163197 * Receiver for updating UI session when home key or recent app key is pressed.
164198 */
--- a/tests/robotests/src/com/android/settings/homepage/contextualcards/ContextualCardLoaderTest.java
+++ b/tests/robotests/src/com/android/settings/homepage/contextualcards/ContextualCardLoaderTest.java
@@ -71,25 +71,25 @@ public class ContextualCardLoaderTest {
7171 }
7272
7373 @Test
74- public void getDisplayableCards_twoEligibleCards_shouldShowAll() {
74+ public void getDisplayableCards_twoEligibleCards_notExceedDefaultCardCount() {
7575 final List<ContextualCard> cards = getContextualCardList().stream().limit(2)
7676 .collect(Collectors.toList());
7777 doReturn(cards).when(mContextualCardLoader).filterEligibleCards(anyList());
7878
7979 final List<ContextualCard> result = mContextualCardLoader.getDisplayableCards(cards);
8080
81- assertThat(result).hasSize(cards.size());
81+ assertThat(result).hasSize(Math.min(cards.size(), DEFAULT_CARD_COUNT));
8282 }
8383
8484 @Test
85- public void getDisplayableCards_fourEligibleCards_shouldShowDefaultCardCount() {
85+ public void getDisplayableCards_fourEligibleCards_notExceedDefaultCardCount() {
8686 final List<ContextualCard> cards = getContextualCardList().stream().limit(4)
8787 .collect(Collectors.toList());
8888 doReturn(cards).when(mContextualCardLoader).filterEligibleCards(anyList());
8989
9090 final List<ContextualCard> result = mContextualCardLoader.getDisplayableCards(cards);
9191
92- assertThat(result).hasSize(DEFAULT_CARD_COUNT);
92+ assertThat(result).hasSize(Math.min(cards.size(), DEFAULT_CARD_COUNT));
9393 }
9494
9595 @Test
@@ -139,7 +139,7 @@ public class ContextualCardLoaderTest {
139139
140140 @Test
141141 public void getCardCount_noConfiguredCardCount_returnDefaultCardCount() {
142- assertThat(mContextualCardLoader.getCardCount()).isEqualTo(DEFAULT_CARD_COUNT);
142+ assertThat(mContextualCardLoader.getCardCount(mContext)).isEqualTo(DEFAULT_CARD_COUNT);
143143 }
144144
145145 @Test
@@ -148,7 +148,7 @@ public class ContextualCardLoaderTest {
148148 Settings.Global.putLong(mContext.getContentResolver(),
149149 ContextualCardLoader.CONTEXTUAL_CARD_COUNT, configCount);
150150
151- assertThat(mContextualCardLoader.getCardCount()).isEqualTo(configCount);
151+ assertThat(mContextualCardLoader.getCardCount(mContext)).isEqualTo(configCount);
152152 }
153153
154154 private List<ContextualCard> getContextualCardList() {