4444import android .widget .BaseAdapter ;
4545import android .widget .FrameLayout ;
4646import android .widget .RemoteViews ;
47+ import android .widget .RemoteViews .OnClickHandler ;
4748import android .widget .RemoteViewsAdapter .RemoteAdapterConnectionCallback ;
4849import android .widget .TextView ;
4950
@@ -83,18 +84,27 @@ public boolean onLoadClass(Class clazz) {
8384 long mFadeStartTime = -1 ;
8485 Bitmap mOld ;
8586 Paint mOldPaint = new Paint ();
86-
87+ private OnClickHandler mOnClickHandler ;
88+
8789 /**
8890 * Create a host view. Uses default fade animations.
8991 */
9092 public AppWidgetHostView (Context context ) {
9193 this (context , android .R .anim .fade_in , android .R .anim .fade_out );
9294 }
9395
96+ /**
97+ * @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+
94104 /**
95105 * Create a host view. Uses specified animations when pushing
96106 * {@link #updateAppWidget(RemoteViews)}.
97- *
107+ *
98108 * @param animationIn Resource ID of in animation to use
99109 * @param animationOut Resource ID of out animation to use
100110 */
@@ -108,6 +118,17 @@ public AppWidgetHostView(Context context, int animationIn, int animationOut) {
108118 setIsRootNamespace (true );
109119 }
110120
121+ /**
122+ * 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+
111132 /**
112133 * Set the AppWidget that will be displayed by this view. This method also adds default padding
113134 * to widgets, as described in {@link #getDefaultPaddingForWidget(Context, ComponentName, Rect)}
@@ -177,7 +198,7 @@ public static Rect getDefaultPaddingForWidget(Context context, ComponentName com
177198 public int getAppWidgetId () {
178199 return mAppWidgetId ;
179200 }
180-
201+
181202 public AppWidgetProviderInfo getAppWidgetInfo () {
182203 return mInfo ;
183204 }
@@ -281,12 +302,13 @@ void resetAppWidget(AppWidgetProviderInfo info) {
281302 * AppWidget provider. Will animate into these new views as needed
282303 */
283304 public void updateAppWidget (RemoteViews remoteViews ) {
305+
284306 if (LOGD ) Log .d (TAG , "updateAppWidget called mOld=" + mOld );
285307
286308 boolean recycled = false ;
287309 View content = null ;
288310 Exception exception = null ;
289-
311+
290312 // Capture the old view into a bitmap so we can do the crossfade.
291313 if (CROSSFADE ) {
292314 if (mFadeStartTime < 0 ) {
@@ -305,7 +327,7 @@ public void updateAppWidget(RemoteViews remoteViews) {
305327 }
306328 }
307329 }
308-
330+
309331 if (remoteViews == null ) {
310332 if (mViewMode == VIEW_MODE_DEFAULT ) {
311333 // We've already done this -- nothing to do.
@@ -324,19 +346,19 @@ public void updateAppWidget(RemoteViews remoteViews) {
324346 // layout matches, try recycling it
325347 if (content == null && layoutId == mLayoutId ) {
326348 try {
327- remoteViews .reapply (mContext , mView );
349+ remoteViews .reapply (mContext , mView , mOnClickHandler );
328350 content = mView ;
329351 recycled = true ;
330352 if (LOGD ) Log .d (TAG , "was able to recycled existing layout" );
331353 } catch (RuntimeException e ) {
332354 exception = e ;
333355 }
334356 }
335-
357+
336358 // Try normal RemoteView inflation
337359 if (content == null ) {
338360 try {
339- content = remoteViews .apply (mContext , this );
361+ content = remoteViews .apply (mContext , this , mOnClickHandler );
340362 if (LOGD ) Log .d (TAG , "had to inflate new layout" );
341363 } catch (RuntimeException e ) {
342364 exception = e ;
@@ -346,7 +368,7 @@ public void updateAppWidget(RemoteViews remoteViews) {
346368 mLayoutId = layoutId ;
347369 mViewMode = VIEW_MODE_CONTENT ;
348370 }
349-
371+
350372 if (content == null ) {
351373 if (mViewMode == VIEW_MODE_ERROR ) {
352374 // We've already done this -- nothing to do.
@@ -356,7 +378,7 @@ public void updateAppWidget(RemoteViews remoteViews) {
356378 content = getErrorView ();
357379 mViewMode = VIEW_MODE_ERROR ;
358380 }
359-
381+
360382 if (!recycled ) {
361383 prepareView (content );
362384 addView (content );
@@ -455,7 +477,7 @@ protected boolean drawChild(Canvas canvas, View child, long drawingTime) {
455477 return super .drawChild (canvas , child , drawingTime );
456478 }
457479 }
458-
480+
459481 /**
460482 * Prepare the given view to be shown. This might include adjusting
461483 * {@link FrameLayout.LayoutParams} before inserting.
@@ -471,7 +493,7 @@ protected void prepareView(View view) {
471493 requested .gravity = Gravity .CENTER ;
472494 view .setLayoutParams (requested );
473495 }
474-
496+
475497 /**
476498 * Inflate and return the default layout requested by AppWidget provider.
477499 */
@@ -481,7 +503,7 @@ protected View getDefaultView() {
481503 }
482504 View defaultView = null ;
483505 Exception exception = null ;
484-
506+
485507 try {
486508 if (mInfo != null ) {
487509 Context theirContext = mContext .createPackageContext (
@@ -500,19 +522,19 @@ protected View getDefaultView() {
500522 } catch (RuntimeException e ) {
501523 exception = e ;
502524 }
503-
525+
504526 if (exception != null ) {
505527 Log .w (TAG , "Error inflating AppWidget " + mInfo + ": " + exception .toString ());
506528 }
507-
529+
508530 if (defaultView == null ) {
509531 if (LOGD ) Log .d (TAG , "getDefaultView couldn't find any view, so inflating error" );
510532 defaultView = getErrorView ();
511533 }
512-
534+
513535 return defaultView ;
514536 }
515-
537+
516538 /**
517539 * Inflate and return a view that represents an error state.
518540 */
0 commit comments