1 /*
2  * Copyright (C) 2024 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.recordissue
18 
19 import android.content.Context
20 import com.android.systemui.dagger.SysUISingleton
21 import com.android.systemui.qs.tiles.RecordIssueTile
22 import com.android.systemui.res.R
23 import com.android.systemui.settings.UserFileManager
24 import com.android.systemui.settings.UserTracker
25 import com.android.traceur.TraceUtils.PresetTraceType
26 import java.util.concurrent.CopyOnWriteArrayList
27 import javax.inject.Inject
28 
29 @SysUISingleton
30 class IssueRecordingState
31 @Inject
32 constructor(
33     userTracker: UserTracker,
34     userFileManager: UserFileManager,
35 ) {
36 
37     private val prefs =
38         userFileManager.getSharedPreferences(
39             RecordIssueTile.TILE_SPEC,
40             Context.MODE_PRIVATE,
41             userTracker.userId
42         )
43 
44     var takeBugreport
45         get() = prefs.getBoolean(KEY_TAKE_BUG_REPORT, false)
46         set(value) = prefs.edit().putBoolean(KEY_TAKE_BUG_REPORT, value).apply()
47 
48     var recordScreen
49         get() = prefs.getBoolean(KEY_RECORD_SCREEN, false)
50         set(value) = prefs.edit().putBoolean(KEY_RECORD_SCREEN, value).apply()
51 
52     var hasUserApprovedScreenRecording
53         get() = prefs.getBoolean(HAS_APPROVED_SCREEN_RECORDING, false)
54         private set(value) = prefs.edit().putBoolean(HAS_APPROVED_SCREEN_RECORDING, value).apply()
55 
56     var issueTypeRes
57         get() = prefs.getInt(KEY_ISSUE_TYPE_RES, ISSUE_TYPE_NOT_SET)
58         set(value) = prefs.edit().putInt(KEY_ISSUE_TYPE_RES, value).apply()
59 
60     val traceType: PresetTraceType
61         get() = ALL_ISSUE_TYPES[issueTypeRes] ?: PresetTraceType.UNSET
62 
63     private val listeners = CopyOnWriteArrayList<Runnable>()
64 
65     var isRecording = false
66         set(value) {
67             field = value
68             listeners.forEach(Runnable::run)
69         }
70 
markUserApprovalForScreenRecordingnull71     fun markUserApprovalForScreenRecording() {
72         hasUserApprovedScreenRecording = true
73     }
74 
addListenernull75     fun addListener(listener: Runnable) {
76         listeners.add(listener)
77     }
78 
removeListenernull79     fun removeListener(listener: Runnable) {
80         listeners.remove(listener)
81     }
82 
83     companion object {
84         private const val KEY_TAKE_BUG_REPORT = "key_takeBugReport"
85         private const val HAS_APPROVED_SCREEN_RECORDING = "HasApprovedScreenRecord"
86         private const val KEY_RECORD_SCREEN = "key_recordScreen"
87         const val KEY_ISSUE_TYPE_RES = "key_issueTypeRes"
88         const val ISSUE_TYPE_NOT_SET = -1
89 
90         val ALL_ISSUE_TYPES: Map<Int, PresetTraceType> =
91             hashMapOf(
92                 Pair(R.string.performance, PresetTraceType.PERFORMANCE),
93                 Pair(R.string.user_interface, PresetTraceType.UI),
94                 Pair(R.string.battery, PresetTraceType.BATTERY),
95                 Pair(R.string.thermal, PresetTraceType.THERMAL)
96             )
97     }
98 }
99