1 /*
2  * Copyright (C) 2023 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.screenshot
18 
19 import android.media.MediaPlayer
20 import android.util.Log
21 import com.android.app.tracing.coroutines.async
22 import com.android.systemui.dagger.qualifiers.Application
23 import com.android.systemui.dagger.qualifiers.Background
24 import javax.inject.Inject
25 import kotlin.time.Duration.Companion.seconds
26 import kotlinx.coroutines.CoroutineDispatcher
27 import kotlinx.coroutines.CoroutineScope
28 import kotlinx.coroutines.Deferred
29 import kotlinx.coroutines.TimeoutCancellationException
30 import kotlinx.coroutines.launch
31 import kotlinx.coroutines.withContext
32 import kotlinx.coroutines.withTimeout
33 
34 /** Controls sound reproduction after a screenshot is taken. */
35 interface ScreenshotSoundController {
36     /** Reproduces the camera sound. */
playScreenshotSoundnull37     suspend fun playScreenshotSound()
38 
39     /**
40      * Releases the sound. [playScreenshotSound] behaviour is undefined after this has been called.
41      */
42     suspend fun releaseScreenshotSound()
43 
44     /** Reproduces the camera sound. Used for compatibility with Java code. */
45     fun playScreenshotSoundAsync()
46 
47     /**
48      * Releases the sound. [playScreenshotSound] behaviour is undefined after this has been called.
49      * Used for compatibility with Java code.
50      */
51     fun releaseScreenshotSoundAsync()
52 }
53 
54 class ScreenshotSoundControllerImpl
55 @Inject
56 constructor(
57     private val soundProvider: ScreenshotSoundProvider,
58     @Application private val coroutineScope: CoroutineScope,
59     @Background private val bgDispatcher: CoroutineDispatcher
60 ) : ScreenshotSoundController {
61 
62     private val player: Deferred<MediaPlayer?> =
63         coroutineScope.async("loadScreenshotSound", bgDispatcher) {
64             try {
65                 soundProvider.getScreenshotSound()
66             } catch (e: IllegalStateException) {
67                 Log.w(TAG, "Screenshot sound initialization failed", e)
68                 null
69             }
70         }
71 
72     override suspend fun playScreenshotSound() {
73         withContext(bgDispatcher) {
74             try {
75                 player.await()?.start()
76             } catch (e: IllegalStateException) {
77                 Log.w(TAG, "Screenshot sound failed to play", e)
78                 releaseScreenshotSound()
79             }
80         }
81     }
82 
83     override suspend fun releaseScreenshotSound() {
84         withContext(bgDispatcher) {
85             try {
86                 withTimeout(1.seconds) { player.await()?.release() }
87             } catch (e: TimeoutCancellationException) {
88                 player.cancel()
89                 Log.w(TAG, "Error releasing shutter sound", e)
90             }
91         }
92     }
93 
94     override fun playScreenshotSoundAsync() {
95         coroutineScope.launch { playScreenshotSound() }
96     }
97 
98     override fun releaseScreenshotSoundAsync() {
99         coroutineScope.launch { releaseScreenshotSound() }
100     }
101 
102     private companion object {
103         const val TAG = "ScreenshotSoundControllerImpl"
104     }
105 }
106