blob: e97f963a0986536b6e097b8188638a6b73d3488d [file] [log] [blame]
Fan Zhang723bf372017-08-25 17:22:31 -07001/*
2 * Copyright (C) 2017 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
17package android.service.settings.suggestions;
18
Fan Zhang6abced32017-09-26 15:29:04 -070019import android.annotation.IntDef;
Fan Zhang723bf372017-08-25 17:22:31 -070020import android.annotation.SystemApi;
21import android.app.PendingIntent;
Fan Zhang6abced32017-09-26 15:29:04 -070022import android.graphics.drawable.Icon;
Fan Zhang723bf372017-08-25 17:22:31 -070023import android.os.Parcel;
24import android.os.Parcelable;
25import android.text.TextUtils;
26
Fan Zhang6abced32017-09-26 15:29:04 -070027import java.lang.annotation.Retention;
28import java.lang.annotation.RetentionPolicy;
29
Fan Zhang723bf372017-08-25 17:22:31 -070030/**
31 * Data object that has information about a device suggestion.
32 *
33 * @hide
34 */
35@SystemApi
36public final class Suggestion implements Parcelable {
37
Fan Zhang6abced32017-09-26 15:29:04 -070038 /**
39 * @hide
40 */
Jeff Sharkeyce8db992017-12-13 20:05:05 -070041 @IntDef(flag = true, prefix = { "FLAG_" }, value = {
Fan Zhang6abced32017-09-26 15:29:04 -070042 FLAG_HAS_BUTTON,
Doris Ling83564812018-01-31 16:39:56 -080043 FLAG_ICON_TINTABLE,
Fan Zhang6abced32017-09-26 15:29:04 -070044 })
45 @Retention(RetentionPolicy.SOURCE)
46 public @interface Flags {
47 }
48
49 /**
50 * Flag for suggestion type with a single button
51 */
52 public static final int FLAG_HAS_BUTTON = 1 << 0;
Doris Ling83564812018-01-31 16:39:56 -080053 /**
54 * @hide
55 */
56 public static final int FLAG_ICON_TINTABLE = 1 << 1;
Fan Zhang6abced32017-09-26 15:29:04 -070057
Fan Zhang723bf372017-08-25 17:22:31 -070058 private final String mId;
59 private final CharSequence mTitle;
60 private final CharSequence mSummary;
Fan Zhang6abced32017-09-26 15:29:04 -070061 private final Icon mIcon;
62 @Flags
63 private final int mFlags;
Fan Zhang723bf372017-08-25 17:22:31 -070064 private final PendingIntent mPendingIntent;
65
66 /**
67 * Gets the id for the suggestion object.
68 */
69 public String getId() {
70 return mId;
71 }
72
73 /**
74 * Title of the suggestion that is shown to the user.
75 */
76 public CharSequence getTitle() {
77 return mTitle;
78 }
79
80 /**
81 * Optional summary describing what this suggestion controls.
82 */
83 public CharSequence getSummary() {
84 return mSummary;
85 }
86
87 /**
Fan Zhang6abced32017-09-26 15:29:04 -070088 * Optional icon for this suggestion.
89 */
90 public Icon getIcon() {
91 return mIcon;
92 }
93
94 /**
95 * Optional flags for this suggestion. This will influence UI when rendering suggestion in
96 * different style.
97 */
98 @Flags
99 public int getFlags() {
100 return mFlags;
101 }
102
103 /**
Fan Zhang723bf372017-08-25 17:22:31 -0700104 * The Intent to launch when the suggestion is activated.
105 */
106 public PendingIntent getPendingIntent() {
107 return mPendingIntent;
108 }
109
110 private Suggestion(Builder builder) {
111 mId = builder.mId;
112 mTitle = builder.mTitle;
113 mSummary = builder.mSummary;
Fan Zhang6abced32017-09-26 15:29:04 -0700114 mIcon = builder.mIcon;
115 mFlags = builder.mFlags;
Fan Zhang723bf372017-08-25 17:22:31 -0700116 mPendingIntent = builder.mPendingIntent;
117 }
118
119 private Suggestion(Parcel in) {
120 mId = in.readString();
121 mTitle = in.readCharSequence();
122 mSummary = in.readCharSequence();
Fan Zhang6abced32017-09-26 15:29:04 -0700123 mIcon = in.readParcelable(Icon.class.getClassLoader());
124 mFlags = in.readInt();
Fan Zhang723bf372017-08-25 17:22:31 -0700125 mPendingIntent = in.readParcelable(PendingIntent.class.getClassLoader());
126 }
127
128 public static final Creator<Suggestion> CREATOR = new Creator<Suggestion>() {
129 @Override
130 public Suggestion createFromParcel(Parcel in) {
131 return new Suggestion(in);
132 }
133
134 @Override
135 public Suggestion[] newArray(int size) {
136 return new Suggestion[size];
137 }
138 };
139
140 @Override
141 public int describeContents() {
142 return 0;
143 }
144
145 @Override
146 public void writeToParcel(Parcel dest, int flags) {
147 dest.writeString(mId);
148 dest.writeCharSequence(mTitle);
149 dest.writeCharSequence(mSummary);
Fan Zhang6abced32017-09-26 15:29:04 -0700150 dest.writeParcelable(mIcon, flags);
151 dest.writeInt(mFlags);
Fan Zhang723bf372017-08-25 17:22:31 -0700152 dest.writeParcelable(mPendingIntent, flags);
153 }
154
155 /**
156 * Builder class for {@link Suggestion}.
157 */
158 public static class Builder {
159 private final String mId;
160 private CharSequence mTitle;
161 private CharSequence mSummary;
Fan Zhang6abced32017-09-26 15:29:04 -0700162 private Icon mIcon;
163 @Flags
164 private int mFlags;
Fan Zhang723bf372017-08-25 17:22:31 -0700165 private PendingIntent mPendingIntent;
166
167 public Builder(String id) {
168 if (TextUtils.isEmpty(id)) {
169 throw new IllegalArgumentException("Suggestion id cannot be empty");
170 }
171 mId = id;
172 }
173
174 /**
175 * Sets suggestion title
176 */
177 public Builder setTitle(CharSequence title) {
178 mTitle = title;
179 return this;
180 }
181
182 /**
183 * Sets suggestion summary
184 */
185 public Builder setSummary(CharSequence summary) {
186 mSummary = summary;
187 return this;
188 }
189
190 /**
Fan Zhang6abced32017-09-26 15:29:04 -0700191 * Sets icon for the suggestion.
192 */
193 public Builder setIcon(Icon icon) {
194 mIcon = icon;
195 return this;
196 }
197
198 /**
199 * Sets a UI type for this suggestion. This will influence UI when rendering suggestion in
200 * different style.
201 */
202 public Builder setFlags(@Flags int flags) {
203 mFlags = flags;
204 return this;
205 }
206
207 /**
Fan Zhang723bf372017-08-25 17:22:31 -0700208 * Sets suggestion intent
209 */
210 public Builder setPendingIntent(PendingIntent pendingIntent) {
211 mPendingIntent = pendingIntent;
212 return this;
213 }
214
215 /**
216 * Builds an immutable {@link Suggestion} object.
217 */
218 public Suggestion build() {
219 return new Suggestion(this /* builder */);
220 }
221 }
222}