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

Commit 51be14de authored by Xiaowen Lei's avatar Xiaowen Lei
Browse files

Use NotificationProgressDrawable for ProgressStyle notifications.

Also tint the indeterminate drawable with the color passed from the API.

Fixed a couple of bugs in NotificationProgressDrawable, caught by the
tests.

Flag: android.app.api_rich_ongoing
Bug: 367805202
Fix: 372907485
Test: post a ProgressStyle Notification
Test: patch ag/29675286 and run ProgressStyleNotificationScreenshotTest
Change-Id: I2449f755fa359278351986f17b8744f6e2bc5827
parent 735aa554
Loading
Loading
Loading
Loading
+45 −4
Original line number Diff line number Diff line
@@ -20,16 +20,20 @@ import android.annotation.NonNull;
import android.annotation.Nullable;
import android.app.Notification.ProgressStyle;
import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.Icon;
import android.graphics.drawable.LayerDrawable;
import android.os.Bundle;
import android.util.AttributeSet;
import android.util.Log;
import android.view.RemotableViewMethod;
import android.widget.ProgressBar;
import android.widget.RemoteViews;

import androidx.annotation.ColorInt;

import com.android.internal.R;
import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.util.Preconditions;
import com.android.internal.widget.NotificationProgressDrawable.Part;
@@ -49,7 +53,13 @@ import java.util.TreeSet;
 */
@RemoteViews.RemoteView
public class NotificationProgressBar extends ProgressBar {
    private static final String TAG = "NotificationProgressBar";

    private NotificationProgressModel mProgressModel;

    @Nullable
    private List<Part> mProgressDrawableParts = null;

    @Nullable
    private Drawable mProgressTrackerDrawable = null;

@@ -58,7 +68,7 @@ public class NotificationProgressBar extends ProgressBar {
    }

    public NotificationProgressBar(Context context, AttributeSet attrs) {
        this(context, attrs, com.android.internal.R.attr.progressBarStyle);
        this(context, attrs, R.attr.progressBarStyle);
    }

    public NotificationProgressBar(Context context, AttributeSet attrs, int defStyleAttr) {
@@ -82,10 +92,42 @@ public class NotificationProgressBar extends ProgressBar {
                "Bundle shouldn't be null");

        mProgressModel = NotificationProgressModel.fromBundle(bundle);

        if (mProgressModel.isIndeterminate()) {
            final int indeterminateColor = mProgressModel.getIndeterminateColor();
            setIndeterminateTintList(ColorStateList.valueOf(indeterminateColor));
        } else {
            mProgressDrawableParts = processAndConvertToDrawableParts(mProgressModel.getSegments(),
                    mProgressModel.getPoints(),
                    mProgressModel.getProgress(), mProgressModel.isStyledByProgress());

            try {
                final NotificationProgressDrawable drawable = getNotificationProgressDrawable();
                drawable.setParts(mProgressDrawableParts);
            } catch (IllegalStateException ex) {
                Log.e(TAG, "Can't set parts because can't get NotificationProgressDrawable", ex);
            }
        }
    }

    @NonNull
    private NotificationProgressDrawable getNotificationProgressDrawable() {
        final Drawable d = getProgressDrawable();
        if (d == null) {
            throw new IllegalStateException("getProgressDrawable() returns null");
        }
        if (!(d instanceof LayerDrawable)) {
            throw new IllegalStateException("getProgressDrawable() doesn't return a LayerDrawable");
        }

    private void setProgressModel(@NonNull NotificationProgressModel model) {
        mProgressModel = model;
        final Drawable layer = ((LayerDrawable) d).findDrawableByLayerId(R.id.background);
        if (!(layer instanceof NotificationProgressDrawable)) {
            throw new IllegalStateException(
                    "Couldn't get NotificationProgressDrawable, retrieved drawable is: " + (
                            layer != null ? layer.toString() : null));
        }

        return (NotificationProgressDrawable) layer;
    }

    /**
@@ -97,7 +139,6 @@ public class NotificationProgressBar extends ProgressBar {
    public void setProgressTrackerIcon(@Nullable Icon icon) {
    }


    /**
     * Async version of {@link #setProgressTrackerIcon}
     */
+16 −5
Original line number Diff line number Diff line
@@ -45,6 +45,7 @@ import org.xmlpull.v1.XmlPullParserException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
@@ -156,11 +157,18 @@ public final class NotificationProgressDrawable extends Drawable {
    }

    /**
     *
     * Set the segments and points that constitute the drawable.
     */
    public void setParts(@NonNull Part... parts) {
    public void setParts(List<Part> parts) {
        mParts.clear();
        mParts.addAll(Arrays.asList(parts));
        mParts.addAll(parts);
    }

    /**
     * Set the segments and points that constitute the drawable.
     */
    public void setParts(@NonNull Part... parts) {
        setParts(Arrays.asList(parts));
    }

    @Override
@@ -379,7 +387,7 @@ public final class NotificationProgressDrawable extends Drawable {
        if (state.mThemeAttrsPoints != null) {
            final TypedArray a = t.resolveAttributes(
                    state.mThemeAttrsPoints, R.styleable.NotificationProgressDrawablePoints);
            updateSegmentsFromTypedArray(a);
            updatePointsFromTypedArray(a);
            a.recycle();
        }
    }
@@ -651,9 +659,11 @@ public final class NotificationProgressDrawable extends Drawable {

        State(@NonNull State orig, @Nullable Resources res) {
            mChangingConfigurations = orig.mChangingConfigurations;
            mSegSegGap = orig.mSegSegGap;
            mSegPointGap = orig.mSegPointGap;
            mStrokeWidth = orig.mStrokeWidth;
            mStrokeColor = orig.mStrokeColor;
            mFadedStrokeColor = orig.mFadedStrokeColor;
            mStrokeWidth = orig.mStrokeWidth;
            mStrokeDashWidth = orig.mStrokeDashWidth;
            mStrokeDashGap = orig.mStrokeDashGap;
            mPointRadius = orig.mPointRadius;
@@ -791,6 +801,7 @@ public final class NotificationProgressDrawable extends Drawable {
        final State state = mState;

        mStrokePaint.setStrokeWidth(state.mStrokeWidth);
        mDashedStrokePaint.setStrokeWidth(state.mStrokeWidth);

        if (state.mStrokeDashWidth != 0.0f) {
            final DashPathEffect e = new DashPathEffect(
+37 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2024 The Android Open Source Project

     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at

          http://www.apache.org/licenses/LICENSE-2.0

     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@id/background"
          android:gravity="center_vertical|fill_horizontal">
        <com.android.internal.widget.NotificationProgressDrawable
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:segSegGap="@dimen/notification_progress_segSeg_gap"
            android:segPointGap="@dimen/notification_progress_segPoint_gap">
            <segments
                android:color="?attr/colorProgressBackgroundNormal"
                android:dashGap="@dimen/notification_progress_segments_dash_gap"
                android:dashWidth="@dimen/notification_progress_segments_dash_width"
                android:width="@dimen/notification_progress_segments_height" />
            <points
                android:color="?attr/colorProgressBackgroundNormal"
                android:radius="@dimen/notification_progress_points_radius"
                android:cornerRadius="@dimen/notification_progress_points_corner_radius"
                android:inset="@dimen/notification_progress_points_inset" />
        </com.android.internal.widget.NotificationProgressDrawable>
    </item>
</layer-list>
+3 −4
Original line number Diff line number Diff line
@@ -75,12 +75,11 @@
                        />


                    <com.android.internal.widget.NotificationProgressBar
                        android:id="@+id/progress"
                    <include
                        android:layout_width="0dp"
                        android:layout_height="@dimen/notification_progress_bar_height"
                        style="@style/Widget.Material.Light.ProgressBar.Horizontal"
                        android:layout_weight="1"
                        android:layout_height="@dimen/notification_progress_tracker_height"
                        layout="@layout/notification_template_notification_progress_bar"
                        />

                    <com.android.internal.widget.CachingIconView
+23 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<!--
  ~ Copyright (C) 2014 The Android Open Source Project
  ~
  ~ Licensed under the Apache License, Version 2.0 (the "License");
  ~ you may not use this file except in compliance with the License.
  ~ You may obtain a copy of the License at
  ~
  ~      http://www.apache.org/licenses/LICENSE-2.0
  ~
  ~ Unless required by applicable law or agreed to in writing, software
  ~ distributed under the License is distributed on an "AS IS" BASIS,
  ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  ~ See the License for the specific language governing permissions and
  ~ limitations under the License
  -->

<com.android.internal.widget.NotificationProgressBar xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/progress"
    android:layout_width="match_parent"
    android:layout_height="@dimen/notification_progress_tracker_height"
    style="@style/Widget.Material.Notification.NotificationProgressBar"
    />
Loading