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 | |
Svetoslav | 7643f7e | 2014-12-17 17:55:34 -0800 | [diff] [blame] | 19 | import java.lang.ref.WeakReference; |
Adam Cohen | d2e20de | 2011-02-25 12:03:37 -0800 | [diff] [blame] | 20 | import java.util.ArrayList; |
| 21 | import java.util.HashMap; |
| 22 | |
Svetoslav | 8e1d299 | 2014-08-08 12:48:06 -0700 | [diff] [blame] | 23 | import android.annotation.NonNull; |
| 24 | import android.annotation.Nullable; |
Svetoslav | 976e8bd | 2014-07-16 15:12:03 -0700 | [diff] [blame] | 25 | import android.app.Activity; |
| 26 | import android.content.ActivityNotFoundException; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 27 | import android.content.Context; |
Svetoslav | 976e8bd | 2014-07-16 15:12:03 -0700 | [diff] [blame] | 28 | import android.content.IntentSender; |
Adam Cohen | 3ff2d86 | 2012-09-26 14:07:57 -0700 | [diff] [blame] | 29 | import android.os.Binder; |
Svetoslav | 8e1d299 | 2014-08-08 12:48:06 -0700 | [diff] [blame] | 30 | import android.os.Bundle; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 31 | import android.os.Handler; |
| 32 | import android.os.IBinder; |
| 33 | import android.os.Looper; |
| 34 | import android.os.Message; |
Jim Miller | f229e4d | 2012-09-12 20:32:50 -0700 | [diff] [blame] | 35 | import android.os.Process; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 36 | import android.os.RemoteException; |
| 37 | import android.os.ServiceManager; |
Adam Cohen | d2e20de | 2011-02-25 12:03:37 -0800 | [diff] [blame] | 38 | import android.util.DisplayMetrics; |
| 39 | import android.util.TypedValue; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 40 | import android.widget.RemoteViews; |
Jim Miller | e667a7a | 2012-08-09 19:22:32 -0700 | [diff] [blame] | 41 | import android.widget.RemoteViews.OnClickHandler; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 42 | |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 43 | import com.android.internal.appwidget.IAppWidgetHost; |
| 44 | import com.android.internal.appwidget.IAppWidgetService; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 45 | |
| 46 | /** |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 47 | * AppWidgetHost provides the interaction with the AppWidget service for apps, |
| 48 | * 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] | 49 | */ |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 50 | public class AppWidgetHost { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 51 | |
| 52 | static final int HANDLE_UPDATE = 1; |
| 53 | static final int HANDLE_PROVIDER_CHANGED = 2; |
Winson Chung | 7fbd284 | 2012-06-13 10:35:51 -0700 | [diff] [blame] | 54 | static final int HANDLE_PROVIDERS_CHANGED = 3; |
| 55 | static final int HANDLE_VIEW_DATA_CHANGED = 4; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 56 | |
Romain Guy | a547559 | 2009-07-01 17:20:08 -0700 | [diff] [blame] | 57 | final static Object sServiceLock = new Object(); |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 58 | static IAppWidgetService sService; |
Adam Cohen | d2e20de | 2011-02-25 12:03:37 -0800 | [diff] [blame] | 59 | private DisplayMetrics mDisplayMetrics; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 60 | |
Winson Chung | 7a96f3c | 2014-12-17 10:31:22 -0800 | [diff] [blame] | 61 | private String mContextOpPackageName; |
Svetoslav | 7643f7e | 2014-12-17 17:55:34 -0800 | [diff] [blame] | 62 | private final Handler mHandler; |
| 63 | private final int mHostId; |
| 64 | private final Callbacks mCallbacks; |
| 65 | private final HashMap<Integer,AppWidgetHostView> mViews = new HashMap<>(); |
Jim Miller | a75a883 | 2013-02-07 16:53:32 -0800 | [diff] [blame] | 66 | private OnClickHandler mOnClickHandler; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 67 | |
Svetoslav | 7643f7e | 2014-12-17 17:55:34 -0800 | [diff] [blame] | 68 | static class Callbacks extends IAppWidgetHost.Stub { |
| 69 | private final WeakReference<Handler> mWeakHandler; |
| 70 | |
| 71 | public Callbacks(Handler handler) { |
| 72 | mWeakHandler = new WeakReference<>(handler); |
| 73 | } |
| 74 | |
Svetoslav | 976e8bd | 2014-07-16 15:12:03 -0700 | [diff] [blame] | 75 | public void updateAppWidget(int appWidgetId, RemoteViews views) { |
Adam Cohen | 3ff2d86 | 2012-09-26 14:07:57 -0700 | [diff] [blame] | 76 | if (isLocalBinder() && views != null) { |
| 77 | views = views.clone(); |
| 78 | } |
Svetoslav | 7643f7e | 2014-12-17 17:55:34 -0800 | [diff] [blame] | 79 | Handler handler = mWeakHandler.get(); |
| 80 | if (handler == null) { |
| 81 | return; |
| 82 | } |
| 83 | Message msg = handler.obtainMessage(HANDLE_UPDATE, appWidgetId, 0, views); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 84 | msg.sendToTarget(); |
| 85 | } |
| 86 | |
Svetoslav | 976e8bd | 2014-07-16 15:12:03 -0700 | [diff] [blame] | 87 | public void providerChanged(int appWidgetId, AppWidgetProviderInfo info) { |
Adam Cohen | 3ff2d86 | 2012-09-26 14:07:57 -0700 | [diff] [blame] | 88 | if (isLocalBinder() && info != null) { |
| 89 | info = info.clone(); |
| 90 | } |
Svetoslav | 7643f7e | 2014-12-17 17:55:34 -0800 | [diff] [blame] | 91 | Handler handler = mWeakHandler.get(); |
| 92 | if (handler == null) { |
| 93 | return; |
| 94 | } |
| 95 | Message msg = handler.obtainMessage(HANDLE_PROVIDER_CHANGED, |
Svetoslav | 976e8bd | 2014-07-16 15:12:03 -0700 | [diff] [blame] | 96 | appWidgetId, 0, info); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 97 | msg.sendToTarget(); |
| 98 | } |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 99 | |
Svetoslav | 976e8bd | 2014-07-16 15:12:03 -0700 | [diff] [blame] | 100 | public void providersChanged() { |
Svetoslav | 7643f7e | 2014-12-17 17:55:34 -0800 | [diff] [blame] | 101 | Handler handler = mWeakHandler.get(); |
| 102 | if (handler == null) { |
| 103 | return; |
| 104 | } |
| 105 | handler.obtainMessage(HANDLE_PROVIDERS_CHANGED).sendToTarget(); |
Winson Chung | 7fbd284 | 2012-06-13 10:35:51 -0700 | [diff] [blame] | 106 | } |
| 107 | |
Svetoslav | 976e8bd | 2014-07-16 15:12:03 -0700 | [diff] [blame] | 108 | public void viewDataChanged(int appWidgetId, int viewId) { |
Svetoslav | 7643f7e | 2014-12-17 17:55:34 -0800 | [diff] [blame] | 109 | Handler handler = mWeakHandler.get(); |
| 110 | if (handler == null) { |
| 111 | return; |
| 112 | } |
| 113 | Message msg = handler.obtainMessage(HANDLE_VIEW_DATA_CHANGED, |
Svetoslav | 976e8bd | 2014-07-16 15:12:03 -0700 | [diff] [blame] | 114 | appWidgetId, viewId); |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 115 | msg.sendToTarget(); |
| 116 | } |
Svetoslav | 7643f7e | 2014-12-17 17:55:34 -0800 | [diff] [blame] | 117 | |
| 118 | private static boolean isLocalBinder() { |
| 119 | return Process.myPid() == Binder.getCallingPid(); |
| 120 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | class UpdateHandler extends Handler { |
| 124 | public UpdateHandler(Looper looper) { |
| 125 | super(looper); |
| 126 | } |
Jim Miller | e667a7a | 2012-08-09 19:22:32 -0700 | [diff] [blame] | 127 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 128 | public void handleMessage(Message msg) { |
| 129 | switch (msg.what) { |
| 130 | case HANDLE_UPDATE: { |
Svetoslav | 976e8bd | 2014-07-16 15:12:03 -0700 | [diff] [blame] | 131 | updateAppWidgetView(msg.arg1, (RemoteViews)msg.obj); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 132 | break; |
| 133 | } |
| 134 | case HANDLE_PROVIDER_CHANGED: { |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 135 | onProviderChanged(msg.arg1, (AppWidgetProviderInfo)msg.obj); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 136 | break; |
| 137 | } |
Winson Chung | 7fbd284 | 2012-06-13 10:35:51 -0700 | [diff] [blame] | 138 | case HANDLE_PROVIDERS_CHANGED: { |
Winson Chung | eeb4ff4 | 2013-06-04 14:25:28 -0700 | [diff] [blame] | 139 | onProvidersChanged(); |
Winson Chung | 7fbd284 | 2012-06-13 10:35:51 -0700 | [diff] [blame] | 140 | break; |
| 141 | } |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 142 | case HANDLE_VIEW_DATA_CHANGED: { |
Svetoslav | 976e8bd | 2014-07-16 15:12:03 -0700 | [diff] [blame] | 143 | viewDataChanged(msg.arg1, msg.arg2); |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 144 | break; |
| 145 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 146 | } |
| 147 | } |
| 148 | } |
Jim Miller | e667a7a | 2012-08-09 19:22:32 -0700 | [diff] [blame] | 149 | |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 150 | public AppWidgetHost(Context context, int hostId) { |
Michael Jurka | e6d5545 | 2012-09-17 17:30:16 -0700 | [diff] [blame] | 151 | this(context, hostId, null, context.getMainLooper()); |
Jim Miller | e667a7a | 2012-08-09 19:22:32 -0700 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | /** |
| 155 | * @hide |
| 156 | */ |
Jim Miller | f229e4d | 2012-09-12 20:32:50 -0700 | [diff] [blame] | 157 | public AppWidgetHost(Context context, int hostId, OnClickHandler handler, Looper looper) { |
Winson Chung | 7a96f3c | 2014-12-17 10:31:22 -0800 | [diff] [blame] | 158 | mContextOpPackageName = context.getOpPackageName(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 159 | mHostId = hostId; |
Jim Miller | e667a7a | 2012-08-09 19:22:32 -0700 | [diff] [blame] | 160 | mOnClickHandler = handler; |
Jim Miller | f229e4d | 2012-09-12 20:32:50 -0700 | [diff] [blame] | 161 | mHandler = new UpdateHandler(looper); |
Svetoslav | 7643f7e | 2014-12-17 17:55:34 -0800 | [diff] [blame] | 162 | mCallbacks = new Callbacks(mHandler); |
Adam Cohen | d2e20de | 2011-02-25 12:03:37 -0800 | [diff] [blame] | 163 | mDisplayMetrics = context.getResources().getDisplayMetrics(); |
Jim Miller | f229e4d | 2012-09-12 20:32:50 -0700 | [diff] [blame] | 164 | bindService(); |
| 165 | } |
| 166 | |
Amith Yamasani | 94022e8 | 2012-12-04 11:05:39 -0800 | [diff] [blame] | 167 | |
Jim Miller | f229e4d | 2012-09-12 20:32:50 -0700 | [diff] [blame] | 168 | private static void bindService() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 169 | synchronized (sServiceLock) { |
| 170 | if (sService == null) { |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 171 | IBinder b = ServiceManager.getService(Context.APPWIDGET_SERVICE); |
| 172 | sService = IAppWidgetService.Stub.asInterface(b); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 173 | } |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | /** |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 178 | * 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] | 179 | * becomes visible, i.e. from onStart() in your Activity. |
| 180 | */ |
| 181 | public void startListening() { |
Romain Guy | a547559 | 2009-07-01 17:20:08 -0700 | [diff] [blame] | 182 | int[] updatedIds; |
| 183 | ArrayList<RemoteViews> updatedViews = new ArrayList<RemoteViews>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 184 | try { |
Winson Chung | 7a96f3c | 2014-12-17 10:31:22 -0800 | [diff] [blame] | 185 | updatedIds = sService.startListening(mCallbacks, mContextOpPackageName, mHostId, |
Svetoslav | 976e8bd | 2014-07-16 15:12:03 -0700 | [diff] [blame] | 186 | updatedViews); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 187 | } |
| 188 | catch (RemoteException e) { |
| 189 | throw new RuntimeException("system server dead?", e); |
| 190 | } |
| 191 | |
| 192 | final int N = updatedIds.length; |
Svetoslav | 976e8bd | 2014-07-16 15:12:03 -0700 | [diff] [blame] | 193 | for (int i = 0; i < N; i++) { |
| 194 | updateAppWidgetView(updatedIds[i], updatedViews.get(i)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 195 | } |
| 196 | } |
| 197 | |
| 198 | /** |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 199 | * 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] | 200 | * no longer visible, i.e. from onStop() in your Activity. |
| 201 | */ |
| 202 | public void stopListening() { |
| 203 | try { |
Winson Chung | 7a96f3c | 2014-12-17 10:31:22 -0800 | [diff] [blame] | 204 | sService.stopListening(mContextOpPackageName, mHostId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 205 | } |
| 206 | catch (RemoteException e) { |
| 207 | throw new RuntimeException("system server dead?", e); |
| 208 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 209 | |
Jim Miller | a75a883 | 2013-02-07 16:53:32 -0800 | [diff] [blame] | 210 | // This is here because keyguard needs it since it'll be switching users after this call. |
| 211 | // If it turns out other apps need to call this often, we should re-think how this works. |
Amith Yamasani | c566b43 | 2012-11-30 15:26:21 -0800 | [diff] [blame] | 212 | clearViews(); |
| 213 | } |
| 214 | |
| 215 | /** |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 216 | * 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] | 217 | * |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 218 | * @return a appWidgetId |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 219 | */ |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 220 | public int allocateAppWidgetId() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 221 | try { |
Winson Chung | 7a96f3c | 2014-12-17 10:31:22 -0800 | [diff] [blame] | 222 | return sService.allocateAppWidgetId(mContextOpPackageName, mHostId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 223 | } |
| 224 | catch (RemoteException e) { |
| 225 | throw new RuntimeException("system server dead?", e); |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | /** |
Svetoslav | 976e8bd | 2014-07-16 15:12:03 -0700 | [diff] [blame] | 230 | * Starts an app widget provider configure activity for result on behalf of the caller. |
| 231 | * Use this method if the provider is in another profile as you are not allowed to start |
Svetoslav | 8e1d299 | 2014-08-08 12:48:06 -0700 | [diff] [blame] | 232 | * an activity in another profile. You can optionally provide a request code that is |
| 233 | * returned in {@link Activity#onActivityResult(int, int, android.content.Intent)} and |
| 234 | * an options bundle to be passed to the started activity. |
| 235 | * <p> |
| 236 | * Note that the provided app widget has to be bound for this method to work. |
| 237 | * </p> |
Jim Miller | f229e4d | 2012-09-12 20:32:50 -0700 | [diff] [blame] | 238 | * |
Svetoslav | 976e8bd | 2014-07-16 15:12:03 -0700 | [diff] [blame] | 239 | * @param activity The activity from which to start the configure one. |
Svetoslav | 8e1d299 | 2014-08-08 12:48:06 -0700 | [diff] [blame] | 240 | * @param appWidgetId The bound app widget whose provider's config activity to start. |
Svetoslav | 976e8bd | 2014-07-16 15:12:03 -0700 | [diff] [blame] | 241 | * @param requestCode Optional request code retuned with the result. |
Svetoslav | 8e1d299 | 2014-08-08 12:48:06 -0700 | [diff] [blame] | 242 | * @param intentFlags Optional intent flags. |
Svetoslav | 976e8bd | 2014-07-16 15:12:03 -0700 | [diff] [blame] | 243 | * |
| 244 | * @throws android.content.ActivityNotFoundException If the activity is not found. |
| 245 | * |
| 246 | * @see AppWidgetProviderInfo#getProfile() |
Jim Miller | f229e4d | 2012-09-12 20:32:50 -0700 | [diff] [blame] | 247 | */ |
Svetoslav | 8e1d299 | 2014-08-08 12:48:06 -0700 | [diff] [blame] | 248 | public final void startAppWidgetConfigureActivityForResult(@NonNull Activity activity, |
| 249 | int appWidgetId, int intentFlags, int requestCode, @Nullable Bundle options) { |
Jim Miller | f229e4d | 2012-09-12 20:32:50 -0700 | [diff] [blame] | 250 | try { |
Svetoslav | 976e8bd | 2014-07-16 15:12:03 -0700 | [diff] [blame] | 251 | IntentSender intentSender = sService.createAppWidgetConfigIntentSender( |
Winson Chung | 7a96f3c | 2014-12-17 10:31:22 -0800 | [diff] [blame] | 252 | mContextOpPackageName, appWidgetId, intentFlags); |
Svetoslav | 976e8bd | 2014-07-16 15:12:03 -0700 | [diff] [blame] | 253 | if (intentSender != null) { |
Svetoslav | 8e1d299 | 2014-08-08 12:48:06 -0700 | [diff] [blame] | 254 | activity.startIntentSenderForResult(intentSender, requestCode, null, 0, 0, 0, |
| 255 | options); |
Svetoslav | 976e8bd | 2014-07-16 15:12:03 -0700 | [diff] [blame] | 256 | } else { |
| 257 | throw new ActivityNotFoundException(); |
Jim Miller | f229e4d | 2012-09-12 20:32:50 -0700 | [diff] [blame] | 258 | } |
Svetoslav | 976e8bd | 2014-07-16 15:12:03 -0700 | [diff] [blame] | 259 | } catch (IntentSender.SendIntentException e) { |
| 260 | throw new ActivityNotFoundException(); |
Jim Miller | f229e4d | 2012-09-12 20:32:50 -0700 | [diff] [blame] | 261 | } catch (RemoteException e) { |
| 262 | throw new RuntimeException("system server dead?", e); |
| 263 | } |
| 264 | } |
| 265 | |
Michael Jurka | 75b5cfb | 2012-11-15 18:22:47 -0800 | [diff] [blame] | 266 | /** |
| 267 | * Gets a list of all the appWidgetIds that are bound to the current host |
| 268 | * |
| 269 | * @hide |
| 270 | */ |
| 271 | public int[] getAppWidgetIds() { |
| 272 | try { |
| 273 | if (sService == null) { |
| 274 | bindService(); |
| 275 | } |
Winson Chung | 7a96f3c | 2014-12-17 10:31:22 -0800 | [diff] [blame] | 276 | return sService.getAppWidgetIdsForHost(mContextOpPackageName, mHostId); |
Michael Jurka | 75b5cfb | 2012-11-15 18:22:47 -0800 | [diff] [blame] | 277 | } catch (RemoteException e) { |
| 278 | throw new RuntimeException("system server dead?", e); |
| 279 | } |
| 280 | } |
| 281 | |
Jim Miller | f229e4d | 2012-09-12 20:32:50 -0700 | [diff] [blame] | 282 | /** |
Jim Miller | e667a7a | 2012-08-09 19:22:32 -0700 | [diff] [blame] | 283 | * Stop listening to changes for this AppWidget. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 284 | */ |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 285 | public void deleteAppWidgetId(int appWidgetId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 286 | synchronized (mViews) { |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 287 | mViews.remove(appWidgetId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 288 | try { |
Winson Chung | 7a96f3c | 2014-12-17 10:31:22 -0800 | [diff] [blame] | 289 | sService.deleteAppWidgetId(mContextOpPackageName, appWidgetId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 290 | } |
| 291 | catch (RemoteException e) { |
| 292 | throw new RuntimeException("system server dead?", e); |
| 293 | } |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | /** |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 298 | * 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] | 299 | * <ul> |
| 300 | * <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] | 301 | * <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] | 302 | * host. Any future calls about this host will cause the records to be re-allocated.</li> |
| 303 | * </ul> |
| 304 | */ |
| 305 | public void deleteHost() { |
| 306 | try { |
Winson Chung | 7a96f3c | 2014-12-17 10:31:22 -0800 | [diff] [blame] | 307 | sService.deleteHost(mContextOpPackageName, mHostId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 308 | } |
| 309 | catch (RemoteException e) { |
| 310 | throw new RuntimeException("system server dead?", e); |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | /** |
| 315 | * Remove all records about all hosts for your package. |
| 316 | * <ul> |
| 317 | * <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] | 318 | * <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] | 319 | * host. Any future calls about this host will cause the records to be re-allocated.</li> |
| 320 | * </ul> |
| 321 | */ |
| 322 | public static void deleteAllHosts() { |
| 323 | try { |
Svetoslav | 976e8bd | 2014-07-16 15:12:03 -0700 | [diff] [blame] | 324 | sService.deleteAllHosts(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 325 | } |
| 326 | catch (RemoteException e) { |
| 327 | throw new RuntimeException("system server dead?", e); |
| 328 | } |
| 329 | } |
| 330 | |
Patrick Dubroy | ec84c3a | 2011-01-13 17:55:37 -0800 | [diff] [blame] | 331 | /** |
| 332 | * Create the AppWidgetHostView for the given widget. |
| 333 | * The AppWidgetHost retains a pointer to the newly-created View. |
| 334 | */ |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 335 | public final AppWidgetHostView createView(Context context, int appWidgetId, |
| 336 | AppWidgetProviderInfo appWidget) { |
Winson Chung | 7a96f3c | 2014-12-17 10:31:22 -0800 | [diff] [blame] | 337 | AppWidgetHostView view = onCreateView(context, appWidgetId, appWidget); |
Jim Miller | e667a7a | 2012-08-09 19:22:32 -0700 | [diff] [blame] | 338 | view.setOnClickHandler(mOnClickHandler); |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 339 | view.setAppWidget(appWidgetId, appWidget); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 340 | synchronized (mViews) { |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 341 | mViews.put(appWidgetId, view); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 342 | } |
Romain Guy | a547559 | 2009-07-01 17:20:08 -0700 | [diff] [blame] | 343 | RemoteViews views; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 344 | try { |
Winson Chung | 7a96f3c | 2014-12-17 10:31:22 -0800 | [diff] [blame] | 345 | views = sService.getAppWidgetViews(mContextOpPackageName, appWidgetId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 346 | } catch (RemoteException e) { |
| 347 | throw new RuntimeException("system server dead?", e); |
| 348 | } |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 349 | view.updateAppWidget(views); |
Jim Miller | e667a7a | 2012-08-09 19:22:32 -0700 | [diff] [blame] | 350 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 351 | return view; |
| 352 | } |
| 353 | |
| 354 | /** |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 355 | * 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] | 356 | * need it. {@more} |
| 357 | */ |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 358 | protected AppWidgetHostView onCreateView(Context context, int appWidgetId, |
| 359 | AppWidgetProviderInfo appWidget) { |
Jim Miller | e667a7a | 2012-08-09 19:22:32 -0700 | [diff] [blame] | 360 | return new AppWidgetHostView(context, mOnClickHandler); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 361 | } |
Adam Cohen | d2e20de | 2011-02-25 12:03:37 -0800 | [diff] [blame] | 362 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 363 | /** |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 364 | * 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] | 365 | */ |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 366 | protected void onProviderChanged(int appWidgetId, AppWidgetProviderInfo appWidget) { |
Joe Onorato | f140be6 | 2010-05-04 11:49:12 -0700 | [diff] [blame] | 367 | AppWidgetHostView v; |
Adam Cohen | d2e20de | 2011-02-25 12:03:37 -0800 | [diff] [blame] | 368 | |
| 369 | // Convert complex to dp -- we are getting the AppWidgetProviderInfo from the |
Jim Miller | e667a7a | 2012-08-09 19:22:32 -0700 | [diff] [blame] | 370 | // AppWidgetService, which doesn't have our context, hence we need to do the |
Adam Cohen | d2e20de | 2011-02-25 12:03:37 -0800 | [diff] [blame] | 371 | // conversion here. |
| 372 | appWidget.minWidth = |
| 373 | TypedValue.complexToDimensionPixelSize(appWidget.minWidth, mDisplayMetrics); |
| 374 | appWidget.minHeight = |
| 375 | TypedValue.complexToDimensionPixelSize(appWidget.minHeight, mDisplayMetrics); |
Adam Cohen | 1bfaf56 | 2011-07-19 18:05:33 -0700 | [diff] [blame] | 376 | appWidget.minResizeWidth = |
| 377 | TypedValue.complexToDimensionPixelSize(appWidget.minResizeWidth, mDisplayMetrics); |
| 378 | appWidget.minResizeHeight = |
| 379 | TypedValue.complexToDimensionPixelSize(appWidget.minResizeHeight, mDisplayMetrics); |
Adam Cohen | d2e20de | 2011-02-25 12:03:37 -0800 | [diff] [blame] | 380 | |
Joe Onorato | f140be6 | 2010-05-04 11:49:12 -0700 | [diff] [blame] | 381 | synchronized (mViews) { |
| 382 | v = mViews.get(appWidgetId); |
| 383 | } |
| 384 | if (v != null) { |
Joe Onorato | c27bb55 | 2010-06-23 17:44:30 -0700 | [diff] [blame] | 385 | v.resetAppWidget(appWidget); |
Joe Onorato | f140be6 | 2010-05-04 11:49:12 -0700 | [diff] [blame] | 386 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 387 | } |
| 388 | |
Winson Chung | 7fbd284 | 2012-06-13 10:35:51 -0700 | [diff] [blame] | 389 | /** |
| 390 | * Called when the set of available widgets changes (ie. widget containing packages |
| 391 | * are added, updated or removed, or widget components are enabled or disabled.) |
| 392 | */ |
| 393 | protected void onProvidersChanged() { |
Jim Miller | a75a883 | 2013-02-07 16:53:32 -0800 | [diff] [blame] | 394 | // Does nothing |
| 395 | } |
| 396 | |
Svetoslav | 976e8bd | 2014-07-16 15:12:03 -0700 | [diff] [blame] | 397 | void updateAppWidgetView(int appWidgetId, RemoteViews views) { |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 398 | AppWidgetHostView v; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 399 | synchronized (mViews) { |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 400 | v = mViews.get(appWidgetId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 401 | } |
| 402 | if (v != null) { |
Joe Onorato | c27bb55 | 2010-06-23 17:44:30 -0700 | [diff] [blame] | 403 | v.updateAppWidget(views); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 404 | } |
| 405 | } |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 406 | |
Svetoslav | 976e8bd | 2014-07-16 15:12:03 -0700 | [diff] [blame] | 407 | void viewDataChanged(int appWidgetId, int viewId) { |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 408 | AppWidgetHostView v; |
| 409 | synchronized (mViews) { |
| 410 | v = mViews.get(appWidgetId); |
| 411 | } |
| 412 | if (v != null) { |
Winson Chung | 6394c0e | 2010-08-16 10:14:56 -0700 | [diff] [blame] | 413 | v.viewDataChanged(viewId); |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 414 | } |
| 415 | } |
Patrick Dubroy | ec84c3a | 2011-01-13 17:55:37 -0800 | [diff] [blame] | 416 | |
| 417 | /** |
| 418 | * Clear the list of Views that have been created by this AppWidgetHost. |
| 419 | */ |
| 420 | protected void clearViews() { |
| 421 | mViews.clear(); |
| 422 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | |