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

Commit f3cba63c authored by Sunny Goyal's avatar Sunny Goyal
Browse files

Fixing onDataSetChanged not getting called when the service factory was being created

The service was caching the serviceFactory and not the Wrapper around
the factory. So any internal state including mCreated was not cached.
Also mCreated was initialized as false for the first time and true for
cache-lookups, where as it should have been the other way round. This
caused the first call to the service not return correct data, if the
app was not doing any caching of its own.

Bug: 439638559
Flag: android.appwidget.flags.remote_adapter_conversion
Test: Verified various legacy list based widgets
Change-Id: Ib1b0d1530afe9fb3be35632363bb521d44fef595
parent 6de49d40
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;
        }
    }