• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.internal.widget;
18 
19 import android.annotation.ColorInt;
20 import android.annotation.FlaggedApi;
21 import android.annotation.NonNull;
22 import android.app.Flags;
23 import android.app.Notification;
24 import android.app.Notification.ProgressStyle.Point;
25 import android.app.Notification.ProgressStyle.Segment;
26 import android.graphics.Color;
27 import android.os.Bundle;
28 
29 import com.android.internal.util.Preconditions;
30 
31 import java.util.Collections;
32 import java.util.List;
33 import java.util.Objects;
34 
35 /**
36  * Data model for {@link NotificationProgressBar}.
37  *
38  * This class holds the necessary data to render the notification progressbar.
39  * It is used to bind the progress style progress data to {@link NotificationProgressBar}.
40  *
41  * @hide
42  * @see NotificationProgressModel#toBundle
43  * @see NotificationProgressModel#fromBundle
44  */
45 @FlaggedApi(Flags.FLAG_API_RICH_ONGOING)
46 public final class NotificationProgressModel {
47     public static final int INVALID_COLOR = Color.TRANSPARENT;
48     private static final String KEY_SEGMENTS = "segments";
49     private static final String KEY_POINTS = "points";
50     private static final String KEY_PROGRESS = "progress";
51     private static final String KEY_IS_STYLED_BY_PROGRESS = "isStyledByProgress";
52     private static final String KEY_SEGMENTS_FALLBACK_COLOR = "segmentsFallColor";
53     private static final String KEY_INDETERMINATE_COLOR = "indeterminateColor";
54     private final List<Segment> mSegments;
55     private final List<Point> mPoints;
56     private final int mProgress;
57     private final boolean mIsStyledByProgress;
58     @ColorInt
59     private final int mSegmentsFallbackColor;
60 
61     @ColorInt
62     private final int mIndeterminateColor;
63 
NotificationProgressModel( @onNull List<Segment> segments, @NonNull List<Point> points, int progress, boolean isStyledByProgress, @ColorInt int segmentsFallbackColor )64     public NotificationProgressModel(
65             @NonNull List<Segment> segments,
66             @NonNull List<Point> points,
67             int progress,
68             boolean isStyledByProgress,
69             @ColorInt int segmentsFallbackColor
70     ) {
71         Preconditions.checkArgument(progress >= 0);
72         Preconditions.checkArgument(!segments.isEmpty());
73         mSegments = segments;
74         mPoints = points;
75         mProgress = progress;
76         mIsStyledByProgress = isStyledByProgress;
77         mSegmentsFallbackColor = segmentsFallbackColor;
78         mIndeterminateColor = INVALID_COLOR;
79     }
80 
NotificationProgressModel( @olorInt int indeterminateColor )81     public NotificationProgressModel(
82             @ColorInt int indeterminateColor
83     ) {
84         Preconditions.checkArgument(indeterminateColor != INVALID_COLOR);
85         mSegments = Collections.emptyList();
86         mPoints = Collections.emptyList();
87         mProgress = 0;
88         mIsStyledByProgress = false;
89         mSegmentsFallbackColor = INVALID_COLOR;
90         mIndeterminateColor = indeterminateColor;
91     }
92 
getSegments()93     public List<Segment> getSegments() {
94         return mSegments;
95     }
96 
getPoints()97     public List<Point> getPoints() {
98         return mPoints;
99     }
100 
getProgress()101     public int getProgress() {
102         return mProgress;
103     }
104 
getProgressMax()105     public int getProgressMax() {
106         return mSegments.stream().mapToInt(Notification.ProgressStyle.Segment::getLength).sum();
107     }
108 
isStyledByProgress()109     public boolean isStyledByProgress() {
110         return mIsStyledByProgress;
111     }
112 
113     @ColorInt
getSegmentsFallbackColor()114     public int getSegmentsFallbackColor() {
115         return mSegmentsFallbackColor;
116     }
117 
118     @ColorInt
getIndeterminateColor()119     public int getIndeterminateColor() {
120         return mIndeterminateColor;
121     }
122 
isIndeterminate()123     public boolean isIndeterminate() {
124         return mIndeterminateColor != INVALID_COLOR;
125     }
126 
127     /**
128      * Returns a {@link Bundle} representation of this {@link NotificationProgressModel}.
129      */
130     @NonNull
toBundle()131     public Bundle toBundle() {
132         final Bundle bundle = new Bundle();
133         if (mIndeterminateColor != INVALID_COLOR) {
134             bundle.putInt(KEY_INDETERMINATE_COLOR, mIndeterminateColor);
135         } else {
136             bundle.putParcelableList(KEY_SEGMENTS,
137                     Notification.ProgressStyle.getProgressSegmentsAsBundleList(mSegments));
138             bundle.putParcelableList(KEY_POINTS,
139                     Notification.ProgressStyle.getProgressPointsAsBundleList(mPoints));
140             bundle.putInt(KEY_PROGRESS, mProgress);
141             bundle.putBoolean(KEY_IS_STYLED_BY_PROGRESS, mIsStyledByProgress);
142             if (mSegmentsFallbackColor != INVALID_COLOR) {
143                 bundle.putInt(KEY_SEGMENTS_FALLBACK_COLOR, mSegmentsFallbackColor);
144             }
145         }
146         return bundle;
147     }
148 
149     /**
150      * Creates a {@link NotificationProgressModel} from a {@link Bundle}.
151      */
152     @NonNull
fromBundle(@onNull Bundle bundle)153     public static NotificationProgressModel fromBundle(@NonNull Bundle bundle) {
154         final int indeterminateColor = bundle.getInt(KEY_INDETERMINATE_COLOR,
155                 INVALID_COLOR);
156         if (indeterminateColor != INVALID_COLOR) {
157             return new NotificationProgressModel(indeterminateColor);
158         } else {
159             final List<Segment> segments =
160                     Notification.ProgressStyle.getProgressSegmentsFromBundleList(
161                             bundle.getParcelableArrayList(KEY_SEGMENTS, Bundle.class));
162             final List<Point> points =
163                     Notification.ProgressStyle.getProgressPointsFromBundleList(
164                             bundle.getParcelableArrayList(KEY_POINTS, Bundle.class));
165             final int progress = bundle.getInt(KEY_PROGRESS);
166             final boolean isStyledByProgress = bundle.getBoolean(KEY_IS_STYLED_BY_PROGRESS);
167             final int segmentsFallbackColor = bundle.getInt(KEY_SEGMENTS_FALLBACK_COLOR,
168                     INVALID_COLOR);
169             return new NotificationProgressModel(segments, points, progress, isStyledByProgress,
170                     segmentsFallbackColor);
171         }
172     }
173 
174     @Override
toString()175     public String toString() {
176         return "NotificationProgressModel{"
177                 + "mSegments=" + mSegments
178                 + ", mPoints=" + mPoints
179                 + ", mProgress=" + mProgress
180                 + ", mIsStyledByProgress=" + mIsStyledByProgress
181                 + ", mSegmentsFallbackColor=" + mSegmentsFallbackColor
182                 + ", mIndeterminateColor=" + mIndeterminateColor + "}";
183     }
184 
185     @Override
equals(Object o)186     public boolean equals(Object o) {
187         if (this == o) return true;
188         if (o == null || getClass() != o.getClass()) return false;
189         final NotificationProgressModel that = (NotificationProgressModel) o;
190         return mProgress == that.mProgress
191                 && mIsStyledByProgress == that.mIsStyledByProgress
192                 && mSegmentsFallbackColor == that.mSegmentsFallbackColor
193                 && mIndeterminateColor == that.mIndeterminateColor
194                 && Objects.equals(mSegments, that.mSegments)
195                 && Objects.equals(mPoints, that.mPoints);
196     }
197 
198     @Override
hashCode()199     public int hashCode() {
200         return Objects.hash(mSegments,
201                 mPoints,
202                 mProgress,
203                 mIsStyledByProgress,
204                 mSegmentsFallbackColor,
205                 mIndeterminateColor);
206     }
207 }
208