Tony Mak | 628cb93 | 2018-06-19 18:30:41 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 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 | package com.android.systemui.statusbar; |
| 17 | |
| 18 | import android.annotation.NonNull; |
| 19 | import android.annotation.Nullable; |
| 20 | import android.app.Notification; |
| 21 | import android.app.RemoteInput; |
| 22 | import android.graphics.drawable.Icon; |
| 23 | import android.text.TextUtils; |
| 24 | |
Tony Mak | 628cb93 | 2018-06-19 18:30:41 +0100 | [diff] [blame] | 25 | import androidx.annotation.VisibleForTesting; |
| 26 | |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 27 | import com.android.systemui.statusbar.notification.collection.NotificationEntry; |
Gustav Sennton | 1463d83 | 2018-11-06 16:12:48 +0000 | [diff] [blame] | 28 | |
Tony Mak | 628cb93 | 2018-06-19 18:30:41 +0100 | [diff] [blame] | 29 | import java.util.Collections; |
| 30 | import java.util.List; |
| 31 | import java.util.Objects; |
| 32 | |
| 33 | /** |
| 34 | * By diffing two entries, determines is view reinflation needed. |
| 35 | */ |
| 36 | public class NotificationUiAdjustment { |
| 37 | |
| 38 | public final String key; |
| 39 | public final List<Notification.Action> smartActions; |
Ned Burns | 5a9e3520 | 2019-09-06 22:26:33 -0400 | [diff] [blame] | 40 | public final List<CharSequence> smartReplies; |
Tony Mak | 628cb93 | 2018-06-19 18:30:41 +0100 | [diff] [blame] | 41 | |
| 42 | @VisibleForTesting |
Tony Mak | c9acf67 | 2018-07-20 13:58:24 +0200 | [diff] [blame] | 43 | NotificationUiAdjustment( |
Ned Burns | 5a9e3520 | 2019-09-06 22:26:33 -0400 | [diff] [blame] | 44 | String key, List<Notification.Action> smartActions, List<CharSequence> smartReplies) { |
Tony Mak | 628cb93 | 2018-06-19 18:30:41 +0100 | [diff] [blame] | 45 | this.key = key; |
| 46 | this.smartActions = smartActions == null |
| 47 | ? Collections.emptyList() |
Ned Burns | 5a9e3520 | 2019-09-06 22:26:33 -0400 | [diff] [blame] | 48 | : smartActions; |
| 49 | this.smartReplies = smartReplies == null |
| 50 | ? Collections.emptyList() |
| 51 | : smartReplies; |
Tony Mak | 628cb93 | 2018-06-19 18:30:41 +0100 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | public static NotificationUiAdjustment extractFromNotificationEntry( |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 55 | NotificationEntry entry) { |
Gustav Sennton | 1463d83 | 2018-11-06 16:12:48 +0000 | [diff] [blame] | 56 | return new NotificationUiAdjustment( |
Ned Burns | 00b4b2d | 2019-10-17 22:09:27 -0400 | [diff] [blame] | 57 | entry.getKey(), entry.getSmartActions(), entry.getSmartReplies()); |
Tony Mak | 628cb93 | 2018-06-19 18:30:41 +0100 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | public static boolean needReinflate( |
| 61 | @NonNull NotificationUiAdjustment oldAdjustment, |
| 62 | @NonNull NotificationUiAdjustment newAdjustment) { |
| 63 | if (oldAdjustment == newAdjustment) { |
| 64 | return false; |
| 65 | } |
Tony Mak | c9acf67 | 2018-07-20 13:58:24 +0200 | [diff] [blame] | 66 | if (areDifferent(oldAdjustment.smartActions, newAdjustment.smartActions)) { |
| 67 | return true; |
| 68 | } |
Ned Burns | 5a9e3520 | 2019-09-06 22:26:33 -0400 | [diff] [blame] | 69 | if (!newAdjustment.smartReplies.equals(oldAdjustment.smartReplies)) { |
Tony Mak | c9acf67 | 2018-07-20 13:58:24 +0200 | [diff] [blame] | 70 | return true; |
| 71 | } |
| 72 | return false; |
Tony Mak | 628cb93 | 2018-06-19 18:30:41 +0100 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | public static boolean areDifferent( |
| 76 | @NonNull List<Notification.Action> first, @NonNull List<Notification.Action> second) { |
| 77 | if (first == second) { |
| 78 | return false; |
| 79 | } |
| 80 | if (first == null || second == null) { |
| 81 | return true; |
| 82 | } |
| 83 | if (first.size() != second.size()) { |
| 84 | return true; |
| 85 | } |
| 86 | for (int i = 0; i < first.size(); i++) { |
| 87 | Notification.Action firstAction = first.get(i); |
| 88 | Notification.Action secondAction = second.get(i); |
| 89 | |
| 90 | if (!TextUtils.equals(firstAction.title, secondAction.title)) { |
| 91 | return true; |
| 92 | } |
| 93 | |
| 94 | if (areDifferent(firstAction.getIcon(), secondAction.getIcon())) { |
| 95 | return true; |
| 96 | } |
| 97 | |
| 98 | if (!Objects.equals(firstAction.actionIntent, secondAction.actionIntent)) { |
| 99 | return true; |
| 100 | } |
| 101 | |
| 102 | if (areDifferent(firstAction.getRemoteInputs(), secondAction.getRemoteInputs())) { |
| 103 | return true; |
| 104 | } |
| 105 | } |
| 106 | return false; |
| 107 | } |
| 108 | |
| 109 | private static boolean areDifferent(@Nullable Icon first, @Nullable Icon second) { |
| 110 | if (first == second) { |
| 111 | return false; |
| 112 | } |
| 113 | if (first == null || second == null) { |
| 114 | return true; |
| 115 | } |
| 116 | return !first.sameAs(second); |
| 117 | } |
| 118 | |
| 119 | private static boolean areDifferent( |
| 120 | @Nullable RemoteInput[] first, @Nullable RemoteInput[] second) { |
| 121 | if (first == second) { |
| 122 | return false; |
| 123 | } |
| 124 | if (first == null || second == null) { |
| 125 | return true; |
| 126 | } |
| 127 | if (first.length != second.length) { |
| 128 | return true; |
| 129 | } |
| 130 | for (int i = 0; i < first.length; i++) { |
| 131 | RemoteInput firstRemoteInput = first[i]; |
| 132 | RemoteInput secondRemoteInput = second[i]; |
| 133 | |
| 134 | if (!TextUtils.equals(firstRemoteInput.getLabel(), secondRemoteInput.getLabel())) { |
| 135 | return true; |
| 136 | } |
| 137 | if (areDifferent(firstRemoteInput.getChoices(), secondRemoteInput.getChoices())) { |
| 138 | return true; |
| 139 | } |
| 140 | } |
| 141 | return false; |
| 142 | } |
| 143 | |
| 144 | private static boolean areDifferent( |
| 145 | @Nullable CharSequence[] first, @Nullable CharSequence[] second) { |
| 146 | if (first == second) { |
| 147 | return false; |
| 148 | } |
| 149 | if (first == null || second == null) { |
| 150 | return true; |
| 151 | } |
| 152 | if (first.length != second.length) { |
| 153 | return true; |
| 154 | } |
| 155 | for (int i = 0; i < first.length; i++) { |
| 156 | CharSequence firstCharSequence = first[i]; |
| 157 | CharSequence secondCharSequence = second[i]; |
| 158 | if (!TextUtils.equals(firstCharSequence, secondCharSequence)) { |
| 159 | return true; |
| 160 | } |
| 161 | } |
| 162 | return false; |
| 163 | } |
| 164 | } |