1 /*
2  * Copyright (C) 2022 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.media.muteawait
18 
19 import android.content.Context
20 import android.media.AudioAttributes.USAGE_MEDIA
21 import android.media.AudioDeviceAttributes
22 import android.media.AudioManager
23 import com.android.systemui.dagger.SysUISingleton
24 import com.android.systemui.statusbar.commandline.Command
25 import com.android.systemui.statusbar.commandline.CommandRegistry
26 import java.io.PrintWriter
27 import java.util.concurrent.TimeUnit
28 import javax.inject.Inject
29 
30 /** A command line interface to manually test [MediaMuteAwaitConnectionManager]. */
31 @SysUISingleton
32 class MediaMuteAwaitConnectionCli @Inject constructor(
33     commandRegistry: CommandRegistry,
34     private val context: Context
35 ) {
36     init {
37         commandRegistry.registerCommand(MEDIA_MUTE_AWAIT_COMMAND) { MuteAwaitCommand() }
38     }
39 
40     inner class MuteAwaitCommand : Command {
41         override fun execute(pw: PrintWriter, args: List<String>) {
42             val device = AudioDeviceAttributes(
43                 AudioDeviceAttributes.ROLE_OUTPUT,
44                 /* type= */ Integer.parseInt(args[0]),
45                 ADDRESS,
46                 /* name= */ args[1],
47                 listOf(),
48                 listOf(),
49             )
50             val startOrCancel = args[2]
51 
52             val audioManager: AudioManager =
53                 context.getSystemService(Context.AUDIO_SERVICE) as AudioManager
54             when (startOrCancel) {
55                 START ->
56                     audioManager.muteAwaitConnection(
57                             intArrayOf(USAGE_MEDIA), device, TIMEOUT, TIMEOUT_UNITS
58                     )
59                 CANCEL -> audioManager.cancelMuteAwaitConnection(device)
60                 else -> pw.println("Must specify `$START` or `$CANCEL`; was $startOrCancel")
61             }
62         }
63         override fun help(pw: PrintWriter) {
64             pw.println("Usage: adb shell cmd statusbar $MEDIA_MUTE_AWAIT_COMMAND " +
65                     "[type] [name] [$START|$CANCEL]")
66         }
67     }
68 }
69 
70 private const val MEDIA_MUTE_AWAIT_COMMAND = "media-mute-await"
71 private const val START = "start"
72 private const val CANCEL = "cancel"
73 private const val ADDRESS = "address"
74 private const val TIMEOUT = 5L
75 private val TIMEOUT_UNITS = TimeUnit.SECONDS
76