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.content.Context
20 import android.media.AudioAttributes
21 import android.media.AudioSystem
22 import android.media.MediaPlayer
23 import android.net.Uri
24 import com.android.internal.R
25 import com.android.systemui.dagger.SysUISingleton
26 import java.io.File
27 import javax.inject.Inject
28 
29 /** Provides a [MediaPlayer] that reproduces the screenshot sound. */
30 interface ScreenshotSoundProvider {
31 
32     /**
33      * Creates a new [MediaPlayer] that reproduces the screenshot sound. This should be called from
34      * a background thread, as it might take time.
35      */
getScreenshotSoundnull36     fun getScreenshotSound(): MediaPlayer
37 }
38 
39 @SysUISingleton
40 class ScreenshotSoundProviderImpl
41 @Inject
42 constructor(
43     private val context: Context,
44 ) : ScreenshotSoundProvider {
45     override fun getScreenshotSound(): MediaPlayer {
46         return MediaPlayer.create(
47             context,
48             Uri.fromFile(File(context.resources.getString(R.string.config_cameraShutterSound))),
49             /* holder = */ null,
50             AudioAttributes.Builder()
51                 .setUsage(AudioAttributes.USAGE_ASSISTANCE_SONIFICATION)
52                 .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
53                 .build(),
54             AudioSystem.newAudioSessionId()
55         )
56     }
57 }
58