The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
Kenny Root | 15a4d2f | 2010-03-11 18:20:12 -0800 | [diff] [blame] | 2 | * Copyright (C) 2009 The Android Open Source Project |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 17 | package android.appwidget; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 18 | |
| 19 | import android.content.Context; |
| 20 | import android.os.Handler; |
| 21 | import android.os.IBinder; |
| 22 | import android.os.Looper; |
| 23 | import android.os.Message; |
| 24 | import android.os.RemoteException; |
| 25 | import android.os.ServiceManager; |
| 26 | import android.widget.RemoteViews; |
| 27 | |
| 28 | import java.util.ArrayList; |
| 29 | import java.util.HashMap; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 30 | |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 31 | import com.android.internal.appwidget.IAppWidgetHost; |
| 32 | import com.android.internal.appwidget.IAppWidgetService; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 33 | |
| 34 | /** |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 35 | * AppWidgetHost provides the interaction with the AppWidget service for apps, |
| 36 | * like the home screen, that want to embed AppWidgets in their UI. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 37 | */ |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 38 | public class AppWidgetHost { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 39 | |
| 40 | static final int HANDLE_UPDATE = 1; |
| 41 | static final int HANDLE_PROVIDER_CHANGED = 2; |
| 42 | |
Romain Guy | a547559 | 2009-07-01 17:20:08 -0700 | [diff] [blame] | 43 | final static Object sServiceLock = new Object(); |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 44 | static IAppWidgetService sService; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 45 | |
| 46 | Context mContext; |
| 47 | String mPackageName; |
| 48 | |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 49 | class Callbacks extends IAppWidgetHost.Stub { |
| 50 | public void updateAppWidget(int appWidgetId, RemoteViews views) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 51 | Message msg = mHandler.obtainMessage(HANDLE_UPDATE); |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 52 | msg.arg1 = appWidgetId; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 53 | msg.obj = views; |
| 54 | msg.sendToTarget(); |
| 55 | } |
| 56 | |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 57 | public void providerChanged(int appWidgetId, AppWidgetProviderInfo info) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 58 | Message msg = mHandler.obtainMessage(HANDLE_PROVIDER_CHANGED); |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 59 | msg.arg1 = appWidgetId; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 60 | msg.obj = info; |
| 61 | msg.sendToTarget(); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | class UpdateHandler extends Handler { |
| 66 | public UpdateHandler(Looper looper) { |
| 67 | super(looper); |
| 68 | } |
| 69 | |
| 70 | public void handleMessage(Message msg) { |
| 71 | switch (msg.what) { |
| 72 | case HANDLE_UPDATE: { |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 73 | updateAppWidgetView(msg.arg1, (RemoteViews)msg.obj); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 74 | break; |
| 75 | } |
| 76 | case HANDLE_PROVIDER_CHANGED: { |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 77 | onProviderChanged(msg.arg1, (AppWidgetProviderInfo)msg.obj); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 78 | break; |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | Handler mHandler; |
| 85 | |
| 86 | int mHostId; |
| 87 | Callbacks mCallbacks = new Callbacks(); |
Romain Guy | a547559 | 2009-07-01 17:20:08 -0700 | [diff] [blame] | 88 | final HashMap<Integer,AppWidgetHostView> mViews = new HashMap<Integer, AppWidgetHostView>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 89 | |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 90 | public AppWidgetHost(Context context, int hostId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 91 | mContext = context; |
| 92 | mHostId = hostId; |
| 93 | mHandler = new UpdateHandler(context.getMainLooper()); |
| 94 | synchronized (sServiceLock) { |
| 95 | if (sService == null) { |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 96 | IBinder b = ServiceManager.getService(Context.APPWIDGET_SERVICE); |
| 97 | sService = IAppWidgetService.Stub.asInterface(b); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 98 | } |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | /** |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 103 | * Start receiving onAppWidgetChanged calls for your AppWidgets. Call this when your activity |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 104 | * becomes visible, i.e. from onStart() in your Activity. |
| 105 | */ |
| 106 | public void startListening() { |
Romain Guy | a547559 | 2009-07-01 17:20:08 -0700 | [diff] [blame] | 107 | int[] updatedIds; |
| 108 | ArrayList<RemoteViews> updatedViews = new ArrayList<RemoteViews>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 109 | |
| 110 | try { |
| 111 | if (mPackageName == null) { |
| 112 | mPackageName = mContext.getPackageName(); |
| 113 | } |
| 114 | updatedIds = sService.startListening(mCallbacks, mPackageName, mHostId, updatedViews); |
| 115 | } |
| 116 | catch (RemoteException e) { |
| 117 | throw new RuntimeException("system server dead?", e); |
| 118 | } |
| 119 | |
| 120 | final int N = updatedIds.length; |
| 121 | for (int i=0; i<N; i++) { |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 122 | updateAppWidgetView(updatedIds[i], updatedViews.get(i)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 123 | } |
| 124 | } |
| 125 | |
| 126 | /** |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 127 | * Stop receiving onAppWidgetChanged calls for your AppWidgets. Call this when your activity is |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 128 | * no longer visible, i.e. from onStop() in your Activity. |
| 129 | */ |
| 130 | public void stopListening() { |
| 131 | try { |
| 132 | sService.stopListening(mHostId); |
| 133 | } |
| 134 | catch (RemoteException e) { |
| 135 | throw new RuntimeException("system server dead?", e); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | /** |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 140 | * Get a appWidgetId for a host in the calling process. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 141 | * |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 142 | * @return a appWidgetId |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 143 | */ |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 144 | public int allocateAppWidgetId() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 145 | try { |
| 146 | if (mPackageName == null) { |
| 147 | mPackageName = mContext.getPackageName(); |
| 148 | } |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 149 | return sService.allocateAppWidgetId(mPackageName, mHostId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 150 | } |
| 151 | catch (RemoteException e) { |
| 152 | throw new RuntimeException("system server dead?", e); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | /** |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 157 | * Stop listening to changes for this AppWidget. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 158 | */ |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 159 | public void deleteAppWidgetId(int appWidgetId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 160 | synchronized (mViews) { |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 161 | mViews.remove(appWidgetId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 162 | try { |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 163 | sService.deleteAppWidgetId(appWidgetId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 164 | } |
| 165 | catch (RemoteException e) { |
| 166 | throw new RuntimeException("system server dead?", e); |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | /** |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 172 | * Remove all records about this host from the AppWidget manager. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 173 | * <ul> |
| 174 | * <li>Call this when initializing your database, as it might be because of a data wipe.</li> |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 175 | * <li>Call this to have the AppWidget manager release all resources associated with your |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 176 | * host. Any future calls about this host will cause the records to be re-allocated.</li> |
| 177 | * </ul> |
| 178 | */ |
| 179 | public void deleteHost() { |
| 180 | try { |
| 181 | sService.deleteHost(mHostId); |
| 182 | } |
| 183 | catch (RemoteException e) { |
| 184 | throw new RuntimeException("system server dead?", e); |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * Remove all records about all hosts for your package. |
| 190 | * <ul> |
| 191 | * <li>Call this when initializing your database, as it might be because of a data wipe.</li> |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 192 | * <li>Call this to have the AppWidget manager release all resources associated with your |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 193 | * host. Any future calls about this host will cause the records to be re-allocated.</li> |
| 194 | * </ul> |
| 195 | */ |
| 196 | public static void deleteAllHosts() { |
| 197 | try { |
| 198 | sService.deleteAllHosts(); |
| 199 | } |
| 200 | catch (RemoteException e) { |
| 201 | throw new RuntimeException("system server dead?", e); |
| 202 | } |
| 203 | } |
| 204 | |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 205 | public final AppWidgetHostView createView(Context context, int appWidgetId, |
| 206 | AppWidgetProviderInfo appWidget) { |
| 207 | AppWidgetHostView view = onCreateView(context, appWidgetId, appWidget); |
| 208 | view.setAppWidget(appWidgetId, appWidget); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 209 | synchronized (mViews) { |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 210 | mViews.put(appWidgetId, view); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 211 | } |
Romain Guy | a547559 | 2009-07-01 17:20:08 -0700 | [diff] [blame] | 212 | RemoteViews views; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 213 | try { |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 214 | views = sService.getAppWidgetViews(appWidgetId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 215 | } catch (RemoteException e) { |
| 216 | throw new RuntimeException("system server dead?", e); |
| 217 | } |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 218 | view.updateAppWidget(views); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 219 | return view; |
| 220 | } |
| 221 | |
| 222 | /** |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 223 | * Called to create the AppWidgetHostView. Override to return a custom subclass if you |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 224 | * need it. {@more} |
| 225 | */ |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 226 | protected AppWidgetHostView onCreateView(Context context, int appWidgetId, |
| 227 | AppWidgetProviderInfo appWidget) { |
| 228 | return new AppWidgetHostView(context); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | /** |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 232 | * Called when the AppWidget provider for a AppWidget has been upgraded to a new apk. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 233 | */ |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 234 | protected void onProviderChanged(int appWidgetId, AppWidgetProviderInfo appWidget) { |
Joe Onorato | f140be6 | 2010-05-04 11:49:12 -0700 | [diff] [blame] | 235 | AppWidgetHostView v; |
| 236 | synchronized (mViews) { |
| 237 | v = mViews.get(appWidgetId); |
| 238 | } |
| 239 | if (v != null) { |
Joe Onorato | c27bb55 | 2010-06-23 17:44:30 -0700 | [diff] [blame] | 240 | v.resetAppWidget(appWidget); |
Joe Onorato | f140be6 | 2010-05-04 11:49:12 -0700 | [diff] [blame] | 241 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 242 | } |
| 243 | |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 244 | void updateAppWidgetView(int appWidgetId, RemoteViews views) { |
| 245 | AppWidgetHostView v; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 246 | synchronized (mViews) { |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 247 | v = mViews.get(appWidgetId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 248 | } |
| 249 | if (v != null) { |
Joe Onorato | c27bb55 | 2010-06-23 17:44:30 -0700 | [diff] [blame] | 250 | v.updateAppWidget(views); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 251 | } |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | |