Fix the janky transition of contextual cards.

When contextual cards are being laid out, there are two separate layout
transitions, which brings users the feeling of slowness.

In the current design, we bind slices in the adapter's
onBindViewHolder(), where slice's binding is acutally done in the
background thread and it's time consuming. So before getting the
callback from the slice framework to have actual contents, the view is
empty but the viewholder is already created. So the RecyclerView would
treat it as completed and starts to lay them out. This introduces the
first time transition. Once we get the actual slice content, the view
will be refreshed and laid out, which is the second time transition.

To tackle this, this CL caches slices that are created at pre-check
time, and use them to render before getting updated slices to fill up
the gap.

Fixes: 156372414
Test: robotest and launch settings to see the transition being smooth.
Change-Id: Ic0a27ff36f1824de499b75ec73b2635de9cbe6b5
This commit is contained in:
Yi-Ling Chuang
2020-05-29 18:10:03 +08:00
parent fb6711ae92
commit bed0f23940
5 changed files with 80 additions and 5 deletions

View File

@@ -23,6 +23,7 @@ import android.net.Uri;
import android.text.TextUtils;
import androidx.annotation.LayoutRes;
import androidx.slice.Slice;
import com.android.settings.homepage.contextualcards.slices.SliceContextualCardRenderer;
@@ -66,6 +67,7 @@ public class ContextualCard {
private final int mViewType;
private final boolean mIsPendingDismiss;
private final boolean mHasInlineAction;
private final Slice mSlice;
public String getName() {
return mName;
@@ -127,6 +129,10 @@ public class ContextualCard {
return mHasInlineAction;
}
public Slice getSlice() {
return mSlice;
}
public Builder mutate() {
return mBuilder;
}
@@ -147,6 +153,7 @@ public class ContextualCard {
mViewType = builder.mViewType;
mIsPendingDismiss = builder.mIsPendingDismiss;
mHasInlineAction = builder.mHasInlineAction;
mSlice = builder.mSlice;
}
ContextualCard(Cursor c) {
@@ -179,6 +186,8 @@ public class ContextualCard {
mBuilder.setIsPendingDismiss(mIsPendingDismiss);
mHasInlineAction = false;
mBuilder.setHasInlineAction(mHasInlineAction);
mSlice = null;
mBuilder.setSlice(mSlice);
}
@Override
@@ -225,6 +234,7 @@ public class ContextualCard {
private int mViewType;
private boolean mIsPendingDismiss;
private boolean mHasInlineAction;
private Slice mSlice;
public Builder setName(String name) {
mName = name;
@@ -296,6 +306,14 @@ public class ContextualCard {
return this;
}
/**
* Cache a slice created at pre-check time for later usage.
*/
public Builder setSlice(Slice slice) {
mSlice = slice;
return this;
}
public ContextualCard build() {
return new ContextualCard(this);
}

View File

@@ -96,14 +96,17 @@ public class EligibleCardChecker implements Callable<ContextualCard> {
final Slice slice = bindSlice(uri);
if (isSliceToggleable(slice)) {
mCard = card.mutate().setHasInlineAction(true).build();
}
if (slice == null || slice.hasHint(HINT_ERROR)) {
Log.w(TAG, "Failed to bind slice, not eligible for display " + uri);
return false;
}
mCard = card.mutate().setSlice(slice).build();
if (isSliceToggleable(slice)) {
mCard = card.mutate().setHasInlineAction(true).build();
}
return true;
}

View File

@@ -47,6 +47,7 @@ import com.android.settings.homepage.contextualcards.CardContentProvider;
import com.android.settings.homepage.contextualcards.ContextualCard;
import com.android.settings.homepage.contextualcards.ContextualCardRenderer;
import com.android.settings.homepage.contextualcards.ControllerRendererPool;
import com.android.settings.homepage.contextualcards.slices.SliceFullCardRendererHelper.SliceViewHolder;
import com.android.settingslib.utils.ThreadUtils;
import java.util.Map;
@@ -102,6 +103,11 @@ public class SliceContextualCardRenderer implements ContextualCardRenderer, Life
return;
}
// Show cached slice first before slice binding completed to avoid jank.
if (holder.getItemViewType() != VIEW_TYPE_HALF_WIDTH) {
((SliceViewHolder) holder).sliceView.setSlice(card.getSlice());
}
LiveData<Slice> sliceLiveData = mSliceLiveDataMap.get(uri);
if (sliceLiveData == null) {