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