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

Commit dd184f9f authored by Winson Chung's avatar Winson Chung Committed by Android (Google) Code Review
Browse files

Merge "Adding callback for widget size changed, and potentially other extra info"

parents 8c44c181 e8724c82
Loading
Loading
Loading
Loading
+11 −0
Original line number Diff line number Diff line
@@ -4303,10 +4303,13 @@ package android.appwidget {
    method protected void prepareView(android.view.View);
    method public void setAppWidget(int, android.appwidget.AppWidgetProviderInfo);
    method public void updateAppWidget(android.widget.RemoteViews);
    method public void updateAppWidgetExtras(android.os.Bundle);
    method public void updateAppWidgetSize(android.os.Bundle, int, int, int, int);
  }
  public class AppWidgetManager {
    method public void bindAppWidgetId(int, android.content.ComponentName);
    method public android.os.Bundle getAppWidgetExtras(int);
    method public int[] getAppWidgetIds(android.content.ComponentName);
    method public android.appwidget.AppWidgetProviderInfo getAppWidgetInfo(int);
    method public java.util.List<android.appwidget.AppWidgetProviderInfo> getInstalledProviders();
@@ -4318,14 +4321,21 @@ package android.appwidget {
    method public void updateAppWidget(int[], android.widget.RemoteViews);
    method public void updateAppWidget(int, android.widget.RemoteViews);
    method public void updateAppWidget(android.content.ComponentName, android.widget.RemoteViews);
    method public void updateAppWidgetExtras(int, android.os.Bundle);
    field public static final java.lang.String ACTION_APPWIDGET_CONFIGURE = "android.appwidget.action.APPWIDGET_CONFIGURE";
    field public static final java.lang.String ACTION_APPWIDGET_DELETED = "android.appwidget.action.APPWIDGET_DELETED";
    field public static final java.lang.String ACTION_APPWIDGET_DISABLED = "android.appwidget.action.APPWIDGET_DISABLED";
    field public static final java.lang.String ACTION_APPWIDGET_ENABLED = "android.appwidget.action.APPWIDGET_ENABLED";
    field public static final java.lang.String ACTION_APPWIDGET_EXTRAS_CHANGED = "android.appwidget.action.APPWIDGET_UPDATE_EXTRAS";
    field public static final java.lang.String ACTION_APPWIDGET_PICK = "android.appwidget.action.APPWIDGET_PICK";
    field public static final java.lang.String ACTION_APPWIDGET_UPDATE = "android.appwidget.action.APPWIDGET_UPDATE";
    field public static final java.lang.String EXTRA_APPWIDGET_EXTRAS = "appWidgetExtras";
    field public static final java.lang.String EXTRA_APPWIDGET_ID = "appWidgetId";
    field public static final java.lang.String EXTRA_APPWIDGET_IDS = "appWidgetIds";
    field public static final java.lang.String EXTRA_APPWIDGET_MAX_HEIGHT = "appWidgetMaxHeight";
    field public static final java.lang.String EXTRA_APPWIDGET_MAX_WIDTH = "appWidgetMaxWidth";
    field public static final java.lang.String EXTRA_APPWIDGET_MIN_HEIGHT = "appWidgetMinHeight";
    field public static final java.lang.String EXTRA_APPWIDGET_MIN_WIDTH = "appWidgetMinWidth";
    field public static final java.lang.String EXTRA_CUSTOM_EXTRAS = "customExtras";
    field public static final java.lang.String EXTRA_CUSTOM_INFO = "customInfo";
    field public static final int INVALID_APPWIDGET_ID = 0; // 0x0
@@ -4334,6 +4344,7 @@ package android.appwidget {
  public class AppWidgetProvider extends android.content.BroadcastReceiver {
    ctor public AppWidgetProvider();
    method public void onAppWidgetExtrasChanged(android.content.Context, android.appwidget.AppWidgetManager, int, android.os.Bundle);
    method public void onDeleted(android.content.Context, int[]);
    method public void onDisabled(android.content.Context);
    method public void onEnabled(android.content.Context);
+40 −0
Original line number Diff line number Diff line
@@ -28,6 +28,7 @@ import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Build;
import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;
import android.os.SystemClock;
@@ -206,6 +207,45 @@ public class AppWidgetHostView extends FrameLayout {
        super.dispatchRestoreInstanceState(jail);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int oldWidth = getMeasuredWidth();
        int oldHeight = getMeasuredHeight();
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int newWidth = getMeasuredWidth();
        int newHeight = getMeasuredHeight();

        // TODO: this is just a hack for now -- we actually have the AppWidgetHost
        // be responsible for updating the size of the widget.
        if (oldWidth != newWidth || oldHeight != newHeight) {
            final float density = mContext.getResources().getDisplayMetrics().density;
            final int newWidthDips = (int) (newWidth / density);
            final int newHeightDips = (int) (newHeight / density);
            updateAppWidgetSize(null, newWidthDips, newHeightDips, newWidthDips, newHeightDips);
        }
    }

    /**
     * Provide guidance about the size of this widget to the AppWidgetManager. This information
     * gets embedded into the AppWidgetExtras and causes a callback to the AppWidgetProvider.
     *
     *  @see AppWidgetProvider#onAppWidgetExtrasChanged(Context, AppWidgetManager, int, Bundle)
     */
    public void updateAppWidgetSize(Bundle extras, int minWidth, int minHeight, int maxWidth, int maxHeight) {
        if (extras == null) {
            extras = new Bundle();
        }
        extras.putInt(AppWidgetManager.EXTRA_APPWIDGET_MIN_WIDTH, minWidth);
        extras.putInt(AppWidgetManager.EXTRA_APPWIDGET_MIN_HEIGHT, minHeight);
        extras.putInt(AppWidgetManager.EXTRA_APPWIDGET_MAX_WIDTH, maxWidth);
        extras.putInt(AppWidgetManager.EXTRA_APPWIDGET_MAX_HEIGHT, maxHeight);
        updateAppWidgetExtras(extras);
    }

    public void updateAppWidgetExtras(Bundle extras) {
        AppWidgetManager.getInstance(mContext).updateAppWidgetExtras(mAppWidgetId, extras);
    }

    /** {@inheritDoc} */
    @Override
    public LayoutParams generateLayoutParams(AttributeSet attrs) {
+75 −0
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@ package android.appwidget;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.os.ServiceManager;
@@ -108,6 +109,32 @@ public class AppWidgetManager {
     */
    public static final String EXTRA_APPWIDGET_ID = "appWidgetId";

    /**
     * An bundle extra that contains the lower bound on the current width, in dips, of a widget instance.
     */
    public static final String EXTRA_APPWIDGET_MIN_WIDTH = "appWidgetMinWidth";

    /**
     * An bundle extra that contains the lower bound on the current height, in dips, of a widget instance.
     */
    public static final String EXTRA_APPWIDGET_MIN_HEIGHT = "appWidgetMinHeight";

    /**
     * An bundle extra that contains the upper bound on the current width, in dips, of a widget instance.
     */
    public static final String EXTRA_APPWIDGET_MAX_WIDTH = "appWidgetMaxWidth";

    /**
     * An bundle extra that contains the upper bound on the current width, in dips, of a widget instance.
     */
    public static final String EXTRA_APPWIDGET_MAX_HEIGHT = "appWidgetMaxHeight";

    /**
     * An intent extra which points to a bundle of extra information for a particular widget id.
     * In particular this bundle can contain EXTRA_APPWIDGET_WIDTH and EXTRA_APPWIDGET_HEIGHT.
     */
    public static final String EXTRA_APPWIDGET_EXTRAS = "appWidgetExtras";

    /**
     * An intent extra that contains multiple appWidgetIds.
     * <p>
@@ -160,6 +187,14 @@ public class AppWidgetManager {
     */
    public static final String ACTION_APPWIDGET_UPDATE = "android.appwidget.action.APPWIDGET_UPDATE";

    /**
     * Sent when the custom extras for an AppWidget change.
     *
     * @see AppWidgetProvider#onAppWidgetExtrasChanged AppWidgetProvider#onAppWidgetExtrasChanged(
     *      Context context, AppWidgetManager appWidgetManager, int appWidgetId, Bundle newExtras)
     */
    public static final String ACTION_APPWIDGET_EXTRAS_CHANGED = "android.appwidget.action.APPWIDGET_UPDATE_EXTRAS";

    /**
     * Sent when an instance of an AppWidget is deleted from its host.
     *
@@ -251,6 +286,46 @@ public class AppWidgetManager {
        }
    }

    /**
     * Update the extras for a given widget instance.
     *
     * The extras can be used to embed additional information about this widget to be accessed
     * by the associated widget's AppWidgetProvider.
     *
     * @see #getAppWidgetExtras(int)
     *
     * @param appWidgetId    The AppWidget instances for which to set the RemoteViews.
     * @param extras         The extras to associate with this widget
     */
    public void updateAppWidgetExtras(int appWidgetId, Bundle extras) {
        try {
            sService.updateAppWidgetExtras(appWidgetId, extras);
        }
        catch (RemoteException e) {
            throw new RuntimeException("system server dead?", e);
        }
    }

    /**
     * Get the extras associated with a given widget instance.
     *
     * The extras can be used to embed additional information about this widget to be accessed
     * by the associated widget's AppWidgetProvider.
     *
     * @see #updateAppWidgetExtras(int, Bundle)
     *
     * @param appWidgetId     The AppWidget instances for which to set the RemoteViews.
     * @return                The extras associated with the given widget instance.
     */
    public Bundle getAppWidgetExtras(int appWidgetId) {
        try {
            return sService.getAppWidgetExtras(appWidgetId);
        }
        catch (RemoteException e) {
            throw new RuntimeException("system server dead?", e);
        }
    }

    /**
     * Set the RemoteViews to use for the specified appWidgetId.
     *
+31 −2
Original line number Diff line number Diff line
@@ -74,6 +74,16 @@ public class AppWidgetProvider extends BroadcastReceiver {
                this.onDeleted(context, new int[] { appWidgetId });
            }
        }
        else if (AppWidgetManager.ACTION_APPWIDGET_EXTRAS_CHANGED.equals(action)) {
            Bundle extras = intent.getExtras();
            if (extras != null && extras.containsKey(AppWidgetManager.EXTRA_APPWIDGET_ID)
                    && extras.containsKey(AppWidgetManager.EXTRA_APPWIDGET_EXTRAS)) {
                int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID);
                Bundle widgetExtras = extras.getBundle(AppWidgetManager.EXTRA_APPWIDGET_EXTRAS);
                this.onAppWidgetExtrasChanged(context, AppWidgetManager.getInstance(context),
                        appWidgetId, widgetExtras);
            }
        }
        else if (AppWidgetManager.ACTION_APPWIDGET_ENABLED.equals(action)) {
            this.onEnabled(context);
        }
@@ -103,6 +113,25 @@ public class AppWidgetProvider extends BroadcastReceiver {
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    }

    /**
     * Called in response to the {@link AppWidgetManager#ACTION_APPWIDGET_EXTRAS_CHANGED}
     * broadcast when this widget has been layed out at a new size.
     *
     * {@more}
     *
     * @param context   The {@link android.content.Context Context} in which this receiver is
     *                  running.
     * @param appWidgetManager A {@link AppWidgetManager} object you can call {@link
     *                  AppWidgetManager#updateAppWidget} on.
     * @param appWidgetId The appWidgetId of the widget who's size changed.
     * @param newExtras The appWidgetId of the widget who's size changed.
     *
     * @see AppWidgetManager#ACTION_APPWIDGET_EXTRAS_CHANGED
     */
    public void onAppWidgetExtrasChanged(Context context, AppWidgetManager appWidgetManager,
            int appWidgetId, Bundle newExtras) {
    }

    /**
     * Called in response to the {@link AppWidgetManager#ACTION_APPWIDGET_DELETED} broadcast when
     * one or more AppWidget instances have been deleted.  Override this method to implement
+3 −0
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@ import android.content.ComponentName;
import android.content.Intent;
import android.appwidget.AppWidgetProviderInfo;
import com.android.internal.appwidget.IAppWidgetHost;
import android.os.Bundle;
import android.os.IBinder;
import android.widget.RemoteViews;

@@ -42,6 +43,8 @@ interface IAppWidgetService {
    // for AppWidgetManager
    //
    void updateAppWidgetIds(in int[] appWidgetIds, in RemoteViews views);
    void updateAppWidgetExtras(int appWidgetId, in Bundle extras);
    Bundle getAppWidgetExtras(int appWidgetId);
    void partiallyUpdateAppWidgetIds(in int[] appWidgetIds, in RemoteViews views);
    void updateAppWidgetProvider(in ComponentName provider, in RemoteViews views);
    void notifyAppWidgetViewDataChanged(in int[] appWidgetIds, int viewId);
Loading