Merge "Merge "Revert "Making adapter child views in RemoteCollectionItemsAdapter size-aware"" into android14-tests-dev am: 679d9285b9" into main
diff --git a/core/java/android/appwidget/AppWidgetHostView.java b/core/java/android/appwidget/AppWidgetHostView.java
index ec181da..27f6a26 100644
--- a/core/java/android/appwidget/AppWidgetHostView.java
+++ b/core/java/android/appwidget/AppWidgetHostView.java
@@ -156,22 +156,6 @@
     }
 
     /**
-     * @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
      * to widgets, as described in {@link #getDefaultPaddingForWidget(Context, ComponentName, Rect)}
      * and can be overridden in order to add custom padding.
@@ -937,31 +921,17 @@
         setColorResources(RemoteViews.ColorResources.create(mContext, colorMapping));
     }
 
-    private void setColorResourcesStates(RemoteViews.ColorResources colorResources) {
-        mColorResources = colorResources;
-        mColorMappingChanged = true;
-        mViewMode = VIEW_MODE_NOINIT;
-    }
-
     /** @hide **/
     public void setColorResources(RemoteViews.ColorResources colorResources) {
         if (colorResources == mColorResources) {
             return;
         }
-        setColorResourcesStates(colorResources);
+        mColorResources = colorResources;
+        mColorMappingChanged = true;
+        mViewMode = VIEW_MODE_NOINIT;
         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. */
     private boolean isSameColorMapping(SparseIntArray oldColors, SparseIntArray newColors) {
         if (oldColors.size() != newColors.size()) {
diff --git a/core/java/android/widget/RemoteCollectionItemsAdapter.java b/core/java/android/widget/RemoteCollectionItemsAdapter.java
index 9b396ae..d843308 100644
--- a/core/java/android/widget/RemoteCollectionItemsAdapter.java
+++ b/core/java/android/widget/RemoteCollectionItemsAdapter.java
@@ -18,7 +18,6 @@
 
 import android.annotation.NonNull;
 import android.annotation.Nullable;
-import android.appwidget.AppWidgetHostView;
 import android.util.SparseIntArray;
 import android.view.View;
 import android.view.ViewGroup;
@@ -26,6 +25,8 @@
 import android.widget.RemoteViews.InteractionHandler;
 import android.widget.RemoteViews.RemoteCollectionItems;
 
+import com.android.internal.R;
+
 import java.util.stream.IntStream;
 
 /**
@@ -177,14 +178,40 @@
 
         RemoteViews item = mItems.getItemView(position);
         item.addFlags(RemoteViews.FLAG_WIDGET_IS_COLLECTION_CHILD);
+        View reapplyView = getViewToReapply(convertView, item);
 
-        AppWidgetHostView newView = convertView instanceof AppWidgetHostView.AdapterChildHostView
-                widgetChildView
-                ? widgetChildView
-                : new AppWidgetHostView.AdapterChildHostView(parent.getContext());
-        newView.setInteractionHandler(mInteractionHandler);
-        newView.setColorResourcesNoReapply(mColorResources);
-        newView.updateAppWidget(item);
-        return newView;
+        // 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;
     }
 }
diff --git a/core/java/android/widget/RemoteViews.java b/core/java/android/widget/RemoteViews.java
index eb3f96e..a740b65 100644
--- a/core/java/android/widget/RemoteViews.java
+++ b/core/java/android/widget/RemoteViews.java
@@ -6913,13 +6913,13 @@
             View parent = (View) view.getParent();
             // Break the for loop on the first encounter of:
             //    1) an AdapterView,
-            //    2) an AppWidgetHostView that is not a child of an adapter view, or
+            //    2) an AppWidgetHostView that is not a RemoteViewsFrameLayout, or
             //    3) a null parent.
             // 2) and 3) are unexpected and catch the case where a child is not
             // correctly parented in an AdapterView.
             while (parent != null && !(parent instanceof AdapterView<?>)
                     && !((parent instanceof AppWidgetHostView)
-                            && !(parent instanceof AppWidgetHostView.AdapterChildHostView))) {
+                    && !(parent instanceof RemoteViewsAdapter.RemoteViewsFrameLayout))) {
                 parent = (View) parent.getParent();
             }
 
diff --git a/core/java/android/widget/RemoteViewsAdapter.java b/core/java/android/widget/RemoteViewsAdapter.java
index 2f28a87..61a7599 100644
--- a/core/java/android/widget/RemoteViewsAdapter.java
+++ b/core/java/android/widget/RemoteViewsAdapter.java
@@ -368,7 +368,7 @@
      * A FrameLayout which contains a loading view, and manages the re/applying of RemoteViews when
      * they are loaded.
      */
-    static class RemoteViewsFrameLayout extends AppWidgetHostView.AdapterChildHostView {
+    static class RemoteViewsFrameLayout extends AppWidgetHostView {
         private final FixedSizeRemoteViewsCache mCache;
 
         public int cacheIndex = -1;
@@ -408,6 +408,11 @@
         }
 
         @Override
+        protected Context getRemoteContextEnsuringCorrectCachedApkPath() {
+            return null;
+        }
+
+        @Override
         protected View getErrorView() {
             // Use the default loading view as the error view.
             return getDefaultView();