blob: 07bd9188dca45ca999f10a568e09c8926cc74a79 [file] [log] [blame]
Winson Chung499cb9f2010-07-16 11:18:17 -07001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.widget;
18
19import java.util.HashMap;
Winson Chung499cb9f2010-07-16 11:18:17 -070020
21import android.app.Service;
22import android.content.Intent;
23import android.os.IBinder;
Winson Chung499cb9f2010-07-16 11:18:17 -070024
25import 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 */
32public abstract class RemoteViewsService extends Service {
33
34 private static final String LOG_TAG = "RemoteViewsService";
35
Winson Chung16c8d8a2011-01-20 16:19:33 -080036 // 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 Chung84bbb022011-02-21 13:57:45 -080041 private static final HashMap<Intent.FilterComparison, RemoteViewsFactory> sRemoteViewFactories =
Winson Chung16c8d8a2011-01-20 16:19:33 -080042 new HashMap<Intent.FilterComparison, RemoteViewsFactory>();
Winson Chung84bbb022011-02-21 13:57:45 -080043 private static final Object sLock = new Object();
Winson Chung499cb9f2010-07-16 11:18:17 -070044
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 Cohen0e2de6d2011-01-19 17:16:34 -080048 * for each item in the data set. This interface is a thin wrapper around {@link Adapter}.
Winson Chung6394c0e2010-08-16 10:14:56 -070049 *
50 * @see android.widget.Adapter
Winson Chung6eceb002010-08-17 18:15:42 -070051 * @see android.appwidget.AppWidgetManager
Winson Chung499cb9f2010-07-16 11:18:17 -070052 */
53 public interface RemoteViewsFactory {
Winson Chung6394c0e2010-08-16 10:14:56 -070054 /**
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 Chung499cb9f2010-07-16 11:18:17 -070058 public void onCreate();
Adam Cohen0e2de6d2011-01-19 17:16:34 -080059
Winson Chung6394c0e2010-08-16 10:14:56 -070060 /**
Winson Chung6eceb002010-08-17 18:15:42 -070061 * 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 Cohen0e2de6d2011-01-19 17:16:34 -080064 * 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 Chung6eceb002010-08-17 18:15:42 -070067 * @see android.appwidget.AppWidgetManager#notifyAppWidgetViewDataChanged(int[], int)
Winson Chung6394c0e2010-08-16 10:14:56 -070068 */
69 public void onDataSetChanged();
Adam Cohen0e2de6d2011-01-19 17:16:34 -080070
Winson Chung6394c0e2010-08-16 10:14:56 -070071 /**
72 * Called when the last RemoteViewsAdapter that is associated with this factory is
73 * unbound.
74 */
Winson Chung499cb9f2010-07-16 11:18:17 -070075 public void onDestroy();
76
Adam Cohen0e2de6d2011-01-19 17:16:34 -080077 /**
78 * See {@link Adapter#getCount()}
79 *
80 * @return Count of items.
81 */
Winson Chung499cb9f2010-07-16 11:18:17 -070082 public int getCount();
Adam Cohen0e2de6d2011-01-19 17:16:34 -080083
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 Chung499cb9f2010-07-16 11:18:17 -070094 public RemoteViews getViewAt(int position);
Adam Cohen0e2de6d2011-01-19 17:16:34 -080095
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 Chung499cb9f2010-07-16 11:18:17 -0700103 public RemoteViews getLoadingView();
Adam Cohen0e2de6d2011-01-19 17:16:34 -0800104
105 /**
106 * See {@link Adapter#getViewTypeCount()}.
107 *
108 * @return The number of types of Views that will be returned by this factory.
109 */
Winson Chung499cb9f2010-07-16 11:18:17 -0700110 public int getViewTypeCount();
Adam Cohen0e2de6d2011-01-19 17:16:34 -0800111
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 Chung499cb9f2010-07-16 11:18:17 -0700118 public long getItemId(int position);
Adam Cohen0e2de6d2011-01-19 17:16:34 -0800119
120 /**
121 * See {@link Adapter#hasStableIds()}.
122 *
123 * @return True if the same id always refers to the same object.
124 */
Winson Chung499cb9f2010-07-16 11:18:17 -0700125 public boolean hasStableIds();
126 }
127
128 /**
129 * A private proxy class for the private IRemoteViewsFactory interface through the
130 * public RemoteViewsFactory interface.
131 */
Winson Chung16c8d8a2011-01-20 16:19:33 -0800132 private static class RemoteViewsFactoryAdapter extends IRemoteViewsFactory.Stub {
133 public RemoteViewsFactoryAdapter(RemoteViewsFactory factory, boolean isCreated) {
Winson Chung499cb9f2010-07-16 11:18:17 -0700134 mFactory = factory;
Winson Chung16c8d8a2011-01-20 16:19:33 -0800135 mIsCreated = isCreated;
136 }
137 public synchronized boolean isCreated() {
138 return mIsCreated;
Winson Chung499cb9f2010-07-16 11:18:17 -0700139 }
Winson Chungf58a9612010-09-28 17:36:42 -0700140 public synchronized void onDataSetChanged() {
Adam Cohen2625fea2011-03-23 17:24:30 -0700141 try {
142 mFactory.onDataSetChanged();
143 } catch (Exception ex) {
144 Thread t = Thread.currentThread();
145 Thread.getDefaultUncaughtExceptionHandler().uncaughtException(t, ex);
146 }
Winson Chung6394c0e2010-08-16 10:14:56 -0700147 }
Winson Chungc3f581b2011-12-05 14:56:29 -0800148 public synchronized void onDataSetChangedAsync() {
149 onDataSetChanged();
150 }
Winson Chungf58a9612010-09-28 17:36:42 -0700151 public synchronized int getCount() {
Adam Cohen2625fea2011-03-23 17:24:30 -0700152 int count = 0;
153 try {
154 count = mFactory.getCount();
155 } catch (Exception ex) {
156 Thread t = Thread.currentThread();
157 Thread.getDefaultUncaughtExceptionHandler().uncaughtException(t, ex);
158 }
159 return count;
Winson Chung499cb9f2010-07-16 11:18:17 -0700160 }
Winson Chungf58a9612010-09-28 17:36:42 -0700161 public synchronized RemoteViews getViewAt(int position) {
Adam Cohen2625fea2011-03-23 17:24:30 -0700162 RemoteViews rv = null;
163 try {
164 rv = mFactory.getViewAt(position);
165 if (rv != null) {
166 rv.setIsWidgetCollectionChild(true);
167 }
168 } catch (Exception ex) {
169 Thread t = Thread.currentThread();
170 Thread.getDefaultUncaughtExceptionHandler().uncaughtException(t, ex);
171 }
Adam Cohenca6fd842010-09-03 18:10:35 -0700172 return rv;
Winson Chung499cb9f2010-07-16 11:18:17 -0700173 }
Winson Chungf58a9612010-09-28 17:36:42 -0700174 public synchronized RemoteViews getLoadingView() {
Adam Cohen2625fea2011-03-23 17:24:30 -0700175 RemoteViews rv = null;
176 try {
177 rv = mFactory.getLoadingView();
178 } catch (Exception ex) {
179 Thread t = Thread.currentThread();
180 Thread.getDefaultUncaughtExceptionHandler().uncaughtException(t, ex);
181 }
182 return rv;
Winson Chung499cb9f2010-07-16 11:18:17 -0700183 }
Winson Chungf58a9612010-09-28 17:36:42 -0700184 public synchronized int getViewTypeCount() {
Adam Cohen2625fea2011-03-23 17:24:30 -0700185 int count = 0;
186 try {
187 count = mFactory.getViewTypeCount();
188 } catch (Exception ex) {
189 Thread t = Thread.currentThread();
190 Thread.getDefaultUncaughtExceptionHandler().uncaughtException(t, ex);
191 }
192 return count;
Winson Chung499cb9f2010-07-16 11:18:17 -0700193 }
Winson Chungf58a9612010-09-28 17:36:42 -0700194 public synchronized long getItemId(int position) {
Adam Cohen2625fea2011-03-23 17:24:30 -0700195 long id = 0;
196 try {
197 id = mFactory.getItemId(position);
198 } catch (Exception ex) {
199 Thread t = Thread.currentThread();
200 Thread.getDefaultUncaughtExceptionHandler().uncaughtException(t, ex);
201 }
202 return id;
Winson Chung499cb9f2010-07-16 11:18:17 -0700203 }
Winson Chungf58a9612010-09-28 17:36:42 -0700204 public synchronized boolean hasStableIds() {
Adam Cohen2625fea2011-03-23 17:24:30 -0700205 boolean hasStableIds = false;
206 try {
207 hasStableIds = mFactory.hasStableIds();
208 } catch (Exception ex) {
209 Thread t = Thread.currentThread();
210 Thread.getDefaultUncaughtExceptionHandler().uncaughtException(t, ex);
211 }
212 return hasStableIds;
Winson Chung499cb9f2010-07-16 11:18:17 -0700213 }
Winson Chung84bbb022011-02-21 13:57:45 -0800214 public void onDestroy(Intent intent) {
215 synchronized (sLock) {
216 Intent.FilterComparison fc = new Intent.FilterComparison(intent);
217 if (RemoteViewsService.sRemoteViewFactories.containsKey(fc)) {
218 RemoteViewsFactory factory = RemoteViewsService.sRemoteViewFactories.get(fc);
Adam Cohen2625fea2011-03-23 17:24:30 -0700219 try {
220 factory.onDestroy();
221 } catch (Exception ex) {
222 Thread t = Thread.currentThread();
223 Thread.getDefaultUncaughtExceptionHandler().uncaughtException(t, ex);
224 }
Winson Chung84bbb022011-02-21 13:57:45 -0800225 RemoteViewsService.sRemoteViewFactories.remove(fc);
226 }
227 }
228 }
Winson Chung499cb9f2010-07-16 11:18:17 -0700229
230 private RemoteViewsFactory mFactory;
Winson Chung16c8d8a2011-01-20 16:19:33 -0800231 private boolean mIsCreated;
Winson Chung499cb9f2010-07-16 11:18:17 -0700232 }
233
234 @Override
235 public IBinder onBind(Intent intent) {
Winson Chung84bbb022011-02-21 13:57:45 -0800236 synchronized (sLock) {
Winson Chung9b3a2cf2010-09-16 14:45:32 -0700237 Intent.FilterComparison fc = new Intent.FilterComparison(intent);
Winson Chung499cb9f2010-07-16 11:18:17 -0700238 RemoteViewsFactory factory = null;
Winson Chung16c8d8a2011-01-20 16:19:33 -0800239 boolean isCreated = false;
Winson Chung84bbb022011-02-21 13:57:45 -0800240 if (!sRemoteViewFactories.containsKey(fc)) {
Winson Chung499cb9f2010-07-16 11:18:17 -0700241 factory = onGetViewFactory(intent);
Winson Chung84bbb022011-02-21 13:57:45 -0800242 sRemoteViewFactories.put(fc, factory);
Winson Chung499cb9f2010-07-16 11:18:17 -0700243 factory.onCreate();
Winson Chung16c8d8a2011-01-20 16:19:33 -0800244 isCreated = false;
Winson Chung499cb9f2010-07-16 11:18:17 -0700245 } else {
Winson Chung84bbb022011-02-21 13:57:45 -0800246 factory = sRemoteViewFactories.get(fc);
Winson Chung16c8d8a2011-01-20 16:19:33 -0800247 isCreated = true;
Winson Chung499cb9f2010-07-16 11:18:17 -0700248 }
Winson Chung16c8d8a2011-01-20 16:19:33 -0800249 return new RemoteViewsFactoryAdapter(factory, isCreated);
Winson Chung499cb9f2010-07-16 11:18:17 -0700250 }
251 }
252
Winson Chung499cb9f2010-07-16 11:18:17 -0700253 /**
254 * To be implemented by the derived service to generate appropriate factories for
255 * the data.
256 */
257 public abstract RemoteViewsFactory onGetViewFactory(Intent intent);
258}