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

Commit 417bfba8 authored by Sihua Ma's avatar Sihua Ma Committed by Android (Google) Code Review
Browse files

Merge "Making adapter child views in RemoteCollectionItemsAdapter size-aware" into main

parents bdf96b34 dc464d6e
Loading
Loading
Loading
Loading
+33 −3
Original line number Original line Diff line number Diff line
@@ -155,6 +155,22 @@ public class AppWidgetHostView extends FrameLayout implements AppWidgetHost.AppW
        mInteractionHandler = getHandler(handler);
        mInteractionHandler = getHandler(handler);
    }
    }


    /**
     * @hide
     */
    public static class AdapterChildHostView extends AppWidgetHostView {

        public AdapterChildHostView(Context context) {
            super(context);
        }

        @Override
        public Context getRemoteContextEnsuringCorrectCachedApkPath() {
            // To reduce noise in error messages
            return null;
        }
    }

    /**
    /**
     * Set the AppWidget that will be displayed by this view. This method also adds default padding
     * Set the AppWidget that will be displayed by this view. This method also adds default padding
     * to widgets, as described in {@link #getDefaultPaddingForWidget(Context, ComponentName, Rect)}
     * to widgets, as described in {@link #getDefaultPaddingForWidget(Context, ComponentName, Rect)}
@@ -921,17 +937,31 @@ public class AppWidgetHostView extends FrameLayout implements AppWidgetHost.AppW
        setColorResources(RemoteViews.ColorResources.create(mContext, colorMapping));
        setColorResources(RemoteViews.ColorResources.create(mContext, colorMapping));
    }
    }


    private void setColorResourcesStates(RemoteViews.ColorResources colorResources) {
        mColorResources = colorResources;
        mColorMappingChanged = true;
        mViewMode = VIEW_MODE_NOINIT;
    }

    /** @hide **/
    /** @hide **/
    public void setColorResources(RemoteViews.ColorResources colorResources) {
    public void setColorResources(RemoteViews.ColorResources colorResources) {
        if (colorResources == mColorResources) {
        if (colorResources == mColorResources) {
            return;
            return;
        }
        }
        mColorResources = colorResources;
        setColorResourcesStates(colorResources);
        mColorMappingChanged = true;
        mViewMode = VIEW_MODE_NOINIT;
        reapplyLastRemoteViews();
        reapplyLastRemoteViews();
    }
    }


    /**
     * @hide
     */
    public void setColorResourcesNoReapply(RemoteViews.ColorResources colorResources) {
        if (colorResources == mColorResources) {
            return;
        }
        setColorResourcesStates(colorResources);
    }

    /** Check if, in the current context, the two color mappings are equivalent. */
    /** Check if, in the current context, the two color mappings are equivalent. */
    private boolean isSameColorMapping(SparseIntArray oldColors, SparseIntArray newColors) {
    private boolean isSameColorMapping(SparseIntArray oldColors, SparseIntArray newColors) {
        if (oldColors.size() != newColors.size()) {
        if (oldColors.size() != newColors.size()) {
+9 −36
Original line number Original line Diff line number Diff line
@@ -18,6 +18,7 @@ package android.widget;


import android.annotation.NonNull;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.Nullable;
import android.appwidget.AppWidgetHostView;
import android.util.SparseIntArray;
import android.util.SparseIntArray;
import android.view.View;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup;
@@ -25,8 +26,6 @@ import android.widget.RemoteViews.ColorResources;
import android.widget.RemoteViews.InteractionHandler;
import android.widget.RemoteViews.InteractionHandler;
import android.widget.RemoteViews.RemoteCollectionItems;
import android.widget.RemoteViews.RemoteCollectionItems;


import com.android.internal.R;

import java.util.stream.IntStream;
import java.util.stream.IntStream;


/**
/**
@@ -178,40 +177,14 @@ class RemoteCollectionItemsAdapter extends BaseAdapter {


        RemoteViews item = mItems.getItemView(position);
        RemoteViews item = mItems.getItemView(position);
        item.addFlags(RemoteViews.FLAG_WIDGET_IS_COLLECTION_CHILD);
        item.addFlags(RemoteViews.FLAG_WIDGET_IS_COLLECTION_CHILD);
        View reapplyView = getViewToReapply(convertView, item);

        // Reapply the RemoteViews if we can.
        if (reapplyView != null) {
            try {
                item.reapply(
                        parent.getContext(),
                        reapplyView,
                        mInteractionHandler,
                        null /* size */,
                        mColorResources);
                return reapplyView;
            } catch (RuntimeException e) {
                // We can't reapply for some reason, we'll fallback to an apply and inflate a
                // new view.
            }
        }

        return item.apply(
                parent.getContext(),
                parent,
                mInteractionHandler,
                null /* size */,
                mColorResources);
    }

    /** Returns {@code convertView} if it can be used to reapply {@code item}, or null otherwise. */
    @Nullable
    private static View getViewToReapply(@Nullable View convertView, @NonNull RemoteViews item) {
        if (convertView == null) return null;

        Object layoutIdTag = convertView.getTag(R.id.widget_frame);
        if (!(layoutIdTag instanceof Integer)) return null;


        return item.getLayoutId() == (Integer) layoutIdTag ? convertView : null;
        AppWidgetHostView newView = convertView instanceof AppWidgetHostView.AdapterChildHostView
                widgetChildView
                ? widgetChildView
                : new AppWidgetHostView.AdapterChildHostView(parent.getContext());
        newView.setInteractionHandler(mInteractionHandler);
        newView.setColorResourcesNoReapply(mColorResources);
        newView.updateAppWidget(item);
        return newView;
    }
    }
}
}
+2 −2
Original line number Original line Diff line number Diff line
@@ -6958,13 +6958,13 @@ public class RemoteViews implements Parcelable, Filter {
            View parent = (View) view.getParent();
            View parent = (View) view.getParent();
            // Break the for loop on the first encounter of:
            // Break the for loop on the first encounter of:
            //    1) an AdapterView,
            //    1) an AdapterView,
            //    2) an AppWidgetHostView that is not a RemoteViewsFrameLayout, or
            //    2) an AppWidgetHostView that is not a child of an adapter view, or
            //    3) a null parent.
            //    3) a null parent.
            // 2) and 3) are unexpected and catch the case where a child is not
            // 2) and 3) are unexpected and catch the case where a child is not
            // correctly parented in an AdapterView.
            // correctly parented in an AdapterView.
            while (parent != null && !(parent instanceof AdapterView<?>)
            while (parent != null && !(parent instanceof AdapterView<?>)
                    && !((parent instanceof AppWidgetHostView)
                    && !((parent instanceof AppWidgetHostView)
                    && !(parent instanceof RemoteViewsAdapter.RemoteViewsFrameLayout))) {
                            && !(parent instanceof AppWidgetHostView.AdapterChildHostView))) {
                parent = (View) parent.getParent();
                parent = (View) parent.getParent();
            }
            }


+1 −6
Original line number Original line Diff line number Diff line
@@ -368,7 +368,7 @@ public class RemoteViewsAdapter extends BaseAdapter implements Handler.Callback
     * A FrameLayout which contains a loading view, and manages the re/applying of RemoteViews when
     * A FrameLayout which contains a loading view, and manages the re/applying of RemoteViews when
     * they are loaded.
     * they are loaded.
     */
     */
    static class RemoteViewsFrameLayout extends AppWidgetHostView {
    static class RemoteViewsFrameLayout extends AppWidgetHostView.AdapterChildHostView {
        private final FixedSizeRemoteViewsCache mCache;
        private final FixedSizeRemoteViewsCache mCache;


        public int cacheIndex = -1;
        public int cacheIndex = -1;
@@ -407,11 +407,6 @@ public class RemoteViewsAdapter extends BaseAdapter implements Handler.Callback
            return loadingTextView;
            return loadingTextView;
        }
        }


        @Override
        protected Context getRemoteContextEnsuringCorrectCachedApkPath() {
            return null;
        }

        @Override
        @Override
        protected View getErrorView() {
        protected View getErrorView() {
            // Use the default loading view as the error view.
            // Use the default loading view as the error view.