1 /*
2  * Copyright (C) 2020 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.systemui.dump
18 
19 import android.content.BroadcastReceiver
20 import android.content.Context
21 import android.content.Intent
22 import android.content.IntentFilter
23 import android.os.UserHandle
24 import android.util.Log
25 import com.android.systemui.broadcast.BroadcastDispatcher
26 import com.android.systemui.dagger.qualifiers.Main
27 import com.android.systemui.util.concurrency.DelayableExecutor
28 import java.util.concurrent.TimeUnit
29 import javax.inject.Inject
30 
31 class LogBufferFreezer constructor(
32     private val dumpManager: DumpManager,
33     @Main private val executor: DelayableExecutor,
34     private val freezeDuration: Long
35 ) {
36     @Inject constructor(
37         dumpManager: DumpManager,
38         @Main executor: DelayableExecutor
39     ) : this(dumpManager, executor, TimeUnit.MINUTES.toMillis(5))
40 
41     private var pendingToken: Runnable? = null
42 
attachnull43     fun attach(broadcastDispatcher: BroadcastDispatcher) {
44         broadcastDispatcher.registerReceiver(
45                 object : BroadcastReceiver() {
46                     override fun onReceive(context: Context?, intent: Intent?) {
47                         onBugreportStarted()
48                     }
49                 },
50                 IntentFilter("com.android.internal.intent.action.BUGREPORT_STARTED"),
51                 executor,
52                 UserHandle.ALL)
53     }
54 
onBugreportStartednull55     private fun onBugreportStarted() {
56         pendingToken?.run()
57 
58         Log.i(TAG, "Freezing log buffers")
59         dumpManager.freezeBuffers()
60 
61         pendingToken = executor.executeDelayed({
62             Log.i(TAG, "Unfreezing log buffers")
63             pendingToken = null
64             dumpManager.unfreezeBuffers()
65         }, freezeDuration)
66     }
67 }
68 
69 private const val TAG = "LogBufferFreezer"