-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Modify to launch output switcher from the volume screen if that is av…
…ailable. (#1640)
- Loading branch information
1 parent
817907e
commit 646b5ed
Showing
3 changed files
with
73 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
68 changes: 68 additions & 0 deletions
68
media/audio/src/main/java/com/google/android/horologist/audio/OutputSwitcher.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters