Suggestion.java revision ce8db9911494225fcd99711d7df85a130de5a6ce
1/*
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
19import android.annotation.IntDef;
20import android.annotation.SystemApi;
21import android.app.PendingIntent;
22import android.graphics.drawable.Icon;
23import android.os.Parcel;
24import android.os.Parcelable;
25import android.text.TextUtils;
26
27import java.lang.annotation.Retention;
28import java.lang.annotation.RetentionPolicy;
29
30/**
31 * Data object that has information about a device suggestion.
32 *
33 * @hide
34 */
35@SystemApi
36public final class Suggestion implements Parcelable {
37
38    /**
39     * @hide
40     */
41    @IntDef(flag = true, prefix = { "FLAG_" }, value = {
42            FLAG_HAS_BUTTON,
43    })
44    @Retention(RetentionPolicy.SOURCE)
45    public @interface Flags {
46    }
47
48    /**
49     * Flag for suggestion type with a single button
50     */
51    public static final int FLAG_HAS_BUTTON = 1 << 0;
52
53    private final String mId;
54    private final CharSequence mTitle;
55    private final CharSequence mSummary;
56    private final Icon mIcon;
57    @Flags
58    private final int mFlags;
59    private final PendingIntent mPendingIntent;
60
61    /**
62     * Gets the id for the suggestion object.
63     */
64    public String getId() {
65        return mId;
66    }
67
68    /**
69     * Title of the suggestion that is shown to the user.
70     */
71    public CharSequence getTitle() {
72        return mTitle;
73    }
74
75    /**
76     * Optional summary describing what this suggestion controls.
77     */
78    public CharSequence getSummary() {
79        return mSummary;
80    }
81
82    /**
83     * Optional icon for this suggestion.
84     */
85    public Icon getIcon() {
86        return mIcon;
87    }
88
89    /**
90     * Optional flags for this suggestion. This will influence UI when rendering suggestion in
91     * different style.
92     */
93    @Flags
94    public int getFlags() {
95        return mFlags;
96    }
97
98    /**
99     * The Intent to launch when the suggestion is activated.
100     */
101    public PendingIntent getPendingIntent() {
102        return mPendingIntent;
103    }
104
105    private Suggestion(Builder builder) {
106        mId = builder.mId;
107        mTitle = builder.mTitle;
108        mSummary = builder.mSummary;
109        mIcon = builder.mIcon;
110        mFlags = builder.mFlags;
111        mPendingIntent = builder.mPendingIntent;
112    }
113
114    private Suggestion(Parcel in) {
115        mId = in.readString();
116        mTitle = in.readCharSequence();
117        mSummary = in.readCharSequence();
118        mIcon = in.readParcelable(Icon.class.getClassLoader());
119        mFlags = in.readInt();
120        mPendingIntent = in.readParcelable(PendingIntent.class.getClassLoader());
121    }
122
123    public static final Creator<Suggestion> CREATOR = new Creator<Suggestion>() {
124        @Override
125        public Suggestion createFromParcel(Parcel in) {
126            return new Suggestion(in);
127        }
128
129        @Override
130        public Suggestion[] newArray(int size) {
131            return new Suggestion[size];
132        }
133    };
134
135    @Override
136    public int describeContents() {
137        return 0;
138    }
139
140    @Override
141    public void writeToParcel(Parcel dest, int flags) {
142        dest.writeString(mId);
143        dest.writeCharSequence(mTitle);
144        dest.writeCharSequence(mSummary);
145        dest.writeParcelable(mIcon, flags);
146        dest.writeInt(mFlags);
147        dest.writeParcelable(mPendingIntent, flags);
148    }
149
150    /**
151     * Builder class for {@link Suggestion}.
152     */
153    public static class Builder {
154        private final String mId;
155        private CharSequence mTitle;
156        private CharSequence mSummary;
157        private Icon mIcon;
158        @Flags
159        private int mFlags;
160        private PendingIntent mPendingIntent;
161
162        public Builder(String id) {
163            if (TextUtils.isEmpty(id)) {
164                throw new IllegalArgumentException("Suggestion id cannot be empty");
165            }
166            mId = id;
167        }
168
169        /**
170         * Sets suggestion title
171         */
172        public Builder setTitle(CharSequence title) {
173            mTitle = title;
174            return this;
175        }
176
177        /**
178         * Sets suggestion summary
179         */
180        public Builder setSummary(CharSequence summary) {
181            mSummary = summary;
182            return this;
183        }
184
185        /**
186         * Sets icon for the suggestion.
187         */
188        public Builder setIcon(Icon icon) {
189            mIcon = icon;
190            return this;
191        }
192
193        /**
194         * Sets a UI type for this suggestion. This will influence UI when rendering suggestion in
195         * different style.
196         */
197        public Builder setFlags(@Flags int flags) {
198            mFlags = flags;
199            return this;
200        }
201
202        /**
203         * Sets suggestion intent
204         */
205        public Builder setPendingIntent(PendingIntent pendingIntent) {
206            mPendingIntent = pendingIntent;
207            return this;
208        }
209
210        /**
211         * Builds an immutable {@link Suggestion} object.
212         */
213        public Suggestion build() {
214            return new Suggestion(this /* builder */);
215        }
216    }
217}
218