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

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

Merge "Move getRemoteCollectionItems to RemoteViewsFactory" into main

parents d7acf52e 36af1e4c
Loading
Loading
Loading
Loading
+36 −23
Original line number Diff line number Diff line
@@ -124,6 +124,41 @@ public abstract class RemoteViewsService extends Service {
         * @return True if the same id always refers to the same object.
         */
        public boolean hasStableIds();

        /**
         * @hide
         */
        default RemoteViews.RemoteCollectionItems getRemoteCollectionItems(int capSize) {
            RemoteViews.RemoteCollectionItems items = new RemoteViews.RemoteCollectionItems
                    .Builder().build();
            Parcel capSizeTestParcel = Parcel.obtain();
            capSizeTestParcel.allowSquashing();

            try {
                RemoteViews.RemoteCollectionItems.Builder itemsBuilder =
                        new RemoteViews.RemoteCollectionItems.Builder();
                onDataSetChanged();

                itemsBuilder.setHasStableIds(hasStableIds());
                final int numOfEntries = getCount();

                for (int i = 0; i < numOfEntries; i++) {
                    final long currentItemId = getItemId(i);
                    final RemoteViews currentView = getViewAt(i);
                    currentView.writeToParcel(capSizeTestParcel, 0);
                    if (capSizeTestParcel.dataSize() > capSize) {
                        break;
                    }
                    itemsBuilder.addItem(currentItemId, currentView);
                }

                items = itemsBuilder.build();
            } finally {
                // Recycle the parcel
                capSizeTestParcel.recycle();
            }
            return items;
        }
    }

    /**
@@ -232,33 +267,11 @@ public abstract class RemoteViewsService extends Service {
        public RemoteViews.RemoteCollectionItems getRemoteCollectionItems(int capSize) {
            RemoteViews.RemoteCollectionItems items = new RemoteViews.RemoteCollectionItems
                    .Builder().build();
            Parcel capSizeTestParcel = Parcel.obtain();

            try {
                RemoteViews.RemoteCollectionItems.Builder itemsBuilder =
                        new RemoteViews.RemoteCollectionItems.Builder();
                mFactory.onDataSetChanged();

                itemsBuilder.setHasStableIds(mFactory.hasStableIds());
                final int numOfEntries = mFactory.getCount();

                for (int i = 0; i < numOfEntries; i++) {
                    final long currentItemId = mFactory.getItemId(i);
                    final RemoteViews currentView = mFactory.getViewAt(i);
                    currentView.writeToParcel(capSizeTestParcel, 0);
                    if (capSizeTestParcel.dataSize() > capSize) {
                        break;
                    }
                    itemsBuilder.addItem(currentItemId, currentView);
                }

                items = itemsBuilder.build();
                items = mFactory.getRemoteCollectionItems(capSize);
            } catch (Exception ex) {
                Thread t = Thread.currentThread();
                Thread.getDefaultUncaughtExceptionHandler().uncaughtException(t, ex);
            } finally {
                // Recycle the parcel
                capSizeTestParcel.recycle();
            }
            return items;
        }