summaryrefslogtreecommitdiff
path: root/core/java/android/appwidget/AppWidgetHostView.java
diff options
context:
space:
mode:
authorJim Miller <jaggies@google.com>2012-08-09 19:22:32 -0700
committerJim Miller <jaggies@google.com>2012-08-20 15:44:05 -0700
commite667a7add46a6389c64f2105bd33943cfe6a3fa4 (patch)
tree7667592bb1a89479e0569421e016e2aaac53799c /core/java/android/appwidget/AppWidgetHostView.java
parentf337a89b4d6f6fd0a49b6edd7f895f06cb96d28b (diff)
Update AppWidgetHost with better support for OnClickHandlers
This updates AppWidgetHost and AppWidgetHostView to do a better job at refreshing widgets and their host views. It now allows an OnClickHandler to be specified when creating the AppWidgetHost which allows it to correctly update AppWidgetHostViews when needed. Change-Id: I710c1d00a8d145bf3a9fd5f5691885bec9d1c7e4
Diffstat (limited to 'core/java/android/appwidget/AppWidgetHostView.java')
-rw-r--r--core/java/android/appwidget/AppWidgetHostView.java56
1 files changed, 39 insertions, 17 deletions
diff --git a/core/java/android/appwidget/AppWidgetHostView.java b/core/java/android/appwidget/AppWidgetHostView.java
index ed95ae587de8..603ceb7626f5 100644
--- a/core/java/android/appwidget/AppWidgetHostView.java
+++ b/core/java/android/appwidget/AppWidgetHostView.java
@@ -44,6 +44,7 @@ import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.RemoteViews;
+import android.widget.RemoteViews.OnClickHandler;
import android.widget.RemoteViewsAdapter.RemoteAdapterConnectionCallback;
import android.widget.TextView;
@@ -83,7 +84,8 @@ public class AppWidgetHostView extends FrameLayout {
long mFadeStartTime = -1;
Bitmap mOld;
Paint mOldPaint = new Paint();
-
+ private OnClickHandler mOnClickHandler;
+
/**
* Create a host view. Uses default fade animations.
*/
@@ -92,9 +94,17 @@ public class AppWidgetHostView extends FrameLayout {
}
/**
+ * @hide
+ */
+ public AppWidgetHostView(Context context, OnClickHandler handler) {
+ this(context, android.R.anim.fade_in, android.R.anim.fade_out);
+ mOnClickHandler = handler;
+ }
+
+ /**
* Create a host view. Uses specified animations when pushing
* {@link #updateAppWidget(RemoteViews)}.
- *
+ *
* @param animationIn Resource ID of in animation to use
* @param animationOut Resource ID of out animation to use
*/
@@ -109,6 +119,17 @@ public class AppWidgetHostView extends FrameLayout {
}
/**
+ * Pass the given handler to RemoteViews when updating this widget. Unless this
+ * is done immediatly after construction, a call to {@link #updateAppWidget(RemoteViews)}
+ * should be made.
+ * @param handler
+ * @hide
+ */
+ public void setOnClickHandler(OnClickHandler handler) {
+ mOnClickHandler = handler;
+ }
+
+ /**
* Set the AppWidget that will be displayed by this view. This method also adds default padding
* to widgets, as described in {@link #getDefaultPaddingForWidget(Context, ComponentName, Rect)}
* and can be overridden in order to add custom padding.
@@ -177,7 +198,7 @@ public class AppWidgetHostView extends FrameLayout {
public int getAppWidgetId() {
return mAppWidgetId;
}
-
+
public AppWidgetProviderInfo getAppWidgetInfo() {
return mInfo;
}
@@ -281,12 +302,13 @@ public class AppWidgetHostView extends FrameLayout {
* AppWidget provider. Will animate into these new views as needed
*/
public void updateAppWidget(RemoteViews remoteViews) {
+
if (LOGD) Log.d(TAG, "updateAppWidget called mOld=" + mOld);
boolean recycled = false;
View content = null;
Exception exception = null;
-
+
// Capture the old view into a bitmap so we can do the crossfade.
if (CROSSFADE) {
if (mFadeStartTime < 0) {
@@ -305,7 +327,7 @@ public class AppWidgetHostView extends FrameLayout {
}
}
}
-
+
if (remoteViews == null) {
if (mViewMode == VIEW_MODE_DEFAULT) {
// We've already done this -- nothing to do.
@@ -324,7 +346,7 @@ public class AppWidgetHostView extends FrameLayout {
// layout matches, try recycling it
if (content == null && layoutId == mLayoutId) {
try {
- remoteViews.reapply(mContext, mView);
+ remoteViews.reapply(mContext, mView, mOnClickHandler);
content = mView;
recycled = true;
if (LOGD) Log.d(TAG, "was able to recycled existing layout");
@@ -332,11 +354,11 @@ public class AppWidgetHostView extends FrameLayout {
exception = e;
}
}
-
+
// Try normal RemoteView inflation
if (content == null) {
try {
- content = remoteViews.apply(mContext, this);
+ content = remoteViews.apply(mContext, this, mOnClickHandler);
if (LOGD) Log.d(TAG, "had to inflate new layout");
} catch (RuntimeException e) {
exception = e;
@@ -346,7 +368,7 @@ public class AppWidgetHostView extends FrameLayout {
mLayoutId = layoutId;
mViewMode = VIEW_MODE_CONTENT;
}
-
+
if (content == null) {
if (mViewMode == VIEW_MODE_ERROR) {
// We've already done this -- nothing to do.
@@ -356,7 +378,7 @@ public class AppWidgetHostView extends FrameLayout {
content = getErrorView();
mViewMode = VIEW_MODE_ERROR;
}
-
+
if (!recycled) {
prepareView(content);
addView(content);
@@ -455,7 +477,7 @@ public class AppWidgetHostView extends FrameLayout {
return super.drawChild(canvas, child, drawingTime);
}
}
-
+
/**
* Prepare the given view to be shown. This might include adjusting
* {@link FrameLayout.LayoutParams} before inserting.
@@ -471,7 +493,7 @@ public class AppWidgetHostView extends FrameLayout {
requested.gravity = Gravity.CENTER;
view.setLayoutParams(requested);
}
-
+
/**
* Inflate and return the default layout requested by AppWidget provider.
*/
@@ -481,7 +503,7 @@ public class AppWidgetHostView extends FrameLayout {
}
View defaultView = null;
Exception exception = null;
-
+
try {
if (mInfo != null) {
Context theirContext = mContext.createPackageContext(
@@ -500,19 +522,19 @@ public class AppWidgetHostView extends FrameLayout {
} catch (RuntimeException e) {
exception = e;
}
-
+
if (exception != null) {
Log.w(TAG, "Error inflating AppWidget " + mInfo + ": " + exception.toString());
}
-
+
if (defaultView == null) {
if (LOGD) Log.d(TAG, "getDefaultView couldn't find any view, so inflating error");
defaultView = getErrorView();
}
-
+
return defaultView;
}
-
+
/**
* Inflate and return a view that represents an error state.
*/