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

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

Merge "Refactor IssueRecordingService to allow for Unit tests" into main

parents e07fd647 7981b99e
Loading
Loading
Loading
Loading
+23 −46
Original line number Diff line number Diff line
@@ -23,8 +23,6 @@ import android.content.Intent
import android.content.res.Resources
import android.net.Uri
import android.os.Handler
import android.os.UserHandle
import android.provider.Settings
import android.util.Log
import com.android.internal.logging.UiEventLogger
import com.android.systemui.animation.DialogTransitionAnimator
@@ -50,11 +48,11 @@ constructor(
    notificationManager: NotificationManager,
    userContextProvider: UserContextProvider,
    keyguardDismissUtil: KeyguardDismissUtil,
    private val dialogTransitionAnimator: DialogTransitionAnimator,
    private val panelInteractor: PanelInteractor,
    private val traceurMessageSender: TraceurMessageSender,
    dialogTransitionAnimator: DialogTransitionAnimator,
    panelInteractor: PanelInteractor,
    traceurMessageSender: TraceurMessageSender,
    private val issueRecordingState: IssueRecordingState,
    private val iActivityManager: IActivityManager,
    iActivityManager: IActivityManager,
) :
    RecordingService(
        controller,
@@ -66,6 +64,18 @@ constructor(
        keyguardDismissUtil
    ) {

    private val commandHandler =
        IssueRecordingServiceCommandHandler(
            bgExecutor,
            dialogTransitionAnimator,
            panelInteractor,
            traceurMessageSender,
            issueRecordingState,
            iActivityManager,
            notificationManager,
            userContextProvider,
        )

    override fun getTag(): String = TAG

    override fun getChannelId(): String = CHANNEL_ID
@@ -76,10 +86,7 @@ constructor(
        Log.d(getTag(), "handling action: ${intent?.action}")
        when (intent?.action) {
            ACTION_START -> {
                bgExecutor.execute {
                    traceurMessageSender.startTracing(issueRecordingState.traceConfig)
                }
                issueRecordingState.isRecording = true
                commandHandler.handleStartCommand()
                if (!issueRecordingState.recordScreen) {
                    // If we don't want to record the screen, the ACTION_SHOW_START_NOTIF action
                    // will circumvent the RecordingService's screen recording start code.
@@ -87,41 +94,13 @@ constructor(
                }
            }
            ACTION_STOP,
            ACTION_STOP_NOTIF -> {
                // ViewCapture needs to save it's data before it is disabled, or else the data will
                // be lost. This is expected to change in the near future, and when that happens
                // this line should be removed.
                bgExecutor.execute {
                    if (issueRecordingState.traceConfig.longTrace) {
                        Settings.Global.putInt(
                            contentResolver,
                            NOTIFY_SESSION_ENDED_SETTING,
                            DISABLED
                        )
                    }
                    traceurMessageSender.stopTracing()
                }
                issueRecordingState.isRecording = false
            }
            ACTION_STOP_NOTIF -> commandHandler.handleStopCommand(contentResolver)
            ACTION_SHARE -> {
                bgExecutor.execute {
                    mNotificationManager.cancelAsUser(
                        null,
                commandHandler.handleShareCommand(
                    intent.getIntExtra(EXTRA_NOTIFICATION_ID, mNotificationId),
                        UserHandle(mUserContextTracker.userContext.userId)
                    intent.getParcelableExtra(EXTRA_PATH, Uri::class.java),
                    this
                )

                    val screenRecording = intent.getParcelableExtra(EXTRA_PATH, Uri::class.java)
                    if (issueRecordingState.takeBugreport) {
                        iActivityManager.requestBugReportWithExtraAttachment(screenRecording)
                    } else {
                        traceurMessageSender.shareTraces(applicationContext, screenRecording)
                    }
                }

                dialogTransitionAnimator.disableAllCurrentDialogsExitAnimations()
                panelInteractor.collapsePanels()

                // Unlike all other actions, action_share has different behavior for the screen
                // recording qs tile than it does for the record issue qs tile. Return sticky to
                // avoid running any of the base class' code for this action.
@@ -135,8 +114,6 @@ constructor(
    companion object {
        private const val TAG = "IssueRecordingService"
        private const val CHANNEL_ID = "issue_record"
        private const val NOTIFY_SESSION_ENDED_SETTING = "should_notify_trace_session_ended"
        private const val DISABLED = 0

        /**
         * Get an intent to stop the issue recording service.
+83 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.systemui.recordissue

import android.app.IActivityManager
import android.app.NotificationManager
import android.content.ContentResolver
import android.content.Context
import android.net.Uri
import android.os.UserHandle
import android.provider.Settings
import com.android.systemui.animation.DialogTransitionAnimator
import com.android.systemui.qs.pipeline.domain.interactor.PanelInteractor
import com.android.systemui.settings.UserContextProvider
import java.util.concurrent.Executor

private const val NOTIFY_SESSION_ENDED_SETTING = "should_notify_trace_session_ended"
private const val DISABLED = 0

/**
 * This class exists to unit test the business logic encapsulated in IssueRecordingService. Android
 * specifically calls out that there is no supported way to test IntentServices here:
 * https://developer.android.com/training/testing/other-components/services
 */
class IssueRecordingServiceCommandHandler(
    private val bgExecutor: Executor,
    private val dialogTransitionAnimator: DialogTransitionAnimator,
    private val panelInteractor: PanelInteractor,
    private val traceurMessageSender: TraceurMessageSender,
    private val issueRecordingState: IssueRecordingState,
    private val iActivityManager: IActivityManager,
    private val notificationManager: NotificationManager,
    private val userContextProvider: UserContextProvider,
) {

    fun handleStartCommand() {
        bgExecutor.execute { traceurMessageSender.startTracing(issueRecordingState.traceConfig) }
        issueRecordingState.isRecording = true
    }

    fun handleStopCommand(contentResolver: ContentResolver) {
        bgExecutor.execute {
            if (issueRecordingState.traceConfig.longTrace) {
                Settings.Global.putInt(contentResolver, NOTIFY_SESSION_ENDED_SETTING, DISABLED)
            }
            traceurMessageSender.stopTracing()
        }
        issueRecordingState.isRecording = false
    }

    fun handleShareCommand(notificationId: Int, screenRecording: Uri?, context: Context) {
        bgExecutor.execute {
            notificationManager.cancelAsUser(
                null,
                notificationId,
                UserHandle(userContextProvider.userContext.userId)
            )

            if (issueRecordingState.takeBugreport) {
                iActivityManager.requestBugReportWithExtraAttachment(screenRecording)
            } else {
                traceurMessageSender.shareTraces(context, screenRecording)
            }
        }

        dialogTransitionAnimator.disableAllCurrentDialogsExitAnimations()
        panelInteractor.collapsePanels()
    }
}