1 /*
2  * Copyright (C) 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5  * except in compliance with the License. You may obtain a copy of the License at
6  *
7  *      http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the
10  * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
11  * KIND, either express or implied. See the License for the specific language governing
12  * permissions and limitations under the License.
13  */
14 package com.android.systemui.shade
15 
16 import android.icu.text.SimpleDateFormat
17 import android.view.MotionEvent
18 import com.android.systemui.common.buffer.RingBuffer
19 import com.android.systemui.dump.DumpsysTableLogger
20 import com.android.systemui.dump.Row
21 import java.util.Locale
22 
23 /** Container for storing information about [MotionEvent.ACTION_DOWN] on
24  * [NotificationPanelViewController].
25  *
26  * This will be used in a dump to log the latest recorded down events.
27  *
28  * @see NotificationPanelViewController.initDownStates
29  */
30 class NPVCDownEventState private constructor(
31     private var timeStamp: Long = 0,
32     private var x: Float = 0f,
33     private var y: Float = 0f,
34     private var qsTouchAboveFalsingThreshold: Boolean = false,
35     private var dozing: Boolean = false,
36     private var collapsed: Boolean = false,
37     private var canCollapseOnQQS: Boolean = false,
38     private var listenForHeadsUp: Boolean = false,
39     private var allowExpandForSmallExpansion: Boolean = false,
40     private var touchSlopExceededBeforeDown: Boolean = false,
41     private var lastEventSynthesized: Boolean = false
42 ) {
43 
44     /**
45      * List of [String] to be used as a [Row] with [DumpsysTableLogger].
46      */
<lambda>null47     val asStringList: List<String> by lazy {
48         listOf(
49             DATE_FORMAT.format(timeStamp),
50             x.toString(),
51             y.toString(),
52             qsTouchAboveFalsingThreshold.toString(),
53             dozing.toString(),
54             collapsed.toString(),
55             canCollapseOnQQS.toString(),
56             listenForHeadsUp.toString(),
57             allowExpandForSmallExpansion.toString(),
58             touchSlopExceededBeforeDown.toString(),
59             lastEventSynthesized.toString()
60         )
61     }
62 
63     /**
64      * [RingBuffer] to store [NPVCDownEventState]. After the buffer is full, it will recycle old
65      * events.
66      *
67      * Do not use [append] to add new elements. Instead use [insert], as it will recycle if
68      * necessary.
69      */
70     class Buffer(capacity: Int) {
71 
<lambda>null72         private val buffer = RingBuffer(capacity) { NPVCDownEventState() }
73 
74         /**
75          * Insert a new element in the buffer.
76          */
insertnull77         fun insert(
78             timeStamp: Long,
79             x: Float,
80             y: Float,
81             qsTouchAboveFalsingThreshold: Boolean,
82             dozing: Boolean,
83             collapsed: Boolean,
84             canCollapseOnQQS: Boolean,
85             listenForHeadsUp: Boolean,
86             allowExpandForSmallExpansion: Boolean,
87             touchSlopExceededBeforeDown: Boolean,
88             lastEventSynthesized: Boolean
89         ) {
90             buffer.advance().apply {
91                 this.timeStamp = timeStamp
92                 this.x = x
93                 this.y = y
94                 this.qsTouchAboveFalsingThreshold = qsTouchAboveFalsingThreshold
95                 this.dozing = dozing
96                 this.collapsed = collapsed
97                 this.canCollapseOnQQS = canCollapseOnQQS
98                 this.listenForHeadsUp = listenForHeadsUp
99                 this.allowExpandForSmallExpansion = allowExpandForSmallExpansion
100                 this.touchSlopExceededBeforeDown = touchSlopExceededBeforeDown
101                 this.lastEventSynthesized = lastEventSynthesized
102             }
103         }
104 
105         /**
106          * Returns the content of the buffer (sorted from latest to newest).
107          *
108          * @see NPVCDownEventState.asStringList
109          */
toListnull110         fun toList(): List<Row> {
111             return buffer.map { it.asStringList }
112         }
113     }
114 
115     companion object {
116         /**
117          * Headers for dumping a table using [DumpsysTableLogger].
118          */
119         @JvmField
120         val TABLE_HEADERS = listOf(
121             "Timestamp",
122             "X",
123             "Y",
124             "QSTouchAboveFalsingThreshold",
125             "Dozing",
126             "Collapsed",
127             "CanCollapseOnQQS",
128             "ListenForHeadsUp",
129             "AllowExpandForSmallExpansion",
130             "TouchSlopExceededBeforeDown",
131             "LastEventSynthesized"
132         )
133     }
134 }
135 
136 private val DATE_FORMAT = SimpleDateFormat("MM-dd HH:mm:ss.SSS", Locale.US)
137