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

Commit 090d5b0c authored by Jeff DeCew's avatar Jeff DeCew
Browse files

Add (still hidden) API to promote the big picture to the collapsed state

Bug: 177698229
Test: atest NotificationTemplateApi30Test
Test: atest CtsAppTestCases:NotificationTemplateTest
Change-Id: I9df6edb4cc7c49430e3c0f3c231d44c1fceab440
parent 2ffea6d3
Loading
Loading
Loading
Loading
+96 −4
Original line number Diff line number Diff line
@@ -1197,6 +1197,15 @@ public class Notification implements Parcelable
    public static final String EXTRA_PICTURE_CONTENT_DESCRIPTION =
            "android.pictureContentDescription";

    /**
     * {@link #extras} key: this is a boolean to indicate that the
     * {@link BigPictureStyle#bigPicture(Bitmap) big picture} is to be shown in the collapsed state
     * of a {@link BigPictureStyle} notification.  This will replace a
     * {@link Builder#setLargeIcon(Icon) large icon} in that state if one was provided.
     * @hide
     */
    public static final String EXTRA_PROMOTE_PICTURE = "android.promotePicture";

    /**
     * {@link #extras} key: An array of CharSequences to show in {@link InboxStyle} expanded
     * notifications, each of which was supplied to {@link InboxStyle#addLine(CharSequence)}.
@@ -5149,6 +5158,7 @@ public class Notification implements Parcelable
        // changes are entirely visual, and should otherwise be undetectable by apps.
        @SuppressWarnings("AndroidFrameworkCompatChange")
        private void calculateLargeIconDimens(boolean largeIconShown,
                @NonNull StandardTemplateParams p,
                @NonNull TemplateBindResult result) {
            final Resources resources = mContext.getResources();
            final float density = resources.getDisplayMetrics().density;
@@ -5159,8 +5169,8 @@ public class Notification implements Parcelable
            final float viewHeightDp = resources.getDimension(
                    R.dimen.notification_right_icon_size) / density;
            float viewWidthDp = viewHeightDp;  // icons are 1:1 by default
            if (largeIconShown && (
                    mContext.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.S
            if (largeIconShown && (p.mPromotePicture
                    || mContext.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.S
                    || DevFlags.shouldBackportSNotifRules(mContext.getContentResolver()))) {
                Drawable drawable = mN.mLargeIcon.loadDrawable(mContext);
                if (drawable != null) {
@@ -5187,7 +5197,7 @@ public class Notification implements Parcelable
                mN.mLargeIcon = Icon.createWithBitmap(mN.largeIcon);
            }
            boolean showLargeIcon = mN.mLargeIcon != null && !p.hideLargeIcon;
            calculateLargeIconDimens(showLargeIcon, result);
            calculateLargeIconDimens(showLargeIcon, p, result);
            if (showLargeIcon) {
                contentView.setViewLayoutWidth(R.id.right_icon,
                        result.mRightIconWidthDp, TypedValue.COMPLEX_UNIT_DIP);
@@ -6995,6 +7005,7 @@ public class Notification implements Parcelable
        private Icon mBigLargeIcon;
        private boolean mBigLargeIconSet = false;
        private CharSequence mPictureContentDescription;
        private boolean mPromotePicture;

        public BigPictureStyle() {
        }
@@ -7049,6 +7060,18 @@ public class Notification implements Parcelable
            return this;
        }

        /**
         * When set, the {@link #bigPicture(Bitmap) big picture} of this style will be promoted and
         * shown in place of the {@link Builder#setLargeIcon(Icon) large icon} in the collapsed
         * state of this notification.
         * @hide
         */
        @NonNull
        public BigPictureStyle showBigPictureWhenCollapsed(boolean show) {
            mPromotePicture = show;
            return this;
        }

        /**
         * Override the large icon when the big notification is shown.
         */
@@ -7109,6 +7132,66 @@ public class Notification implements Parcelable
            }
        }

        /**
         * @hide
         */
        @Override
        public RemoteViews makeContentView(boolean increasedHeight) {
            if (mPicture == null || !mPromotePicture) {
                return super.makeContentView(increasedHeight);
            }

            Icon oldLargeIcon = mBuilder.mN.mLargeIcon;
            mBuilder.mN.mLargeIcon = Icon.createWithBitmap(mPicture);
            // The legacy largeIcon might not allow us to clear the image, as it's taken in
            // replacement if the other one is null. Because we're restoring these legacy icons
            // for old listeners, this is in general non-null.
            Bitmap largeIconLegacy = mBuilder.mN.largeIcon;
            mBuilder.mN.largeIcon = null;

            StandardTemplateParams p = mBuilder.mParams.reset()
                    .viewType(StandardTemplateParams.VIEW_TYPE_NORMAL)
                    .fillTextsFrom(mBuilder)
                    .promotePicture(true);
            RemoteViews contentView = getStandardView(mBuilder.getBaseLayoutResource(),
                    p, null /* result */);

            mBuilder.mN.mLargeIcon = oldLargeIcon;
            mBuilder.mN.largeIcon = largeIconLegacy;

            return contentView;
        }

        /**
         * @hide
         */
        @Override
        public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
            if (mPicture == null || !mPromotePicture) {
                return super.makeHeadsUpContentView(increasedHeight);
            }

            Icon oldLargeIcon = mBuilder.mN.mLargeIcon;
            mBuilder.mN.mLargeIcon = Icon.createWithBitmap(mPicture);
            // The legacy largeIcon might not allow us to clear the image, as it's taken in
            // replacement if the other one is null. Because we're restoring these legacy icons
            // for old listeners, this is in general non-null.
            Bitmap largeIconLegacy = mBuilder.mN.largeIcon;
            mBuilder.mN.largeIcon = null;

            StandardTemplateParams p = mBuilder.mParams.reset()
                    .viewType(StandardTemplateParams.VIEW_TYPE_HEADS_UP)
                    .fillTextsFrom(mBuilder)
                    .promotePicture(true);
            RemoteViews contentView = getStandardView(mBuilder.getHeadsUpBaseLayoutResource(),
                    p, null /* result */);

            mBuilder.mN.mLargeIcon = oldLargeIcon;
            mBuilder.mN.largeIcon = largeIconLegacy;

            return contentView;
        }

        /**
         * @hide
         */
@@ -7168,6 +7251,7 @@ public class Notification implements Parcelable
                extras.putCharSequence(EXTRA_PICTURE_CONTENT_DESCRIPTION,
                        mPictureContentDescription);
            }
            extras.putBoolean(EXTRA_PROMOTE_PICTURE, mPromotePicture);
            extras.putParcelable(EXTRA_PICTURE, mPicture);
        }

@@ -7188,6 +7272,7 @@ public class Notification implements Parcelable
                        extras.getCharSequence(EXTRA_PICTURE_CONTENT_DESCRIPTION);
            }

            mPromotePicture = extras.getBoolean(EXTRA_PROMOTE_PICTURE);
            mPicture = extras.getParcelable(EXTRA_PICTURE);
        }

@@ -11306,6 +11391,7 @@ public class Notification implements Parcelable
        boolean mHideTitle;
        boolean mHideActions;
        boolean mHideProgress;
        boolean mPromotePicture;
        CharSequence title;
        CharSequence text;
        CharSequence headerTextSecondary;
@@ -11321,6 +11407,7 @@ public class Notification implements Parcelable
            mHideTitle = false;
            mHideActions = false;
            mHideProgress = false;
            mPromotePicture = false;
            title = null;
            text = null;
            summaryText = null;
@@ -11360,6 +11447,11 @@ public class Notification implements Parcelable
            return this;
        }

        final StandardTemplateParams promotePicture(boolean promotePicture) {
            this.mPromotePicture = promotePicture;
            return this;
        }

        final StandardTemplateParams title(CharSequence title) {
            this.title = title;
            return this;