summaryrefslogtreecommitdiff
path: root/packages/SystemUI/src/com/android/systemui/statusbar/notification/collection/inflation/NotifUiAdjustment.kt
blob: 9d86b7831f1840cfe463bc78df3ad00e80675697 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*
 * Copyright 2021 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.systemui.statusbar.notification.collection.inflation

import android.app.Notification
import android.app.RemoteInput
import android.graphics.drawable.Icon
import android.text.TextUtils

/**
 * An immutable object which contains minimal state extracted from an entry that represents state
 * which can change without a direct app update (e.g. with a ranking update).
 * Diffing two entries determines if view re-inflation is needed.
 */
class NotifUiAdjustment internal constructor(
    val key: String,
    val smartActions: List<Notification.Action>,
    val smartReplies: List<CharSequence>,
    val isConversation: Boolean,
    val isMinimized: Boolean
) {
    companion object {
        @JvmStatic
        fun needReinflate(
            oldAdjustment: NotifUiAdjustment,
            newAdjustment: NotifUiAdjustment
        ): Boolean = when {
            oldAdjustment === newAdjustment -> false
            oldAdjustment.isConversation != newAdjustment.isConversation -> true
            oldAdjustment.isMinimized != newAdjustment.isMinimized -> true
            areDifferent(oldAdjustment.smartActions, newAdjustment.smartActions) -> true
            newAdjustment.smartReplies != oldAdjustment.smartReplies -> true
            else -> false
        }

        private fun areDifferent(
            first: List<Notification.Action>,
            second: List<Notification.Action>
        ): Boolean = when {
            first === second -> false
            first.size != second.size -> true
            else -> first.asSequence().zip(second.asSequence()).any {
                (!TextUtils.equals(it.first.title, it.second.title)) ||
                        (areDifferent(it.first.getIcon(), it.second.getIcon())) ||
                        (it.first.actionIntent != it.second.actionIntent) ||
                        (areDifferent(it.first.remoteInputs, it.second.remoteInputs))
            }
        }

        private fun areDifferent(first: Icon?, second: Icon?): Boolean = when {
            first === second -> false
            first == null || second == null -> true
            else -> !first.sameAs(second)
        }

        private fun areDifferent(
            first: Array<RemoteInput>?,
            second: Array<RemoteInput>?
        ): Boolean = when {
            first === second -> false
            first == null || second == null -> true
            first.size != second.size -> true
            else -> first.asSequence().zip(second.asSequence()).any {
                (!TextUtils.equals(it.first.label, it.second.label)) ||
                        (areDifferent(it.first.choices, it.second.choices))
            }
        }

        private fun areDifferent(
            first: Array<CharSequence>?,
            second: Array<CharSequence>?
        ): Boolean = when {
            first === second -> false
            first == null || second == null -> true
            first.size != second.size -> true
            else -> first.asSequence().zip(second.asSequence()).any {
                !TextUtils.equals(it.first, it.second)
            }
        }
    }
}