@@ -67,7 +67,6 @@ import kotlinx.coroutines.flow.SharingStarted
6767import kotlinx.coroutines.flow.StateFlow
6868import kotlinx.coroutines.flow.map
6969import kotlinx.coroutines.flow.stateIn
70- import kotlinx.coroutines.launch
7170import splitties.bitflags.withFlag
7271import timber.log.Timber
7372
@@ -114,6 +113,7 @@ class PerformActionsUseCaseImpl(
114113 accessibilityService,
115114 shizukuInputEventInjector,
116115 permissionAdapter,
116+ coroutineScope,
117117 )
118118 }
119119
@@ -124,15 +124,15 @@ class PerformActionsUseCaseImpl(
124124 permissionAdapter.isGrantedFlow(Permission .SHIZUKU )
125125 .stateIn(coroutineScope, SharingStarted .Eagerly , false )
126126
127- override fun perform (
127+ override suspend fun perform (
128128 action : ActionData ,
129129 inputEventType : InputEventType ,
130130 keyMetaState : Int ,
131131 ) {
132132 /* *
133133 * Is null if the action is being performed asynchronously
134134 */
135- val result: Result <* >?
135+ val result: Result <* >
136136
137137 when (action) {
138138 is ActionData .App -> {
@@ -254,20 +254,15 @@ class PerformActionsUseCaseImpl(
254254 }
255255
256256 is ActionData .SwitchKeyboard -> {
257- coroutineScope.launch {
258- inputMethodAdapter
259- .chooseImeWithoutUserInput(action.imeId)
260- .onSuccess {
261- val message = resourceProvider.getString(
262- R .string.toast_chose_keyboard,
263- it.label,
264- )
265- popupMessageAdapter.showPopupMessage(message)
266- }
267- .showErrorMessageOnFail()
268- }
269-
270- result = null
257+ result = inputMethodAdapter
258+ .chooseImeWithoutUserInput(action.imeId)
259+ .onSuccess {
260+ val message = resourceProvider.getString(
261+ R .string.toast_chose_keyboard,
262+ it.label,
263+ )
264+ popupMessageAdapter.showPopupMessage(message)
265+ }
271266 }
272267
273268 is ActionData .Volume .Down -> {
@@ -578,14 +573,10 @@ class PerformActionsUseCaseImpl(
578573 }
579574
580575 is ActionData .GoLastApp -> {
581- coroutineScope.launch {
582- accessibilityService.doGlobalAction( AccessibilityService . GLOBAL_ACTION_RECENTS )
583- delay( 100 )
576+ accessibilityService.doGlobalAction( AccessibilityService . GLOBAL_ACTION_RECENTS )
577+ delay( 100 )
578+ result =
584579 accessibilityService.doGlobalAction(AccessibilityService .GLOBAL_ACTION_RECENTS )
585- .showErrorMessageOnFail()
586- }
587-
588- result = null
589580 }
590581
591582 is ActionData .OpenMenu -> {
@@ -711,11 +702,11 @@ class PerformActionsUseCaseImpl(
711702
712703 is ActionData .Screenshot -> {
713704 if (Build .VERSION .SDK_INT < Build .VERSION_CODES .P ) {
714- coroutineScope.launch {
715- val picturesFolder = fileAdapter.getPicturesFolder()
716- val screenshotsFolder = " $picturesFolder /Screenshots"
717- val fileDate = FileUtils .createFileDate()
705+ val picturesFolder = fileAdapter.getPicturesFolder()
706+ val screenshotsFolder = " $picturesFolder /Screenshots"
707+ val fileDate = FileUtils .createFileDate()
718708
709+ result =
719710 suAdapter.execute(" mkdir -p $screenshotsFolder ; screencap -p $screenshotsFolder /Screenshot_$fileDate .png" )
720711 .onSuccess {
721712 // Wait 3 seconds so the message isn't shown in the screenshot.
@@ -726,9 +717,7 @@ class PerformActionsUseCaseImpl(
726717 R .string.toast_screenshot_taken,
727718 ),
728719 )
729- }.showErrorMessageOnFail()
730- }
731- result = null
720+ }
732721 } else {
733722 result =
734723 accessibilityService.doGlobalAction(AccessibilityService .GLOBAL_ACTION_TAKE_SCREENSHOT )
@@ -781,19 +770,11 @@ class PerformActionsUseCaseImpl(
781770 }
782771
783772 ActionData .DismissAllNotifications -> {
784- coroutineScope.launch {
785- notificationReceiverAdapter.send(ServiceEvent .DismissAllNotifications )
786- }
787-
788- result = null
773+ result = notificationReceiverAdapter.send(ServiceEvent .DismissAllNotifications )
789774 }
790775
791776 ActionData .DismissLastNotification -> {
792- coroutineScope.launch {
793- notificationReceiverAdapter.send(ServiceEvent .DismissLastNotification )
794- }
795-
796- result = null
777+ result = notificationReceiverAdapter.send(ServiceEvent .DismissLastNotification )
797778 }
798779
799780 ActionData .AnswerCall -> {
@@ -815,27 +796,23 @@ class PerformActionsUseCaseImpl(
815796 }
816797
817798 is ActionData .HttpRequest -> {
818- coroutineScope.launch {
819- networkAdapter.sendHttpRequest(
820- method = action.method,
821- url = action.url,
822- body = action.body,
823- authorizationHeader = action.authorizationHeader,
824- ).showErrorMessageOnFail()
825- }
826-
827- result = null
799+ result = networkAdapter.sendHttpRequest(
800+ method = action.method,
801+ url = action.url,
802+ body = action.body,
803+ authorizationHeader = action.authorizationHeader,
804+ )
828805 }
829806 }
830807
831808 when (result) {
832- null , is Success -> Timber .d(" Performed action $action , input event type: $inputEventType , key meta state: $keyMetaState " )
809+ is Success -> Timber .d(" Performed action $action , input event type: $inputEventType , key meta state: $keyMetaState " )
833810 is Error -> Timber .d(
834811 " Failed to perform action $action , reason: ${result.getFullMessage(resourceProvider)} , action: $action , input event type: $inputEventType , key meta state: $keyMetaState " ,
835812 )
836813 }
837814
838- result? .showErrorMessageOnFail()
815+ result.showErrorMessageOnFail()
839816 }
840817
841818 override fun getErrorSnapshot (): ActionErrorSnapshot {
@@ -925,7 +902,7 @@ interface PerformActionsUseCase {
925902 val defaultRepeatDelay: Flow <Long >
926903 val defaultRepeatRate: Flow <Long >
927904
928- fun perform (
905+ suspend fun perform (
929906 action : ActionData ,
930907 inputEventType : InputEventType = InputEventType .DOWN_UP ,
931908 keyMetaState : Int = 0,
0 commit comments