• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
<lambda>null2  * Copyright (C) 2021 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  */
16 
17 package com.android.keyguard
18 
19 import androidx.annotation.VisibleForTesting
20 import java.io.PrintWriter
21 import java.text.DateFormat
22 import java.text.SimpleDateFormat
23 import java.util.Date
24 import java.util.Locale
25 import kotlin.collections.ArrayDeque
26 
27 private val DEFAULT_FORMATTING = SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS", Locale.US)
28 
29 /** Queue for verbose logging checks for the listening state. */
30 class KeyguardListenQueue(
31     val sizePerModality: Int = 20
32 ) {
33     private val faceQueue = ArrayDeque<KeyguardFaceListenModel>()
34     private val fingerprintQueue = ArrayDeque<KeyguardFingerprintListenModel>()
35 
36     @get:VisibleForTesting val models: List<KeyguardListenModel>
37         get() = faceQueue + fingerprintQueue
38 
39     /** Push a [model] to the queue (will be logged until the queue exceeds [sizePerModality]). */
40     fun add(model: KeyguardListenModel) {
41         val queue = when (model) {
42             is KeyguardFaceListenModel -> faceQueue.apply { add(model) }
43             is KeyguardFingerprintListenModel -> fingerprintQueue.apply { add(model) }
44         }
45 
46         if (queue.size > sizePerModality) {
47             queue.removeFirstOrNull()
48         }
49     }
50 
51     /** Print verbose logs via the [writer]. */
52     @JvmOverloads
53     fun print(writer: PrintWriter, dateFormat: DateFormat = DEFAULT_FORMATTING) {
54         val stringify: (KeyguardListenModel) -> String = { model ->
55             "    ${dateFormat.format(Date(model.timeMillis))} $model"
56         }
57 
58         writer.println("  Face listen results (last ${faceQueue.size} calls):")
59         for (model in faceQueue) {
60             writer.println(stringify(model))
61         }
62         writer.println("  Fingerprint listen results (last ${fingerprintQueue.size} calls):")
63         for (model in fingerprintQueue) {
64             writer.println(stringify(model))
65         }
66     }
67 }
68