Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 4a94e6fd authored by Treehugger Robot's avatar Treehugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Move ScreenRecordingService binder calls to a background thread" into main

parents 4372a34f a7f49465
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -20,7 +20,9 @@ import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.ServiceConnection
import android.graphics.drawable.Icon
import android.media.projection.StopReason
import android.net.Uri
import android.os.IBinder
import androidx.annotation.WorkerThread
import com.android.app.tracing.coroutines.flow.asStateFlowTraced
@@ -182,6 +184,8 @@ constructor(
        override fun onRecordingInterrupted(userId: Int, reason: Int) {
            stopRecording(reason)
        }

        override fun onRecordingSaved(recordingUri: Uri?, thumbnail: Icon?) {}
    }

    private data class RecordingContext(val status: Status, val service: IScreenRecordingService?)
+4 −0
Original line number Diff line number Diff line
@@ -19,6 +19,8 @@ package com.android.systemui.screenrecord.service;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.net.Uri;
import android.graphics.drawable.Icon;

oneway interface IScreenRecordingServiceCallback {

@@ -31,4 +33,6 @@ oneway interface IScreenRecordingServiceCallback {
    * Called when the recording is interrupted for some reason.
    */
    void onRecordingInterrupted(int userId, int reason);

    void onRecordingSaved(in Uri recordingUri, in Icon thumbnail);
}
 No newline at end of file
+18 −8
Original line number Diff line number Diff line
@@ -78,19 +78,20 @@ protected constructor(
            },
        )

    private val backgroundContext = Dispatchers.IO
    private val binder = BinderInterface()
    private val screenMediaRecorderListener: ScreenMediaRecorder.ScreenMediaRecorderListener =
        object : ScreenMediaRecorder.ScreenMediaRecorderListener {
            override fun onStarted() {
                callback?.onRecordingStarted()
                launchCallbackAction { onRecordingStarted() }
            }

            override fun onInfo(mr: MediaRecorder?, what: Int, extra: Int) {
                callback?.onRecordingInterrupted(userId, StopReason.STOP_ERROR)
                launchCallbackAction { onRecordingInterrupted(userId, StopReason.STOP_ERROR) }
            }

            override fun onStopped(userId: Int, @StopReason stopReason: Int) {
                callback?.onRecordingInterrupted(userId, stopReason)
                launchCallbackAction { onRecordingInterrupted(userId, stopReason) }
            }
        }

@@ -108,11 +109,13 @@ protected constructor(
        val action = intent?.action
        when (action) {
            ACTION_STOP ->
                callback?.onRecordingInterrupted(
                launchCallbackAction {
                    onRecordingInterrupted(
                        userId,
                        intent.getIntExtra(EXTRA_STOP_REASON, StopReason.STOP_UNKNOWN),
                    )
                }
        }
        return START_NOT_STICKY
    }

@@ -143,7 +146,10 @@ protected constructor(
                notificationId = notificationId,
                audioSource = audioSource,
            )
            val savedRecording: SavedRecording = withContext(Dispatchers.IO) { recorder.save() }
            val savedRecording: SavedRecording =
                withContext(backgroundContext) {
                    recorder.save().apply { callback?.onRecordingSaved(uri, thumbnail) }
                }
            onRecordingSaved(this, savedRecording)
        } catch (e: Exception) {
            notificationInteractor.notifyErrorSaving(notificationId)
@@ -178,6 +184,10 @@ protected constructor(
    private fun getShouldShowTouches(): Boolean =
        Settings.System.getInt(contentResolver, Settings.System.SHOW_TOUCHES, 0) != 0

    private fun launchCallbackAction(action: IScreenRecordingServiceCallback.() -> Unit) {
        callback?.let { coroutineScope.launch(backgroundContext) { it.action() } }
    }

    private inner class BinderInterface : IScreenRecordingService.Stub() {

        override fun setCallback(serviceCallback: IScreenRecordingServiceCallback?) {
+9 −0
Original line number Diff line number Diff line
@@ -16,6 +16,8 @@

package com.android.systemui.screenrecord.service

import android.graphics.drawable.Icon
import android.net.Uri
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
@@ -36,12 +38,19 @@ class FakeScreenRecordingServiceCallbackWrapper(private val real: IScreenRecordi
        real.onRecordingInterrupted(userId, reason)
    }

    override fun onRecordingSaved(recordingUri: Uri, thumbnail: Icon) {
        _status.value = RecordingStatus.Saved(recordingUri = recordingUri, thumbnail = thumbnail)
        real.onRecordingSaved(recordingUri, thumbnail)
    }

    sealed interface RecordingStatus {

        data object Initial : RecordingStatus

        data object Started : RecordingStatus

        data class Saved(val recordingUri: Uri, val thumbnail: Icon) : RecordingStatus

        data class Interrupted(val userId: Int, val reason: Int) : RecordingStatus
    }
}