RemoteViewsAdapter.java revision 6364f2bbe5254b4274f3feffc48f4259eacc205e
1/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.widget;
18
19import java.lang.ref.WeakReference;
20import java.util.HashMap;
21import java.util.HashSet;
22import java.util.LinkedList;
23import java.util.Map;
24
25import android.content.ComponentName;
26import android.content.Context;
27import android.content.Intent;
28import android.content.ServiceConnection;
29import android.graphics.Color;
30import android.os.Handler;
31import android.os.HandlerThread;
32import android.os.IBinder;
33import android.os.Looper;
34import android.util.Log;
35import android.view.Gravity;
36import android.view.View;
37import android.view.View.MeasureSpec;
38import android.view.ViewGroup;
39
40import com.android.internal.widget.IRemoteViewsFactory;
41
42/**
43 * An adapter to a RemoteViewsService which fetches and caches RemoteViews
44 * to be later inflated as child views.
45 */
46/** @hide */
47public class RemoteViewsAdapter extends BaseAdapter {
48    private static final String TAG = "RemoteViewsAdapter";
49
50    private Context mContext;
51    private Intent mIntent;
52    private RemoteViewsAdapterServiceConnection mServiceConnection;
53    private WeakReference<RemoteAdapterConnectionCallback> mCallback;
54    private FixedSizeRemoteViewsCache mCache;
55
56    // The set of requested views that are to be notified when the associated RemoteViews are
57    // loaded.
58    private RemoteViewsFrameLayoutRefSet mRequestedViews;
59
60    private HandlerThread mWorkerThread;
61    // items may be interrupted within the normally processed queues
62    private Handler mWorkerQueue;
63    private Handler mMainQueue;
64
65    /**
66     * An interface for the RemoteAdapter to notify other classes when adapters
67     * are actually connected to/disconnected from their actual services.
68     */
69    public interface RemoteAdapterConnectionCallback {
70        public void onRemoteAdapterConnected();
71
72        public void onRemoteAdapterDisconnected();
73    }
74
75    /**
76     * The service connection that gets populated when the RemoteViewsService is
77     * bound.  This must be a static inner class to ensure that no references to the outer
78     * RemoteViewsAdapter instance is retained (this would prevent the RemoteViewsAdapter from being
79     * garbage collected, and would cause us to leak activities due to the caching mechanism for
80     * FrameLayouts in the adapter).
81     */
82    private static class RemoteViewsAdapterServiceConnection implements ServiceConnection {
83        private boolean mConnected;
84        private WeakReference<RemoteViewsAdapter> mAdapter;
85        private IRemoteViewsFactory mRemoteViewsFactory;
86
87        public RemoteViewsAdapterServiceConnection(RemoteViewsAdapter adapter) {
88            mAdapter = new WeakReference<RemoteViewsAdapter>(adapter);
89        }
90
91        public void onServiceConnected(ComponentName name,
92                IBinder service) {
93            mRemoteViewsFactory = IRemoteViewsFactory.Stub.asInterface(service);
94            mConnected = true;
95
96            // Queue up work that we need to do for the callback to run
97            final RemoteViewsAdapter adapter = mAdapter.get();
98            if (adapter == null) return;
99            adapter.mWorkerQueue.post(new Runnable() {
100                @Override
101                public void run() {
102                    // Call back to the service to notify that the data set changed
103                    if (adapter.mServiceConnection.isConnected()) {
104                        IRemoteViewsFactory factory =
105                            adapter.mServiceConnection.getRemoteViewsFactory();
106                        try {
107                            // call back to the factory
108                            factory.onDataSetChanged();
109                        } catch (Exception e) {
110                            Log.e(TAG, "Error notifying factory of data set changed in " +
111                                        "onServiceConnected(): " + e.getMessage());
112                            e.printStackTrace();
113
114                            // Return early to prevent anything further from being notified
115                            // (effectively nothing has changed)
116                            return;
117                        }
118
119                        // Request meta data so that we have up to date data when calling back to
120                        // the remote adapter callback
121                        adapter.updateMetaData();
122
123                        // Post a runnable to call back to the view to notify it that we have
124                        // connected
125                        adapter.mMainQueue.post(new Runnable() {
126                            @Override
127                            public void run() {
128                                final RemoteAdapterConnectionCallback callback =
129                                    adapter.mCallback.get();
130                                if (callback != null) {
131                                    callback.onRemoteAdapterConnected();
132                                }
133                            }
134                        });
135                    }
136                }
137            });
138        }
139
140        public void onServiceDisconnected(ComponentName name) {
141            mConnected = false;
142            mRemoteViewsFactory = null;
143
144            final RemoteViewsAdapter adapter = mAdapter.get();
145            if (adapter == null) return;
146
147            // Clear the main/worker queues
148            adapter.mMainQueue.removeMessages(0);
149            adapter.mWorkerQueue.removeMessages(0);
150
151            // Clear the cache (the meta data will be re-requested on service re-connection)
152            synchronized (adapter.mCache) {
153                adapter.mCache.reset();
154            }
155
156            final RemoteAdapterConnectionCallback callback = adapter.mCallback.get();
157            if (callback != null) {
158                callback.onRemoteAdapterDisconnected();
159            }
160        }
161
162        public IRemoteViewsFactory getRemoteViewsFactory() {
163            return mRemoteViewsFactory;
164        }
165
166        public boolean isConnected() {
167            return mConnected;
168        }
169    }
170
171    /**
172     * A FrameLayout which contains a loading view, and manages the re/applying of RemoteViews when
173     * they are loaded.
174     */
175    private class RemoteViewsFrameLayout extends FrameLayout {
176        public RemoteViewsFrameLayout(Context context) {
177            super(context);
178        }
179
180        /**
181         * Updates this RemoteViewsFrameLayout depending on the view that was loaded.
182         * @param view the RemoteViews that was loaded. If null, the RemoteViews was not loaded
183         *             successfully.
184         */
185        public void onRemoteViewsLoaded(RemoteViews view) {
186            try {
187                // Remove all the children of this layout first
188                removeAllViews();
189                addView(view.apply(getContext(), this));
190            } catch (Exception e) {
191                Log.e(TAG, "Failed to apply RemoteViews.");
192            }
193        }
194    }
195
196    /**
197     * Stores the references of all the RemoteViewsFrameLayouts that have been returned by the
198     * adapter that have not yet had their RemoteViews loaded.
199     */
200    private class RemoteViewsFrameLayoutRefSet {
201        private HashMap<Integer, LinkedList<RemoteViewsFrameLayout>> mReferences;
202
203        public RemoteViewsFrameLayoutRefSet() {
204            mReferences = new HashMap<Integer, LinkedList<RemoteViewsFrameLayout>>();
205        }
206
207        /**
208         * Adds a new reference to a RemoteViewsFrameLayout returned by the adapter.
209         */
210        public void add(int position, RemoteViewsFrameLayout layout) {
211            final Integer pos = position;
212            LinkedList<RemoteViewsFrameLayout> refs;
213
214            // Create the list if necessary
215            if (mReferences.containsKey(pos)) {
216                refs = mReferences.get(pos);
217            } else {
218                refs = new LinkedList<RemoteViewsFrameLayout>();
219                mReferences.put(pos, refs);
220            }
221
222            // Add the references to the list
223            refs.add(layout);
224        }
225
226        /**
227         * Notifies each of the RemoteViewsFrameLayouts associated with a particular position that
228         * the associated RemoteViews has loaded.
229         */
230        public void notifyOnRemoteViewsLoaded(int position, RemoteViews view, int typeId) {
231            if (view == null) return;
232
233            final Integer pos = position;
234            if (mReferences.containsKey(pos)) {
235                // Notify all the references for that position of the newly loaded RemoteViews
236                final LinkedList<RemoteViewsFrameLayout> refs = mReferences.get(pos);
237                for (final RemoteViewsFrameLayout ref : refs) {
238                    ref.onRemoteViewsLoaded(view);
239                }
240                refs.clear();
241
242                // Remove this set from the original mapping
243                mReferences.remove(pos);
244            }
245        }
246
247        /**
248         * Removes all references to all RemoteViewsFrameLayouts returned by the adapter.
249         */
250        public void clear() {
251            // We currently just clear the references, and leave all the previous layouts returned
252            // in their default state of the loading view.
253            mReferences.clear();
254        }
255    }
256
257    /**
258     * The meta-data associated with the cache in it's current state.
259     */
260    private class RemoteViewsMetaData {
261        int count;
262        int viewTypeCount;
263        boolean hasStableIds;
264        boolean isDataDirty;
265
266        // Used to determine how to construct loading views.  If a loading view is not specified
267        // by the user, then we try and load the first view, and use its height as the height for
268        // the default loading view.
269        RemoteViews mUserLoadingView;
270        RemoteViews mFirstView;
271        int mFirstViewHeight;
272
273        // A mapping from type id to a set of unique type ids
274        private Map<Integer, Integer> mTypeIdIndexMap;
275
276        public RemoteViewsMetaData() {
277            reset();
278        }
279
280        public void reset() {
281            count = 0;
282            // by default there is at least one dummy view type
283            viewTypeCount = 1;
284            hasStableIds = true;
285            isDataDirty = false;
286            mUserLoadingView = null;
287            mFirstView = null;
288            mFirstViewHeight = 0;
289            mTypeIdIndexMap = new HashMap<Integer, Integer>();
290        }
291
292        public void setLoadingViewTemplates(RemoteViews loadingView, RemoteViews firstView) {
293            mUserLoadingView = loadingView;
294            if (firstView != null) {
295                mFirstView = firstView;
296                mFirstViewHeight = -1;
297            }
298        }
299
300        public int getMappedViewType(int typeId) {
301            if (mTypeIdIndexMap.containsKey(typeId)) {
302                return mTypeIdIndexMap.get(typeId);
303            } else {
304                // We +1 because the loading view always has view type id of 0
305                int incrementalTypeId = mTypeIdIndexMap.size() + 1;
306                mTypeIdIndexMap.put(typeId, incrementalTypeId);
307                return incrementalTypeId;
308            }
309        }
310
311        private RemoteViewsFrameLayout createLoadingView(int position, View convertView,
312                ViewGroup parent) {
313            // Create and return a new FrameLayout, and setup the references for this position
314            final Context context = parent.getContext();
315            RemoteViewsFrameLayout layout = new RemoteViewsFrameLayout(context);
316
317            // Create a new loading view
318            synchronized (mCache) {
319                if (mUserLoadingView != null) {
320                    // A user-specified loading view
321                    View loadingView = mUserLoadingView.apply(parent.getContext(), parent);
322                    loadingView.setTag(new Integer(0));
323                    layout.addView(loadingView);
324                } else {
325                    // A default loading view
326                    // Use the size of the first row as a guide for the size of the loading view
327                    if (mFirstViewHeight < 0) {
328                        View firstView = mFirstView.apply(parent.getContext(), parent);
329                        firstView.measure(
330                                MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
331                                MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
332                        mFirstViewHeight = firstView.getMeasuredHeight();
333                        mFirstView = null;
334                    }
335
336                    // Compose the loading view text
337                    TextView textView = new TextView(parent.getContext());
338                    textView.setText(com.android.internal.R.string.loading);
339                    textView.setHeight(mFirstViewHeight);
340                    textView.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
341                    textView.setTextSize(18.0f);
342                    textView.setTextColor(Color.argb(96, 255, 255, 255));
343                    textView.setShadowLayer(2.0f, 0.0f, 1.0f, Color.BLACK);
344                    textView.setTag(new Integer(0));
345
346                    layout.addView(textView);
347                }
348            }
349
350            return layout;
351        }
352    }
353
354    /**
355     * The meta-data associated with a single item in the cache.
356     */
357    private class RemoteViewsIndexMetaData {
358        int typeId;
359        long itemId;
360
361        public RemoteViewsIndexMetaData(RemoteViews v, long itemId) {
362            set(v, itemId);
363        }
364
365        public void set(RemoteViews v, long id) {
366            itemId = id;
367            if (v != null)
368                typeId = v.getLayoutId();
369            else
370                typeId = 0;
371        }
372    }
373
374    /**
375     *
376     */
377    private class FixedSizeRemoteViewsCache {
378        private static final String TAG = "FixedSizeRemoteViewsCache";
379
380        // The meta data related to all the RemoteViews, ie. count, is stable, etc.
381        private RemoteViewsMetaData mMetaData;
382
383        // The cache/mapping of position to RemoteViewsMetaData.  This set is guaranteed to be
384        // greater than or equal to the set of RemoteViews.
385        // Note: The reason that we keep this separate from the RemoteViews cache below is that this
386        // we still need to be able to access the mapping of position to meta data, without keeping
387        // the heavy RemoteViews around.  The RemoteViews cache is trimmed to fixed constraints wrt.
388        // memory and size, but this metadata cache will retain information until the data at the
389        // position is guaranteed as not being necessary any more (usually on notifyDataSetChanged).
390        private HashMap<Integer, RemoteViewsIndexMetaData> mIndexMetaData;
391
392        // The cache of actual RemoteViews, which may be pruned if the cache gets too large, or uses
393        // too much memory.
394        private HashMap<Integer, RemoteViews> mIndexRemoteViews;
395
396        // The set of indices that have been explicitly requested by the collection view
397        private HashSet<Integer> mRequestedIndices;
398
399        // The set of indices to load, including those explicitly requested, as well as those
400        // determined by the preloading algorithm to be prefetched
401        private HashSet<Integer> mLoadIndices;
402
403        // The lower and upper bounds of the preloaded range
404        private int mPreloadLowerBound;
405        private int mPreloadUpperBound;
406
407        // The bounds of this fixed cache, we will try and fill as many items into the cache up to
408        // the maxCount number of items, or the maxSize memory usage.
409        // The maxCountSlack is used to determine if a new position in the cache to be loaded is
410        // sufficiently ouside the old set, prompting a shifting of the "window" of items to be
411        // preloaded.
412        private int mMaxCount;
413        private int mMaxCountSlack;
414        private static final float sMaxCountSlackPercent = 0.75f;
415        private static final int sMaxMemoryUsage = 1024 * 1024;
416
417        public FixedSizeRemoteViewsCache(int maxCacheSize) {
418            mMaxCount = maxCacheSize;
419            mMaxCountSlack = Math.round(sMaxCountSlackPercent * (mMaxCount / 2));
420            mPreloadLowerBound = 0;
421            mPreloadUpperBound = -1;
422            mMetaData = new RemoteViewsMetaData();
423            mIndexMetaData = new HashMap<Integer, RemoteViewsIndexMetaData>();
424            mIndexRemoteViews = new HashMap<Integer, RemoteViews>();
425            mRequestedIndices = new HashSet<Integer>();
426            mLoadIndices = new HashSet<Integer>();
427        }
428
429        public void insert(int position, RemoteViews v, long itemId) {
430            // Trim the cache if we go beyond the count
431            if (mIndexRemoteViews.size() >= mMaxCount) {
432                mIndexRemoteViews.remove(getFarthestPositionFrom(position));
433            }
434
435            // Trim the cache if we go beyond the available memory size constraints
436            while (getRemoteViewsBitmapMemoryUsage() >= sMaxMemoryUsage) {
437                // Note: This is currently the most naive mechanism for deciding what to prune when
438                // we hit the memory limit.  In the future, we may want to calculate which index to
439                // remove based on both its position as well as it's current memory usage, as well
440                // as whether it was directly requested vs. whether it was preloaded by our caching
441                // mechanism.
442                mIndexRemoteViews.remove(getFarthestPositionFrom(position));
443            }
444
445            // Update the metadata cache
446            if (mIndexMetaData.containsKey(position)) {
447                final RemoteViewsIndexMetaData metaData = mIndexMetaData.get(position);
448                metaData.set(v, itemId);
449            } else {
450                mIndexMetaData.put(position, new RemoteViewsIndexMetaData(v, itemId));
451            }
452            mIndexRemoteViews.put(position, v);
453        }
454
455        public RemoteViewsMetaData getMetaData() {
456            return mMetaData;
457        }
458        public RemoteViews getRemoteViewsAt(int position) {
459            if (mIndexRemoteViews.containsKey(position)) {
460                return mIndexRemoteViews.get(position);
461            }
462            return null;
463        }
464        public RemoteViewsIndexMetaData getMetaDataAt(int position) {
465            if (mIndexMetaData.containsKey(position)) {
466                return mIndexMetaData.get(position);
467            }
468            return null;
469        }
470
471        private int getRemoteViewsBitmapMemoryUsage() {
472            // Calculate the memory usage of all the RemoteViews bitmaps being cached
473            int mem = 0;
474            for (Integer i : mIndexRemoteViews.keySet()) {
475                final RemoteViews v = mIndexRemoteViews.get(i);
476                mem += v.estimateBitmapMemoryUsage();
477            }
478            return mem;
479        }
480        private int getFarthestPositionFrom(int pos) {
481            // Find the index farthest away and remove that
482            int maxDist = 0;
483            int maxDistIndex = -1;
484            for (int i : mIndexRemoteViews.keySet()) {
485                int dist = Math.abs(i-pos);
486                if (dist > maxDist) {
487                    maxDistIndex = i;
488                    maxDist = dist;
489                }
490            }
491            return maxDistIndex;
492        }
493
494        public void queueRequestedPositionToLoad(int position) {
495            synchronized (mLoadIndices) {
496                mRequestedIndices.add(position);
497                mLoadIndices.add(position);
498            }
499        }
500        public void queuePositionsToBePreloadedFromRequestedPosition(int position) {
501            // Check if we need to preload any items
502            if (mPreloadLowerBound <= position && position <= mPreloadUpperBound) {
503                int center = (mPreloadUpperBound + mPreloadLowerBound) / 2;
504                if (Math.abs(position - center) < mMaxCountSlack) {
505                    return;
506                }
507            }
508
509            int count = 0;
510            synchronized (mMetaData) {
511                count = mMetaData.count;
512            }
513            synchronized (mLoadIndices) {
514                mLoadIndices.clear();
515
516                // Add all the requested indices
517                mLoadIndices.addAll(mRequestedIndices);
518
519                // Add all the preload indices
520                int halfMaxCount = mMaxCount / 2;
521                mPreloadLowerBound = position - halfMaxCount;
522                mPreloadUpperBound = position + halfMaxCount;
523                int effectiveLowerBound = Math.max(0, mPreloadLowerBound);
524                int effectiveUpperBound = Math.min(mPreloadUpperBound, count - 1);
525                for (int i = effectiveLowerBound; i <= effectiveUpperBound; ++i) {
526                    mLoadIndices.add(i);
527                }
528
529                // But remove all the indices that have already been loaded and are cached
530                mLoadIndices.removeAll(mIndexRemoteViews.keySet());
531            }
532        }
533        public int getNextIndexToLoad() {
534            // We try and prioritize items that have been requested directly, instead
535            // of items that are loaded as a result of the caching mechanism
536            synchronized (mLoadIndices) {
537                // Prioritize requested indices to be loaded first
538                if (!mRequestedIndices.isEmpty()) {
539                    Integer i = mRequestedIndices.iterator().next();
540                    mRequestedIndices.remove(i);
541                    mLoadIndices.remove(i);
542                    return i.intValue();
543                }
544
545                // Otherwise, preload other indices as necessary
546                if (!mLoadIndices.isEmpty()) {
547                    Integer i = mLoadIndices.iterator().next();
548                    mLoadIndices.remove(i);
549                    return i.intValue();
550                }
551
552                return -1;
553            }
554        }
555
556        public boolean containsRemoteViewAt(int position) {
557            return mIndexRemoteViews.containsKey(position);
558        }
559        public boolean containsMetaDataAt(int position) {
560            return mIndexMetaData.containsKey(position);
561        }
562
563        public void reset() {
564            // Note: We do not try and reset the meta data, since that information is still used by
565            // collection views to validate it's own contents (and will be re-requested if the data
566            // is invalidated through the notifyDataSetChanged() flow).
567
568            mPreloadLowerBound = 0;
569            mPreloadUpperBound = -1;
570            mIndexRemoteViews.clear();
571            mIndexMetaData.clear();
572            synchronized (mLoadIndices) {
573                mRequestedIndices.clear();
574                mLoadIndices.clear();
575            }
576        }
577    }
578
579    public RemoteViewsAdapter(Context context, Intent intent, RemoteAdapterConnectionCallback callback) {
580        mContext = context;
581        mIntent = intent;
582        if (mIntent == null) {
583            throw new IllegalArgumentException("Non-null Intent must be specified.");
584        }
585        mRequestedViews = new RemoteViewsFrameLayoutRefSet();
586
587        // initialize the worker thread
588        mWorkerThread = new HandlerThread("RemoteViewsCache-loader");
589        mWorkerThread.start();
590        mWorkerQueue = new Handler(mWorkerThread.getLooper());
591        mMainQueue = new Handler(Looper.myLooper());
592
593        // initialize the cache and the service connection on startup
594        mCache = new FixedSizeRemoteViewsCache(50);
595        mCallback = new WeakReference<RemoteAdapterConnectionCallback>(callback);
596        mServiceConnection = new RemoteViewsAdapterServiceConnection(this);
597        requestBindService();
598    }
599
600    private void loadNextIndexInBackground() {
601        mWorkerQueue.post(new Runnable() {
602            @Override
603            public void run() {
604                // Get the next index to load
605                int position = -1;
606                synchronized (mCache) {
607                    position = mCache.getNextIndexToLoad();
608                }
609                if (position > -1) {
610                    // Load the item, and notify any existing RemoteViewsFrameLayouts
611                    updateRemoteViews(position);
612
613                    // Queue up for the next one to load
614                    loadNextIndexInBackground();
615                }
616            }
617        });
618    }
619
620    private void updateMetaData() {
621        if (mServiceConnection.isConnected()) {
622            try {
623                IRemoteViewsFactory factory = mServiceConnection.getRemoteViewsFactory();
624
625                // get the properties/first view (so that we can use it to
626                // measure our dummy views)
627                boolean hasStableIds = factory.hasStableIds();
628                int viewTypeCount = factory.getViewTypeCount();
629                int count = factory.getCount();
630                RemoteViews loadingView = factory.getLoadingView();
631                RemoteViews firstView = null;
632                if ((count > 0) && (loadingView == null)) {
633                    firstView = factory.getViewAt(0);
634                }
635                final RemoteViewsMetaData metaData = mCache.getMetaData();
636                synchronized (metaData) {
637                    metaData.hasStableIds = hasStableIds;
638                    metaData.viewTypeCount = viewTypeCount + 1;
639                    metaData.count = count;
640                    metaData.setLoadingViewTemplates(loadingView, firstView);
641                }
642            } catch (Exception e) {
643                // print the error
644                Log.e(TAG, "Error in requestMetaData(): " + e.getMessage());
645
646                // reset any members after the failed call
647                final RemoteViewsMetaData metaData = mCache.getMetaData();
648                synchronized (metaData) {
649                    metaData.reset();
650                }
651            }
652        }
653    }
654
655    private void updateRemoteViews(final int position) {
656        if (mServiceConnection.isConnected()) {
657            IRemoteViewsFactory factory = mServiceConnection.getRemoteViewsFactory();
658
659            // Load the item information from the remote service
660            RemoteViews remoteViews = null;
661            long itemId = 0;
662            try {
663                remoteViews = factory.getViewAt(position);
664                itemId = factory.getItemId(position);
665            } catch (Exception e) {
666                // Print the error
667                Log.e(TAG, "Error in updateRemoteViewsInfo(" + position + "): " +
668                        e.getMessage());
669                e.printStackTrace();
670
671                // Return early to prevent additional work in re-centering the view cache, and
672                // swapping from the loading view
673                return;
674            }
675
676            synchronized (mCache) {
677                // Cache the RemoteViews we loaded
678                mCache.insert(position, remoteViews, itemId);
679
680                // Notify all the views that we have previously returned for this index that
681                // there is new data for it.
682                final RemoteViews rv = remoteViews;
683                final int typeId = mCache.getMetaDataAt(position).typeId;
684                mMainQueue.post(new Runnable() {
685                    @Override
686                    public void run() {
687                        mRequestedViews.notifyOnRemoteViewsLoaded(position, rv, typeId);
688                    }
689                });
690            }
691        }
692    }
693
694    public Intent getRemoteViewsServiceIntent() {
695        return mIntent;
696    }
697
698    public int getCount() {
699        requestBindService();
700        final RemoteViewsMetaData metaData = mCache.getMetaData();
701        synchronized (metaData) {
702            return metaData.count;
703        }
704    }
705
706    public Object getItem(int position) {
707        // Disallow arbitrary object to be associated with an item for the time being
708        return null;
709    }
710
711    public long getItemId(int position) {
712        requestBindService();
713        synchronized (mCache) {
714            if (mCache.containsMetaDataAt(position)) {
715                return mCache.getMetaDataAt(position).itemId;
716            }
717            return 0;
718        }
719    }
720
721    public int getItemViewType(int position) {
722        requestBindService();
723        int typeId = 0;
724        synchronized (mCache) {
725            if (mCache.containsMetaDataAt(position)) {
726                typeId = mCache.getMetaDataAt(position).typeId;
727            } else {
728                return 0;
729            }
730        }
731
732        final RemoteViewsMetaData metaData = mCache.getMetaData();
733        synchronized (metaData) {
734            return metaData.getMappedViewType(typeId);
735        }
736    }
737
738    /**
739     * Returns the item type id for the specified convert view.  Returns -1 if the convert view
740     * is invalid.
741     */
742    private int getConvertViewTypeId(View convertView) {
743        int typeId = -1;
744        if (convertView != null && convertView.getTag() != null) {
745            typeId = (Integer) convertView.getTag();
746        }
747        return typeId;
748    }
749
750    public View getView(int position, View convertView, ViewGroup parent) {
751        requestBindService();
752        if (mServiceConnection.isConnected()) {
753            // "Request" an index so that we can queue it for loading, initiate subsequent
754            // preloading, etc.
755            synchronized (mCache) {
756                // Queue up other indices to be preloaded based on this position
757                mCache.queuePositionsToBePreloadedFromRequestedPosition(position);
758
759                RemoteViewsFrameLayout layout = (RemoteViewsFrameLayout) convertView;
760                View convertViewChild = null;
761                int convertViewTypeId = 0;
762                if (convertView != null) {
763                    convertViewChild = layout.getChildAt(0);
764                    convertViewTypeId = getConvertViewTypeId(convertViewChild);
765                }
766
767                // Second, we try and retrieve the RemoteViews from the cache, returning a loading
768                // view and queueing it to be loaded if it has not already been loaded.
769                if (mCache.containsRemoteViewAt(position)) {
770                    Context context = parent.getContext();
771                    RemoteViews rv = mCache.getRemoteViewsAt(position);
772                    int typeId = mCache.getMetaDataAt(position).typeId;
773
774                    // Reuse the convert view where possible
775                    if (convertView != null) {
776                        if (convertViewTypeId == typeId) {
777                            rv.reapply(context, convertViewChild);
778                            return convertView;
779                        }
780                    }
781
782                    // Otherwise, create a new view to be returned
783                    View newView = rv.apply(context, parent);
784                    newView.setTag(new Integer(typeId));
785                    if (convertView != null) {
786                        layout.removeAllViews();
787                    } else {
788                        layout = new RemoteViewsFrameLayout(context);
789                    }
790                    layout.addView(newView);
791                    return layout;
792                } else {
793                    // If the cache does not have the RemoteViews at this position, then create a
794                    // loading view and queue the actual position to be loaded in the background
795                    RemoteViewsFrameLayout loadingView = null;
796                    final RemoteViewsMetaData metaData = mCache.getMetaData();
797                    synchronized (metaData) {
798                        loadingView = metaData.createLoadingView(position, convertView, parent);
799                    }
800
801                    mRequestedViews.add(position, loadingView);
802                    mCache.queueRequestedPositionToLoad(position);
803                    loadNextIndexInBackground();
804
805                    return loadingView;
806                }
807            }
808        }
809        return new View(parent.getContext());
810    }
811
812    public int getViewTypeCount() {
813        requestBindService();
814        final RemoteViewsMetaData metaData = mCache.getMetaData();
815        synchronized (metaData) {
816            return metaData.viewTypeCount;
817        }
818    }
819
820    public boolean hasStableIds() {
821        requestBindService();
822        final RemoteViewsMetaData metaData = mCache.getMetaData();
823        synchronized (metaData) {
824            return metaData.hasStableIds;
825        }
826    }
827
828    public boolean isEmpty() {
829        return getCount() <= 0;
830    }
831
832    public void notifyDataSetChanged() {
833        mWorkerQueue.post(new Runnable() {
834            @Override
835            public void run() {
836                // Complete the actual notifyDataSetChanged() call initiated earlier
837                if (mServiceConnection.isConnected()) {
838                    IRemoteViewsFactory factory = mServiceConnection.getRemoteViewsFactory();
839                    try {
840                        factory.onDataSetChanged();
841                    } catch (Exception e) {
842                        Log.e(TAG, "Error in updateNotifyDataSetChanged(): " + e.getMessage());
843
844                        // Return early to prevent from further being notified (since nothing has
845                        // changed)
846                        return;
847                    }
848                }
849
850                // Flush the cache so that we can reload new items from the service
851                synchronized (mCache) {
852                    mCache.reset();
853                }
854
855                // Re-request the new metadata (only after the notification to the factory)
856                updateMetaData();
857
858                // Propagate the notification back to the base adapter
859                mMainQueue.post(new Runnable() {
860                    @Override
861                    public void run() {
862                        superNotifyDataSetChanged();
863                    }
864                });
865            }
866        });
867
868        // Note: we do not call super.notifyDataSetChanged() until the RemoteViewsFactory has had
869        // a chance to update itself and return new meta data associated with the new data.
870    }
871
872    private void superNotifyDataSetChanged() {
873        super.notifyDataSetChanged();
874    }
875
876    private boolean requestBindService() {
877        // try binding the service (which will start it if it's not already running)
878        if (!mServiceConnection.isConnected()) {
879            mContext.bindService(mIntent, mServiceConnection, Context.BIND_AUTO_CREATE);
880        }
881
882        return mServiceConnection.isConnected();
883    }
884}
885