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

Commit d4fe14d3 authored by Treehugger Robot's avatar Treehugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Fixing onDataSetChanged not getting called when the service factory was...

Merge "Fixing onDataSetChanged not getting called when the service factory was being created" into main
parents 1039cff4 f3cba63c
Loading
Loading
Loading
Loading
+24 −29
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@ package android.widget;

import android.app.Service;
import android.content.Intent;
import android.content.Intent.FilterComparison;
import android.os.IBinder;
import android.os.Parcel;

@@ -39,8 +40,8 @@ public abstract class RemoteViewsService extends Service {
    // reclaimed), the references to the factories that are created need to be stored and used when
    // the service is restarted (in response to user input for example).  When the process is
    // destroyed, so is this static cache of RemoteViewsFactories.
    private static final HashMap<Intent.FilterComparison, RemoteViewsFactory> sRemoteViewFactories =
            new HashMap<Intent.FilterComparison, RemoteViewsFactory>();
    private static final HashMap<FilterComparison, RemoteViewsFactoryAdapter> sFactoriesCache =
            new HashMap<>();
    private static final Object sLock = new Object();

    /**
@@ -178,9 +179,9 @@ public abstract class RemoteViewsService extends Service {
     * public RemoteViewsFactory interface.
     */
    private static class RemoteViewsFactoryAdapter extends IRemoteViewsFactory.Stub {
        public RemoteViewsFactoryAdapter(RemoteViewsFactory factory, boolean isCreated) {
        RemoteViewsFactoryAdapter(RemoteViewsFactory factory) {
            mFactory = factory;
            mIsCreated = isCreated;
            mIsDataUpdatePending = true;
        }
        public synchronized boolean isCreated() {
            return mIsCreated;
@@ -260,18 +261,17 @@ public abstract class RemoteViewsService extends Service {
            return hasStableIds;
        }
        public void onDestroy(Intent intent) {
            RemoteViewsFactoryAdapter adapter;
            synchronized (sLock) {
                Intent.FilterComparison fc = new Intent.FilterComparison(intent);
                if (RemoteViewsService.sRemoteViewFactories.containsKey(fc)) {
                    RemoteViewsFactory factory = RemoteViewsService.sRemoteViewFactories.get(fc);
                adapter = sFactoriesCache.remove(new FilterComparison(intent));
            }
            if (adapter != null) {
                try {
                        factory.onDestroy();
                    adapter.mFactory.onDestroy();
                } catch (Exception ex) {
                    Thread t = Thread.currentThread();
                    Thread.getDefaultUncaughtExceptionHandler().uncaughtException(t, ex);
                }
                    RemoteViewsService.sRemoteViewFactories.remove(fc);
                }
            }
        }

@@ -281,9 +281,9 @@ public abstract class RemoteViewsService extends Service {
            RemoteViews.RemoteCollectionItems items = new RemoteViews.RemoteCollectionItems
                    .Builder().build();
            try {
                if (mIsCreated || invalidateData) {
                if (mIsDataUpdatePending || invalidateData) {
                    mFactory.onDataSetChanged();
                    mIsCreated = false;
                    mIsDataUpdatePending = false;
                }
                items = mFactory.getRemoteCollectionItems(capSize, capBitmapSize);
            } catch (Exception ex) {
@@ -293,26 +293,21 @@ public abstract class RemoteViewsService extends Service {
            return items;
        }

        private RemoteViewsFactory mFactory;
        private final RemoteViewsFactory mFactory;
        private boolean mIsCreated;
        private boolean mIsDataUpdatePending;
    }

    @Override
    public IBinder onBind(Intent intent) {
        synchronized (sLock) {
            Intent.FilterComparison fc = new Intent.FilterComparison(intent);
            RemoteViewsFactory factory = null;
            boolean isCreated = false;
            if (!sRemoteViewFactories.containsKey(fc)) {
                factory = onGetViewFactory(intent);
                sRemoteViewFactories.put(fc, factory);
                factory.onCreate();
                isCreated = false;
            } else {
                factory = sRemoteViewFactories.get(fc);
                isCreated = true;
            FilterComparison fc = new FilterComparison(intent);
            RemoteViewsFactoryAdapter factory = sFactoriesCache.get(fc);
            if (factory == null) {
                factory = new RemoteViewsFactoryAdapter(onGetViewFactory(intent));
                sFactoriesCache.put(fc, factory);
            }
            return new RemoteViewsFactoryAdapter(factory, isCreated);
            return factory;
        }
    }