The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2 | * |
| 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | * you may not use this file except in compliance with the License. |
| 5 | * You may obtain a copy of the License at |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software |
| 10 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | * See the License for the specific language governing permissions and |
| 13 | * limitations under the License. |
| 14 | */ |
| 15 | |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 16 | package android.appwidget; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 17 | |
| 18 | import android.content.Context; |
| 19 | import android.os.Handler; |
| 20 | import android.os.IBinder; |
| 21 | import android.os.Looper; |
| 22 | import android.os.Message; |
| 23 | import android.os.RemoteException; |
| 24 | import android.os.ServiceManager; |
| 25 | import android.widget.RemoteViews; |
| 26 | |
| 27 | import java.util.ArrayList; |
| 28 | import java.util.HashMap; |
| 29 | import java.util.List; |
| 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 | |
| 43 | 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(); |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 88 | HashMap<Integer,AppWidgetHostView> mViews = new HashMap(); |
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() { |
| 107 | int[] updatedIds = null; |
| 108 | ArrayList<RemoteViews> updatedViews = new ArrayList(); |
| 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 | } |
| 212 | RemoteViews views = null; |
| 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) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 235 | } |
| 236 | |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 237 | void updateAppWidgetView(int appWidgetId, RemoteViews views) { |
| 238 | AppWidgetHostView v; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 239 | synchronized (mViews) { |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 240 | v = mViews.get(appWidgetId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 241 | } |
| 242 | if (v != null) { |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 243 | v.updateAppWidget(views); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 244 | } |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | |