Kevin Han | 131f30d | 2020-01-06 14:11:28 -0800 | [diff] [blame] | 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.android.systemui.statusbar.notification.row; |
| 18 | |
| 19 | import android.annotation.MainThread; |
| 20 | import android.util.ArrayMap; |
| 21 | |
| 22 | import androidx.annotation.NonNull; |
| 23 | |
| 24 | import com.android.systemui.statusbar.notification.collection.NotificationEntry; |
| 25 | |
| 26 | import java.util.Map; |
| 27 | |
| 28 | /** |
| 29 | * A {@link BindStage} is an abstraction for a unit of work in inflating/binding/unbinding |
| 30 | * views to a notification. Used by {@link NotifBindPipeline}. |
| 31 | * |
| 32 | * Clients may also use {@link #getStageParams} to provide parameters for this stage for a given |
| 33 | * notification and request a rebind. |
| 34 | * |
| 35 | * @param <Params> params to do this stage |
| 36 | */ |
| 37 | @MainThread |
| 38 | public abstract class BindStage<Params> extends BindRequester { |
| 39 | |
| 40 | private Map<NotificationEntry, Params> mContentParams = new ArrayMap<>(); |
| 41 | |
| 42 | /** |
| 43 | * Execute the stage asynchronously. |
| 44 | * |
| 45 | * @param row notification top-level view to bind views to |
| 46 | * @param callback callback after stage finishes |
| 47 | */ |
| 48 | protected abstract void executeStage( |
| 49 | @NonNull NotificationEntry entry, |
| 50 | @NonNull ExpandableNotificationRow row, |
| 51 | @NonNull StageCallback callback); |
| 52 | |
| 53 | /** |
| 54 | * Abort the stage if in progress. |
| 55 | * |
| 56 | * @param row notification top-level view to bind views to |
| 57 | */ |
| 58 | protected abstract void abortStage( |
| 59 | @NonNull NotificationEntry entry, |
| 60 | @NonNull ExpandableNotificationRow row); |
| 61 | |
| 62 | /** |
| 63 | * Get the stage parameters for the entry. Clients should use this to modify how the stage |
| 64 | * handles the notification content. |
| 65 | */ |
| 66 | public final Params getStageParams(@NonNull NotificationEntry entry) { |
| 67 | Params params = mContentParams.get(entry); |
| 68 | if (params == null) { |
| 69 | throw new IllegalStateException( |
| 70 | String.format("Entry does not have any stage parameters. key: %s", |
| 71 | entry.getKey())); |
| 72 | } |
| 73 | return params; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Create a params entry for the notification for this stage. |
| 78 | */ |
| 79 | final void createStageParams(@NonNull NotificationEntry entry) { |
| 80 | mContentParams.put(entry, newStageParams()); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Delete params entry for notification. |
| 85 | */ |
| 86 | final void deleteStageParams(@NonNull NotificationEntry entry) { |
| 87 | mContentParams.remove(entry); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Create a new, empty stage params object. |
| 92 | */ |
| 93 | protected abstract Params newStageParams(); |
| 94 | |
| 95 | /** |
| 96 | * Interface for callback. |
| 97 | */ |
| 98 | interface StageCallback { |
| 99 | /** |
| 100 | * Callback for when the stage is complete. |
| 101 | */ |
| 102 | void onStageFinished(NotificationEntry entry); |
| 103 | } |
| 104 | } |