Skip to content

Commit

Permalink
Modify to launch output switcher from the volume screen if that is av…
Browse files Browse the repository at this point in the history
…ailable. (#1640)
  • Loading branch information
anubhavkakkar authored Sep 1, 2023
1 parent 817907e commit 646b5ed
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .idea/kotlinc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -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"
}
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -105,7 +106,9 @@ public class SystemAudioRepository(
}

override fun launchOutputSelection(closeOnConnect: Boolean) {
application.launchBluetoothSettings(closeOnConnect)
if (!application.launchSystemMediaOutputSwitcherUi()) {
application.launchBluetoothSettings(closeOnConnect)
}
}

public companion object {
Expand Down

0 comments on commit 646b5ed

Please sign in to comment.