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