blob: e6bdb2631912f1bb6aa783921bcfa8cf574a752a [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
25import androidx.annotation.VisibleForTesting;
26
27import java.util.ArrayList;
28import java.util.Collections;
29import java.util.List;
30import java.util.Objects;
31
32/**
33 * By diffing two entries, determines is view reinflation needed.
34 */
35public class NotificationUiAdjustment {
36
37 public final String key;
38 public final List<Notification.Action> smartActions;
39
40 @VisibleForTesting
41 NotificationUiAdjustment(String key, List<Notification.Action> smartActions) {
42 this.key = key;
43 this.smartActions = smartActions == null
44 ? Collections.emptyList()
45 : new ArrayList<>(smartActions);
46 }
47
48 public static NotificationUiAdjustment extractFromNotificationEntry(
49 NotificationData.Entry entry) {
50 return new NotificationUiAdjustment(entry.key, entry.smartActions);
51 }
52
53 public static boolean needReinflate(
54 @NonNull NotificationUiAdjustment oldAdjustment,
55 @NonNull NotificationUiAdjustment newAdjustment) {
56 if (oldAdjustment == newAdjustment) {
57 return false;
58 }
59 return areDifferent(oldAdjustment.smartActions, newAdjustment.smartActions);
60 }
61
62 public static boolean areDifferent(
63 @NonNull List<Notification.Action> first, @NonNull List<Notification.Action> second) {
64 if (first == second) {
65 return false;
66 }
67 if (first == null || second == null) {
68 return true;
69 }
70 if (first.size() != second.size()) {
71 return true;
72 }
73 for (int i = 0; i < first.size(); i++) {
74 Notification.Action firstAction = first.get(i);
75 Notification.Action secondAction = second.get(i);
76
77 if (!TextUtils.equals(firstAction.title, secondAction.title)) {
78 return true;
79 }
80
81 if (areDifferent(firstAction.getIcon(), secondAction.getIcon())) {
82 return true;
83 }
84
85 if (!Objects.equals(firstAction.actionIntent, secondAction.actionIntent)) {
86 return true;
87 }
88
89 if (areDifferent(firstAction.getRemoteInputs(), secondAction.getRemoteInputs())) {
90 return true;
91 }
92 }
93 return false;
94 }
95
96 private static boolean areDifferent(@Nullable Icon first, @Nullable Icon second) {
97 if (first == second) {
98 return false;
99 }
100 if (first == null || second == null) {
101 return true;
102 }
103 return !first.sameAs(second);
104 }
105
106 private static boolean areDifferent(
107 @Nullable RemoteInput[] first, @Nullable RemoteInput[] second) {
108 if (first == second) {
109 return false;
110 }
111 if (first == null || second == null) {
112 return true;
113 }
114 if (first.length != second.length) {
115 return true;
116 }
117 for (int i = 0; i < first.length; i++) {
118 RemoteInput firstRemoteInput = first[i];
119 RemoteInput secondRemoteInput = second[i];
120
121 if (!TextUtils.equals(firstRemoteInput.getLabel(), secondRemoteInput.getLabel())) {
122 return true;
123 }
124 if (areDifferent(firstRemoteInput.getChoices(), secondRemoteInput.getChoices())) {
125 return true;
126 }
127 }
128 return false;
129 }
130
131 private static boolean areDifferent(
132 @Nullable CharSequence[] first, @Nullable CharSequence[] second) {
133 if (first == second) {
134 return false;
135 }
136 if (first == null || second == null) {
137 return true;
138 }
139 if (first.length != second.length) {
140 return true;
141 }
142 for (int i = 0; i < first.length; i++) {
143 CharSequence firstCharSequence = first[i];
144 CharSequence secondCharSequence = second[i];
145 if (!TextUtils.equals(firstCharSequence, secondCharSequence)) {
146 return true;
147 }
148 }
149 return false;
150 }
151}