| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.app.slice; |
| 18 |
|
| 19 |
import android.annotation.NonNull; |
| 20 |
import android.annotation.StringDef; |
| 21 |
import android.app.PendingIntent; |
| 22 |
import android.app.RemoteInput; |
| 23 |
import android.graphics.drawable.Icon; |
| 24 |
import android.os.Bundle; |
| 25 |
import android.os.Parcel; |
| 26 |
import android.os.Parcelable; |
| 27 |
import android.text.TextUtils; |
| 28 |
import android.util.Pair; |
| 29 |
import android.widget.RemoteViews; |
| 30 |
|
| 31 |
import com.android.internal.util.ArrayUtils; |
| 32 |
|
| 33 |
import java.lang.annotation.Retention; |
| 34 |
import java.lang.annotation.RetentionPolicy; |
| 35 |
import java.util.Arrays; |
| 36 |
import java.util.List; |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
public final class SliceItem implements Parcelable { |
| 58 |
|
| 59 |
private static final String TAG = "SliceItem"; |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
@StringDef(prefix = { "FORMAT_" }, value = { |
| 65 |
FORMAT_SLICE, |
| 66 |
FORMAT_TEXT, |
| 67 |
FORMAT_IMAGE, |
| 68 |
FORMAT_ACTION, |
| 69 |
FORMAT_INT, |
| 70 |
FORMAT_LONG, |
| 71 |
FORMAT_REMOTE_INPUT, |
| 72 |
FORMAT_BUNDLE, |
| 73 |
}) |
| 74 |
@Retention(RetentionPolicy.SOURCE) |
| 75 |
public @interface SliceType {} |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
public static final String FORMAT_SLICE = "slice"; |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
public static final String FORMAT_TEXT = "text"; |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
public static final String FORMAT_IMAGE = "image"; |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
public static final String FORMAT_ACTION = "action"; |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
public static final String FORMAT_INT = "int"; |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
public static final String FORMAT_LONG = "long"; |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
@Deprecated |
| 109 |
public static final String FORMAT_TIMESTAMP = FORMAT_LONG; |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
public static final String FORMAT_REMOTE_INPUT = "input"; |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
public static final String FORMAT_BUNDLE = "bundle"; |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
protected @Slice.SliceHint |
| 123 |
String[] mHints; |
| 124 |
private final String mFormat; |
| 125 |
private final String mSubType; |
| 126 |
private final Object mObj; |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
public SliceItem(Object obj, @SliceType String format, String subType, |
| 132 |
List<String> hints) { |
| 133 |
this(obj, format, subType, hints.toArray(new String[hints.size()])); |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
public SliceItem(Object obj, @SliceType String format, String subType, |
| 140 |
@Slice.SliceHint String[] hints) { |
| 141 |
mHints = hints; |
| 142 |
mFormat = format; |
| 143 |
mSubType = subType; |
| 144 |
mObj = obj; |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
public SliceItem(PendingIntent intent, Slice slice, String format, String subType, |
| 151 |
@Slice.SliceHint String[] hints) { |
| 152 |
this(new Pair<>(intent, slice), format, subType, hints); |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
public @NonNull @Slice.SliceHint List<String> getHints() { |
| 160 |
return Arrays.asList(mHints); |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
public String getFormat() { |
| 178 |
return mFormat; |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
public String getSubType() { |
| 190 |
return mSubType; |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
public CharSequence getText() { |
| 197 |
return (CharSequence) mObj; |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
public Bundle getBundle() { |
| 204 |
return (Bundle) mObj; |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
public Icon getIcon() { |
| 211 |
return (Icon) mObj; |
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
public PendingIntent getAction() { |
| 218 |
return ((Pair<PendingIntent, Slice>) mObj).first; |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
public RemoteViews getRemoteView() { |
| 225 |
return (RemoteViews) mObj; |
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
public RemoteInput getRemoteInput() { |
| 232 |
return (RemoteInput) mObj; |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
public int getInt() { |
| 239 |
return (Integer) mObj; |
| 240 |
} |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
public Slice getSlice() { |
| 246 |
if (FORMAT_ACTION.equals(getFormat())) { |
| 247 |
return ((Pair<PendingIntent, Slice>) mObj).second; |
| 248 |
} |
| 249 |
return (Slice) mObj; |
| 250 |
} |
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
public long getLong() { |
| 256 |
return (Long) mObj; |
| 257 |
} |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
@Deprecated |
| 264 |
public long getTimestamp() { |
| 265 |
return (Long) mObj; |
| 266 |
} |
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
public boolean hasHint(@Slice.SliceHint String hint) { |
| 273 |
return ArrayUtils.contains(mHints, hint); |
| 274 |
} |
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
public SliceItem(Parcel in) { |
| 280 |
mHints = in.readStringArray(); |
| 281 |
mFormat = in.readString(); |
| 282 |
mSubType = in.readString(); |
| 283 |
mObj = readObj(mFormat, in); |
| 284 |
} |
| 285 |
|
| 286 |
@Override |
| 287 |
public int describeContents() { |
| 288 |
return 0; |
| 289 |
} |
| 290 |
|
| 291 |
@Override |
| 292 |
public void writeToParcel(Parcel dest, int flags) { |
| 293 |
dest.writeStringArray(mHints); |
| 294 |
dest.writeString(mFormat); |
| 295 |
dest.writeString(mSubType); |
| 296 |
writeObj(dest, flags, mObj, mFormat); |
| 297 |
} |
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
public boolean hasHints(@Slice.SliceHint String[] hints) { |
| 303 |
if (hints == null) return true; |
| 304 |
for (String hint : hints) { |
| 305 |
if (!TextUtils.isEmpty(hint) && !ArrayUtils.contains(mHints, hint)) { |
| 306 |
return false; |
| 307 |
} |
| 308 |
} |
| 309 |
return true; |
| 310 |
} |
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
public boolean hasAnyHints(@Slice.SliceHint String[] hints) { |
| 316 |
if (hints == null) return false; |
| 317 |
for (String hint : hints) { |
| 318 |
if (ArrayUtils.contains(mHints, hint)) { |
| 319 |
return true; |
| 320 |
} |
| 321 |
} |
| 322 |
return false; |
| 323 |
} |
| 324 |
|
| 325 |
private static String getBaseType(String type) { |
| 326 |
int index = type.indexOf('/'); |
| 327 |
if (index >= 0) { |
| 328 |
return type.substring(0, index); |
| 329 |
} |
| 330 |
return type; |
| 331 |
} |
| 332 |
|
| 333 |
private static void writeObj(Parcel dest, int flags, Object obj, String type) { |
| 334 |
switch (getBaseType(type)) { |
| 335 |
case FORMAT_SLICE: |
| 336 |
case FORMAT_IMAGE: |
| 337 |
case FORMAT_REMOTE_INPUT: |
| 338 |
case FORMAT_BUNDLE: |
| 339 |
((Parcelable) obj).writeToParcel(dest, flags); |
| 340 |
break; |
| 341 |
case FORMAT_ACTION: |
| 342 |
((Pair<PendingIntent, Slice>) obj).first.writeToParcel(dest, flags); |
| 343 |
((Pair<PendingIntent, Slice>) obj).second.writeToParcel(dest, flags); |
| 344 |
break; |
| 345 |
case FORMAT_TEXT: |
| 346 |
TextUtils.writeToParcel((CharSequence) obj, dest, flags); |
| 347 |
break; |
| 348 |
case FORMAT_INT: |
| 349 |
dest.writeInt((Integer) obj); |
| 350 |
break; |
| 351 |
case FORMAT_TIMESTAMP: |
| 352 |
dest.writeLong((Long) obj); |
| 353 |
break; |
| 354 |
} |
| 355 |
} |
| 356 |
|
| 357 |
private static Object readObj(String type, Parcel in) { |
| 358 |
switch (getBaseType(type)) { |
| 359 |
case FORMAT_SLICE: |
| 360 |
return Slice.CREATOR.createFromParcel(in); |
| 361 |
case FORMAT_TEXT: |
| 362 |
return TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in); |
| 363 |
case FORMAT_IMAGE: |
| 364 |
return Icon.CREATOR.createFromParcel(in); |
| 365 |
case FORMAT_ACTION: |
| 366 |
return new Pair<>( |
| 367 |
PendingIntent.CREATOR.createFromParcel(in), |
| 368 |
Slice.CREATOR.createFromParcel(in)); |
| 369 |
case FORMAT_INT: |
| 370 |
return in.readInt(); |
| 371 |
case FORMAT_TIMESTAMP: |
| 372 |
return in.readLong(); |
| 373 |
case FORMAT_REMOTE_INPUT: |
| 374 |
return RemoteInput.CREATOR.createFromParcel(in); |
| 375 |
case FORMAT_BUNDLE: |
| 376 |
return Bundle.CREATOR.createFromParcel(in); |
| 377 |
} |
| 378 |
throw new RuntimeException("Unsupported type " + type); |
| 379 |
} |
| 380 |
|
| 381 |
public static final @android.annotation.NonNull Creator<SliceItem> CREATOR = new Creator<SliceItem>() { |
| 382 |
@Override |
| 383 |
public SliceItem createFromParcel(Parcel in) { |
| 384 |
return new SliceItem(in); |
| 385 |
} |
| 386 |
|
| 387 |
@Override |
| 388 |
public SliceItem[] newArray(int size) { |
| 389 |
return new SliceItem[size]; |
| 390 |
} |
| 391 |
}; |
| 392 |
} |