1 /*
2  * Copyright (C) 2022 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 android.safetycenter;
18 
19 import static android.os.Build.VERSION_CODES.TIRAMISU;
20 
21 import static java.util.Objects.requireNonNull;
22 
23 import android.annotation.NonNull;
24 import android.annotation.Nullable;
25 import android.annotation.SystemApi;
26 import android.app.PendingIntent;
27 import android.os.Parcel;
28 import android.os.Parcelable;
29 import android.text.TextUtils;
30 
31 import androidx.annotation.RequiresApi;
32 
33 import java.util.Objects;
34 
35 /**
36  * A static, stateless entry in the Safety Center.
37  *
38  * <p>Static entries have no changing severity level or associated issues. They provide simple links
39  * or actions for safety-related features via {@link #getPendingIntent()}.
40  *
41  * @hide
42  */
43 @SystemApi
44 @RequiresApi(TIRAMISU)
45 public final class SafetyCenterStaticEntry implements Parcelable {
46 
47     @NonNull
48     public static final Creator<SafetyCenterStaticEntry> CREATOR =
49             new Creator<SafetyCenterStaticEntry>() {
50                 @Override
51                 public SafetyCenterStaticEntry createFromParcel(Parcel in) {
52                     CharSequence title = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
53                     return new SafetyCenterStaticEntry.Builder(title)
54                             .setSummary(TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in))
55                             .setPendingIntent(in.readTypedObject(PendingIntent.CREATOR))
56                             .build();
57                 }
58 
59                 @Override
60                 public SafetyCenterStaticEntry[] newArray(int size) {
61                     return new SafetyCenterStaticEntry[size];
62                 }
63             };
64 
65     @NonNull private final CharSequence mTitle;
66     @Nullable private final CharSequence mSummary;
67     @Nullable private final PendingIntent mPendingIntent;
68 
SafetyCenterStaticEntry( @onNull CharSequence title, @Nullable CharSequence summary, @Nullable PendingIntent pendingIntent)69     private SafetyCenterStaticEntry(
70             @NonNull CharSequence title,
71             @Nullable CharSequence summary,
72             @Nullable PendingIntent pendingIntent) {
73         mTitle = title;
74         mSummary = summary;
75         mPendingIntent = pendingIntent;
76     }
77 
78     /** Returns the title that describes this entry. */
79     @NonNull
getTitle()80     public CharSequence getTitle() {
81         return mTitle;
82     }
83 
84     /**
85      * Returns the optional summary text that describes this entry if present, or {@code null}
86      * otherwise.
87      */
88     @Nullable
getSummary()89     public CharSequence getSummary() {
90         return mSummary;
91     }
92 
93     /**
94      * Returns the optional {@link PendingIntent} to execute when this entry is selected if present,
95      * or {@code null} otherwise.
96      */
97     @Nullable
getPendingIntent()98     public PendingIntent getPendingIntent() {
99         return mPendingIntent;
100     }
101 
102     @Override
equals(Object o)103     public boolean equals(Object o) {
104         if (this == o) return true;
105         if (!(o instanceof SafetyCenterStaticEntry)) return false;
106         SafetyCenterStaticEntry that = (SafetyCenterStaticEntry) o;
107         return TextUtils.equals(mTitle, that.mTitle)
108                 && TextUtils.equals(mSummary, that.mSummary)
109                 && Objects.equals(mPendingIntent, that.mPendingIntent);
110     }
111 
112     @Override
hashCode()113     public int hashCode() {
114         return Objects.hash(mTitle, mSummary, mPendingIntent);
115     }
116 
117     @Override
toString()118     public String toString() {
119         return "SafetyCenterStaticEntry{"
120                 + "mTitle="
121                 + mTitle
122                 + ", mSummary="
123                 + mSummary
124                 + ", mPendingIntent="
125                 + mPendingIntent
126                 + '}';
127     }
128 
129     @Override
describeContents()130     public int describeContents() {
131         return 0;
132     }
133 
134     @Override
writeToParcel(@onNull Parcel dest, int flags)135     public void writeToParcel(@NonNull Parcel dest, int flags) {
136         TextUtils.writeToParcel(mTitle, dest, flags);
137         TextUtils.writeToParcel(mSummary, dest, flags);
138         dest.writeTypedObject(mPendingIntent, flags);
139     }
140 
141     /** Builder class for {@link SafetyCenterStaticEntry}. */
142     public static final class Builder {
143 
144         @NonNull private CharSequence mTitle;
145         @Nullable private CharSequence mSummary;
146         @Nullable private PendingIntent mPendingIntent;
147 
148         /**
149          * Creates a {@link Builder} for a {@link SafetyCenterEntry}.
150          *
151          * @param title a title that describes this static entry
152          */
Builder(@onNull CharSequence title)153         public Builder(@NonNull CharSequence title) {
154             mTitle = requireNonNull(title);
155         }
156 
157         /**
158          * Creates a {@link Builder} with the values from the given {@link SafetyCenterStaticEntry}.
159          */
Builder(@onNull SafetyCenterStaticEntry safetyCenterStaticEntry)160         public Builder(@NonNull SafetyCenterStaticEntry safetyCenterStaticEntry) {
161             mTitle = safetyCenterStaticEntry.mTitle;
162             mSummary = safetyCenterStaticEntry.mSummary;
163             mPendingIntent = safetyCenterStaticEntry.mPendingIntent;
164         }
165 
166         /** Sets the title for this entry. */
167         @NonNull
setTitle(@onNull CharSequence title)168         public Builder setTitle(@NonNull CharSequence title) {
169             mTitle = requireNonNull(title);
170             return this;
171         }
172 
173         /** Sets the optional summary text for this entry. */
174         @NonNull
setSummary(@ullable CharSequence summary)175         public Builder setSummary(@Nullable CharSequence summary) {
176             mSummary = summary;
177             return this;
178         }
179 
180         /** Sets the optional {@link PendingIntent} to execute when this entry is selected. */
181         @NonNull
setPendingIntent(@ullable PendingIntent pendingIntent)182         public Builder setPendingIntent(@Nullable PendingIntent pendingIntent) {
183             mPendingIntent = pendingIntent;
184             return this;
185         }
186 
187         /** Creates the {@link SafetyCenterStaticEntry} defined by this {@link Builder}. */
188         @NonNull
build()189         public SafetyCenterStaticEntry build() {
190             return new SafetyCenterStaticEntry(mTitle, mSummary, mPendingIntent);
191         }
192     }
193 }
194