1 /* 2 * Copyright (C) 2020 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 com.google.android.setupcompat.portal; 18 19 import android.os.Bundle; 20 import android.os.Parcel; 21 import android.os.Parcelable; 22 import androidx.annotation.IntDef; 23 import java.lang.annotation.Retention; 24 import java.lang.annotation.RetentionPolicy; 25 26 /** 27 * A class that represents how a persistent notification is to be presented to the user using the 28 * {@link com.google.android.setupcompat.portal.ISetupNotificationService}. 29 */ 30 public class NotificationComponent implements Parcelable { 31 32 @NotificationType private final int notificationType; 33 private Bundle extraBundle = new Bundle(); 34 NotificationComponent(@otificationType int notificationType)35 private NotificationComponent(@NotificationType int notificationType) { 36 this.notificationType = notificationType; 37 } 38 NotificationComponent(Parcel in)39 protected NotificationComponent(Parcel in) { 40 this(in.readInt()); 41 extraBundle = in.readBundle(Bundle.class.getClassLoader()); 42 } 43 getIntExtra(String key, int defValue)44 public int getIntExtra(String key, int defValue) { 45 return extraBundle.getInt(key, defValue); 46 } 47 48 @NotificationType getNotificationType()49 public int getNotificationType() { 50 return notificationType; 51 } 52 53 @Override writeToParcel(Parcel dest, int flags)54 public void writeToParcel(Parcel dest, int flags) { 55 dest.writeInt(notificationType); 56 dest.writeBundle(extraBundle); 57 } 58 59 @Override describeContents()60 public int describeContents() { 61 return 0; 62 } 63 64 public static final Creator<NotificationComponent> CREATOR = 65 new Creator<NotificationComponent>() { 66 @Override 67 public NotificationComponent createFromParcel(Parcel in) { 68 return new NotificationComponent(in); 69 } 70 71 @Override 72 public NotificationComponent[] newArray(int size) { 73 return new NotificationComponent[size]; 74 } 75 }; 76 77 @Retention(RetentionPolicy.SOURCE) 78 @IntDef({ 79 NotificationType.INITIAL_ONGOING, 80 NotificationType.PREDEFERRED, 81 NotificationType.PREDEFERRED_PREPARING, 82 NotificationType.DEFERRED, 83 NotificationType.DEFERRED_ONGOING, 84 NotificationType.PORTAL 85 }) 86 public @interface NotificationType { 87 int UNKNOWN = 0; 88 int INITIAL_ONGOING = 1; 89 int PREDEFERRED = 2; 90 int PREDEFERRED_PREPARING = 3; 91 int DEFERRED = 4; 92 int DEFERRED_ONGOING = 5; 93 int PORTAL = 6; 94 int MAX = 7; 95 } 96 97 public static class Builder { 98 99 private final NotificationComponent component; 100 Builder(@otificationType int notificationType)101 public Builder(@NotificationType int notificationType) { 102 component = new NotificationComponent(notificationType); 103 } 104 putIntExtra(String key, int value)105 public Builder putIntExtra(String key, int value) { 106 component.extraBundle.putInt(key, value); 107 return this; 108 } 109 build()110 public NotificationComponent build() { 111 return component; 112 } 113 } 114 } 115