blob: 52771eebecbb9d95c1e05fe06c2e1e6ef683dbb1 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -070017package android.appwidget;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018
Adam Cohen5d5f3402011-09-30 15:02:43 -070019import android.content.ComponentName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020import android.content.Context;
Adam Cohen5d5f3402011-09-30 15:02:43 -070021import android.content.pm.ApplicationInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022import android.content.pm.PackageManager;
Jeff Sharkeyb8560b62009-08-06 21:24:25 -070023import android.content.pm.PackageManager.NameNotFoundException;
Adam Cohen5d5f3402011-09-30 15:02:43 -070024import android.content.res.Resources;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025import android.graphics.Bitmap;
26import android.graphics.Canvas;
27import android.graphics.Color;
28import android.graphics.Paint;
Adam Cohen4c96a592011-11-04 12:12:48 -070029import android.graphics.Rect;
Adam Cohen5d5f3402011-09-30 15:02:43 -070030import android.os.Build;
Adam Cohene8724c82012-04-19 17:11:40 -070031import android.os.Bundle;
Romain Guy09ddc082009-08-11 12:10:08 -070032import android.os.Parcel;
Gilles Debunne30301932010-06-16 18:32:00 -070033import android.os.Parcelable;
34import android.os.SystemClock;
Jeff Sharkeyb8560b62009-08-06 21:24:25 -070035import android.util.AttributeSet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036import android.util.Log;
Romain Guy09ddc082009-08-11 12:10:08 -070037import android.util.SparseArray;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038import android.view.Gravity;
39import android.view.LayoutInflater;
40import android.view.View;
Guang Zhu1affb932012-05-15 14:25:51 -070041import android.view.accessibility.AccessibilityNodeInfo;
Winson Chung499cb9f2010-07-16 11:18:17 -070042import android.widget.Adapter;
43import android.widget.AdapterView;
44import android.widget.BaseAdapter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045import android.widget.FrameLayout;
46import android.widget.RemoteViews;
Jim Millere667a7a2012-08-09 19:22:32 -070047import android.widget.RemoteViews.OnClickHandler;
Adam Cohen2148d432011-07-28 14:59:54 -070048import android.widget.RemoteViewsAdapter.RemoteAdapterConnectionCallback;
Adam Cohen4c96a592011-11-04 12:12:48 -070049import android.widget.TextView;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050
51/**
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -070052 * Provides the glue to show AppWidget views. This class offers automatic animation
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053 * between updates, and will try recycling old views for each incoming
54 * {@link RemoteViews}.
55 */
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -070056public class AppWidgetHostView extends FrameLayout {
57 static final String TAG = "AppWidgetHostView";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058 static final boolean LOGD = false;
59 static final boolean CROSSFADE = false;
60
61 static final int VIEW_MODE_NOINIT = 0;
62 static final int VIEW_MODE_CONTENT = 1;
63 static final int VIEW_MODE_ERROR = 2;
64 static final int VIEW_MODE_DEFAULT = 3;
65
66 static final int FADE_DURATION = 1000;
67
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -070068 // When we're inflating the initialLayout for a AppWidget, we only allow
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069 // views that are allowed in RemoteViews.
70 static final LayoutInflater.Filter sInflaterFilter = new LayoutInflater.Filter() {
Gilles Debunnee6ac8b92010-06-17 10:55:04 -070071 public boolean onLoadClass(Class clazz) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072 return clazz.isAnnotationPresent(RemoteViews.RemoteView.class);
73 }
74 };
75
76 Context mContext;
Jeff Sharkeyb8560b62009-08-06 21:24:25 -070077 Context mRemoteContext;
78
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -070079 int mAppWidgetId;
80 AppWidgetProviderInfo mInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081 View mView;
82 int mViewMode = VIEW_MODE_NOINIT;
83 int mLayoutId = -1;
84 long mFadeStartTime = -1;
85 Bitmap mOld;
86 Paint mOldPaint = new Paint();
Jim Millere667a7a2012-08-09 19:22:32 -070087 private OnClickHandler mOnClickHandler;
88
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089 /**
90 * Create a host view. Uses default fade animations.
91 */
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -070092 public AppWidgetHostView(Context context) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093 this(context, android.R.anim.fade_in, android.R.anim.fade_out);
94 }
95
96 /**
Jim Millere667a7a2012-08-09 19:22:32 -070097 * @hide
98 */
99 public AppWidgetHostView(Context context, OnClickHandler handler) {
100 this(context, android.R.anim.fade_in, android.R.anim.fade_out);
101 mOnClickHandler = handler;
102 }
103
104 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105 * Create a host view. Uses specified animations when pushing
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700106 * {@link #updateAppWidget(RemoteViews)}.
Jim Millere667a7a2012-08-09 19:22:32 -0700107 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108 * @param animationIn Resource ID of in animation to use
109 * @param animationOut Resource ID of out animation to use
110 */
Romain Guya5475592009-07-01 17:20:08 -0700111 @SuppressWarnings({"UnusedDeclaration"})
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700112 public AppWidgetHostView(Context context, int animationIn, int animationOut) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113 super(context);
114 mContext = context;
Adam Cohend1108892011-01-16 19:40:49 -0800115
116 // We want to segregate the view ids within AppWidgets to prevent
117 // problems when those ids collide with view ids in the AppWidgetHost.
118 setIsRootNamespace(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119 }
Adam Cohend1108892011-01-16 19:40:49 -0800120
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800121 /**
Jim Millere667a7a2012-08-09 19:22:32 -0700122 * Pass the given handler to RemoteViews when updating this widget. Unless this
123 * is done immediatly after construction, a call to {@link #updateAppWidget(RemoteViews)}
124 * should be made.
125 * @param handler
126 * @hide
127 */
128 public void setOnClickHandler(OnClickHandler handler) {
129 mOnClickHandler = handler;
130 }
131
132 /**
Adam Cohen4c96a592011-11-04 12:12:48 -0700133 * Set the AppWidget that will be displayed by this view. This method also adds default padding
134 * to widgets, as described in {@link #getDefaultPaddingForWidget(Context, ComponentName, Rect)}
135 * and can be overridden in order to add custom padding.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800136 */
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700137 public void setAppWidget(int appWidgetId, AppWidgetProviderInfo info) {
138 mAppWidgetId = appWidgetId;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800139 mInfo = info;
Adam Cohen5d5f3402011-09-30 15:02:43 -0700140
141 // Sometimes the AppWidgetManager returns a null AppWidgetProviderInfo object for
142 // a widget, eg. for some widgets in safe mode.
143 if (info != null) {
144 // We add padding to the AppWidgetHostView if necessary
Adam Cohen4c96a592011-11-04 12:12:48 -0700145 Rect padding = getDefaultPaddingForWidget(mContext, info.provider, null);
Adam Cohen5d5f3402011-09-30 15:02:43 -0700146 setPadding(padding.left, padding.top, padding.right, padding.bottom);
Svetoslav Ganovc4842c12012-10-31 14:33:32 -0700147 setContentDescription(info.label);
Adam Cohen5d5f3402011-09-30 15:02:43 -0700148 }
149 }
150
Adam Cohen5d5f3402011-09-30 15:02:43 -0700151 /**
152 * As of ICE_CREAM_SANDWICH we are automatically adding padding to widgets targeting
153 * ICE_CREAM_SANDWICH and higher. The new widget design guidelines strongly recommend
154 * that widget developers do not add extra padding to their widgets. This will help
155 * achieve consistency among widgets.
Adam Cohen4c96a592011-11-04 12:12:48 -0700156 *
157 * Note: this method is only needed by developers of AppWidgetHosts. The method is provided in
158 * order for the AppWidgetHost to account for the automatic padding when computing the number
159 * of cells to allocate to a particular widget.
160 *
161 * @param context the current context
162 * @param component the component name of the widget
163 * @param padding Rect in which to place the output, if null, a new Rect will be allocated and
164 * returned
Adam Cohena21bf0c2012-10-05 18:20:46 -0700165 * @return default padding for this widget, in pixels
Adam Cohen5d5f3402011-09-30 15:02:43 -0700166 */
Adam Cohen4c96a592011-11-04 12:12:48 -0700167 public static Rect getDefaultPaddingForWidget(Context context, ComponentName component,
168 Rect padding) {
169 PackageManager packageManager = context.getPackageManager();
Adam Cohen5d5f3402011-09-30 15:02:43 -0700170 ApplicationInfo appInfo;
171
Adam Cohen4c96a592011-11-04 12:12:48 -0700172 if (padding == null) {
173 padding = new Rect(0, 0, 0, 0);
174 } else {
175 padding.set(0, 0, 0, 0);
176 }
177
Adam Cohen5d5f3402011-09-30 15:02:43 -0700178 try {
179 appInfo = packageManager.getApplicationInfo(component.getPackageName(), 0);
Adam Cohen4c96a592011-11-04 12:12:48 -0700180 } catch (NameNotFoundException e) {
Adam Cohen5d5f3402011-09-30 15:02:43 -0700181 // if we can't find the package, return 0 padding
Adam Cohen4c96a592011-11-04 12:12:48 -0700182 return padding;
Adam Cohen5d5f3402011-09-30 15:02:43 -0700183 }
184
185 if (appInfo.targetSdkVersion >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
Adam Cohen4c96a592011-11-04 12:12:48 -0700186 Resources r = context.getResources();
187 padding.left = r.getDimensionPixelSize(com.android.internal.
Adam Cohen5d5f3402011-09-30 15:02:43 -0700188 R.dimen.default_app_widget_padding_left);
Adam Cohen4c96a592011-11-04 12:12:48 -0700189 padding.right = r.getDimensionPixelSize(com.android.internal.
Adam Cohen5d5f3402011-09-30 15:02:43 -0700190 R.dimen.default_app_widget_padding_right);
Adam Cohen4c96a592011-11-04 12:12:48 -0700191 padding.top = r.getDimensionPixelSize(com.android.internal.
Adam Cohen5d5f3402011-09-30 15:02:43 -0700192 R.dimen.default_app_widget_padding_top);
Adam Cohen4c96a592011-11-04 12:12:48 -0700193 padding.bottom = r.getDimensionPixelSize(com.android.internal.
Adam Cohen5d5f3402011-09-30 15:02:43 -0700194 R.dimen.default_app_widget_padding_bottom);
195 }
Adam Cohen4c96a592011-11-04 12:12:48 -0700196 return padding;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800197 }
Joe Onoratoc27bb552010-06-23 17:44:30 -0700198
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700199 public int getAppWidgetId() {
200 return mAppWidgetId;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800201 }
Jim Millere667a7a2012-08-09 19:22:32 -0700202
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700203 public AppWidgetProviderInfo getAppWidgetInfo() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800204 return mInfo;
205 }
206
Romain Guy09ddc082009-08-11 12:10:08 -0700207 @Override
208 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
209 final ParcelableSparseArray jail = new ParcelableSparseArray();
210 super.dispatchSaveInstanceState(jail);
211 container.put(generateId(), jail);
212 }
213
214 private int generateId() {
215 final int id = getId();
216 return id == View.NO_ID ? mAppWidgetId : id;
217 }
218
219 @Override
220 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
Romain Guyae8dd292009-11-05 13:36:15 -0800221 final Parcelable parcelable = container.get(generateId());
222
223 ParcelableSparseArray jail = null;
224 if (parcelable != null && parcelable instanceof ParcelableSparseArray) {
225 jail = (ParcelableSparseArray) parcelable;
226 }
227
Romain Guya8a720d2009-10-15 15:09:26 -0700228 if (jail == null) jail = new ParcelableSparseArray();
Romain Guyae8dd292009-11-05 13:36:15 -0800229
Adam Cohen6ccf4e22012-09-21 16:09:34 -0700230 try {
231 super.dispatchRestoreInstanceState(jail);
232 } catch (Exception e) {
233 Log.e(TAG, "failed to restoreInstanceState for widget id: " + mAppWidgetId + ", "
234 + (mInfo == null ? "null" : mInfo.provider), e);
235 }
Romain Guy09ddc082009-08-11 12:10:08 -0700236 }
237
Adam Cohene8724c82012-04-19 17:11:40 -0700238 /**
Adam Cohen88f041e2012-05-08 15:32:23 -0700239 * Provide guidance about the size of this widget to the AppWidgetManager. The widths and
240 * heights should correspond to the full area the AppWidgetHostView is given. Padding added by
241 * the framework will be accounted for automatically. This information gets embedded into the
242 * AppWidget options and causes a callback to the AppWidgetProvider.
Adam Cohend2097eb2012-05-01 18:10:28 -0700243 * @see AppWidgetProvider#onAppWidgetOptionsChanged(Context, AppWidgetManager, int, Bundle)
Adam Cohene8724c82012-04-19 17:11:40 -0700244 *
Adam Cohena21bf0c2012-10-05 18:20:46 -0700245 * @param newOptions The bundle of options, in addition to the size information,
Adam Cohenc88d11f2012-04-27 18:33:18 -0700246 * can be null.
Adam Cohen4ddcd572012-11-01 17:36:32 -0700247 * @param minWidth The minimum width in dips that the widget will be displayed at.
248 * @param minHeight The maximum height in dips that the widget will be displayed at.
249 * @param maxWidth The maximum width in dips that the widget will be displayed at.
250 * @param maxHeight The maximum height in dips that the widget will be displayed at.
Adam Cohenc88d11f2012-04-27 18:33:18 -0700251 *
Adam Cohene8724c82012-04-19 17:11:40 -0700252 */
Adam Cohena21bf0c2012-10-05 18:20:46 -0700253 public void updateAppWidgetSize(Bundle newOptions, int minWidth, int minHeight, int maxWidth,
Adam Cohenc88d11f2012-04-27 18:33:18 -0700254 int maxHeight) {
Adam Cohen4ddcd572012-11-01 17:36:32 -0700255 updateAppWidgetSize(newOptions, minWidth, minHeight, maxWidth, maxHeight, false);
256 }
257
258 /**
259 * @hide
260 */
261 public void updateAppWidgetSize(Bundle newOptions, int minWidth, int minHeight, int maxWidth,
262 int maxHeight, boolean ignorePadding) {
Adam Cohena21bf0c2012-10-05 18:20:46 -0700263 if (newOptions == null) {
264 newOptions = new Bundle();
Adam Cohene8724c82012-04-19 17:11:40 -0700265 }
Adam Cohen88f041e2012-05-08 15:32:23 -0700266
267 Rect padding = new Rect();
268 if (mInfo != null) {
269 padding = getDefaultPaddingForWidget(mContext, mInfo.provider, padding);
270 }
Adam Cohenf065c962012-05-09 11:13:54 -0700271 float density = getResources().getDisplayMetrics().density;
Adam Cohen88f041e2012-05-08 15:32:23 -0700272
Adam Cohenf065c962012-05-09 11:13:54 -0700273 int xPaddingDips = (int) ((padding.left + padding.right) / density);
274 int yPaddingDips = (int) ((padding.top + padding.bottom) / density);
Adam Cohen88f041e2012-05-08 15:32:23 -0700275
Adam Cohen4ddcd572012-11-01 17:36:32 -0700276 int newMinWidth = minWidth - (ignorePadding ? 0 : xPaddingDips);
277 int newMinHeight = minHeight - (ignorePadding ? 0 : yPaddingDips);
278 int newMaxWidth = maxWidth - (ignorePadding ? 0 : xPaddingDips);
279 int newMaxHeight = maxHeight - (ignorePadding ? 0 : yPaddingDips);
Adam Cohena21bf0c2012-10-05 18:20:46 -0700280
281 AppWidgetManager widgetManager = AppWidgetManager.getInstance(mContext);
282
283 // We get the old options to see if the sizes have changed
284 Bundle oldOptions = widgetManager.getAppWidgetOptions(mAppWidgetId);
285 boolean needsUpdate = false;
286 if (newMinWidth != oldOptions.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH) ||
287 newMinHeight != oldOptions.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT) ||
288 newMaxWidth != oldOptions.getInt(AppWidgetManager.OPTION_APPWIDGET_MAX_WIDTH) ||
289 newMaxHeight != oldOptions.getInt(AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT)) {
290 needsUpdate = true;
291 }
292
293 if (needsUpdate) {
294 newOptions.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH, newMinWidth);
295 newOptions.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT, newMinHeight);
296 newOptions.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_WIDTH, newMaxWidth);
297 newOptions.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT, newMaxHeight);
298 updateAppWidgetOptions(newOptions);
299 }
Adam Cohene8724c82012-04-19 17:11:40 -0700300 }
301
Adam Cohenc88d11f2012-04-27 18:33:18 -0700302 /**
303 * Specify some extra information for the widget provider. Causes a callback to the
304 * AppWidgetProvider.
Adam Cohend2097eb2012-05-01 18:10:28 -0700305 * @see AppWidgetProvider#onAppWidgetOptionsChanged(Context, AppWidgetManager, int, Bundle)
Adam Cohenc88d11f2012-04-27 18:33:18 -0700306 *
Adam Cohend2097eb2012-05-01 18:10:28 -0700307 * @param options The bundle of options information.
Adam Cohenc88d11f2012-04-27 18:33:18 -0700308 */
Adam Cohend2097eb2012-05-01 18:10:28 -0700309 public void updateAppWidgetOptions(Bundle options) {
310 AppWidgetManager.getInstance(mContext).updateAppWidgetOptions(mAppWidgetId, options);
Adam Cohene8724c82012-04-19 17:11:40 -0700311 }
312
Jeff Sharkeyb8560b62009-08-06 21:24:25 -0700313 /** {@inheritDoc} */
314 @Override
315 public LayoutParams generateLayoutParams(AttributeSet attrs) {
316 // We're being asked to inflate parameters, probably by a LayoutInflater
317 // in a remote Context. To help resolve any remote references, we
318 // inflate through our last mRemoteContext when it exists.
319 final Context context = mRemoteContext != null ? mRemoteContext : mContext;
320 return new FrameLayout.LayoutParams(context, attrs);
321 }
322
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800323 /**
Joe Onoratoc27bb552010-06-23 17:44:30 -0700324 * Update the AppWidgetProviderInfo for this view, and reset it to the
325 * initial layout.
326 */
327 void resetAppWidget(AppWidgetProviderInfo info) {
328 mInfo = info;
329 mViewMode = VIEW_MODE_NOINIT;
330 updateAppWidget(null);
331 }
332
333 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800334 * Process a set of {@link RemoteViews} coming in as an update from the
Romain Guyae8dd292009-11-05 13:36:15 -0800335 * AppWidget provider. Will animate into these new views as needed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800336 */
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700337 public void updateAppWidget(RemoteViews remoteViews) {
Jim Millere667a7a2012-08-09 19:22:32 -0700338
Joe Onoratoc27bb552010-06-23 17:44:30 -0700339 if (LOGD) Log.d(TAG, "updateAppWidget called mOld=" + mOld);
Joe Onoratof140be62010-05-04 11:49:12 -0700340
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800341 boolean recycled = false;
342 View content = null;
343 Exception exception = null;
Jim Millere667a7a2012-08-09 19:22:32 -0700344
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800345 // Capture the old view into a bitmap so we can do the crossfade.
346 if (CROSSFADE) {
347 if (mFadeStartTime < 0) {
348 if (mView != null) {
349 final int width = mView.getWidth();
350 final int height = mView.getHeight();
351 try {
352 mOld = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
353 } catch (OutOfMemoryError e) {
354 // we just won't do the fade
355 mOld = null;
356 }
357 if (mOld != null) {
358 //mView.drawIntoBitmap(mOld);
359 }
360 }
361 }
362 }
Jim Millere667a7a2012-08-09 19:22:32 -0700363
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800364 if (remoteViews == null) {
365 if (mViewMode == VIEW_MODE_DEFAULT) {
366 // We've already done this -- nothing to do.
367 return;
368 }
369 content = getDefaultView();
370 mLayoutId = -1;
371 mViewMode = VIEW_MODE_DEFAULT;
372 } else {
Jeff Sharkeyb8560b62009-08-06 21:24:25 -0700373 // Prepare a local reference to the remote Context so we're ready to
374 // inflate any requested LayoutParams.
375 mRemoteContext = getRemoteContext(remoteViews);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800376 int layoutId = remoteViews.getLayoutId();
377
378 // If our stale view has been prepared to match active, and the new
379 // layout matches, try recycling it
380 if (content == null && layoutId == mLayoutId) {
381 try {
Jim Millere667a7a2012-08-09 19:22:32 -0700382 remoteViews.reapply(mContext, mView, mOnClickHandler);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800383 content = mView;
384 recycled = true;
385 if (LOGD) Log.d(TAG, "was able to recycled existing layout");
386 } catch (RuntimeException e) {
387 exception = e;
388 }
389 }
Jim Millere667a7a2012-08-09 19:22:32 -0700390
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800391 // Try normal RemoteView inflation
392 if (content == null) {
393 try {
Jim Millere667a7a2012-08-09 19:22:32 -0700394 content = remoteViews.apply(mContext, this, mOnClickHandler);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800395 if (LOGD) Log.d(TAG, "had to inflate new layout");
396 } catch (RuntimeException e) {
397 exception = e;
398 }
399 }
400
401 mLayoutId = layoutId;
402 mViewMode = VIEW_MODE_CONTENT;
403 }
Jim Millere667a7a2012-08-09 19:22:32 -0700404
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800405 if (content == null) {
406 if (mViewMode == VIEW_MODE_ERROR) {
407 // We've already done this -- nothing to do.
408 return ;
409 }
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700410 Log.w(TAG, "updateAppWidget couldn't find any view, using error view", exception);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800411 content = getErrorView();
412 mViewMode = VIEW_MODE_ERROR;
413 }
Jim Millere667a7a2012-08-09 19:22:32 -0700414
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800415 if (!recycled) {
416 prepareView(content);
417 addView(content);
418 }
419
420 if (mView != content) {
421 removeView(mView);
422 mView = content;
423 }
424
425 if (CROSSFADE) {
426 if (mFadeStartTime < 0) {
427 // if there is already an animation in progress, don't do anything --
428 // the new view will pop in on top of the old one during the cross fade,
429 // and that looks okay.
430 mFadeStartTime = SystemClock.uptimeMillis();
431 invalidate();
432 }
433 }
434 }
435
Jeff Sharkeyb8560b62009-08-06 21:24:25 -0700436 /**
Winson Chung499cb9f2010-07-16 11:18:17 -0700437 * Process data-changed notifications for the specified view in the specified
438 * set of {@link RemoteViews} views.
439 */
Winson Chung6394c0e2010-08-16 10:14:56 -0700440 void viewDataChanged(int viewId) {
Winson Chung499cb9f2010-07-16 11:18:17 -0700441 View v = findViewById(viewId);
442 if ((v != null) && (v instanceof AdapterView<?>)) {
443 AdapterView<?> adapterView = (AdapterView<?>) v;
444 Adapter adapter = adapterView.getAdapter();
445 if (adapter instanceof BaseAdapter) {
446 BaseAdapter baseAdapter = (BaseAdapter) adapter;
447 baseAdapter.notifyDataSetChanged();
Adam Cohen2148d432011-07-28 14:59:54 -0700448 } else if (adapter == null && adapterView instanceof RemoteAdapterConnectionCallback) {
449 // If the adapter is null, it may mean that the RemoteViewsAapter has not yet
450 // connected to its associated service, and hence the adapter hasn't been set.
451 // In this case, we need to defer the notify call until it has been set.
452 ((RemoteAdapterConnectionCallback) adapterView).deferNotifyDataSetChanged();
Winson Chung499cb9f2010-07-16 11:18:17 -0700453 }
454 }
455 }
456
457 /**
Jeff Sharkeyb8560b62009-08-06 21:24:25 -0700458 * Build a {@link Context} cloned into another package name, usually for the
459 * purposes of reading remote resources.
460 */
461 private Context getRemoteContext(RemoteViews views) {
462 // Bail if missing package name
463 final String packageName = views.getPackage();
464 if (packageName == null) return mContext;
465
466 try {
467 // Return if cloned successfully, otherwise default
468 return mContext.createPackageContext(packageName, Context.CONTEXT_RESTRICTED);
469 } catch (NameNotFoundException e) {
470 Log.e(TAG, "Package name " + packageName + " not found");
471 return mContext;
472 }
473 }
474
Gilles Debunne30301932010-06-16 18:32:00 -0700475 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800476 protected boolean drawChild(Canvas canvas, View child, long drawingTime) {
477 if (CROSSFADE) {
478 int alpha;
479 int l = child.getLeft();
480 int t = child.getTop();
481 if (mFadeStartTime > 0) {
482 alpha = (int)(((drawingTime-mFadeStartTime)*255)/FADE_DURATION);
483 if (alpha > 255) {
484 alpha = 255;
485 }
486 Log.d(TAG, "drawChild alpha=" + alpha + " l=" + l + " t=" + t
487 + " w=" + child.getWidth());
488 if (alpha != 255 && mOld != null) {
489 mOldPaint.setAlpha(255-alpha);
490 //canvas.drawBitmap(mOld, l, t, mOldPaint);
491 }
492 } else {
493 alpha = 255;
494 }
495 int restoreTo = canvas.saveLayerAlpha(l, t, child.getWidth(), child.getHeight(), alpha,
496 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.CLIP_TO_LAYER_SAVE_FLAG);
497 boolean rv = super.drawChild(canvas, child, drawingTime);
498 canvas.restoreToCount(restoreTo);
499 if (alpha < 255) {
500 invalidate();
501 } else {
502 mFadeStartTime = -1;
503 if (mOld != null) {
504 mOld.recycle();
505 mOld = null;
506 }
507 }
508 return rv;
509 } else {
510 return super.drawChild(canvas, child, drawingTime);
511 }
512 }
Jim Millere667a7a2012-08-09 19:22:32 -0700513
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800514 /**
515 * Prepare the given view to be shown. This might include adjusting
516 * {@link FrameLayout.LayoutParams} before inserting.
517 */
518 protected void prepareView(View view) {
Jeff Sharkeyb8560b62009-08-06 21:24:25 -0700519 // Take requested dimensions from child, but apply default gravity.
520 FrameLayout.LayoutParams requested = (FrameLayout.LayoutParams)view.getLayoutParams();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800521 if (requested == null) {
Romain Guy980a9382010-01-08 15:06:28 -0800522 requested = new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT,
523 LayoutParams.MATCH_PARENT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800524 }
Jeff Sharkeyb8560b62009-08-06 21:24:25 -0700525
526 requested.gravity = Gravity.CENTER;
527 view.setLayoutParams(requested);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800528 }
Jim Millere667a7a2012-08-09 19:22:32 -0700529
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800530 /**
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700531 * Inflate and return the default layout requested by AppWidget provider.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800532 */
533 protected View getDefaultView() {
Joe Onoratof140be62010-05-04 11:49:12 -0700534 if (LOGD) {
535 Log.d(TAG, "getDefaultView");
536 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800537 View defaultView = null;
538 Exception exception = null;
Jim Millere667a7a2012-08-09 19:22:32 -0700539
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800540 try {
541 if (mInfo != null) {
542 Context theirContext = mContext.createPackageContext(
Romain Guy870e09f2009-07-06 16:35:25 -0700543 mInfo.provider.getPackageName(), Context.CONTEXT_RESTRICTED);
Jeff Sharkey220dec22009-08-19 09:31:51 -0700544 mRemoteContext = theirContext;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800545 LayoutInflater inflater = (LayoutInflater)
546 theirContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
547 inflater = inflater.cloneInContext(theirContext);
548 inflater.setFilter(sInflaterFilter);
Adam Cohen0aa2d422012-09-07 17:37:26 -0700549 AppWidgetManager manager = AppWidgetManager.getInstance(mContext);
550 Bundle options = manager.getAppWidgetOptions(mAppWidgetId);
551
552 int layoutId = mInfo.initialLayout;
553 if (options.containsKey(AppWidgetManager.OPTION_APPWIDGET_HOST_CATEGORY)) {
554 int category = options.getInt(AppWidgetManager.OPTION_APPWIDGET_HOST_CATEGORY);
555 if (category == AppWidgetProviderInfo.WIDGET_CATEGORY_KEYGUARD) {
Adam Cohen180cfd52012-11-04 12:03:12 -0800556 int kgLayoutId = mInfo.initialKeyguardLayout;
557 // If a default keyguard layout is not specified, use the standard
558 // default layout.
559 layoutId = kgLayoutId == 0 ? layoutId : kgLayoutId;
Adam Cohen0aa2d422012-09-07 17:37:26 -0700560 }
561 }
562 defaultView = inflater.inflate(layoutId, this, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800563 } else {
564 Log.w(TAG, "can't inflate defaultView because mInfo is missing");
565 }
566 } catch (PackageManager.NameNotFoundException e) {
567 exception = e;
568 } catch (RuntimeException e) {
569 exception = e;
570 }
Jim Millere667a7a2012-08-09 19:22:32 -0700571
Jeff Sharkey220dec22009-08-19 09:31:51 -0700572 if (exception != null) {
573 Log.w(TAG, "Error inflating AppWidget " + mInfo + ": " + exception.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800574 }
Jim Millere667a7a2012-08-09 19:22:32 -0700575
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800576 if (defaultView == null) {
577 if (LOGD) Log.d(TAG, "getDefaultView couldn't find any view, so inflating error");
578 defaultView = getErrorView();
579 }
Jim Millere667a7a2012-08-09 19:22:32 -0700580
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800581 return defaultView;
582 }
Jim Millere667a7a2012-08-09 19:22:32 -0700583
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800584 /**
585 * Inflate and return a view that represents an error state.
586 */
587 protected View getErrorView() {
588 TextView tv = new TextView(mContext);
589 tv.setText(com.android.internal.R.string.gadget_host_error_inflating);
590 // TODO: get this color from somewhere.
591 tv.setBackgroundColor(Color.argb(127, 0, 0, 0));
592 return tv;
593 }
Romain Guy09ddc082009-08-11 12:10:08 -0700594
Guang Zhu1affb932012-05-15 14:25:51 -0700595 @Override
596 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
597 super.onInitializeAccessibilityNodeInfo(info);
598 info.setClassName(AppWidgetHostView.class.getName());
599 }
600
Romain Guy09ddc082009-08-11 12:10:08 -0700601 private static class ParcelableSparseArray extends SparseArray<Parcelable> implements Parcelable {
602 public int describeContents() {
603 return 0;
604 }
605
606 public void writeToParcel(Parcel dest, int flags) {
607 final int count = size();
608 dest.writeInt(count);
609 for (int i = 0; i < count; i++) {
610 dest.writeInt(keyAt(i));
611 dest.writeParcelable(valueAt(i), 0);
612 }
613 }
614
615 public static final Parcelable.Creator<ParcelableSparseArray> CREATOR =
616 new Parcelable.Creator<ParcelableSparseArray>() {
617 public ParcelableSparseArray createFromParcel(Parcel source) {
618 final ParcelableSparseArray array = new ParcelableSparseArray();
619 final ClassLoader loader = array.getClass().getClassLoader();
620 final int count = source.readInt();
621 for (int i = 0; i < count; i++) {
622 array.put(source.readInt(), source.readParcelable(loader));
623 }
624 return array;
625 }
626
627 public ParcelableSparseArray[] newArray(int size) {
628 return new ParcelableSparseArray[size];
629 }
630 };
631 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800632}