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

Commit 97bfa6b5 authored by Anna Zappone's avatar Anna Zappone Committed by Automerger Merge Worker
Browse files

Merge "Create RemoteViews for all supported app widget sizes" into sc-dev am: 5ce1b8c6

Original change: https://googleplex-android-review.googlesource.com/c/platform/frameworks/base/+/15079800

Change-Id: Ie0536104c76393a5f3f5314437e1b3101a1efbb3
parents bb0ca9c6 5ce1b8c6
Loading
Loading
Loading
Loading
+54 −15
Original line number Diff line number Diff line
@@ -29,6 +29,7 @@ import static android.appwidget.AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT;
import static android.appwidget.AppWidgetManager.OPTION_APPWIDGET_MAX_WIDTH;
import static android.appwidget.AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT;
import static android.appwidget.AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH;
import static android.appwidget.AppWidgetManager.OPTION_APPWIDGET_SIZES;
import static android.util.TypedValue.COMPLEX_UNIT_DIP;
import static android.util.TypedValue.COMPLEX_UNIT_PX;

@@ -43,7 +44,6 @@ import android.app.people.ConversationStatus;
import android.app.people.PeopleSpaceTile;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
@@ -59,6 +59,7 @@ import android.text.TextUtils;
import android.util.IconDrawableFactory;
import android.util.Log;
import android.util.Pair;
import android.util.SizeF;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
@@ -85,8 +86,10 @@ import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Function;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
@@ -173,28 +176,64 @@ public class PeopleTileViewHelper {
    private Locale mLocale;
    private NumberFormat mIntegerFormat;

    public PeopleTileViewHelper(Context context, @Nullable PeopleSpaceTile tile,
            int appWidgetId, Bundle options, PeopleTileKey key) {
    PeopleTileViewHelper(Context context, @Nullable PeopleSpaceTile tile,
            int appWidgetId, int width, int height, PeopleTileKey key) {
        mContext = context;
        mTile = tile;
        mKey = key;
        mAppWidgetId = appWidgetId;
        mDensity = mContext.getResources().getDisplayMetrics().density;
        int display = mContext.getResources().getConfiguration().orientation;
        mWidth = display == Configuration.ORIENTATION_PORTRAIT
                ? options.getInt(OPTION_APPWIDGET_MIN_WIDTH,
                getSizeInDp(R.dimen.default_width)) : options.getInt(
                OPTION_APPWIDGET_MAX_WIDTH,
                getSizeInDp(R.dimen.default_width));
        mHeight = display == Configuration.ORIENTATION_PORTRAIT ? options.getInt(
                OPTION_APPWIDGET_MAX_HEIGHT,
                getSizeInDp(R.dimen.default_height))
                : options.getInt(OPTION_APPWIDGET_MIN_HEIGHT,
                        getSizeInDp(R.dimen.default_height));
        mWidth = width;
        mHeight = height;
        mLayoutSize = getLayoutSize();
    }

    public RemoteViews getViews() {
    /**
     * Creates a {@link RemoteViews} for the specified arguments. The RemoteViews will support all
     * the sizes present in {@code options.}.
     */
    public static RemoteViews createRemoteViews(Context context, @Nullable PeopleSpaceTile tile,
            int appWidgetId, Bundle options, PeopleTileKey key) {
        List<SizeF> widgetSizes = getWidgetSizes(context, options);
        Map<SizeF, RemoteViews> sizeToRemoteView =
                widgetSizes
                        .stream()
                        .distinct()
                        .collect(Collectors.toMap(
                                Function.identity(),
                                size -> new PeopleTileViewHelper(
                                        context, tile, appWidgetId,
                                        (int) size.getWidth(),
                                        (int) size.getHeight(),
                                        key)
                                        .getViews()));
        return new RemoteViews(sizeToRemoteView);
    }

    private static List<SizeF> getWidgetSizes(Context context, Bundle options) {
        float density = context.getResources().getDisplayMetrics().density;
        List<SizeF> widgetSizes = options.getParcelableArrayList(OPTION_APPWIDGET_SIZES);
        // If the full list of sizes was provided in the options bundle, use that.
        if (widgetSizes != null && !widgetSizes.isEmpty()) return widgetSizes;

        // Otherwise, create a list using the portrait/landscape sizes.
        int defaultWidth = getSizeInDp(context, R.dimen.default_width, density);
        int defaultHeight =  getSizeInDp(context, R.dimen.default_height, density);
        widgetSizes = new ArrayList<>(2);

        int portraitWidth = options.getInt(OPTION_APPWIDGET_MIN_WIDTH, defaultWidth);
        int portraitHeight = options.getInt(OPTION_APPWIDGET_MAX_HEIGHT, defaultHeight);
        widgetSizes.add(new SizeF(portraitWidth, portraitHeight));

        int landscapeWidth = options.getInt(OPTION_APPWIDGET_MAX_WIDTH, defaultWidth);
        int landscapeHeight = options.getInt(OPTION_APPWIDGET_MIN_HEIGHT, defaultHeight);
        widgetSizes.add(new SizeF(landscapeWidth, landscapeHeight));

        return widgetSizes;
    }

    @VisibleForTesting
    RemoteViews getViews() {
        RemoteViews viewsForTile = getViewForTile();
        int maxAvatarSize = getMaxAvatarSize(viewsForTile);
        RemoteViews views = setCommonRemoteViewsFields(viewsForTile, maxAvatarSize);
+4 −4
Original line number Diff line number Diff line
@@ -297,8 +297,8 @@ public class PeopleSpaceWidgetManager {
            Log.e(TAG, "Cannot update invalid widget");
            return;
        }
        RemoteViews views = new PeopleTileViewHelper(mContext, tile, appWidgetId,
                options, key).getViews();
        RemoteViews views = PeopleTileViewHelper.createRemoteViews(mContext, tile, appWidgetId,
                options, key);

        // Tell the AppWidgetManager to perform an update on the current app widget.
        mAppWidgetManager.updateAppWidget(appWidgetId, views);
@@ -1031,8 +1031,8 @@ public class PeopleSpaceWidgetManager {
                Optional.empty());

        if (DEBUG) Log.i(TAG, "Returning tile preview for shortcutId: " + shortcutId);
        return new PeopleTileViewHelper(mContext, augmentedTile, 0, options,
                new PeopleTileKey(augmentedTile)).getViews();
        return PeopleTileViewHelper.createRemoteViews(mContext, augmentedTile, 0, options,
                new PeopleTileKey(augmentedTile));
    }

    protected final BroadcastReceiver mBaseBroadcastReceiver = new BroadcastReceiver() {
+76 −131

File changed.

Preview size limit exceeded, changes collapsed.