Fan Zhang | 723bf37 | 2017-08-25 17:22:31 -0700 | [diff] [blame] | 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 | |
| 17 | package android.service.settings.suggestions; |
| 18 | |
Fan Zhang | 6abced3 | 2017-09-26 15:29:04 -0700 | [diff] [blame] | 19 | import android.annotation.IntDef; |
Fan Zhang | 723bf37 | 2017-08-25 17:22:31 -0700 | [diff] [blame] | 20 | import android.annotation.SystemApi; |
| 21 | import android.app.PendingIntent; |
Fan Zhang | 6abced3 | 2017-09-26 15:29:04 -0700 | [diff] [blame] | 22 | import android.graphics.drawable.Icon; |
Fan Zhang | 723bf37 | 2017-08-25 17:22:31 -0700 | [diff] [blame] | 23 | import android.os.Parcel; |
| 24 | import android.os.Parcelable; |
| 25 | import android.text.TextUtils; |
| 26 | |
Fan Zhang | 6abced3 | 2017-09-26 15:29:04 -0700 | [diff] [blame] | 27 | import java.lang.annotation.Retention; |
| 28 | import java.lang.annotation.RetentionPolicy; |
| 29 | |
Fan Zhang | 723bf37 | 2017-08-25 17:22:31 -0700 | [diff] [blame] | 30 | /** |
| 31 | * Data object that has information about a device suggestion. |
| 32 | * |
| 33 | * @hide |
| 34 | */ |
| 35 | @SystemApi |
| 36 | public final class Suggestion implements Parcelable { |
| 37 | |
Fan Zhang | 6abced3 | 2017-09-26 15:29:04 -0700 | [diff] [blame] | 38 | /** |
| 39 | * @hide |
| 40 | */ |
Jeff Sharkey | ce8db99 | 2017-12-13 20:05:05 -0700 | [diff] [blame] | 41 | @IntDef(flag = true, prefix = { "FLAG_" }, value = { |
Fan Zhang | 6abced3 | 2017-09-26 15:29:04 -0700 | [diff] [blame] | 42 | FLAG_HAS_BUTTON, |
Doris Ling | 8356481 | 2018-01-31 16:39:56 -0800 | [diff] [blame] | 43 | FLAG_ICON_TINTABLE, |
Fan Zhang | 6abced3 | 2017-09-26 15:29:04 -0700 | [diff] [blame] | 44 | }) |
| 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 Ling | 8356481 | 2018-01-31 16:39:56 -0800 | [diff] [blame] | 53 | /** |
| 54 | * @hide |
| 55 | */ |
| 56 | public static final int FLAG_ICON_TINTABLE = 1 << 1; |
Fan Zhang | 6abced3 | 2017-09-26 15:29:04 -0700 | [diff] [blame] | 57 | |
Fan Zhang | 723bf37 | 2017-08-25 17:22:31 -0700 | [diff] [blame] | 58 | private final String mId; |
| 59 | private final CharSequence mTitle; |
| 60 | private final CharSequence mSummary; |
Fan Zhang | 6abced3 | 2017-09-26 15:29:04 -0700 | [diff] [blame] | 61 | private final Icon mIcon; |
| 62 | @Flags |
| 63 | private final int mFlags; |
Fan Zhang | 723bf37 | 2017-08-25 17:22:31 -0700 | [diff] [blame] | 64 | 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 Zhang | 6abced3 | 2017-09-26 15:29:04 -0700 | [diff] [blame] | 88 | * 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 Zhang | 723bf37 | 2017-08-25 17:22:31 -0700 | [diff] [blame] | 104 | * 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 Zhang | 6abced3 | 2017-09-26 15:29:04 -0700 | [diff] [blame] | 114 | mIcon = builder.mIcon; |
| 115 | mFlags = builder.mFlags; |
Fan Zhang | 723bf37 | 2017-08-25 17:22:31 -0700 | [diff] [blame] | 116 | mPendingIntent = builder.mPendingIntent; |
| 117 | } |
| 118 | |
| 119 | private Suggestion(Parcel in) { |
| 120 | mId = in.readString(); |
| 121 | mTitle = in.readCharSequence(); |
| 122 | mSummary = in.readCharSequence(); |
Fan Zhang | 6abced3 | 2017-09-26 15:29:04 -0700 | [diff] [blame] | 123 | mIcon = in.readParcelable(Icon.class.getClassLoader()); |
| 124 | mFlags = in.readInt(); |
Fan Zhang | 723bf37 | 2017-08-25 17:22:31 -0700 | [diff] [blame] | 125 | mPendingIntent = in.readParcelable(PendingIntent.class.getClassLoader()); |
| 126 | } |
| 127 | |
Jeff Sharkey | 9e8f83d | 2019-02-28 12:06:45 -0700 | [diff] [blame^] | 128 | public static final @android.annotation.NonNull Creator<Suggestion> CREATOR = new Creator<Suggestion>() { |
Fan Zhang | 723bf37 | 2017-08-25 17:22:31 -0700 | [diff] [blame] | 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 Zhang | 6abced3 | 2017-09-26 15:29:04 -0700 | [diff] [blame] | 150 | dest.writeParcelable(mIcon, flags); |
| 151 | dest.writeInt(mFlags); |
Fan Zhang | 723bf37 | 2017-08-25 17:22:31 -0700 | [diff] [blame] | 152 | 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 Zhang | 6abced3 | 2017-09-26 15:29:04 -0700 | [diff] [blame] | 162 | private Icon mIcon; |
| 163 | @Flags |
| 164 | private int mFlags; |
Fan Zhang | 723bf37 | 2017-08-25 17:22:31 -0700 | [diff] [blame] | 165 | 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 Zhang | 6abced3 | 2017-09-26 15:29:04 -0700 | [diff] [blame] | 191 | * 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 Zhang | 723bf37 | 2017-08-25 17:22:31 -0700 | [diff] [blame] | 208 | * 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 | } |