Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit e667a7ad authored by Jim Miller's avatar Jim Miller
Browse files

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
parent f337a89b
Loading
Loading
Loading
Loading
+18 −6
Original line number Diff line number Diff line
@@ -29,6 +29,7 @@ import android.os.ServiceManager;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.widget.RemoteViews;
import android.widget.RemoteViews.OnClickHandler;

import com.android.internal.appwidget.IAppWidgetHost;
import com.android.internal.appwidget.IAppWidgetService;
@@ -111,10 +112,19 @@ public class AppWidgetHost {
    int mHostId;
    Callbacks mCallbacks = new Callbacks();
    final HashMap<Integer,AppWidgetHostView> mViews = new HashMap<Integer, AppWidgetHostView>();
    private OnClickHandler mOnClickHandler;

    public AppWidgetHost(Context context, int hostId) {
        this(context, hostId, null);
    }

    /**
     * @hide
     */
    public AppWidgetHost(Context context, int hostId, OnClickHandler handler) {
        mContext = context;
        mHostId = hostId;
        mOnClickHandler = handler;
        mHandler = new UpdateHandler(context.getMainLooper());
        mDisplayMetrics = context.getResources().getDisplayMetrics();
        synchronized (sServiceLock) {
@@ -235,6 +245,7 @@ public class AppWidgetHost {
    public final AppWidgetHostView createView(Context context, int appWidgetId,
            AppWidgetProviderInfo appWidget) {
        AppWidgetHostView view = onCreateView(context, appWidgetId, appWidget);
        view.setOnClickHandler(mOnClickHandler);
        view.setAppWidget(appWidgetId, appWidget);
        synchronized (mViews) {
            mViews.put(appWidgetId, view);
@@ -246,6 +257,7 @@ public class AppWidgetHost {
            throw new RuntimeException("system server dead?", e);
        }
        view.updateAppWidget(views);

        return view;
    }

@@ -255,7 +267,7 @@ public class AppWidgetHost {
     */
    protected AppWidgetHostView onCreateView(Context context, int appWidgetId,
            AppWidgetProviderInfo appWidget) {
        return new AppWidgetHostView(context);
        return new AppWidgetHostView(context, mOnClickHandler);
    }

    /**
+39 −17
Original line number Diff line number Diff line
@@ -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,6 +84,7 @@ 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.
@@ -91,6 +93,14 @@ public class AppWidgetHostView extends FrameLayout {
        this(context, android.R.anim.fade_in, android.R.anim.fade_out);
    }

    /**
     * @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)}.
@@ -108,6 +118,17 @@ public class AppWidgetHostView extends FrameLayout {
        setIsRootNamespace(true);
    }

    /**
     * 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)}
@@ -281,6 +302,7 @@ 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;
@@ -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");
@@ -336,7 +358,7 @@ public class AppWidgetHostView extends FrameLayout {
            // 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;
+44 −47
Original line number Diff line number Diff line
@@ -1671,11 +1671,7 @@ public class RemoteViews implements Parcelable, Filter {
     * layout direction), or 0
     * @param top The id of a drawable to place above the text, or 0
     * @param end The id of a drawable to place after the text, or 0
<<<<<<< HEAD
     * @param bottom The id of a drawable to place below the text, or 0
=======
     * @param bottom The id of a drawable to place below the text, or 0
>>>>>>> 0a43f67e
     */
    public void setTextViewCompoundDrawablesRelative(int viewId, int start, int top, int end, int bottom) {
        addAction(new TextViewDrawableAction(viewId, true, start, top, end, bottom));
@@ -2114,7 +2110,7 @@ public class RemoteViews implements Parcelable, Filter {
     * @return The inflated view hierarchy
     */
    public View apply(Context context, ViewGroup parent) {
        return apply(context, parent, DEFAULT_ON_CLICK_HANDLER);
        return apply(context, parent, null);
    }

    /** @hide */
@@ -2147,7 +2143,7 @@ public class RemoteViews implements Parcelable, Filter {
     * the {@link #apply(Context,ViewGroup)} call.
     */
    public void reapply(Context context, View v) {
        reapply(context, v, DEFAULT_ON_CLICK_HANDLER);
        reapply(context, v, null);
    }

    /** @hide */
@@ -2170,6 +2166,7 @@ public class RemoteViews implements Parcelable, Filter {

    private void performApply(View v, ViewGroup parent, OnClickHandler handler) {
        if (mActions != null) {
            handler = handler == null ? DEFAULT_ON_CLICK_HANDLER : handler;
            final int count = mActions.size();
            for (int i = 0; i < count; i++) {
                Action a = mActions.get(i);