diff --git a/.idea/kotlinc.xml b/.idea/kotlinc.xml
index 951989e5d2..5892dfeff5 100644
--- a/.idea/kotlinc.xml
+++ b/.idea/kotlinc.xml
@@ -4,6 +4,6 @@
-
+
\ No newline at end of file
diff --git a/media/audio/src/main/java/com/google/android/horologist/audio/OutputSwitcher.kt b/media/audio/src/main/java/com/google/android/horologist/audio/OutputSwitcher.kt
new file mode 100644
index 0000000000..ab2733db17
--- /dev/null
+++ b/media/audio/src/main/java/com/google/android/horologist/audio/OutputSwitcher.kt
@@ -0,0 +1,68 @@
+/*
+ * Copyright 2021 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
+ *
+ * https://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.google.android.horologist.audio
+
+import android.content.ComponentName
+import android.content.Context
+import android.content.Intent
+import android.content.pm.ApplicationInfo
+
+/**
+ * Support for launching the system output switcher.
+ *
+ * https://developer.android.com/guide/topics/media/media-routing#output-switcher.
+ *
+ */
+public object OutputSwitcher {
+ /**
+ * Open the Output Switcher Dialog.
+ */
+ public fun Context.launchSystemMediaOutputSwitcherUi(): Boolean {
+ val outputSwitcherLaunchIntent: Intent = Intent(OUTPUT_SWITCHER_INTENT_ACTION_NAME)
+ .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
+ .putExtra(EXTRA_OUTPUT_SWITCHER_PACKAGE_NAME, packageName)
+ val outputSwitcherSystemComponentName =
+ getSystemOrSystemUpdatedAppComponent(outputSwitcherLaunchIntent)
+ if (outputSwitcherSystemComponentName != null) {
+ outputSwitcherLaunchIntent.component = outputSwitcherSystemComponentName
+ startActivity(outputSwitcherLaunchIntent)
+ return true
+ }
+ return false
+ }
+
+ private fun Context.getSystemOrSystemUpdatedAppComponent(intent: Intent): ComponentName? {
+ val packageManager = packageManager
+ val resolveInfos = packageManager.queryIntentActivities(intent, 0)
+ for (resolveInfo in resolveInfos) {
+ val activityInfo = resolveInfo.activityInfo
+ if (activityInfo?.applicationInfo == null) {
+ continue
+ }
+ val appInfo = activityInfo.applicationInfo
+ val systemAndUpdatedSystemAppFlags =
+ ApplicationInfo.FLAG_SYSTEM or ApplicationInfo.FLAG_UPDATED_SYSTEM_APP
+ if (systemAndUpdatedSystemAppFlags and appInfo.flags != 0) {
+ return ComponentName(activityInfo.packageName, activityInfo.name)
+ }
+ }
+ return null
+ }
+
+ private const val EXTRA_OUTPUT_SWITCHER_PACKAGE_NAME = "com.android.settings.panel.extra.PACKAGE_NAME"
+ private const val OUTPUT_SWITCHER_INTENT_ACTION_NAME = "com.android.settings.panel.action.MEDIA_OUTPUT"
+}
diff --git a/media/audio/src/main/java/com/google/android/horologist/audio/SystemAudioRepository.kt b/media/audio/src/main/java/com/google/android/horologist/audio/SystemAudioRepository.kt
index d0b2bdd0e6..f0abf50bbb 100644
--- a/media/audio/src/main/java/com/google/android/horologist/audio/SystemAudioRepository.kt
+++ b/media/audio/src/main/java/com/google/android/horologist/audio/SystemAudioRepository.kt
@@ -22,6 +22,7 @@ import androidx.mediarouter.media.MediaRouteSelector
import androidx.mediarouter.media.MediaRouter
import androidx.mediarouter.media.MediaRouter.RouteInfo
import com.google.android.horologist.audio.BluetoothSettings.launchBluetoothSettings
+import com.google.android.horologist.audio.OutputSwitcher.launchSystemMediaOutputSwitcherUi
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
@@ -105,7 +106,9 @@ public class SystemAudioRepository(
}
override fun launchOutputSelection(closeOnConnect: Boolean) {
- application.launchBluetoothSettings(closeOnConnect)
+ if (!application.launchSystemMediaOutputSwitcherUi()) {
+ application.launchBluetoothSettings(closeOnConnect)
+ }
}
public companion object {