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

Commit c248da29 authored by Yi-Ling Chuang's avatar Yi-Ling Chuang Committed by Automerger Merge Worker
Browse files

[DO NOT MERGE] Pre-allocate height for contextual cards. am: 228bc78b

Original change: https://googleplex-android-review.googlesource.com/c/platform/packages/apps/Settings/+/12343231

Change-Id: I76c77c66be5da635571f876d9b76906f789e8472
parents ec053834 228bc78b
Loading
Loading
Loading
Loading
+1 −0
Original line number Original line Diff line number Diff line
@@ -348,6 +348,7 @@
    <dimen name="contextual_half_card_padding_top">12dp</dimen>
    <dimen name="contextual_half_card_padding_top">12dp</dimen>
    <dimen name="contextual_half_card_padding_bottom">16dp</dimen>
    <dimen name="contextual_half_card_padding_bottom">16dp</dimen>
    <dimen name="contextual_half_card_title_margin_top">12dp</dimen>
    <dimen name="contextual_half_card_title_margin_top">12dp</dimen>
    <dimen name="contextual_card_preallocated_height">0dp</dimen>


    <!-- Homepage dismissal cards size and padding -->
    <!-- Homepage dismissal cards size and padding -->
    <dimen name="contextual_card_dismissal_margin_top">12dp</dimen>
    <dimen name="contextual_card_dismissal_margin_top">12dp</dimen>
+4 −5
Original line number Original line Diff line number Diff line
@@ -50,7 +50,7 @@ import java.util.stream.Collectors;
public class ContextualCardLoader extends AsyncLoaderCompat<List<ContextualCard>> {
public class ContextualCardLoader extends AsyncLoaderCompat<List<ContextualCard>> {


    @VisibleForTesting
    @VisibleForTesting
    static final int DEFAULT_CARD_COUNT = 3;
    static final int DEFAULT_CARD_COUNT = 1;
    @VisibleForTesting
    @VisibleForTesting
    static final String CONTEXTUAL_CARD_COUNT = "contextual_card_count";
    static final String CONTEXTUAL_CARD_COUNT = "contextual_card_count";
    static final int CARD_CONTENT_LOADER_ID = 1;
    static final int CARD_CONTENT_LOADER_ID = 1;
@@ -131,7 +131,7 @@ public class ContextualCardLoader extends AsyncLoaderCompat<List<ContextualCard>
        final List<ContextualCard> visibleCards = new ArrayList<>();
        final List<ContextualCard> visibleCards = new ArrayList<>();
        final List<ContextualCard> hiddenCards = new ArrayList<>();
        final List<ContextualCard> hiddenCards = new ArrayList<>();


        final int maxCardCount = getCardCount();
        final int maxCardCount = getCardCount(mContext);
        eligibleCards.forEach(card -> {
        eligibleCards.forEach(card -> {
            if (card.getCategory() != STICKY_VALUE) {
            if (card.getCategory() != STICKY_VALUE) {
                return;
                return;
@@ -177,11 +177,10 @@ public class ContextualCardLoader extends AsyncLoaderCompat<List<ContextualCard>
        return visibleCards;
        return visibleCards;
    }
    }


    @VisibleForTesting
    static int getCardCount(Context context) {
    int getCardCount() {
        // Return the card count if Settings.Global has KEY_CONTEXTUAL_CARD_COUNT key,
        // Return the card count if Settings.Global has KEY_CONTEXTUAL_CARD_COUNT key,
        // otherwise return the default one.
        // otherwise return the default one.
        return Settings.Global.getInt(mContext.getContentResolver(),
        return Settings.Global.getInt(context.getContentResolver(),
                CONTEXTUAL_CARD_COUNT, DEFAULT_CARD_COUNT);
                CONTEXTUAL_CARD_COUNT, DEFAULT_CARD_COUNT);
    }
    }


+4 −0
Original line number Original line Diff line number Diff line
@@ -122,6 +122,10 @@ public class ContextualCardManager implements ContextualCardLoader.CardContentLo
            Log.w(TAG, "Legacy suggestion contextual card enabled, skipping contextual cards.");
            Log.w(TAG, "Legacy suggestion contextual card enabled, skipping contextual cards.");
            return;
            return;
        }
        }
        if (ContextualCardLoader.getCardCount(mContext) <= 0) {
            Log.w(TAG, "Card count is zero, skipping contextual cards.");
            return;
        }
        mStartTime = System.currentTimeMillis();
        mStartTime = System.currentTimeMillis();
        final CardContentLoaderCallbacks cardContentLoaderCallbacks =
        final CardContentLoaderCallbacks cardContentLoaderCallbacks =
                new CardContentLoaderCallbacks(mContext);
                new CardContentLoaderCallbacks(mContext);
+19 −1
Original line number Original line Diff line number Diff line
@@ -16,7 +16,10 @@


package com.android.settings.homepage.contextualcards;
package com.android.settings.homepage.contextualcards;


import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;

import android.content.Context;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup;
@@ -131,7 +134,22 @@ public class ContextualCardsAdapter extends RecyclerView.Adapter<RecyclerView.Vi
            diffResult.dispatchUpdatesTo(this);
            diffResult.dispatchUpdatesTo(this);
        }
        }


        if (mRecyclerView != null && previouslyEmpty && !nowEmpty) {
        if (mRecyclerView == null) {
            return;
        }

        // When no card gets displayed either because a card's condition no longer meets
        // or when it's dismissed, the height should be rearranged.
        if (mContextualCards.isEmpty()) {
            final ViewGroup.LayoutParams params = mRecyclerView.getLayoutParams();
            if (params.height != WRAP_CONTENT) {
                Log.d(TAG, "mContextualCards is empty. Set the RV to wrap_content");
                params.height = WRAP_CONTENT;
                mRecyclerView.setLayoutParams(params);
            }
        }

        if (previouslyEmpty && !nowEmpty) {
            // Adding items to empty list, should animate.
            // Adding items to empty list, should animate.
            mRecyclerView.scheduleLayoutAnimation();
            mRecyclerView.scheduleLayoutAnimation();
        }
        }
+35 −1
Original line number Original line Diff line number Diff line
@@ -16,6 +16,8 @@


package com.android.settings.homepage.contextualcards;
package com.android.settings.homepage.contextualcards;


import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;

import static com.android.settings.homepage.contextualcards.ContextualCardsAdapter.SPAN_COUNT;
import static com.android.settings.homepage.contextualcards.ContextualCardsAdapter.SPAN_COUNT;


import android.app.settings.SettingsEnums;
import android.app.settings.SettingsEnums;
@@ -34,6 +36,7 @@ import androidx.annotation.VisibleForTesting;
import androidx.loader.app.LoaderManager;
import androidx.loader.app.LoaderManager;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.RecyclerView;


import com.android.settings.R;
import com.android.settings.R;
import com.android.settings.core.InstrumentedFragment;
import com.android.settings.core.InstrumentedFragment;
@@ -105,8 +108,20 @@ public class ContextualCardsFragment extends InstrumentedFragment implements
        final View rootView = inflater.inflate(R.layout.settings_homepage, container, false);
        final View rootView = inflater.inflate(R.layout.settings_homepage, container, false);
        mCardsContainer = rootView.findViewById(R.id.card_container);
        mCardsContainer = rootView.findViewById(R.id.card_container);
        mLayoutManager = new GridLayoutManager(getActivity(), SPAN_COUNT,
        mLayoutManager = new GridLayoutManager(getActivity(), SPAN_COUNT,
                GridLayoutManager.VERTICAL, false /* reverseLayout */);
                GridLayoutManager.VERTICAL, false /* reverseLayout */) {
            @Override
            public void onLayoutCompleted(RecyclerView.State state) {
                super.onLayoutCompleted(state);
                // Once cards finish laying out, make the RV back to wrap content for flexibility.
                final ViewGroup.LayoutParams params = mCardsContainer.getLayoutParams();
                if (params.height != WRAP_CONTENT) {
                    params.height = WRAP_CONTENT;
                    mCardsContainer.setLayoutParams(params);
                }
            }
        };
        mCardsContainer.setLayoutManager(mLayoutManager);
        mCardsContainer.setLayoutManager(mLayoutManager);
        preAllocateHeight(context);
        mContextualCardsAdapter = new ContextualCardsAdapter(context, this /* lifecycleOwner */,
        mContextualCardsAdapter = new ContextualCardsAdapter(context, this /* lifecycleOwner */,
                mContextualCardManager);
                mContextualCardManager);
        mCardsContainer.setItemAnimator(null);
        mCardsContainer.setItemAnimator(null);
@@ -159,6 +174,25 @@ public class ContextualCardsFragment extends InstrumentedFragment implements
        FeatureFactory.getFactory(context).getSlicesFeatureProvider().newUiSession();
        FeatureFactory.getFactory(context).getSlicesFeatureProvider().newUiSession();
    }
    }


    private void preAllocateHeight(Context context) {
        final int cardCount = ContextualCardLoader.getCardCount(context);
        if (cardCount != 1) {
            // only pre-allocate space when card count is one
            Log.d(TAG, "Skip height pre-allocating. card count = " + cardCount);
            return;
        }

        final int preAllocatedHeight = getResources().getDimensionPixelSize(
                R.dimen.contextual_card_preallocated_height);
        if (preAllocatedHeight == 0) {
            return;
        }

        final ViewGroup.LayoutParams params = mCardsContainer.getLayoutParams();
        params.height = preAllocatedHeight;
        mCardsContainer.setLayoutParams(params);
    }

    /**
    /**
     * Receiver for updating UI session when home key or recent app key is pressed.
     * Receiver for updating UI session when home key or recent app key is pressed.
     */
     */
Loading