Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 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 | |
| 17 | package android.widget; |
| 18 | |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 19 | import java.lang.ref.WeakReference; |
Adam Cohen | 591ff97 | 2012-07-24 22:46:11 -0700 | [diff] [blame] | 20 | import java.util.ArrayList; |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 21 | import java.util.HashMap; |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 22 | import java.util.HashSet; |
Winson Chung | c6d6d4a | 2010-07-22 13:54:27 -0700 | [diff] [blame] | 23 | import java.util.LinkedList; |
Winson Chung | 81f39eb | 2011-01-11 18:05:01 -0800 | [diff] [blame] | 24 | import android.appwidget.AppWidgetManager; |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 25 | import android.content.Context; |
| 26 | import android.content.Intent; |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 27 | import android.os.Handler; |
| 28 | import android.os.HandlerThread; |
| 29 | import android.os.IBinder; |
| 30 | import android.os.Looper; |
Winson Chung | 81f39eb | 2011-01-11 18:05:01 -0800 | [diff] [blame] | 31 | import android.os.Message; |
Adam Cohen | 2625fea | 2011-03-23 17:24:30 -0700 | [diff] [blame] | 32 | import android.os.RemoteException; |
Winson Chung | fbc3590 | 2010-09-09 16:45:06 -0700 | [diff] [blame] | 33 | import android.util.Log; |
Adam Cohen | 335c3b6 | 2012-07-24 17:18:16 -0700 | [diff] [blame] | 34 | import android.util.Pair; |
Winson Chung | a5f6f80 | 2010-09-29 09:24:21 -0700 | [diff] [blame] | 35 | import android.view.LayoutInflater; |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 36 | import android.view.View; |
Adam Cohen | 181d2e3 | 2011-01-17 12:40:29 -0800 | [diff] [blame] | 37 | import android.view.View.MeasureSpec; |
Winson Chung | 84bbb02 | 2011-02-21 13:57:45 -0800 | [diff] [blame] | 38 | import android.view.ViewGroup; |
Adam Cohen | a6a4cbc | 2012-09-26 17:36:40 -0700 | [diff] [blame] | 39 | import android.widget.RemoteViews.OnClickHandler; |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 40 | |
Winson Chung | 81f39eb | 2011-01-11 18:05:01 -0800 | [diff] [blame] | 41 | import com.android.internal.widget.IRemoteViewsAdapterConnection; |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 42 | import com.android.internal.widget.IRemoteViewsFactory; |
| 43 | |
| 44 | /** |
| 45 | * An adapter to a RemoteViewsService which fetches and caches RemoteViews |
| 46 | * to be later inflated as child views. |
| 47 | */ |
| 48 | /** @hide */ |
Winson Chung | 81f39eb | 2011-01-11 18:05:01 -0800 | [diff] [blame] | 49 | public class RemoteViewsAdapter extends BaseAdapter implements Handler.Callback { |
Winson Chung | fbc3590 | 2010-09-09 16:45:06 -0700 | [diff] [blame] | 50 | private static final String TAG = "RemoteViewsAdapter"; |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 51 | |
Adam Cohen | 4a9df8d | 2012-07-27 15:35:13 -0700 | [diff] [blame] | 52 | // The max number of items in the cache |
Winson Chung | b90a91c | 2011-01-26 13:36:34 -0800 | [diff] [blame] | 53 | private static final int sDefaultCacheSize = 40; |
Winson Chung | 81f39eb | 2011-01-11 18:05:01 -0800 | [diff] [blame] | 54 | // The delay (in millis) to wait until attempting to unbind from a service after a request. |
| 55 | // This ensures that we don't stay continually bound to the service and that it can be destroyed |
| 56 | // if we need the memory elsewhere in the system. |
Winson Chung | b90a91c | 2011-01-26 13:36:34 -0800 | [diff] [blame] | 57 | private static final int sUnbindServiceDelay = 5000; |
Adam Cohen | b7ffea6 | 2011-07-14 14:45:07 -0700 | [diff] [blame] | 58 | |
| 59 | // Default height for the default loading view, in case we cannot get inflate the first view |
| 60 | private static final int sDefaultLoadingViewHeight = 50; |
| 61 | |
Winson Chung | 81f39eb | 2011-01-11 18:05:01 -0800 | [diff] [blame] | 62 | // Type defs for controlling different messages across the main and worker message queues |
Adam Cohen | 4a9df8d | 2012-07-27 15:35:13 -0700 | [diff] [blame] | 63 | private static final int sDefaultMessageType = 0; |
Winson Chung | 81f39eb | 2011-01-11 18:05:01 -0800 | [diff] [blame] | 64 | private static final int sUnbindServiceMessageType = 1; |
| 65 | |
| 66 | private final Context mContext; |
| 67 | private final Intent mIntent; |
| 68 | private final int mAppWidgetId; |
Winson Chung | a5f6f80 | 2010-09-29 09:24:21 -0700 | [diff] [blame] | 69 | private LayoutInflater mLayoutInflater; |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 70 | private RemoteViewsAdapterServiceConnection mServiceConnection; |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 71 | private WeakReference<RemoteAdapterConnectionCallback> mCallback; |
Adam Cohen | a6a4cbc | 2012-09-26 17:36:40 -0700 | [diff] [blame] | 72 | private OnClickHandler mRemoteViewsOnClickHandler; |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 73 | private FixedSizeRemoteViewsCache mCache; |
Adam Cohen | b967392 | 2012-01-05 13:58:47 -0800 | [diff] [blame] | 74 | private int mVisibleWindowLowerBound; |
| 75 | private int mVisibleWindowUpperBound; |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 76 | |
Winson Chung | 16c8d8a | 2011-01-20 16:19:33 -0800 | [diff] [blame] | 77 | // A flag to determine whether we should notify data set changed after we connect |
| 78 | private boolean mNotifyDataSetChangedAfterOnServiceConnected = false; |
| 79 | |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 80 | // The set of requested views that are to be notified when the associated RemoteViews are |
| 81 | // loaded. |
| 82 | private RemoteViewsFrameLayoutRefSet mRequestedViews; |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 83 | |
| 84 | private HandlerThread mWorkerThread; |
| 85 | // items may be interrupted within the normally processed queues |
| 86 | private Handler mWorkerQueue; |
| 87 | private Handler mMainQueue; |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 88 | |
Adam Cohen | 335c3b6 | 2012-07-24 17:18:16 -0700 | [diff] [blame] | 89 | // We cache the FixedSizeRemoteViewsCaches across orientation. These are the related data |
| 90 | // structures; |
Adam Cohen | 4a9df8d | 2012-07-27 15:35:13 -0700 | [diff] [blame] | 91 | private static final HashMap<Pair<Intent.FilterComparison, Integer>, FixedSizeRemoteViewsCache> |
Adam Cohen | 335c3b6 | 2012-07-24 17:18:16 -0700 | [diff] [blame] | 92 | sCachedRemoteViewsCaches = new HashMap<Pair<Intent.FilterComparison, Integer>, |
Adam Cohen | 4a9df8d | 2012-07-27 15:35:13 -0700 | [diff] [blame] | 93 | FixedSizeRemoteViewsCache>(); |
Adam Cohen | 335c3b6 | 2012-07-24 17:18:16 -0700 | [diff] [blame] | 94 | private static final HashMap<Pair<Intent.FilterComparison, Integer>, Runnable> |
| 95 | sRemoteViewsCacheRemoveRunnables = new HashMap<Pair<Intent.FilterComparison, Integer>, |
| 96 | Runnable>(); |
| 97 | private static HandlerThread sCacheRemovalThread; |
| 98 | private static Handler sCacheRemovalQueue; |
| 99 | |
| 100 | // We keep the cache around for a duration after onSaveInstanceState for use on re-inflation. |
| 101 | // If a new RemoteViewsAdapter with the same intent / widget id isn't constructed within this |
| 102 | // duration, the cache is dropped. |
| 103 | private static final int REMOTE_VIEWS_CACHE_DURATION = 5000; |
| 104 | |
| 105 | // Used to indicate to the AdapterView that it can use this Adapter immediately after |
| 106 | // construction (happens when we have a cached FixedSizeRemoteViewsCache). |
| 107 | private boolean mDataReady = false; |
| 108 | |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 109 | /** |
| 110 | * An interface for the RemoteAdapter to notify other classes when adapters |
| 111 | * are actually connected to/disconnected from their actual services. |
| 112 | */ |
| 113 | public interface RemoteAdapterConnectionCallback { |
Winson Chung | 16c8d8a | 2011-01-20 16:19:33 -0800 | [diff] [blame] | 114 | /** |
| 115 | * @return whether the adapter was set or not. |
| 116 | */ |
| 117 | public boolean onRemoteAdapterConnected(); |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 118 | |
| 119 | public void onRemoteAdapterDisconnected(); |
Adam Cohen | 2148d43 | 2011-07-28 14:59:54 -0700 | [diff] [blame] | 120 | |
| 121 | /** |
| 122 | * This defers a notifyDataSetChanged on the pending RemoteViewsAdapter if it has not |
| 123 | * connected yet. |
| 124 | */ |
| 125 | public void deferNotifyDataSetChanged(); |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | /** |
| 129 | * The service connection that gets populated when the RemoteViewsService is |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 130 | * bound. This must be a static inner class to ensure that no references to the outer |
| 131 | * RemoteViewsAdapter instance is retained (this would prevent the RemoteViewsAdapter from being |
| 132 | * garbage collected, and would cause us to leak activities due to the caching mechanism for |
| 133 | * FrameLayouts in the adapter). |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 134 | */ |
Winson Chung | 81f39eb | 2011-01-11 18:05:01 -0800 | [diff] [blame] | 135 | private static class RemoteViewsAdapterServiceConnection extends |
| 136 | IRemoteViewsAdapterConnection.Stub { |
Winson Chung | 16c8d8a | 2011-01-20 16:19:33 -0800 | [diff] [blame] | 137 | private boolean mIsConnected; |
| 138 | private boolean mIsConnecting; |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 139 | private WeakReference<RemoteViewsAdapter> mAdapter; |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 140 | private IRemoteViewsFactory mRemoteViewsFactory; |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 141 | |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 142 | public RemoteViewsAdapterServiceConnection(RemoteViewsAdapter adapter) { |
| 143 | mAdapter = new WeakReference<RemoteViewsAdapter>(adapter); |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 144 | } |
| 145 | |
Winson Chung | 16c8d8a | 2011-01-20 16:19:33 -0800 | [diff] [blame] | 146 | public synchronized void bind(Context context, int appWidgetId, Intent intent) { |
| 147 | if (!mIsConnecting) { |
| 148 | try { |
| 149 | final AppWidgetManager mgr = AppWidgetManager.getInstance(context); |
| 150 | mgr.bindRemoteViewsService(appWidgetId, intent, asBinder()); |
| 151 | mIsConnecting = true; |
| 152 | } catch (Exception e) { |
| 153 | Log.e("RemoteViewsAdapterServiceConnection", "bind(): " + e.getMessage()); |
| 154 | mIsConnecting = false; |
| 155 | mIsConnected = false; |
| 156 | } |
| 157 | } |
| 158 | } |
Winson Chung | c6d6d4a | 2010-07-22 13:54:27 -0700 | [diff] [blame] | 159 | |
Winson Chung | 16c8d8a | 2011-01-20 16:19:33 -0800 | [diff] [blame] | 160 | public synchronized void unbind(Context context, int appWidgetId, Intent intent) { |
| 161 | try { |
| 162 | final AppWidgetManager mgr = AppWidgetManager.getInstance(context); |
| 163 | mgr.unbindRemoteViewsService(appWidgetId, intent); |
| 164 | mIsConnecting = false; |
| 165 | } catch (Exception e) { |
| 166 | Log.e("RemoteViewsAdapterServiceConnection", "unbind(): " + e.getMessage()); |
| 167 | mIsConnecting = false; |
| 168 | mIsConnected = false; |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | public synchronized void onServiceConnected(IBinder service) { |
| 173 | mRemoteViewsFactory = IRemoteViewsFactory.Stub.asInterface(service); |
| 174 | |
| 175 | // Remove any deferred unbind messages |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 176 | final RemoteViewsAdapter adapter = mAdapter.get(); |
| 177 | if (adapter == null) return; |
Winson Chung | 16c8d8a | 2011-01-20 16:19:33 -0800 | [diff] [blame] | 178 | |
| 179 | // Queue up work that we need to do for the callback to run |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 180 | adapter.mWorkerQueue.post(new Runnable() { |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 181 | @Override |
| 182 | public void run() { |
Winson Chung | 16c8d8a | 2011-01-20 16:19:33 -0800 | [diff] [blame] | 183 | if (adapter.mNotifyDataSetChangedAfterOnServiceConnected) { |
| 184 | // Handle queued notifyDataSetChanged() if necessary |
| 185 | adapter.onNotifyDataSetChanged(); |
| 186 | } else { |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 187 | IRemoteViewsFactory factory = |
| 188 | adapter.mServiceConnection.getRemoteViewsFactory(); |
| 189 | try { |
Winson Chung | 16c8d8a | 2011-01-20 16:19:33 -0800 | [diff] [blame] | 190 | if (!factory.isCreated()) { |
| 191 | // We only call onDataSetChanged() if this is the factory was just |
| 192 | // create in response to this bind |
| 193 | factory.onDataSetChanged(); |
| 194 | } |
Adam Cohen | 2625fea | 2011-03-23 17:24:30 -0700 | [diff] [blame] | 195 | } catch (RemoteException e) { |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 196 | Log.e(TAG, "Error notifying factory of data set changed in " + |
| 197 | "onServiceConnected(): " + e.getMessage()); |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 198 | |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 199 | // Return early to prevent anything further from being notified |
| 200 | // (effectively nothing has changed) |
| 201 | return; |
Adam Cohen | 2625fea | 2011-03-23 17:24:30 -0700 | [diff] [blame] | 202 | } catch (RuntimeException e) { |
| 203 | Log.e(TAG, "Error notifying factory of data set changed in " + |
| 204 | "onServiceConnected(): " + e.getMessage()); |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 205 | } |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 206 | |
| 207 | // Request meta data so that we have up to date data when calling back to |
| 208 | // the remote adapter callback |
Winson Chung | 16c8d8a | 2011-01-20 16:19:33 -0800 | [diff] [blame] | 209 | adapter.updateTemporaryMetaData(); |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 210 | |
Winson Chung | 16c8d8a | 2011-01-20 16:19:33 -0800 | [diff] [blame] | 211 | // Notify the host that we've connected |
Winson Chung | 61ac7e3 | 2010-09-28 10:08:31 -0700 | [diff] [blame] | 212 | adapter.mMainQueue.post(new Runnable() { |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 213 | @Override |
| 214 | public void run() { |
Winson Chung | 16c8d8a | 2011-01-20 16:19:33 -0800 | [diff] [blame] | 215 | synchronized (adapter.mCache) { |
| 216 | adapter.mCache.commitTemporaryMetaData(); |
| 217 | } |
| 218 | |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 219 | final RemoteAdapterConnectionCallback callback = |
| 220 | adapter.mCallback.get(); |
| 221 | if (callback != null) { |
| 222 | callback.onRemoteAdapterConnected(); |
| 223 | } |
| 224 | } |
| 225 | }); |
| 226 | } |
Winson Chung | 16c8d8a | 2011-01-20 16:19:33 -0800 | [diff] [blame] | 227 | |
| 228 | // Enqueue unbind message |
| 229 | adapter.enqueueDeferredUnbindServiceMessage(); |
| 230 | mIsConnected = true; |
| 231 | mIsConnecting = false; |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 232 | } |
| 233 | }); |
| 234 | } |
| 235 | |
Winson Chung | 16c8d8a | 2011-01-20 16:19:33 -0800 | [diff] [blame] | 236 | public synchronized void onServiceDisconnected() { |
| 237 | mIsConnected = false; |
| 238 | mIsConnecting = false; |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 239 | mRemoteViewsFactory = null; |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 240 | |
Winson Chung | 16c8d8a | 2011-01-20 16:19:33 -0800 | [diff] [blame] | 241 | // Clear the main/worker queues |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 242 | final RemoteViewsAdapter adapter = mAdapter.get(); |
| 243 | if (adapter == null) return; |
Winson Chung | c6d6d4a | 2010-07-22 13:54:27 -0700 | [diff] [blame] | 244 | |
Winson Chung | 16c8d8a | 2011-01-20 16:19:33 -0800 | [diff] [blame] | 245 | adapter.mMainQueue.post(new Runnable() { |
| 246 | @Override |
| 247 | public void run() { |
| 248 | // Dequeue any unbind messages |
| 249 | adapter.mMainQueue.removeMessages(sUnbindServiceMessageType); |
Winson Chung | c6d6d4a | 2010-07-22 13:54:27 -0700 | [diff] [blame] | 250 | |
Winson Chung | 16c8d8a | 2011-01-20 16:19:33 -0800 | [diff] [blame] | 251 | final RemoteAdapterConnectionCallback callback = adapter.mCallback.get(); |
| 252 | if (callback != null) { |
| 253 | callback.onRemoteAdapterDisconnected(); |
| 254 | } |
| 255 | } |
| 256 | }); |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 257 | } |
| 258 | |
Winson Chung | 16c8d8a | 2011-01-20 16:19:33 -0800 | [diff] [blame] | 259 | public synchronized IRemoteViewsFactory getRemoteViewsFactory() { |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 260 | return mRemoteViewsFactory; |
| 261 | } |
| 262 | |
Winson Chung | 16c8d8a | 2011-01-20 16:19:33 -0800 | [diff] [blame] | 263 | public synchronized boolean isConnected() { |
| 264 | return mIsConnected; |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 265 | } |
| 266 | } |
| 267 | |
| 268 | /** |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 269 | * A FrameLayout which contains a loading view, and manages the re/applying of RemoteViews when |
| 270 | * they are loaded. |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 271 | */ |
Adam Cohen | a5a0687 | 2012-07-11 15:23:10 -0700 | [diff] [blame] | 272 | private static class RemoteViewsFrameLayout extends FrameLayout { |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 273 | public RemoteViewsFrameLayout(Context context) { |
| 274 | super(context); |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | /** |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 278 | * Updates this RemoteViewsFrameLayout depending on the view that was loaded. |
| 279 | * @param view the RemoteViews that was loaded. If null, the RemoteViews was not loaded |
| 280 | * successfully. |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 281 | */ |
Adam Cohen | a6a4cbc | 2012-09-26 17:36:40 -0700 | [diff] [blame] | 282 | public void onRemoteViewsLoaded(RemoteViews view, OnClickHandler handler) { |
Winson Chung | 61ac7e3 | 2010-09-28 10:08:31 -0700 | [diff] [blame] | 283 | try { |
| 284 | // Remove all the children of this layout first |
| 285 | removeAllViews(); |
Adam Cohen | a6a4cbc | 2012-09-26 17:36:40 -0700 | [diff] [blame] | 286 | addView(view.apply(getContext(), this, handler)); |
Winson Chung | 61ac7e3 | 2010-09-28 10:08:31 -0700 | [diff] [blame] | 287 | } catch (Exception e) { |
| 288 | Log.e(TAG, "Failed to apply RemoteViews."); |
| 289 | } |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 290 | } |
| 291 | } |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 292 | |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 293 | /** |
| 294 | * Stores the references of all the RemoteViewsFrameLayouts that have been returned by the |
| 295 | * adapter that have not yet had their RemoteViews loaded. |
| 296 | */ |
| 297 | private class RemoteViewsFrameLayoutRefSet { |
| 298 | private HashMap<Integer, LinkedList<RemoteViewsFrameLayout>> mReferences; |
| 299 | |
| 300 | public RemoteViewsFrameLayoutRefSet() { |
| 301 | mReferences = new HashMap<Integer, LinkedList<RemoteViewsFrameLayout>>(); |
| 302 | } |
| 303 | |
| 304 | /** |
| 305 | * Adds a new reference to a RemoteViewsFrameLayout returned by the adapter. |
| 306 | */ |
| 307 | public void add(int position, RemoteViewsFrameLayout layout) { |
| 308 | final Integer pos = position; |
| 309 | LinkedList<RemoteViewsFrameLayout> refs; |
| 310 | |
| 311 | // Create the list if necessary |
| 312 | if (mReferences.containsKey(pos)) { |
| 313 | refs = mReferences.get(pos); |
| 314 | } else { |
| 315 | refs = new LinkedList<RemoteViewsFrameLayout>(); |
| 316 | mReferences.put(pos, refs); |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 317 | } |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 318 | |
| 319 | // Add the references to the list |
| 320 | refs.add(layout); |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 321 | } |
| 322 | |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 323 | /** |
| 324 | * Notifies each of the RemoteViewsFrameLayouts associated with a particular position that |
| 325 | * the associated RemoteViews has loaded. |
| 326 | */ |
Adam Cohen | a5a0687 | 2012-07-11 15:23:10 -0700 | [diff] [blame] | 327 | public void notifyOnRemoteViewsLoaded(int position, RemoteViews view) { |
Winson Chung | 61ac7e3 | 2010-09-28 10:08:31 -0700 | [diff] [blame] | 328 | if (view == null) return; |
| 329 | |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 330 | final Integer pos = position; |
| 331 | if (mReferences.containsKey(pos)) { |
| 332 | // Notify all the references for that position of the newly loaded RemoteViews |
| 333 | final LinkedList<RemoteViewsFrameLayout> refs = mReferences.get(pos); |
| 334 | for (final RemoteViewsFrameLayout ref : refs) { |
Adam Cohen | a6a4cbc | 2012-09-26 17:36:40 -0700 | [diff] [blame] | 335 | ref.onRemoteViewsLoaded(view, mRemoteViewsOnClickHandler); |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 336 | } |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 337 | refs.clear(); |
| 338 | |
| 339 | // Remove this set from the original mapping |
| 340 | mReferences.remove(pos); |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 341 | } |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 342 | } |
| 343 | |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 344 | /** |
| 345 | * Removes all references to all RemoteViewsFrameLayouts returned by the adapter. |
| 346 | */ |
| 347 | public void clear() { |
| 348 | // We currently just clear the references, and leave all the previous layouts returned |
| 349 | // in their default state of the loading view. |
| 350 | mReferences.clear(); |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | /** |
| 355 | * The meta-data associated with the cache in it's current state. |
| 356 | */ |
Adam Cohen | 4a9df8d | 2012-07-27 15:35:13 -0700 | [diff] [blame] | 357 | private static class RemoteViewsMetaData { |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 358 | int count; |
| 359 | int viewTypeCount; |
| 360 | boolean hasStableIds; |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 361 | |
| 362 | // Used to determine how to construct loading views. If a loading view is not specified |
| 363 | // by the user, then we try and load the first view, and use its height as the height for |
| 364 | // the default loading view. |
| 365 | RemoteViews mUserLoadingView; |
| 366 | RemoteViews mFirstView; |
| 367 | int mFirstViewHeight; |
| 368 | |
| 369 | // A mapping from type id to a set of unique type ids |
Winson Chung | 16c8d8a | 2011-01-20 16:19:33 -0800 | [diff] [blame] | 370 | private final HashMap<Integer, Integer> mTypeIdIndexMap = new HashMap<Integer, Integer>(); |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 371 | |
| 372 | public RemoteViewsMetaData() { |
| 373 | reset(); |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 374 | } |
| 375 | |
Winson Chung | 16c8d8a | 2011-01-20 16:19:33 -0800 | [diff] [blame] | 376 | public void set(RemoteViewsMetaData d) { |
| 377 | synchronized (d) { |
| 378 | count = d.count; |
| 379 | viewTypeCount = d.viewTypeCount; |
| 380 | hasStableIds = d.hasStableIds; |
| 381 | setLoadingViewTemplates(d.mUserLoadingView, d.mFirstView); |
| 382 | } |
| 383 | } |
| 384 | |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 385 | public void reset() { |
| 386 | count = 0; |
Winson Chung | 16c8d8a | 2011-01-20 16:19:33 -0800 | [diff] [blame] | 387 | |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 388 | // by default there is at least one dummy view type |
| 389 | viewTypeCount = 1; |
| 390 | hasStableIds = true; |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 391 | mUserLoadingView = null; |
| 392 | mFirstView = null; |
| 393 | mFirstViewHeight = 0; |
Winson Chung | 16c8d8a | 2011-01-20 16:19:33 -0800 | [diff] [blame] | 394 | mTypeIdIndexMap.clear(); |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 395 | } |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 396 | |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 397 | public void setLoadingViewTemplates(RemoteViews loadingView, RemoteViews firstView) { |
| 398 | mUserLoadingView = loadingView; |
| 399 | if (firstView != null) { |
| 400 | mFirstView = firstView; |
| 401 | mFirstViewHeight = -1; |
| 402 | } |
| 403 | } |
Winson Chung | fbc3590 | 2010-09-09 16:45:06 -0700 | [diff] [blame] | 404 | |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 405 | public int getMappedViewType(int typeId) { |
| 406 | if (mTypeIdIndexMap.containsKey(typeId)) { |
| 407 | return mTypeIdIndexMap.get(typeId); |
| 408 | } else { |
| 409 | // We +1 because the loading view always has view type id of 0 |
| 410 | int incrementalTypeId = mTypeIdIndexMap.size() + 1; |
| 411 | mTypeIdIndexMap.put(typeId, incrementalTypeId); |
| 412 | return incrementalTypeId; |
| 413 | } |
| 414 | } |
| 415 | |
Adam Cohen | a5a0687 | 2012-07-11 15:23:10 -0700 | [diff] [blame] | 416 | public boolean isViewTypeInRange(int typeId) { |
| 417 | int mappedType = getMappedViewType(typeId); |
| 418 | if (mappedType >= viewTypeCount) { |
| 419 | return false; |
| 420 | } else { |
| 421 | return true; |
| 422 | } |
| 423 | } |
| 424 | |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 425 | private RemoteViewsFrameLayout createLoadingView(int position, View convertView, |
Adam Cohen | a6a4cbc | 2012-09-26 17:36:40 -0700 | [diff] [blame] | 426 | ViewGroup parent, Object lock, LayoutInflater layoutInflater, OnClickHandler |
| 427 | handler) { |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 428 | // Create and return a new FrameLayout, and setup the references for this position |
| 429 | final Context context = parent.getContext(); |
| 430 | RemoteViewsFrameLayout layout = new RemoteViewsFrameLayout(context); |
| 431 | |
| 432 | // Create a new loading view |
Adam Cohen | a5a0687 | 2012-07-11 15:23:10 -0700 | [diff] [blame] | 433 | synchronized (lock) { |
Adam Cohen | b7ffea6 | 2011-07-14 14:45:07 -0700 | [diff] [blame] | 434 | boolean customLoadingViewAvailable = false; |
| 435 | |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 436 | if (mUserLoadingView != null) { |
Adam Cohen | b7ffea6 | 2011-07-14 14:45:07 -0700 | [diff] [blame] | 437 | // Try to inflate user-specified loading view |
| 438 | try { |
Adam Cohen | a6a4cbc | 2012-09-26 17:36:40 -0700 | [diff] [blame] | 439 | View loadingView = mUserLoadingView.apply(parent.getContext(), parent, |
| 440 | handler); |
Adam Cohen | b7ffea6 | 2011-07-14 14:45:07 -0700 | [diff] [blame] | 441 | loadingView.setTagInternal(com.android.internal.R.id.rowTypeId, |
| 442 | new Integer(0)); |
| 443 | layout.addView(loadingView); |
| 444 | customLoadingViewAvailable = true; |
| 445 | } catch (Exception e) { |
| 446 | Log.w(TAG, "Error inflating custom loading view, using default loading" + |
| 447 | "view instead", e); |
| 448 | } |
| 449 | } |
| 450 | if (!customLoadingViewAvailable) { |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 451 | // A default loading view |
| 452 | // Use the size of the first row as a guide for the size of the loading view |
| 453 | if (mFirstViewHeight < 0) { |
Adam Cohen | b7ffea6 | 2011-07-14 14:45:07 -0700 | [diff] [blame] | 454 | try { |
Adam Cohen | a6a4cbc | 2012-09-26 17:36:40 -0700 | [diff] [blame] | 455 | View firstView = mFirstView.apply(parent.getContext(), parent, handler); |
Adam Cohen | b7ffea6 | 2011-07-14 14:45:07 -0700 | [diff] [blame] | 456 | firstView.measure( |
| 457 | MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), |
| 458 | MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); |
| 459 | mFirstViewHeight = firstView.getMeasuredHeight(); |
| 460 | mFirstView = null; |
| 461 | } catch (Exception e) { |
Adam Cohen | a5a0687 | 2012-07-11 15:23:10 -0700 | [diff] [blame] | 462 | float density = context.getResources().getDisplayMetrics().density; |
Adam Cohen | b7ffea6 | 2011-07-14 14:45:07 -0700 | [diff] [blame] | 463 | mFirstViewHeight = (int) |
| 464 | Math.round(sDefaultLoadingViewHeight * density); |
| 465 | mFirstView = null; |
| 466 | Log.w(TAG, "Error inflating first RemoteViews" + e); |
| 467 | } |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 468 | } |
| 469 | |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 470 | // Compose the loading view text |
Adam Cohen | a5a0687 | 2012-07-11 15:23:10 -0700 | [diff] [blame] | 471 | TextView loadingTextView = (TextView) layoutInflater.inflate( |
Adam Cohen | fb60386 | 2010-12-17 12:03:17 -0800 | [diff] [blame] | 472 | com.android.internal.R.layout.remote_views_adapter_default_loading_view, |
| 473 | layout, false); |
Winson Chung | a5f6f80 | 2010-09-29 09:24:21 -0700 | [diff] [blame] | 474 | loadingTextView.setHeight(mFirstViewHeight); |
| 475 | loadingTextView.setTag(new Integer(0)); |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 476 | |
Winson Chung | a5f6f80 | 2010-09-29 09:24:21 -0700 | [diff] [blame] | 477 | layout.addView(loadingTextView); |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 478 | } |
| 479 | } |
| 480 | |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 481 | return layout; |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | /** |
| 486 | * The meta-data associated with a single item in the cache. |
| 487 | */ |
Adam Cohen | 4a9df8d | 2012-07-27 15:35:13 -0700 | [diff] [blame] | 488 | private static class RemoteViewsIndexMetaData { |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 489 | int typeId; |
| 490 | long itemId; |
| 491 | |
Adam Cohen | 591ff97 | 2012-07-24 22:46:11 -0700 | [diff] [blame] | 492 | public RemoteViewsIndexMetaData(RemoteViews v, long itemId) { |
| 493 | set(v, itemId); |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 494 | } |
| 495 | |
Adam Cohen | 591ff97 | 2012-07-24 22:46:11 -0700 | [diff] [blame] | 496 | public void set(RemoteViews v, long id) { |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 497 | itemId = id; |
Adam Cohen | a5a0687 | 2012-07-11 15:23:10 -0700 | [diff] [blame] | 498 | if (v != null) { |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 499 | typeId = v.getLayoutId(); |
Adam Cohen | a5a0687 | 2012-07-11 15:23:10 -0700 | [diff] [blame] | 500 | } else { |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 501 | typeId = 0; |
Adam Cohen | a5a0687 | 2012-07-11 15:23:10 -0700 | [diff] [blame] | 502 | } |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 503 | } |
| 504 | } |
| 505 | |
| 506 | /** |
| 507 | * |
| 508 | */ |
Adam Cohen | 4a9df8d | 2012-07-27 15:35:13 -0700 | [diff] [blame] | 509 | private static class FixedSizeRemoteViewsCache { |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 510 | private static final String TAG = "FixedSizeRemoteViewsCache"; |
| 511 | |
| 512 | // The meta data related to all the RemoteViews, ie. count, is stable, etc. |
Adam Cohen | 4c99498 | 2012-04-02 13:30:32 -0700 | [diff] [blame] | 513 | // The meta data objects are made final so that they can be locked on independently |
| 514 | // of the FixedSizeRemoteViewsCache. If we ever lock on both meta data objects, it is in |
| 515 | // the order mTemporaryMetaData followed by mMetaData. |
| 516 | private final RemoteViewsMetaData mMetaData; |
| 517 | private final RemoteViewsMetaData mTemporaryMetaData; |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 518 | |
| 519 | // The cache/mapping of position to RemoteViewsMetaData. This set is guaranteed to be |
| 520 | // greater than or equal to the set of RemoteViews. |
| 521 | // Note: The reason that we keep this separate from the RemoteViews cache below is that this |
| 522 | // we still need to be able to access the mapping of position to meta data, without keeping |
| 523 | // the heavy RemoteViews around. The RemoteViews cache is trimmed to fixed constraints wrt. |
| 524 | // memory and size, but this metadata cache will retain information until the data at the |
| 525 | // position is guaranteed as not being necessary any more (usually on notifyDataSetChanged). |
| 526 | private HashMap<Integer, RemoteViewsIndexMetaData> mIndexMetaData; |
| 527 | |
| 528 | // The cache of actual RemoteViews, which may be pruned if the cache gets too large, or uses |
| 529 | // too much memory. |
| 530 | private HashMap<Integer, RemoteViews> mIndexRemoteViews; |
| 531 | |
| 532 | // The set of indices that have been explicitly requested by the collection view |
| 533 | private HashSet<Integer> mRequestedIndices; |
| 534 | |
Winson Chung | b90a91c | 2011-01-26 13:36:34 -0800 | [diff] [blame] | 535 | // We keep a reference of the last requested index to determine which item to prune the |
| 536 | // farthest items from when we hit the memory limit |
| 537 | private int mLastRequestedIndex; |
| 538 | |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 539 | // The set of indices to load, including those explicitly requested, as well as those |
| 540 | // determined by the preloading algorithm to be prefetched |
| 541 | private HashSet<Integer> mLoadIndices; |
| 542 | |
| 543 | // The lower and upper bounds of the preloaded range |
| 544 | private int mPreloadLowerBound; |
| 545 | private int mPreloadUpperBound; |
| 546 | |
| 547 | // The bounds of this fixed cache, we will try and fill as many items into the cache up to |
| 548 | // the maxCount number of items, or the maxSize memory usage. |
| 549 | // The maxCountSlack is used to determine if a new position in the cache to be loaded is |
| 550 | // sufficiently ouside the old set, prompting a shifting of the "window" of items to be |
| 551 | // preloaded. |
| 552 | private int mMaxCount; |
| 553 | private int mMaxCountSlack; |
| 554 | private static final float sMaxCountSlackPercent = 0.75f; |
Winson Chung | b90a91c | 2011-01-26 13:36:34 -0800 | [diff] [blame] | 555 | private static final int sMaxMemoryLimitInBytes = 2 * 1024 * 1024; |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 556 | |
| 557 | public FixedSizeRemoteViewsCache(int maxCacheSize) { |
| 558 | mMaxCount = maxCacheSize; |
| 559 | mMaxCountSlack = Math.round(sMaxCountSlackPercent * (mMaxCount / 2)); |
| 560 | mPreloadLowerBound = 0; |
| 561 | mPreloadUpperBound = -1; |
| 562 | mMetaData = new RemoteViewsMetaData(); |
Winson Chung | 16c8d8a | 2011-01-20 16:19:33 -0800 | [diff] [blame] | 563 | mTemporaryMetaData = new RemoteViewsMetaData(); |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 564 | mIndexMetaData = new HashMap<Integer, RemoteViewsIndexMetaData>(); |
| 565 | mIndexRemoteViews = new HashMap<Integer, RemoteViews>(); |
| 566 | mRequestedIndices = new HashSet<Integer>(); |
Winson Chung | b90a91c | 2011-01-26 13:36:34 -0800 | [diff] [blame] | 567 | mLastRequestedIndex = -1; |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 568 | mLoadIndices = new HashSet<Integer>(); |
| 569 | } |
| 570 | |
Adam Cohen | 591ff97 | 2012-07-24 22:46:11 -0700 | [diff] [blame] | 571 | public void insert(int position, RemoteViews v, long itemId, |
| 572 | ArrayList<Integer> visibleWindow) { |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 573 | // Trim the cache if we go beyond the count |
| 574 | if (mIndexRemoteViews.size() >= mMaxCount) { |
Adam Cohen | 591ff97 | 2012-07-24 22:46:11 -0700 | [diff] [blame] | 575 | mIndexRemoteViews.remove(getFarthestPositionFrom(position, visibleWindow)); |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 576 | } |
| 577 | |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 578 | // Trim the cache if we go beyond the available memory size constraints |
Winson Chung | b90a91c | 2011-01-26 13:36:34 -0800 | [diff] [blame] | 579 | int pruneFromPosition = (mLastRequestedIndex > -1) ? mLastRequestedIndex : position; |
| 580 | while (getRemoteViewsBitmapMemoryUsage() >= sMaxMemoryLimitInBytes) { |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 581 | // Note: This is currently the most naive mechanism for deciding what to prune when |
| 582 | // we hit the memory limit. In the future, we may want to calculate which index to |
| 583 | // remove based on both its position as well as it's current memory usage, as well |
| 584 | // as whether it was directly requested vs. whether it was preloaded by our caching |
| 585 | // mechanism. |
Adam Cohen | 591ff97 | 2012-07-24 22:46:11 -0700 | [diff] [blame] | 586 | mIndexRemoteViews.remove(getFarthestPositionFrom(pruneFromPosition, visibleWindow)); |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 587 | } |
| 588 | |
| 589 | // Update the metadata cache |
| 590 | if (mIndexMetaData.containsKey(position)) { |
| 591 | final RemoteViewsIndexMetaData metaData = mIndexMetaData.get(position); |
Adam Cohen | 591ff97 | 2012-07-24 22:46:11 -0700 | [diff] [blame] | 592 | metaData.set(v, itemId); |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 593 | } else { |
Adam Cohen | 591ff97 | 2012-07-24 22:46:11 -0700 | [diff] [blame] | 594 | mIndexMetaData.put(position, new RemoteViewsIndexMetaData(v, itemId)); |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 595 | } |
| 596 | mIndexRemoteViews.put(position, v); |
| 597 | } |
| 598 | |
| 599 | public RemoteViewsMetaData getMetaData() { |
| 600 | return mMetaData; |
| 601 | } |
Winson Chung | 16c8d8a | 2011-01-20 16:19:33 -0800 | [diff] [blame] | 602 | public RemoteViewsMetaData getTemporaryMetaData() { |
| 603 | return mTemporaryMetaData; |
| 604 | } |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 605 | public RemoteViews getRemoteViewsAt(int position) { |
| 606 | if (mIndexRemoteViews.containsKey(position)) { |
| 607 | return mIndexRemoteViews.get(position); |
| 608 | } |
| 609 | return null; |
| 610 | } |
| 611 | public RemoteViewsIndexMetaData getMetaDataAt(int position) { |
| 612 | if (mIndexMetaData.containsKey(position)) { |
| 613 | return mIndexMetaData.get(position); |
| 614 | } |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 615 | return null; |
| 616 | } |
| 617 | |
Winson Chung | 16c8d8a | 2011-01-20 16:19:33 -0800 | [diff] [blame] | 618 | public void commitTemporaryMetaData() { |
| 619 | synchronized (mTemporaryMetaData) { |
| 620 | synchronized (mMetaData) { |
| 621 | mMetaData.set(mTemporaryMetaData); |
| 622 | } |
| 623 | } |
| 624 | } |
| 625 | |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 626 | private int getRemoteViewsBitmapMemoryUsage() { |
| 627 | // Calculate the memory usage of all the RemoteViews bitmaps being cached |
| 628 | int mem = 0; |
| 629 | for (Integer i : mIndexRemoteViews.keySet()) { |
| 630 | final RemoteViews v = mIndexRemoteViews.get(i); |
Winson Chung | aaffa8b | 2010-10-30 14:04:05 -0700 | [diff] [blame] | 631 | if (v != null) { |
Adam Cohen | 5d20064 | 2012-04-24 10:43:31 -0700 | [diff] [blame] | 632 | mem += v.estimateMemoryUsage(); |
Winson Chung | aaffa8b | 2010-10-30 14:04:05 -0700 | [diff] [blame] | 633 | } |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 634 | } |
| 635 | return mem; |
| 636 | } |
Adam Cohen | 591ff97 | 2012-07-24 22:46:11 -0700 | [diff] [blame] | 637 | |
| 638 | private int getFarthestPositionFrom(int pos, ArrayList<Integer> visibleWindow) { |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 639 | // Find the index farthest away and remove that |
| 640 | int maxDist = 0; |
| 641 | int maxDistIndex = -1; |
Adam Cohen | 591ff97 | 2012-07-24 22:46:11 -0700 | [diff] [blame] | 642 | int maxDistNotVisible = 0; |
| 643 | int maxDistIndexNotVisible = -1; |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 644 | for (int i : mIndexRemoteViews.keySet()) { |
| 645 | int dist = Math.abs(i-pos); |
Adam Cohen | 591ff97 | 2012-07-24 22:46:11 -0700 | [diff] [blame] | 646 | if (dist > maxDistNotVisible && !visibleWindow.contains(i)) { |
| 647 | // maxDistNotVisible/maxDistIndexNotVisible will store the index of the |
| 648 | // farthest non-visible position |
| 649 | maxDistIndexNotVisible = i; |
| 650 | maxDistNotVisible = dist; |
Winson Chung | b90a91c | 2011-01-26 13:36:34 -0800 | [diff] [blame] | 651 | } |
Adam Cohen | 35fbe2a | 2012-05-22 14:10:14 -0700 | [diff] [blame] | 652 | if (dist >= maxDist) { |
Winson Chung | b90a91c | 2011-01-26 13:36:34 -0800 | [diff] [blame] | 653 | // maxDist/maxDistIndex will store the index of the farthest position |
Adam Cohen | 591ff97 | 2012-07-24 22:46:11 -0700 | [diff] [blame] | 654 | // regardless of whether it is visible or not |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 655 | maxDistIndex = i; |
| 656 | maxDist = dist; |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 657 | } |
| 658 | } |
Adam Cohen | 591ff97 | 2012-07-24 22:46:11 -0700 | [diff] [blame] | 659 | if (maxDistIndexNotVisible > -1) { |
| 660 | return maxDistIndexNotVisible; |
Winson Chung | b90a91c | 2011-01-26 13:36:34 -0800 | [diff] [blame] | 661 | } |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 662 | return maxDistIndex; |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 663 | } |
| 664 | |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 665 | public void queueRequestedPositionToLoad(int position) { |
Winson Chung | b90a91c | 2011-01-26 13:36:34 -0800 | [diff] [blame] | 666 | mLastRequestedIndex = position; |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 667 | synchronized (mLoadIndices) { |
| 668 | mRequestedIndices.add(position); |
| 669 | mLoadIndices.add(position); |
| 670 | } |
| 671 | } |
Winson Chung | 16c8d8a | 2011-01-20 16:19:33 -0800 | [diff] [blame] | 672 | public boolean queuePositionsToBePreloadedFromRequestedPosition(int position) { |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 673 | // Check if we need to preload any items |
| 674 | if (mPreloadLowerBound <= position && position <= mPreloadUpperBound) { |
| 675 | int center = (mPreloadUpperBound + mPreloadLowerBound) / 2; |
| 676 | if (Math.abs(position - center) < mMaxCountSlack) { |
Winson Chung | 16c8d8a | 2011-01-20 16:19:33 -0800 | [diff] [blame] | 677 | return false; |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 678 | } |
| 679 | } |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 680 | |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 681 | int count = 0; |
| 682 | synchronized (mMetaData) { |
| 683 | count = mMetaData.count; |
Winson Chung | c6d6d4a | 2010-07-22 13:54:27 -0700 | [diff] [blame] | 684 | } |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 685 | synchronized (mLoadIndices) { |
| 686 | mLoadIndices.clear(); |
Winson Chung | c6d6d4a | 2010-07-22 13:54:27 -0700 | [diff] [blame] | 687 | |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 688 | // Add all the requested indices |
| 689 | mLoadIndices.addAll(mRequestedIndices); |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 690 | |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 691 | // Add all the preload indices |
| 692 | int halfMaxCount = mMaxCount / 2; |
| 693 | mPreloadLowerBound = position - halfMaxCount; |
| 694 | mPreloadUpperBound = position + halfMaxCount; |
| 695 | int effectiveLowerBound = Math.max(0, mPreloadLowerBound); |
| 696 | int effectiveUpperBound = Math.min(mPreloadUpperBound, count - 1); |
| 697 | for (int i = effectiveLowerBound; i <= effectiveUpperBound; ++i) { |
| 698 | mLoadIndices.add(i); |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 699 | } |
| 700 | |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 701 | // But remove all the indices that have already been loaded and are cached |
| 702 | mLoadIndices.removeAll(mIndexRemoteViews.keySet()); |
| 703 | } |
Winson Chung | 16c8d8a | 2011-01-20 16:19:33 -0800 | [diff] [blame] | 704 | return true; |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 705 | } |
Winson Chung | b90a91c | 2011-01-26 13:36:34 -0800 | [diff] [blame] | 706 | /** Returns the next index to load, and whether that index was directly requested or not */ |
| 707 | public int[] getNextIndexToLoad() { |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 708 | // We try and prioritize items that have been requested directly, instead |
| 709 | // of items that are loaded as a result of the caching mechanism |
| 710 | synchronized (mLoadIndices) { |
| 711 | // Prioritize requested indices to be loaded first |
| 712 | if (!mRequestedIndices.isEmpty()) { |
| 713 | Integer i = mRequestedIndices.iterator().next(); |
| 714 | mRequestedIndices.remove(i); |
| 715 | mLoadIndices.remove(i); |
Winson Chung | b90a91c | 2011-01-26 13:36:34 -0800 | [diff] [blame] | 716 | return new int[]{i.intValue(), 1}; |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 717 | } |
| 718 | |
| 719 | // Otherwise, preload other indices as necessary |
| 720 | if (!mLoadIndices.isEmpty()) { |
| 721 | Integer i = mLoadIndices.iterator().next(); |
| 722 | mLoadIndices.remove(i); |
Winson Chung | b90a91c | 2011-01-26 13:36:34 -0800 | [diff] [blame] | 723 | return new int[]{i.intValue(), 0}; |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 724 | } |
| 725 | |
Winson Chung | b90a91c | 2011-01-26 13:36:34 -0800 | [diff] [blame] | 726 | return new int[]{-1, 0}; |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 727 | } |
| 728 | } |
| 729 | |
| 730 | public boolean containsRemoteViewAt(int position) { |
| 731 | return mIndexRemoteViews.containsKey(position); |
| 732 | } |
| 733 | public boolean containsMetaDataAt(int position) { |
| 734 | return mIndexMetaData.containsKey(position); |
| 735 | } |
| 736 | |
| 737 | public void reset() { |
Winson Chung | 61ac7e3 | 2010-09-28 10:08:31 -0700 | [diff] [blame] | 738 | // Note: We do not try and reset the meta data, since that information is still used by |
| 739 | // collection views to validate it's own contents (and will be re-requested if the data |
| 740 | // is invalidated through the notifyDataSetChanged() flow). |
| 741 | |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 742 | mPreloadLowerBound = 0; |
| 743 | mPreloadUpperBound = -1; |
Winson Chung | b90a91c | 2011-01-26 13:36:34 -0800 | [diff] [blame] | 744 | mLastRequestedIndex = -1; |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 745 | mIndexRemoteViews.clear(); |
| 746 | mIndexMetaData.clear(); |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 747 | synchronized (mLoadIndices) { |
| 748 | mRequestedIndices.clear(); |
| 749 | mLoadIndices.clear(); |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 750 | } |
| 751 | } |
| 752 | } |
| 753 | |
| 754 | public RemoteViewsAdapter(Context context, Intent intent, RemoteAdapterConnectionCallback callback) { |
| 755 | mContext = context; |
| 756 | mIntent = intent; |
Winson Chung | 81f39eb | 2011-01-11 18:05:01 -0800 | [diff] [blame] | 757 | mAppWidgetId = intent.getIntExtra(RemoteViews.EXTRA_REMOTEADAPTER_APPWIDGET_ID, -1); |
Winson Chung | a5f6f80 | 2010-09-29 09:24:21 -0700 | [diff] [blame] | 758 | mLayoutInflater = LayoutInflater.from(context); |
Winson Chung | 9b3a2cf | 2010-09-16 14:45:32 -0700 | [diff] [blame] | 759 | if (mIntent == null) { |
| 760 | throw new IllegalArgumentException("Non-null Intent must be specified."); |
| 761 | } |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 762 | mRequestedViews = new RemoteViewsFrameLayoutRefSet(); |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 763 | |
Winson Chung | 81f39eb | 2011-01-11 18:05:01 -0800 | [diff] [blame] | 764 | // Strip the previously injected app widget id from service intent |
| 765 | if (intent.hasExtra(RemoteViews.EXTRA_REMOTEADAPTER_APPWIDGET_ID)) { |
| 766 | intent.removeExtra(RemoteViews.EXTRA_REMOTEADAPTER_APPWIDGET_ID); |
| 767 | } |
| 768 | |
| 769 | // Initialize the worker thread |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 770 | mWorkerThread = new HandlerThread("RemoteViewsCache-loader"); |
| 771 | mWorkerThread.start(); |
| 772 | mWorkerQueue = new Handler(mWorkerThread.getLooper()); |
Winson Chung | 81f39eb | 2011-01-11 18:05:01 -0800 | [diff] [blame] | 773 | mMainQueue = new Handler(Looper.myLooper(), this); |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 774 | |
Adam Cohen | 335c3b6 | 2012-07-24 17:18:16 -0700 | [diff] [blame] | 775 | if (sCacheRemovalThread == null) { |
| 776 | sCacheRemovalThread = new HandlerThread("RemoteViewsAdapter-cachePruner"); |
| 777 | sCacheRemovalThread.start(); |
| 778 | sCacheRemovalQueue = new Handler(sCacheRemovalThread.getLooper()); |
| 779 | } |
| 780 | |
Winson Chung | 81f39eb | 2011-01-11 18:05:01 -0800 | [diff] [blame] | 781 | // Initialize the cache and the service connection on startup |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 782 | mCallback = new WeakReference<RemoteAdapterConnectionCallback>(callback); |
| 783 | mServiceConnection = new RemoteViewsAdapterServiceConnection(this); |
Adam Cohen | 335c3b6 | 2012-07-24 17:18:16 -0700 | [diff] [blame] | 784 | |
| 785 | Pair<Intent.FilterComparison, Integer> key = new Pair<Intent.FilterComparison, Integer> |
| 786 | (new Intent.FilterComparison(mIntent), mAppWidgetId); |
| 787 | |
| 788 | synchronized(sCachedRemoteViewsCaches) { |
| 789 | if (sCachedRemoteViewsCaches.containsKey(key)) { |
Adam Cohen | 4a9df8d | 2012-07-27 15:35:13 -0700 | [diff] [blame] | 790 | mCache = sCachedRemoteViewsCaches.get(key); |
| 791 | synchronized (mCache.mMetaData) { |
| 792 | if (mCache.mMetaData.count > 0) { |
| 793 | // As a precautionary measure, we verify that the meta data indicates a |
| 794 | // non-zero count before declaring that data is ready. |
| 795 | mDataReady = true; |
| 796 | } |
| 797 | } |
Adam Cohen | 335c3b6 | 2012-07-24 17:18:16 -0700 | [diff] [blame] | 798 | } else { |
| 799 | mCache = new FixedSizeRemoteViewsCache(sDefaultCacheSize); |
Adam Cohen | 4a9df8d | 2012-07-27 15:35:13 -0700 | [diff] [blame] | 800 | } |
| 801 | if (!mDataReady) { |
Adam Cohen | 335c3b6 | 2012-07-24 17:18:16 -0700 | [diff] [blame] | 802 | requestBindService(); |
| 803 | } |
| 804 | } |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 805 | } |
| 806 | |
Jeff Brown | fc442bd | 2011-06-10 21:34:48 -0700 | [diff] [blame] | 807 | @Override |
| 808 | protected void finalize() throws Throwable { |
| 809 | try { |
| 810 | if (mWorkerThread != null) { |
| 811 | mWorkerThread.quit(); |
| 812 | } |
| 813 | } finally { |
| 814 | super.finalize(); |
| 815 | } |
| 816 | } |
| 817 | |
Adam Cohen | 335c3b6 | 2012-07-24 17:18:16 -0700 | [diff] [blame] | 818 | public boolean isDataReady() { |
| 819 | return mDataReady; |
| 820 | } |
| 821 | |
Adam Cohen | a6a4cbc | 2012-09-26 17:36:40 -0700 | [diff] [blame] | 822 | public void setRemoteViewsOnClickHandler(OnClickHandler handler) { |
| 823 | mRemoteViewsOnClickHandler = handler; |
| 824 | } |
| 825 | |
Adam Cohen | 335c3b6 | 2012-07-24 17:18:16 -0700 | [diff] [blame] | 826 | public void saveRemoteViewsCache() { |
| 827 | final Pair<Intent.FilterComparison, Integer> key = new Pair<Intent.FilterComparison, |
| 828 | Integer> (new Intent.FilterComparison(mIntent), mAppWidgetId); |
| 829 | |
| 830 | synchronized(sCachedRemoteViewsCaches) { |
| 831 | // If we already have a remove runnable posted for this key, remove it. |
| 832 | if (sRemoteViewsCacheRemoveRunnables.containsKey(key)) { |
| 833 | sCacheRemovalQueue.removeCallbacks(sRemoteViewsCacheRemoveRunnables.get(key)); |
| 834 | sRemoteViewsCacheRemoveRunnables.remove(key); |
| 835 | } |
| 836 | |
Adam Cohen | 4a9df8d | 2012-07-27 15:35:13 -0700 | [diff] [blame] | 837 | int metaDataCount = 0; |
| 838 | int numRemoteViewsCached = 0; |
| 839 | synchronized (mCache.mMetaData) { |
| 840 | metaDataCount = mCache.mMetaData.count; |
Adam Cohen | 335c3b6 | 2012-07-24 17:18:16 -0700 | [diff] [blame] | 841 | } |
Adam Cohen | 4a9df8d | 2012-07-27 15:35:13 -0700 | [diff] [blame] | 842 | synchronized (mCache) { |
| 843 | numRemoteViewsCached = mCache.mIndexRemoteViews.size(); |
| 844 | } |
| 845 | if (metaDataCount > 0 && numRemoteViewsCached > 0) { |
| 846 | sCachedRemoteViewsCaches.put(key, mCache); |
| 847 | } |
| 848 | |
Adam Cohen | 335c3b6 | 2012-07-24 17:18:16 -0700 | [diff] [blame] | 849 | Runnable r = new Runnable() { |
| 850 | @Override |
| 851 | public void run() { |
| 852 | synchronized (sCachedRemoteViewsCaches) { |
| 853 | if (sCachedRemoteViewsCaches.containsKey(key)) { |
| 854 | sCachedRemoteViewsCaches.remove(key); |
| 855 | } |
| 856 | if (sRemoteViewsCacheRemoveRunnables.containsKey(key)) { |
| 857 | sRemoteViewsCacheRemoveRunnables.remove(key); |
| 858 | } |
| 859 | } |
| 860 | } |
| 861 | }; |
| 862 | sRemoteViewsCacheRemoveRunnables.put(key, r); |
| 863 | sCacheRemovalQueue.postDelayed(r, REMOTE_VIEWS_CACHE_DURATION); |
| 864 | } |
| 865 | } |
| 866 | |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 867 | private void loadNextIndexInBackground() { |
| 868 | mWorkerQueue.post(new Runnable() { |
| 869 | @Override |
| 870 | public void run() { |
Winson Chung | 16c8d8a | 2011-01-20 16:19:33 -0800 | [diff] [blame] | 871 | if (mServiceConnection.isConnected()) { |
| 872 | // Get the next index to load |
| 873 | int position = -1; |
| 874 | synchronized (mCache) { |
Winson Chung | b90a91c | 2011-01-26 13:36:34 -0800 | [diff] [blame] | 875 | int[] res = mCache.getNextIndexToLoad(); |
| 876 | position = res[0]; |
Winson Chung | 16c8d8a | 2011-01-20 16:19:33 -0800 | [diff] [blame] | 877 | } |
| 878 | if (position > -1) { |
| 879 | // Load the item, and notify any existing RemoteViewsFrameLayouts |
Adam Cohen | 591ff97 | 2012-07-24 22:46:11 -0700 | [diff] [blame] | 880 | updateRemoteViews(position, true); |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 881 | |
Winson Chung | 16c8d8a | 2011-01-20 16:19:33 -0800 | [diff] [blame] | 882 | // Queue up for the next one to load |
| 883 | loadNextIndexInBackground(); |
| 884 | } else { |
| 885 | // No more items to load, so queue unbind |
| 886 | enqueueDeferredUnbindServiceMessage(); |
| 887 | } |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 888 | } |
| 889 | } |
| 890 | }); |
| 891 | } |
| 892 | |
Winson Chung | 16c8d8a | 2011-01-20 16:19:33 -0800 | [diff] [blame] | 893 | private void processException(String method, Exception e) { |
| 894 | Log.e("RemoteViewsAdapter", "Error in " + method + ": " + e.getMessage()); |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 895 | |
Winson Chung | 16c8d8a | 2011-01-20 16:19:33 -0800 | [diff] [blame] | 896 | // If we encounter a crash when updating, we should reset the metadata & cache and trigger |
| 897 | // a notifyDataSetChanged to update the widget accordingly |
| 898 | final RemoteViewsMetaData metaData = mCache.getMetaData(); |
| 899 | synchronized (metaData) { |
| 900 | metaData.reset(); |
| 901 | } |
| 902 | synchronized (mCache) { |
| 903 | mCache.reset(); |
| 904 | } |
| 905 | mMainQueue.post(new Runnable() { |
| 906 | @Override |
| 907 | public void run() { |
| 908 | superNotifyDataSetChanged(); |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 909 | } |
Winson Chung | 16c8d8a | 2011-01-20 16:19:33 -0800 | [diff] [blame] | 910 | }); |
| 911 | } |
| 912 | |
| 913 | private void updateTemporaryMetaData() { |
| 914 | IRemoteViewsFactory factory = mServiceConnection.getRemoteViewsFactory(); |
| 915 | |
| 916 | try { |
| 917 | // get the properties/first view (so that we can use it to |
| 918 | // measure our dummy views) |
| 919 | boolean hasStableIds = factory.hasStableIds(); |
| 920 | int viewTypeCount = factory.getViewTypeCount(); |
| 921 | int count = factory.getCount(); |
| 922 | RemoteViews loadingView = factory.getLoadingView(); |
| 923 | RemoteViews firstView = null; |
| 924 | if ((count > 0) && (loadingView == null)) { |
| 925 | firstView = factory.getViewAt(0); |
| 926 | } |
| 927 | final RemoteViewsMetaData tmpMetaData = mCache.getTemporaryMetaData(); |
| 928 | synchronized (tmpMetaData) { |
| 929 | tmpMetaData.hasStableIds = hasStableIds; |
| 930 | // We +1 because the base view type is the loading view |
| 931 | tmpMetaData.viewTypeCount = viewTypeCount + 1; |
| 932 | tmpMetaData.count = count; |
| 933 | tmpMetaData.setLoadingViewTemplates(loadingView, firstView); |
| 934 | } |
Adam Cohen | 2625fea | 2011-03-23 17:24:30 -0700 | [diff] [blame] | 935 | } catch(RemoteException e) { |
Winson Chung | 16c8d8a | 2011-01-20 16:19:33 -0800 | [diff] [blame] | 936 | processException("updateMetaData", e); |
Adam Cohen | fa2e3ff | 2011-04-07 16:48:19 -0700 | [diff] [blame] | 937 | } catch(RuntimeException e) { |
| 938 | processException("updateMetaData", e); |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 939 | } |
| 940 | } |
| 941 | |
Adam Cohen | 591ff97 | 2012-07-24 22:46:11 -0700 | [diff] [blame] | 942 | private void updateRemoteViews(final int position, boolean notifyWhenLoaded) { |
Winson Chung | 16c8d8a | 2011-01-20 16:19:33 -0800 | [diff] [blame] | 943 | IRemoteViewsFactory factory = mServiceConnection.getRemoteViewsFactory(); |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 944 | |
Winson Chung | 16c8d8a | 2011-01-20 16:19:33 -0800 | [diff] [blame] | 945 | // Load the item information from the remote service |
| 946 | RemoteViews remoteViews = null; |
| 947 | long itemId = 0; |
| 948 | try { |
| 949 | remoteViews = factory.getViewAt(position); |
| 950 | itemId = factory.getItemId(position); |
Adam Cohen | 2625fea | 2011-03-23 17:24:30 -0700 | [diff] [blame] | 951 | } catch (RemoteException e) { |
Winson Chung | 16c8d8a | 2011-01-20 16:19:33 -0800 | [diff] [blame] | 952 | Log.e(TAG, "Error in updateRemoteViews(" + position + "): " + e.getMessage()); |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 953 | |
Winson Chung | 16c8d8a | 2011-01-20 16:19:33 -0800 | [diff] [blame] | 954 | // Return early to prevent additional work in re-centering the view cache, and |
| 955 | // swapping from the loading view |
| 956 | return; |
Adam Cohen | 2625fea | 2011-03-23 17:24:30 -0700 | [diff] [blame] | 957 | } catch (RuntimeException e) { |
| 958 | Log.e(TAG, "Error in updateRemoteViews(" + position + "): " + e.getMessage()); |
| 959 | return; |
Winson Chung | 16c8d8a | 2011-01-20 16:19:33 -0800 | [diff] [blame] | 960 | } |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 961 | |
Winson Chung | 16c8d8a | 2011-01-20 16:19:33 -0800 | [diff] [blame] | 962 | if (remoteViews == null) { |
| 963 | // If a null view was returned, we break early to prevent it from getting |
| 964 | // into our cache and causing problems later. The effect is that the child at this |
| 965 | // position will remain as a loading view until it is updated. |
| 966 | Log.e(TAG, "Error in updateRemoteViews(" + position + "): " + " null RemoteViews " + |
| 967 | "returned from RemoteViewsFactory."); |
| 968 | return; |
| 969 | } |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 970 | |
Adam Cohen | a5a0687 | 2012-07-11 15:23:10 -0700 | [diff] [blame] | 971 | int layoutId = remoteViews.getLayoutId(); |
| 972 | RemoteViewsMetaData metaData = mCache.getMetaData(); |
| 973 | boolean viewTypeInRange; |
Adam Cohen | 591ff97 | 2012-07-24 22:46:11 -0700 | [diff] [blame] | 974 | int cacheCount; |
Adam Cohen | a5a0687 | 2012-07-11 15:23:10 -0700 | [diff] [blame] | 975 | synchronized (metaData) { |
| 976 | viewTypeInRange = metaData.isViewTypeInRange(layoutId); |
Adam Cohen | 591ff97 | 2012-07-24 22:46:11 -0700 | [diff] [blame] | 977 | cacheCount = mCache.mMetaData.count; |
Adam Cohen | a5a0687 | 2012-07-11 15:23:10 -0700 | [diff] [blame] | 978 | } |
| 979 | synchronized (mCache) { |
| 980 | if (viewTypeInRange) { |
Adam Cohen | 591ff97 | 2012-07-24 22:46:11 -0700 | [diff] [blame] | 981 | ArrayList<Integer> visibleWindow = getVisibleWindow(mVisibleWindowLowerBound, |
| 982 | mVisibleWindowUpperBound, cacheCount); |
Adam Cohen | a5a0687 | 2012-07-11 15:23:10 -0700 | [diff] [blame] | 983 | // Cache the RemoteViews we loaded |
Adam Cohen | 591ff97 | 2012-07-24 22:46:11 -0700 | [diff] [blame] | 984 | mCache.insert(position, remoteViews, itemId, visibleWindow); |
Adam Cohen | a5a0687 | 2012-07-11 15:23:10 -0700 | [diff] [blame] | 985 | |
| 986 | // Notify all the views that we have previously returned for this index that |
| 987 | // there is new data for it. |
| 988 | final RemoteViews rv = remoteViews; |
| 989 | if (notifyWhenLoaded) { |
| 990 | mMainQueue.post(new Runnable() { |
| 991 | @Override |
| 992 | public void run() { |
| 993 | mRequestedViews.notifyOnRemoteViewsLoaded(position, rv); |
| 994 | } |
| 995 | }); |
| 996 | } |
| 997 | } else { |
| 998 | // We need to log an error here, as the the view type count specified by the |
| 999 | // factory is less than the number of view types returned. We don't return this |
| 1000 | // view to the AdapterView, as this will cause an exception in the hosting process, |
| 1001 | // which contains the associated AdapterView. |
| 1002 | Log.e(TAG, "Error: widget's RemoteViewsFactory returns more view types than " + |
| 1003 | " indicated by getViewTypeCount() "); |
Adam Cohen | b967392 | 2012-01-05 13:58:47 -0800 | [diff] [blame] | 1004 | } |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 1005 | } |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 1006 | } |
| 1007 | |
Winson Chung | 9b3a2cf | 2010-09-16 14:45:32 -0700 | [diff] [blame] | 1008 | public Intent getRemoteViewsServiceIntent() { |
| 1009 | return mIntent; |
| 1010 | } |
| 1011 | |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 1012 | public int getCount() { |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 1013 | final RemoteViewsMetaData metaData = mCache.getMetaData(); |
| 1014 | synchronized (metaData) { |
| 1015 | return metaData.count; |
| 1016 | } |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 1017 | } |
| 1018 | |
| 1019 | public Object getItem(int position) { |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 1020 | // Disallow arbitrary object to be associated with an item for the time being |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 1021 | return null; |
| 1022 | } |
| 1023 | |
| 1024 | public long getItemId(int position) { |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 1025 | synchronized (mCache) { |
| 1026 | if (mCache.containsMetaDataAt(position)) { |
| 1027 | return mCache.getMetaDataAt(position).itemId; |
| 1028 | } |
| 1029 | return 0; |
| 1030 | } |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 1031 | } |
| 1032 | |
| 1033 | public int getItemViewType(int position) { |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 1034 | int typeId = 0; |
| 1035 | synchronized (mCache) { |
| 1036 | if (mCache.containsMetaDataAt(position)) { |
| 1037 | typeId = mCache.getMetaDataAt(position).typeId; |
| 1038 | } else { |
| 1039 | return 0; |
| 1040 | } |
| 1041 | } |
| 1042 | |
| 1043 | final RemoteViewsMetaData metaData = mCache.getMetaData(); |
| 1044 | synchronized (metaData) { |
| 1045 | return metaData.getMappedViewType(typeId); |
| 1046 | } |
| 1047 | } |
| 1048 | |
| 1049 | /** |
| 1050 | * Returns the item type id for the specified convert view. Returns -1 if the convert view |
| 1051 | * is invalid. |
| 1052 | */ |
| 1053 | private int getConvertViewTypeId(View convertView) { |
| 1054 | int typeId = -1; |
Adam Cohen | a32edd4 | 2010-10-26 10:35:01 -0700 | [diff] [blame] | 1055 | if (convertView != null) { |
| 1056 | Object tag = convertView.getTag(com.android.internal.R.id.rowTypeId); |
| 1057 | if (tag != null) { |
| 1058 | typeId = (Integer) tag; |
| 1059 | } |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 1060 | } |
| 1061 | return typeId; |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 1062 | } |
| 1063 | |
Adam Cohen | b967392 | 2012-01-05 13:58:47 -0800 | [diff] [blame] | 1064 | /** |
| 1065 | * This method allows an AdapterView using this Adapter to provide information about which |
| 1066 | * views are currently being displayed. This allows for certain optimizations and preloading |
| 1067 | * which wouldn't otherwise be possible. |
| 1068 | */ |
| 1069 | public void setVisibleRangeHint(int lowerBound, int upperBound) { |
| 1070 | mVisibleWindowLowerBound = lowerBound; |
| 1071 | mVisibleWindowUpperBound = upperBound; |
| 1072 | } |
| 1073 | |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 1074 | public View getView(int position, View convertView, ViewGroup parent) { |
Winson Chung | 16c8d8a | 2011-01-20 16:19:33 -0800 | [diff] [blame] | 1075 | // "Request" an index so that we can queue it for loading, initiate subsequent |
| 1076 | // preloading, etc. |
| 1077 | synchronized (mCache) { |
| 1078 | boolean isInCache = mCache.containsRemoteViewAt(position); |
| 1079 | boolean isConnected = mServiceConnection.isConnected(); |
| 1080 | boolean hasNewItems = false; |
| 1081 | |
Winson Chung | 7ab73e7 | 2011-03-04 15:47:23 -0800 | [diff] [blame] | 1082 | if (!isInCache && !isConnected) { |
Winson Chung | 16c8d8a | 2011-01-20 16:19:33 -0800 | [diff] [blame] | 1083 | // Requesting bind service will trigger a super.notifyDataSetChanged(), which will |
| 1084 | // in turn trigger another request to getView() |
| 1085 | requestBindService(); |
| 1086 | } else { |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 1087 | // Queue up other indices to be preloaded based on this position |
Winson Chung | 16c8d8a | 2011-01-20 16:19:33 -0800 | [diff] [blame] | 1088 | hasNewItems = mCache.queuePositionsToBePreloadedFromRequestedPosition(position); |
| 1089 | } |
| 1090 | |
| 1091 | if (isInCache) { |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 1092 | View convertViewChild = null; |
| 1093 | int convertViewTypeId = 0; |
Adam Cohen | 181d2e3 | 2011-01-17 12:40:29 -0800 | [diff] [blame] | 1094 | RemoteViewsFrameLayout layout = null; |
| 1095 | |
| 1096 | if (convertView instanceof RemoteViewsFrameLayout) { |
| 1097 | layout = (RemoteViewsFrameLayout) convertView; |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 1098 | convertViewChild = layout.getChildAt(0); |
| 1099 | convertViewTypeId = getConvertViewTypeId(convertViewChild); |
| 1100 | } |
| 1101 | |
| 1102 | // Second, we try and retrieve the RemoteViews from the cache, returning a loading |
| 1103 | // view and queueing it to be loaded if it has not already been loaded. |
Winson Chung | 16c8d8a | 2011-01-20 16:19:33 -0800 | [diff] [blame] | 1104 | Context context = parent.getContext(); |
| 1105 | RemoteViews rv = mCache.getRemoteViewsAt(position); |
Adam Cohen | aeb66ca | 2011-02-10 16:08:12 -0800 | [diff] [blame] | 1106 | RemoteViewsIndexMetaData indexMetaData = mCache.getMetaDataAt(position); |
Adam Cohen | aeb66ca | 2011-02-10 16:08:12 -0800 | [diff] [blame] | 1107 | int typeId = indexMetaData.typeId; |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 1108 | |
Adam Cohen | b7ffea6 | 2011-07-14 14:45:07 -0700 | [diff] [blame] | 1109 | try { |
| 1110 | // Reuse the convert view where possible |
| 1111 | if (layout != null) { |
| 1112 | if (convertViewTypeId == typeId) { |
Adam Cohen | a6a4cbc | 2012-09-26 17:36:40 -0700 | [diff] [blame] | 1113 | rv.reapply(context, convertViewChild, mRemoteViewsOnClickHandler); |
Adam Cohen | b7ffea6 | 2011-07-14 14:45:07 -0700 | [diff] [blame] | 1114 | return layout; |
| 1115 | } |
| 1116 | layout.removeAllViews(); |
| 1117 | } else { |
| 1118 | layout = new RemoteViewsFrameLayout(context); |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 1119 | } |
Adam Cohen | b7ffea6 | 2011-07-14 14:45:07 -0700 | [diff] [blame] | 1120 | |
| 1121 | // Otherwise, create a new view to be returned |
Adam Cohen | a6a4cbc | 2012-09-26 17:36:40 -0700 | [diff] [blame] | 1122 | View newView = rv.apply(context, parent, mRemoteViewsOnClickHandler); |
Adam Cohen | b7ffea6 | 2011-07-14 14:45:07 -0700 | [diff] [blame] | 1123 | newView.setTagInternal(com.android.internal.R.id.rowTypeId, |
| 1124 | new Integer(typeId)); |
| 1125 | layout.addView(newView); |
| 1126 | return layout; |
| 1127 | |
| 1128 | } catch (Exception e){ |
| 1129 | // We have to make sure that we successfully inflated the RemoteViews, if not |
| 1130 | // we return the loading view instead. |
| 1131 | Log.w(TAG, "Error inflating RemoteViews at position: " + position + ", using" + |
| 1132 | "loading view instead" + e); |
| 1133 | |
| 1134 | RemoteViewsFrameLayout loadingView = null; |
| 1135 | final RemoteViewsMetaData metaData = mCache.getMetaData(); |
| 1136 | synchronized (metaData) { |
Adam Cohen | a5a0687 | 2012-07-11 15:23:10 -0700 | [diff] [blame] | 1137 | loadingView = metaData.createLoadingView(position, convertView, parent, |
Adam Cohen | a6a4cbc | 2012-09-26 17:36:40 -0700 | [diff] [blame] | 1138 | mCache, mLayoutInflater, mRemoteViewsOnClickHandler); |
Adam Cohen | b7ffea6 | 2011-07-14 14:45:07 -0700 | [diff] [blame] | 1139 | } |
| 1140 | return loadingView; |
| 1141 | } finally { |
| 1142 | if (hasNewItems) loadNextIndexInBackground(); |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 1143 | } |
Winson Chung | 16c8d8a | 2011-01-20 16:19:33 -0800 | [diff] [blame] | 1144 | } else { |
| 1145 | // If the cache does not have the RemoteViews at this position, then create a |
| 1146 | // loading view and queue the actual position to be loaded in the background |
| 1147 | RemoteViewsFrameLayout loadingView = null; |
| 1148 | final RemoteViewsMetaData metaData = mCache.getMetaData(); |
| 1149 | synchronized (metaData) { |
Adam Cohen | a5a0687 | 2012-07-11 15:23:10 -0700 | [diff] [blame] | 1150 | loadingView = metaData.createLoadingView(position, convertView, parent, |
Adam Cohen | a6a4cbc | 2012-09-26 17:36:40 -0700 | [diff] [blame] | 1151 | mCache, mLayoutInflater, mRemoteViewsOnClickHandler); |
Winson Chung | 16c8d8a | 2011-01-20 16:19:33 -0800 | [diff] [blame] | 1152 | } |
| 1153 | |
| 1154 | mRequestedViews.add(position, loadingView); |
| 1155 | mCache.queueRequestedPositionToLoad(position); |
| 1156 | loadNextIndexInBackground(); |
| 1157 | |
| 1158 | return loadingView; |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 1159 | } |
| 1160 | } |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 1161 | } |
| 1162 | |
| 1163 | public int getViewTypeCount() { |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 1164 | final RemoteViewsMetaData metaData = mCache.getMetaData(); |
| 1165 | synchronized (metaData) { |
| 1166 | return metaData.viewTypeCount; |
| 1167 | } |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 1168 | } |
| 1169 | |
| 1170 | public boolean hasStableIds() { |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 1171 | final RemoteViewsMetaData metaData = mCache.getMetaData(); |
| 1172 | synchronized (metaData) { |
| 1173 | return metaData.hasStableIds; |
| 1174 | } |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 1175 | } |
| 1176 | |
| 1177 | public boolean isEmpty() { |
| 1178 | return getCount() <= 0; |
| 1179 | } |
| 1180 | |
Winson Chung | 16c8d8a | 2011-01-20 16:19:33 -0800 | [diff] [blame] | 1181 | private void onNotifyDataSetChanged() { |
| 1182 | // Complete the actual notifyDataSetChanged() call initiated earlier |
| 1183 | IRemoteViewsFactory factory = mServiceConnection.getRemoteViewsFactory(); |
| 1184 | try { |
| 1185 | factory.onDataSetChanged(); |
Adam Cohen | 2625fea | 2011-03-23 17:24:30 -0700 | [diff] [blame] | 1186 | } catch (RemoteException e) { |
Winson Chung | 16c8d8a | 2011-01-20 16:19:33 -0800 | [diff] [blame] | 1187 | Log.e(TAG, "Error in updateNotifyDataSetChanged(): " + e.getMessage()); |
| 1188 | |
| 1189 | // Return early to prevent from further being notified (since nothing has |
| 1190 | // changed) |
| 1191 | return; |
Adam Cohen | 2625fea | 2011-03-23 17:24:30 -0700 | [diff] [blame] | 1192 | } catch (RuntimeException e) { |
| 1193 | Log.e(TAG, "Error in updateNotifyDataSetChanged(): " + e.getMessage()); |
| 1194 | return; |
Winson Chung | 16c8d8a | 2011-01-20 16:19:33 -0800 | [diff] [blame] | 1195 | } |
| 1196 | |
| 1197 | // Flush the cache so that we can reload new items from the service |
| 1198 | synchronized (mCache) { |
| 1199 | mCache.reset(); |
| 1200 | } |
| 1201 | |
| 1202 | // Re-request the new metadata (only after the notification to the factory) |
| 1203 | updateTemporaryMetaData(); |
Adam Cohen | 4c99498 | 2012-04-02 13:30:32 -0700 | [diff] [blame] | 1204 | int newCount; |
Adam Cohen | 591ff97 | 2012-07-24 22:46:11 -0700 | [diff] [blame] | 1205 | ArrayList<Integer> visibleWindow; |
Adam Cohen | 4c99498 | 2012-04-02 13:30:32 -0700 | [diff] [blame] | 1206 | synchronized(mCache.getTemporaryMetaData()) { |
| 1207 | newCount = mCache.getTemporaryMetaData().count; |
Adam Cohen | 591ff97 | 2012-07-24 22:46:11 -0700 | [diff] [blame] | 1208 | visibleWindow = getVisibleWindow(mVisibleWindowLowerBound, |
| 1209 | mVisibleWindowUpperBound, newCount); |
Adam Cohen | 4c99498 | 2012-04-02 13:30:32 -0700 | [diff] [blame] | 1210 | } |
Winson Chung | 16c8d8a | 2011-01-20 16:19:33 -0800 | [diff] [blame] | 1211 | |
Adam Cohen | b967392 | 2012-01-05 13:58:47 -0800 | [diff] [blame] | 1212 | // Pre-load (our best guess of) the views which are currently visible in the AdapterView. |
| 1213 | // This mitigates flashing and flickering of loading views when a widget notifies that |
| 1214 | // its data has changed. |
Adam Cohen | 591ff97 | 2012-07-24 22:46:11 -0700 | [diff] [blame] | 1215 | for (int i: visibleWindow) { |
Adam Cohen | 4c99498 | 2012-04-02 13:30:32 -0700 | [diff] [blame] | 1216 | // Because temporary meta data is only ever modified from this thread (ie. |
| 1217 | // mWorkerThread), it is safe to assume that count is a valid representation. |
| 1218 | if (i < newCount) { |
Adam Cohen | 591ff97 | 2012-07-24 22:46:11 -0700 | [diff] [blame] | 1219 | updateRemoteViews(i, false); |
Adam Cohen | 4c99498 | 2012-04-02 13:30:32 -0700 | [diff] [blame] | 1220 | } |
Adam Cohen | b967392 | 2012-01-05 13:58:47 -0800 | [diff] [blame] | 1221 | } |
| 1222 | |
Winson Chung | 16c8d8a | 2011-01-20 16:19:33 -0800 | [diff] [blame] | 1223 | // Propagate the notification back to the base adapter |
| 1224 | mMainQueue.post(new Runnable() { |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 1225 | @Override |
| 1226 | public void run() { |
Winson Chung | 6364f2b | 2010-09-29 11:14:30 -0700 | [diff] [blame] | 1227 | synchronized (mCache) { |
Winson Chung | 16c8d8a | 2011-01-20 16:19:33 -0800 | [diff] [blame] | 1228 | mCache.commitTemporaryMetaData(); |
Winson Chung | 6364f2b | 2010-09-29 11:14:30 -0700 | [diff] [blame] | 1229 | } |
| 1230 | |
Winson Chung | 16c8d8a | 2011-01-20 16:19:33 -0800 | [diff] [blame] | 1231 | superNotifyDataSetChanged(); |
| 1232 | enqueueDeferredUnbindServiceMessage(); |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 1233 | } |
| 1234 | }); |
Winson Chung | 6364f2b | 2010-09-29 11:14:30 -0700 | [diff] [blame] | 1235 | |
Winson Chung | 16c8d8a | 2011-01-20 16:19:33 -0800 | [diff] [blame] | 1236 | // Reset the notify flagflag |
| 1237 | mNotifyDataSetChangedAfterOnServiceConnected = false; |
| 1238 | } |
| 1239 | |
Adam Cohen | 591ff97 | 2012-07-24 22:46:11 -0700 | [diff] [blame] | 1240 | private ArrayList<Integer> getVisibleWindow(int lower, int upper, int count) { |
| 1241 | ArrayList<Integer> window = new ArrayList<Integer>(); |
Adam Cohen | 4a9df8d | 2012-07-27 15:35:13 -0700 | [diff] [blame] | 1242 | |
| 1243 | // In the case that the window is invalid or uninitialized, return an empty window. |
| 1244 | if ((lower == 0 && upper == 0) || lower < 0 || upper < 0) { |
| 1245 | return window; |
| 1246 | } |
| 1247 | |
Adam Cohen | 591ff97 | 2012-07-24 22:46:11 -0700 | [diff] [blame] | 1248 | if (lower <= upper) { |
| 1249 | for (int i = lower; i <= upper; i++){ |
| 1250 | window.add(i); |
| 1251 | } |
| 1252 | } else { |
| 1253 | // If the upper bound is less than the lower bound it means that the visible window |
| 1254 | // wraps around. |
| 1255 | for (int i = lower; i < count; i++) { |
| 1256 | window.add(i); |
| 1257 | } |
| 1258 | for (int i = 0; i <= upper; i++) { |
| 1259 | window.add(i); |
| 1260 | } |
| 1261 | } |
| 1262 | return window; |
| 1263 | } |
| 1264 | |
Winson Chung | 16c8d8a | 2011-01-20 16:19:33 -0800 | [diff] [blame] | 1265 | public void notifyDataSetChanged() { |
| 1266 | // Dequeue any unbind messages |
| 1267 | mMainQueue.removeMessages(sUnbindServiceMessageType); |
| 1268 | |
| 1269 | // If we are not connected, queue up the notifyDataSetChanged to be handled when we do |
| 1270 | // connect |
| 1271 | if (!mServiceConnection.isConnected()) { |
| 1272 | if (mNotifyDataSetChangedAfterOnServiceConnected) { |
| 1273 | return; |
| 1274 | } |
| 1275 | |
| 1276 | mNotifyDataSetChangedAfterOnServiceConnected = true; |
| 1277 | requestBindService(); |
| 1278 | return; |
| 1279 | } |
| 1280 | |
| 1281 | mWorkerQueue.post(new Runnable() { |
| 1282 | @Override |
| 1283 | public void run() { |
| 1284 | onNotifyDataSetChanged(); |
| 1285 | } |
| 1286 | }); |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 1287 | } |
| 1288 | |
Adam Cohen | fb60386 | 2010-12-17 12:03:17 -0800 | [diff] [blame] | 1289 | void superNotifyDataSetChanged() { |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 1290 | super.notifyDataSetChanged(); |
| 1291 | } |
| 1292 | |
Winson Chung | 81f39eb | 2011-01-11 18:05:01 -0800 | [diff] [blame] | 1293 | @Override |
| 1294 | public boolean handleMessage(Message msg) { |
| 1295 | boolean result = false; |
| 1296 | switch (msg.what) { |
| 1297 | case sUnbindServiceMessageType: |
Winson Chung | 81f39eb | 2011-01-11 18:05:01 -0800 | [diff] [blame] | 1298 | if (mServiceConnection.isConnected()) { |
Winson Chung | 16c8d8a | 2011-01-20 16:19:33 -0800 | [diff] [blame] | 1299 | mServiceConnection.unbind(mContext, mAppWidgetId, mIntent); |
Winson Chung | 81f39eb | 2011-01-11 18:05:01 -0800 | [diff] [blame] | 1300 | } |
| 1301 | result = true; |
| 1302 | break; |
| 1303 | default: |
| 1304 | break; |
| 1305 | } |
| 1306 | return result; |
| 1307 | } |
| 1308 | |
| 1309 | private void enqueueDeferredUnbindServiceMessage() { |
| 1310 | // Remove any existing deferred-unbind messages |
| 1311 | mMainQueue.removeMessages(sUnbindServiceMessageType); |
| 1312 | mMainQueue.sendEmptyMessageDelayed(sUnbindServiceMessageType, sUnbindServiceDelay); |
| 1313 | } |
| 1314 | |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 1315 | private boolean requestBindService() { |
Winson Chung | 81f39eb | 2011-01-11 18:05:01 -0800 | [diff] [blame] | 1316 | // Try binding the service (which will start it if it's not already running) |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 1317 | if (!mServiceConnection.isConnected()) { |
Winson Chung | 16c8d8a | 2011-01-20 16:19:33 -0800 | [diff] [blame] | 1318 | mServiceConnection.bind(mContext, mAppWidgetId, mIntent); |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 1319 | } |
| 1320 | |
Winson Chung | 16c8d8a | 2011-01-20 16:19:33 -0800 | [diff] [blame] | 1321 | // Remove any existing deferred-unbind messages |
| 1322 | mMainQueue.removeMessages(sUnbindServiceMessageType); |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 1323 | return mServiceConnection.isConnected(); |
| 1324 | } |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 1325 | } |