@@ -17,36 +17,49 @@ import io.github.sds100.keymapper.system.files.FileAdapter
1717import io.github.sds100.keymapper.system.files.FileUtils
1818import io.github.sds100.keymapper.system.files.IFile
1919import javax.inject.Inject
20+ import javax.inject.Singleton
21+ import kotlinx.coroutines.CoroutineScope
2022import kotlinx.coroutines.Dispatchers
23+ import kotlinx.coroutines.Job
2124import kotlinx.coroutines.flow.Flow
25+ import kotlinx.coroutines.flow.MutableStateFlow
2226import kotlinx.coroutines.flow.map
27+ import kotlinx.coroutines.flow.update
28+ import kotlinx.coroutines.launch
2329import kotlinx.coroutines.withContext
2430
25- interface GetEventOutputUseCase {
31+ interface GetEventRecorder {
32+ val isRecording: Flow <Boolean >
2633 val deviceInfoOutput: Flow <String >
2734 val eventsOutput: Flow <String >
2835
2936 suspend fun refreshDeviceInfo ()
30- suspend fun recordEvents ()
31- suspend fun stopRecording ()
37+ fun recordEvents ()
38+ fun stopRecording ()
3239 fun copyOutput (output : String )
3340 suspend fun shareOutput (output : String )
3441}
3542
36- class GetEventOutputUseCaseImpl @Inject constructor(
43+ @Singleton
44+ class GetEventRecorderImpl @Inject constructor(
3745 @ApplicationContext private val context : Context ,
46+ private val coroutineScope : CoroutineScope ,
3847 private val executeShellCommandUseCase : ExecuteShellCommandUseCase ,
3948 private val preferenceRepository : PreferenceRepository ,
4049 private val clipboardAdapter : ClipboardAdapter ,
4150 private val fileAdapter : FileAdapter ,
4251 private val buildConfigProvider : BuildConfigProvider ,
4352 private val resourceProvider : ResourceProvider ,
44- ) : GetEventOutputUseCase {
53+ ) : GetEventRecorder {
4554
4655 companion object {
4756 private const val MAX_COPY_OUTPUT_LENGTH = 150_000
4857 }
4958
59+ private val recordingJobState: MutableStateFlow <Job ?> = MutableStateFlow (null )
60+
61+ override val isRecording: Flow <Boolean > = recordingJobState.map { it != null && it.isActive }
62+
5063 override val deviceInfoOutput: Flow <String > = preferenceRepository
5164 .get(Keys .getEventDeviceInfoOutput)
5265 .map { it.orEmpty() }
@@ -59,34 +72,49 @@ class GetEventOutputUseCaseImpl @Inject constructor(
5972 val output = executeShellCommandUseCase.execute(
6073 command = " getevent -il" ,
6174 executionMode = ShellExecutionMode .ADB ,
62- timeoutMillis = 30_000L ,
75+ timeoutMillis = 5_000L ,
6376 ).handle(
6477 onSuccess = { it.stdout },
6578 onError = { " Error: ${it.getFullMessage(resourceProvider)} " },
6679 )
6780 preferenceRepository.set(Keys .getEventDeviceInfoOutput, output)
6881 }
6982
70- override suspend fun recordEvents () {
71- val output = executeShellCommandUseCase.execute(
72- command = " getevent -lt" ,
73- executionMode = ShellExecutionMode .ADB ,
74- timeoutMillis = 300_000L ,
75- ).handle(
76- onSuccess = { it.stdout },
77- onError = { " " },
78- )
79- if (output.isNotEmpty()) {
80- preferenceRepository.set(Keys .getEventEventsOutput, output)
83+ override fun recordEvents () {
84+ recordingJobState.update { oldJob ->
85+ oldJob?.cancel()
86+
87+ coroutineScope.launch {
88+ val output = executeShellCommandUseCase.execute(
89+ command = " getevent -lt" ,
90+ executionMode = ShellExecutionMode .ADB ,
91+ timeoutMillis = 60_000L ,
92+ ).handle(
93+ onSuccess = { it.stdout },
94+ onError = { " " },
95+ )
96+
97+ if (output.isNotEmpty()) {
98+ preferenceRepository.set(Keys .getEventEventsOutput, output)
99+ }
100+ }
81101 }
82102 }
83103
84- override suspend fun stopRecording () {
85- executeShellCommandUseCase.execute(
86- command = " pkill -x getevent || true" ,
87- executionMode = ShellExecutionMode .ADB ,
88- timeoutMillis = 5_000L ,
89- )
104+ override fun stopRecording () {
105+ coroutineScope.launch {
106+ executeShellCommandUseCase.execute(
107+ command = " pkill -x getevent || true" ,
108+ executionMode = ShellExecutionMode .ADB ,
109+ timeoutMillis = 5_000L ,
110+ )
111+
112+ recordingJobState.update { oldJob ->
113+ oldJob?.join()
114+ oldJob?.cancel()
115+ null
116+ }
117+ }
90118 }
91119
92120 override fun copyOutput (output : String ) {
0 commit comments