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