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 | |
| 19 | import java.util.HashMap; |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 20 | |
| 21 | import android.app.Service; |
| 22 | import android.content.Intent; |
| 23 | import android.os.IBinder; |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 24 | |
| 25 | import com.android.internal.widget.IRemoteViewsFactory; |
| 26 | |
| 27 | /** |
| 28 | * The service to be connected to for a remote adapter to request RemoteViews. Users should |
| 29 | * extend the RemoteViewsService to provide the appropriate RemoteViewsFactory's used to |
| 30 | * populate the remote collection view (ListView, GridView, etc). |
| 31 | */ |
| 32 | public abstract class RemoteViewsService extends Service { |
| 33 | |
| 34 | private static final String LOG_TAG = "RemoteViewsService"; |
| 35 | |
Winson Chung | 16c8d8a | 2011-01-20 16:19:33 -0800 | [diff] [blame] | 36 | // Used for reference counting of RemoteViewsFactories |
| 37 | // Because we are now unbinding when we are not using the Service (to allow them to be |
| 38 | // reclaimed), the references to the factories that are created need to be stored and used when |
| 39 | // the service is restarted (in response to user input for example). When the process is |
| 40 | // destroyed, so is this static cache of RemoteViewsFactories. |
Winson Chung | 84bbb02 | 2011-02-21 13:57:45 -0800 | [diff] [blame] | 41 | private static final HashMap<Intent.FilterComparison, RemoteViewsFactory> sRemoteViewFactories = |
Winson Chung | 16c8d8a | 2011-01-20 16:19:33 -0800 | [diff] [blame] | 42 | new HashMap<Intent.FilterComparison, RemoteViewsFactory>(); |
Winson Chung | 84bbb02 | 2011-02-21 13:57:45 -0800 | [diff] [blame] | 43 | private static final Object sLock = new Object(); |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 44 | |
| 45 | /** |
| 46 | * An interface for an adapter between a remote collection view (ListView, GridView, etc) and |
| 47 | * the underlying data for that view. The implementor is responsible for making a RemoteView |
Adam Cohen | 0e2de6d | 2011-01-19 17:16:34 -0800 | [diff] [blame] | 48 | * for each item in the data set. This interface is a thin wrapper around {@link Adapter}. |
Winson Chung | 6394c0e | 2010-08-16 10:14:56 -0700 | [diff] [blame] | 49 | * |
| 50 | * @see android.widget.Adapter |
Winson Chung | 6eceb00d | 2010-08-17 18:15:42 -0700 | [diff] [blame] | 51 | * @see android.appwidget.AppWidgetManager |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 52 | */ |
| 53 | public interface RemoteViewsFactory { |
Winson Chung | 6394c0e | 2010-08-16 10:14:56 -0700 | [diff] [blame] | 54 | /** |
| 55 | * Called when your factory is first constructed. The same factory may be shared across |
| 56 | * multiple RemoteViewAdapters depending on the intent passed. |
| 57 | */ |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 58 | public void onCreate(); |
Adam Cohen | 0e2de6d | 2011-01-19 17:16:34 -0800 | [diff] [blame] | 59 | |
Winson Chung | 6394c0e | 2010-08-16 10:14:56 -0700 | [diff] [blame] | 60 | /** |
Winson Chung | 6eceb00d | 2010-08-17 18:15:42 -0700 | [diff] [blame] | 61 | * Called when notifyDataSetChanged() is triggered on the remote adapter. This allows a |
| 62 | * RemoteViewsFactory to respond to data changes by updating any internal references. |
| 63 | * |
Adam Cohen | 0e2de6d | 2011-01-19 17:16:34 -0800 | [diff] [blame] | 64 | * Note: expensive tasks can be safely performed synchronously within this method. In the |
| 65 | * interim, the old data will be displayed within the widget. |
| 66 | * |
Winson Chung | 6eceb00d | 2010-08-17 18:15:42 -0700 | [diff] [blame] | 67 | * @see android.appwidget.AppWidgetManager#notifyAppWidgetViewDataChanged(int[], int) |
Winson Chung | 6394c0e | 2010-08-16 10:14:56 -0700 | [diff] [blame] | 68 | */ |
| 69 | public void onDataSetChanged(); |
Adam Cohen | 0e2de6d | 2011-01-19 17:16:34 -0800 | [diff] [blame] | 70 | |
Winson Chung | 6394c0e | 2010-08-16 10:14:56 -0700 | [diff] [blame] | 71 | /** |
| 72 | * Called when the last RemoteViewsAdapter that is associated with this factory is |
| 73 | * unbound. |
| 74 | */ |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 75 | public void onDestroy(); |
| 76 | |
Adam Cohen | 0e2de6d | 2011-01-19 17:16:34 -0800 | [diff] [blame] | 77 | /** |
| 78 | * See {@link Adapter#getCount()} |
| 79 | * |
| 80 | * @return Count of items. |
| 81 | */ |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 82 | public int getCount(); |
Adam Cohen | 0e2de6d | 2011-01-19 17:16:34 -0800 | [diff] [blame] | 83 | |
| 84 | /** |
| 85 | * See {@link Adapter#getView(int, android.view.View, android.view.ViewGroup)}. |
| 86 | * |
| 87 | * Note: expensive tasks can be safely performed synchronously within this method, and a |
| 88 | * loading view will be displayed in the interim. See {@link #getLoadingView()}. |
| 89 | * |
| 90 | * @param position The position of the item within the Factory's data set of the item whose |
| 91 | * view we want. |
| 92 | * @return A RemoteViews object corresponding to the data at the specified position. |
| 93 | */ |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 94 | public RemoteViews getViewAt(int position); |
Adam Cohen | 0e2de6d | 2011-01-19 17:16:34 -0800 | [diff] [blame] | 95 | |
| 96 | /** |
| 97 | * This allows for the use of a custom loading view which appears between the time that |
| 98 | * {@link #getViewAt(int)} is called and returns. If null is returned, a default loading |
| 99 | * view will be used. |
| 100 | * |
| 101 | * @return The RemoteViews representing the desired loading view. |
| 102 | */ |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 103 | public RemoteViews getLoadingView(); |
Adam Cohen | 0e2de6d | 2011-01-19 17:16:34 -0800 | [diff] [blame] | 104 | |
| 105 | /** |
| 106 | * See {@link Adapter#getViewTypeCount()}. |
| 107 | * |
| 108 | * @return The number of types of Views that will be returned by this factory. |
| 109 | */ |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 110 | public int getViewTypeCount(); |
Adam Cohen | 0e2de6d | 2011-01-19 17:16:34 -0800 | [diff] [blame] | 111 | |
| 112 | /** |
| 113 | * See {@link Adapter#getItemId(int)}. |
| 114 | * |
| 115 | * @param position The position of the item within the data set whose row id we want. |
| 116 | * @return The id of the item at the specified position. |
| 117 | */ |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 118 | public long getItemId(int position); |
Adam Cohen | 0e2de6d | 2011-01-19 17:16:34 -0800 | [diff] [blame] | 119 | |
| 120 | /** |
| 121 | * See {@link Adapter#hasStableIds()}. |
| 122 | * |
| 123 | * @return True if the same id always refers to the same object. |
| 124 | */ |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 125 | public boolean hasStableIds(); |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * A private proxy class for the private IRemoteViewsFactory interface through the |
| 130 | * public RemoteViewsFactory interface. |
| 131 | */ |
Winson Chung | 16c8d8a | 2011-01-20 16:19:33 -0800 | [diff] [blame] | 132 | private static class RemoteViewsFactoryAdapter extends IRemoteViewsFactory.Stub { |
| 133 | public RemoteViewsFactoryAdapter(RemoteViewsFactory factory, boolean isCreated) { |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 134 | mFactory = factory; |
Winson Chung | 16c8d8a | 2011-01-20 16:19:33 -0800 | [diff] [blame] | 135 | mIsCreated = isCreated; |
| 136 | } |
| 137 | public synchronized boolean isCreated() { |
| 138 | return mIsCreated; |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 139 | } |
Winson Chung | f58a961 | 2010-09-28 17:36:42 -0700 | [diff] [blame] | 140 | public synchronized void onDataSetChanged() { |
Winson Chung | 6394c0e | 2010-08-16 10:14:56 -0700 | [diff] [blame] | 141 | mFactory.onDataSetChanged(); |
| 142 | } |
Winson Chung | f58a961 | 2010-09-28 17:36:42 -0700 | [diff] [blame] | 143 | public synchronized int getCount() { |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 144 | return mFactory.getCount(); |
| 145 | } |
Winson Chung | f58a961 | 2010-09-28 17:36:42 -0700 | [diff] [blame] | 146 | public synchronized RemoteViews getViewAt(int position) { |
Adam Cohen | ca6fd84 | 2010-09-03 18:10:35 -0700 | [diff] [blame] | 147 | RemoteViews rv = mFactory.getViewAt(position); |
| 148 | rv.setIsWidgetCollectionChild(true); |
| 149 | return rv; |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 150 | } |
Winson Chung | f58a961 | 2010-09-28 17:36:42 -0700 | [diff] [blame] | 151 | public synchronized RemoteViews getLoadingView() { |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 152 | return mFactory.getLoadingView(); |
| 153 | } |
Winson Chung | f58a961 | 2010-09-28 17:36:42 -0700 | [diff] [blame] | 154 | public synchronized int getViewTypeCount() { |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 155 | return mFactory.getViewTypeCount(); |
| 156 | } |
Winson Chung | f58a961 | 2010-09-28 17:36:42 -0700 | [diff] [blame] | 157 | public synchronized long getItemId(int position) { |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 158 | return mFactory.getItemId(position); |
| 159 | } |
Winson Chung | f58a961 | 2010-09-28 17:36:42 -0700 | [diff] [blame] | 160 | public synchronized boolean hasStableIds() { |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 161 | return mFactory.hasStableIds(); |
| 162 | } |
Winson Chung | 84bbb02 | 2011-02-21 13:57:45 -0800 | [diff] [blame] | 163 | public void onDestroy(Intent intent) { |
| 164 | synchronized (sLock) { |
| 165 | Intent.FilterComparison fc = new Intent.FilterComparison(intent); |
| 166 | if (RemoteViewsService.sRemoteViewFactories.containsKey(fc)) { |
| 167 | RemoteViewsFactory factory = RemoteViewsService.sRemoteViewFactories.get(fc); |
| 168 | factory.onDestroy(); |
| 169 | RemoteViewsService.sRemoteViewFactories.remove(fc); |
| 170 | } |
| 171 | } |
| 172 | } |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 173 | |
| 174 | private RemoteViewsFactory mFactory; |
Winson Chung | 16c8d8a | 2011-01-20 16:19:33 -0800 | [diff] [blame] | 175 | private boolean mIsCreated; |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | @Override |
| 179 | public IBinder onBind(Intent intent) { |
Winson Chung | 84bbb02 | 2011-02-21 13:57:45 -0800 | [diff] [blame] | 180 | synchronized (sLock) { |
Winson Chung | 9b3a2cf | 2010-09-16 14:45:32 -0700 | [diff] [blame] | 181 | Intent.FilterComparison fc = new Intent.FilterComparison(intent); |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 182 | RemoteViewsFactory factory = null; |
Winson Chung | 16c8d8a | 2011-01-20 16:19:33 -0800 | [diff] [blame] | 183 | boolean isCreated = false; |
Winson Chung | 84bbb02 | 2011-02-21 13:57:45 -0800 | [diff] [blame] | 184 | if (!sRemoteViewFactories.containsKey(fc)) { |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 185 | factory = onGetViewFactory(intent); |
Winson Chung | 84bbb02 | 2011-02-21 13:57:45 -0800 | [diff] [blame] | 186 | sRemoteViewFactories.put(fc, factory); |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 187 | factory.onCreate(); |
Winson Chung | 16c8d8a | 2011-01-20 16:19:33 -0800 | [diff] [blame] | 188 | isCreated = false; |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 189 | } else { |
Winson Chung | 84bbb02 | 2011-02-21 13:57:45 -0800 | [diff] [blame] | 190 | factory = sRemoteViewFactories.get(fc); |
Winson Chung | 16c8d8a | 2011-01-20 16:19:33 -0800 | [diff] [blame] | 191 | isCreated = true; |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 192 | } |
Winson Chung | 16c8d8a | 2011-01-20 16:19:33 -0800 | [diff] [blame] | 193 | return new RemoteViewsFactoryAdapter(factory, isCreated); |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 194 | } |
| 195 | } |
| 196 | |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 197 | /** |
| 198 | * To be implemented by the derived service to generate appropriate factories for |
| 199 | * the data. |
| 200 | */ |
| 201 | public abstract RemoteViewsFactory onGetViewFactory(Intent intent); |
| 202 | } |