blob: 47b7fe9d7277c8009c8ea7b1115deb679f0bd938 [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
Rohan Shah20790b82018-07-02 17:21:04 -070025import com.android.systemui.statusbar.notification.NotificationData;
26
Tony Mak628cb932018-06-19 18:30:41 +010027import androidx.annotation.VisibleForTesting;
28
29import java.util.ArrayList;
Tony Makc9acf672018-07-20 13:58:24 +020030import java.util.Arrays;
Tony Mak628cb932018-06-19 18:30:41 +010031import java.util.Collections;
32import java.util.List;
33import java.util.Objects;
34
35/**
36 * By diffing two entries, determines is view reinflation needed.
37 */
38public class NotificationUiAdjustment {
39
40 public final String key;
41 public final List<Notification.Action> smartActions;
Tony Makc9acf672018-07-20 13:58:24 +020042 public final CharSequence[] smartReplies;
Tony Mak628cb932018-06-19 18:30:41 +010043
44 @VisibleForTesting
Tony Makc9acf672018-07-20 13:58:24 +020045 NotificationUiAdjustment(
46 String key, List<Notification.Action> smartActions, CharSequence[] smartReplies) {
Tony Mak628cb932018-06-19 18:30:41 +010047 this.key = key;
48 this.smartActions = smartActions == null
49 ? Collections.emptyList()
50 : new ArrayList<>(smartActions);
Tony Makc9acf672018-07-20 13:58:24 +020051 this.smartReplies = smartReplies == null ? new CharSequence[0] : smartReplies.clone();
Tony Mak628cb932018-06-19 18:30:41 +010052 }
53
54 public static NotificationUiAdjustment extractFromNotificationEntry(
55 NotificationData.Entry entry) {
Tony Makc9acf672018-07-20 13:58:24 +020056 return new NotificationUiAdjustment(entry.key, entry.smartActions, entry.smartReplies);
Tony Mak628cb932018-06-19 18:30:41 +010057 }
58
59 public static boolean needReinflate(
60 @NonNull NotificationUiAdjustment oldAdjustment,
61 @NonNull NotificationUiAdjustment newAdjustment) {
62 if (oldAdjustment == newAdjustment) {
63 return false;
64 }
Tony Makc9acf672018-07-20 13:58:24 +020065 if (areDifferent(oldAdjustment.smartActions, newAdjustment.smartActions)) {
66 return true;
67 }
68 if (!Arrays.equals(oldAdjustment.smartReplies, newAdjustment.smartReplies)) {
69 return true;
70 }
71 return false;
Tony Mak628cb932018-06-19 18:30:41 +010072 }
73
74 public static boolean areDifferent(
75 @NonNull List<Notification.Action> first, @NonNull List<Notification.Action> second) {
76 if (first == second) {
77 return false;
78 }
79 if (first == null || second == null) {
80 return true;
81 }
82 if (first.size() != second.size()) {
83 return true;
84 }
85 for (int i = 0; i < first.size(); i++) {
86 Notification.Action firstAction = first.get(i);
87 Notification.Action secondAction = second.get(i);
88
89 if (!TextUtils.equals(firstAction.title, secondAction.title)) {
90 return true;
91 }
92
93 if (areDifferent(firstAction.getIcon(), secondAction.getIcon())) {
94 return true;
95 }
96
97 if (!Objects.equals(firstAction.actionIntent, secondAction.actionIntent)) {
98 return true;
99 }
100
101 if (areDifferent(firstAction.getRemoteInputs(), secondAction.getRemoteInputs())) {
102 return true;
103 }
104 }
105 return false;
106 }
107
108 private static boolean areDifferent(@Nullable Icon first, @Nullable Icon second) {
109 if (first == second) {
110 return false;
111 }
112 if (first == null || second == null) {
113 return true;
114 }
115 return !first.sameAs(second);
116 }
117
118 private static boolean areDifferent(
119 @Nullable RemoteInput[] first, @Nullable RemoteInput[] second) {
120 if (first == second) {
121 return false;
122 }
123 if (first == null || second == null) {
124 return true;
125 }
126 if (first.length != second.length) {
127 return true;
128 }
129 for (int i = 0; i < first.length; i++) {
130 RemoteInput firstRemoteInput = first[i];
131 RemoteInput secondRemoteInput = second[i];
132
133 if (!TextUtils.equals(firstRemoteInput.getLabel(), secondRemoteInput.getLabel())) {
134 return true;
135 }
136 if (areDifferent(firstRemoteInput.getChoices(), secondRemoteInput.getChoices())) {
137 return true;
138 }
139 }
140 return false;
141 }
142
143 private static boolean areDifferent(
144 @Nullable CharSequence[] first, @Nullable CharSequence[] second) {
145 if (first == second) {
146 return false;
147 }
148 if (first == null || second == null) {
149 return true;
150 }
151 if (first.length != second.length) {
152 return true;
153 }
154 for (int i = 0; i < first.length; i++) {
155 CharSequence firstCharSequence = first[i];
156 CharSequence secondCharSequence = second[i];
157 if (!TextUtils.equals(firstCharSequence, secondCharSequence)) {
158 return true;
159 }
160 }
161 return false;
162 }
163}