blob: 564d8bc14c8c5418795990a5900f5fb8ab11907a [file] [log] [blame]
Tony Mak628cb932018-06-19 18:30:41 +01001/*
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 */
16package com.android.systemui.statusbar;
17
18import android.annotation.NonNull;
19import android.annotation.Nullable;
20import android.app.Notification;
21import android.app.RemoteInput;
22import android.graphics.drawable.Icon;
23import android.text.TextUtils;
24
Tony Mak628cb932018-06-19 18:30:41 +010025import androidx.annotation.VisibleForTesting;
26
Ned Burnsf81c4c42019-01-07 14:10:43 -050027import com.android.systemui.statusbar.notification.collection.NotificationEntry;
Gustav Sennton1463d832018-11-06 16:12:48 +000028
Tony Mak628cb932018-06-19 18:30:41 +010029import java.util.Collections;
30import java.util.List;
31import java.util.Objects;
32
33/**
34 * By diffing two entries, determines is view reinflation needed.
35 */
36public class NotificationUiAdjustment {
37
38 public final String key;
39 public final List<Notification.Action> smartActions;
Ned Burns5a9e35202019-09-06 22:26:33 -040040 public final List<CharSequence> smartReplies;
Tony Mak628cb932018-06-19 18:30:41 +010041
42 @VisibleForTesting
Tony Makc9acf672018-07-20 13:58:24 +020043 NotificationUiAdjustment(
Ned Burns5a9e35202019-09-06 22:26:33 -040044 String key, List<Notification.Action> smartActions, List<CharSequence> smartReplies) {
Tony Mak628cb932018-06-19 18:30:41 +010045 this.key = key;
46 this.smartActions = smartActions == null
47 ? Collections.emptyList()
Ned Burns5a9e35202019-09-06 22:26:33 -040048 : smartActions;
49 this.smartReplies = smartReplies == null
50 ? Collections.emptyList()
51 : smartReplies;
Tony Mak628cb932018-06-19 18:30:41 +010052 }
53
54 public static NotificationUiAdjustment extractFromNotificationEntry(
Ned Burnsf81c4c42019-01-07 14:10:43 -050055 NotificationEntry entry) {
Gustav Sennton1463d832018-11-06 16:12:48 +000056 return new NotificationUiAdjustment(
Ned Burns00b4b2d2019-10-17 22:09:27 -040057 entry.getKey(), entry.getSmartActions(), entry.getSmartReplies());
Tony Mak628cb932018-06-19 18:30:41 +010058 }
59
60 public static boolean needReinflate(
61 @NonNull NotificationUiAdjustment oldAdjustment,
62 @NonNull NotificationUiAdjustment newAdjustment) {
63 if (oldAdjustment == newAdjustment) {
64 return false;
65 }
Tony Makc9acf672018-07-20 13:58:24 +020066 if (areDifferent(oldAdjustment.smartActions, newAdjustment.smartActions)) {
67 return true;
68 }
Ned Burns5a9e35202019-09-06 22:26:33 -040069 if (!newAdjustment.smartReplies.equals(oldAdjustment.smartReplies)) {
Tony Makc9acf672018-07-20 13:58:24 +020070 return true;
71 }
72 return false;
Tony Mak628cb932018-06-19 18:30:41 +010073 }
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}