-
Notifications
You must be signed in to change notification settings - Fork 357
Migrate TV snippets #873
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
kkuan2011
wants to merge
2
commits into
main
Choose a base branch
from
katherinekuan/tv-snippets
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Migrate TV snippets #873
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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,43 @@ | ||
| /* | ||
| * Copyright 2026 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.example.tv.ui | ||
|
|
||
| import android.view.WindowManager | ||
| import androidx.activity.ComponentActivity | ||
| import androidx.activity.compose.LocalActivity | ||
| import androidx.compose.runtime.Composable | ||
| import androidx.compose.runtime.DisposableEffect | ||
|
|
||
| class AmbientModeActivity : ComponentActivity() { | ||
|
|
||
| @Composable | ||
| private fun KeepScreenOn(enable: Boolean = true) { | ||
| val activity = LocalActivity.current | ||
| DisposableEffect(enable) { | ||
| if (enable) { | ||
| // [START android_tv_ambient_mode_on] | ||
| activity?.window?.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON) | ||
| // [END android_tv_ambient_mode_on] | ||
| } | ||
| onDispose { | ||
| // [START android_tv_ambient_mode_off] | ||
| activity?.window?.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON) | ||
| // [END android_tv_ambient_mode_off] | ||
| } | ||
| } | ||
| } | ||
| } |
116 changes: 116 additions & 0 deletions
116
tv/src/main/java/com/example/tv/ui/AudioCapabilities.kt
This file contains hidden or 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,116 @@ | ||
| /* | ||
| * Copyright 2026 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.example.tv.ui | ||
|
|
||
| import android.media.AudioAttributes | ||
| import android.media.AudioDeviceInfo | ||
| import android.media.AudioFormat | ||
| import android.media.AudioManager | ||
| import android.media.AudioTrack | ||
| import android.os.Build | ||
| import android.os.Handler | ||
| import android.os.Looper | ||
| import androidx.activity.ComponentActivity | ||
| import androidx.annotation.RequiresApi | ||
|
|
||
| class AudioCapabilitiesActivity : ComponentActivity() { | ||
| private lateinit var audioManager: AudioManager | ||
|
|
||
| @RequiresApi(Build.VERSION_CODES.TIRAMISU) | ||
| fun anticipatoryAudioRouteCheck() { | ||
| // [START android_tv_audio_capabilities_check] | ||
| val format = AudioFormat.Builder() | ||
| .setEncoding(AudioFormat.ENCODING_E_AC3) | ||
| .setChannelMask(AudioFormat.CHANNEL_OUT_5POINT1) | ||
| .setSampleRate(48000) | ||
| .build() | ||
| val attributes = AudioAttributes.Builder() | ||
| .setUsage(AudioAttributes.USAGE_MEDIA) | ||
| .build() | ||
|
|
||
| if (AudioManager.getDirectPlaybackSupport(format, attributes) != | ||
| AudioManager.DIRECT_PLAYBACK_NOT_SUPPORTED | ||
| ) { | ||
| // The format and attributes are supported for direct playback | ||
| // on the currently active routed audio path | ||
| } else { | ||
| // The format and attributes are NOT supported for direct playback | ||
| // on the currently active routed audio path | ||
| } | ||
| // [END android_tv_audio_capabilities_check] | ||
| } | ||
|
|
||
| private fun findBestSampleRate(profile: Any): Int = 48000 | ||
| private fun findBestChannelMask(profile: Any): Int = AudioFormat.CHANNEL_OUT_STEREO | ||
|
|
||
| @RequiresApi(Build.VERSION_CODES.TIRAMISU) | ||
| // [START android_tv_audio_capabilities_best_format] | ||
| private fun findBestAudioFormat(audioAttributes: AudioAttributes): AudioFormat { | ||
| val preferredFormats = listOf( | ||
| AudioFormat.ENCODING_E_AC3, | ||
| AudioFormat.ENCODING_AC3, | ||
| AudioFormat.ENCODING_PCM_16BIT, | ||
| AudioFormat.ENCODING_DEFAULT | ||
| ) | ||
| val audioProfiles = audioManager.getDirectProfilesForAttributes(audioAttributes) | ||
| val bestAudioProfile = preferredFormats.firstNotNullOf { format -> | ||
| audioProfiles.firstOrNull { it.format == format } | ||
| } | ||
| val sampleRate = findBestSampleRate(bestAudioProfile) | ||
| val channelMask = findBestChannelMask(bestAudioProfile) | ||
| return AudioFormat.Builder() | ||
| .setEncoding(bestAudioProfile.format) | ||
| .setSampleRate(sampleRate) | ||
| .setChannelMask(channelMask) | ||
| .build() | ||
| } | ||
| // [END android_tv_audio_capabilities_best_format] | ||
|
|
||
| private fun restartAudioTrack(info: AudioDeviceInfo?) {} | ||
| private fun findDefaultAudioDeviceInfo(): AudioDeviceInfo? = null | ||
| private fun needsAudioFormatChange(info: AudioDeviceInfo): Boolean = false | ||
|
|
||
| fun interceptAudioDeviceChanges() { | ||
| val audioPlayer = AudioPlayerWrapper() | ||
| val handler = Handler(Looper.getMainLooper()) | ||
|
|
||
| // [START android_tv_audio_capabilities_intercept] | ||
| // audioPlayer is a wrapper around an AudioTrack | ||
| // which calls a callback for an AudioTrack write error | ||
| audioPlayer.addAudioTrackWriteErrorListener { | ||
| // error code can be checked here, | ||
| // in case of write error try to recreate the audio track | ||
| restartAudioTrack(findDefaultAudioDeviceInfo()) | ||
| } | ||
|
|
||
| audioPlayer.audioTrack.addOnRoutingChangedListener({ audioRouting -> | ||
| audioRouting?.routedDevice?.let { audioDeviceInfo -> | ||
| // use the updated audio routed device to determine | ||
| // what audio format should be used | ||
| if (needsAudioFormatChange(audioDeviceInfo)) { | ||
| restartAudioTrack(audioDeviceInfo) | ||
| } | ||
| } | ||
| }, handler) | ||
| // [END android_tv_audio_capabilities_intercept] | ||
| } | ||
| } | ||
|
|
||
| private class AudioPlayerWrapper { | ||
| val audioTrack = AudioTrack.Builder().build() | ||
| fun addAudioTrackWriteErrorListener(listener: () -> Unit) {} | ||
| } | ||
This file contains hidden or 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,72 @@ | ||
| /* | ||
| * Copyright 2026 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.example.tv.ui | ||
|
|
||
| import android.content.Context | ||
| import android.media.MediaFormat | ||
| import android.media.quality.MediaQualityManager | ||
| import android.media.quality.PictureProfile | ||
| import android.os.Build | ||
| import android.os.Bundle | ||
| import androidx.activity.ComponentActivity | ||
|
|
||
| // [START android_tv_adjust_display_constants] | ||
| const val NAME_STANDARD: String = "standard" | ||
| const val NAME_VIVID: String = "vivid" | ||
| const val NAME_SPORTS: String = "sports" | ||
| const val NAME_GAME: String = "game" | ||
| const val NAME_MOVIE: String = "movie" | ||
| const val NAME_ENERGY_SAVING: String = "energy_saving" | ||
| const val NAME_USER: String = "user" | ||
| // [END android_tv_adjust_display_constants] | ||
|
|
||
| class DisplaySettingsActivity : ComponentActivity() { | ||
| private lateinit var context: Context | ||
| private lateinit var mediaCodec: android.media.MediaCodec | ||
| private lateinit var mediaQualityManager: MediaQualityManager | ||
|
|
||
| fun queryAndApplySportsProfile() { | ||
| // [START android_tv_adjust_display_query] | ||
| if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.BAKLAVA) { | ||
| val mediaQualityManager: MediaQualityManager = | ||
| context.getSystemService(MediaQualityManager::class.java) | ||
| val profiles = mediaQualityManager.getAvailablePictureProfiles(null) | ||
| for (profile in profiles) { | ||
| // If we have a system sports profile, apply it to our media codec | ||
| if (profile.profileType == PictureProfile.TYPE_SYSTEM && | ||
| profile.name == NAME_SPORTS | ||
| ) { | ||
| val bundle = Bundle().apply { | ||
| putParcelable(MediaFormat.KEY_PICTURE_PROFILE_INSTANCE, profile) | ||
| } | ||
| mediaCodec.setParameters(bundle) | ||
| } | ||
| } | ||
| } | ||
| // [END android_tv_adjust_display_query] | ||
| } | ||
|
|
||
| fun getSpecificProfileByName() { | ||
| // [START android_tv_adjust_display_get_specific] | ||
| if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.BAKLAVA) { | ||
| val profile = mediaQualityManager.getPictureProfile( | ||
| PictureProfile.TYPE_SYSTEM, NAME_SPORTS, null | ||
| ) | ||
| } | ||
| // [END android_tv_adjust_display_get_specific] | ||
| } | ||
| } |
This file contains hidden or 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,99 @@ | ||
| /* | ||
| * Copyright 2026 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.example.tv.ui | ||
|
|
||
| import android.annotation.SuppressLint | ||
| import androidx.activity.ComponentActivity | ||
| import android.content.Context | ||
| import android.content.pm.PackageManager | ||
| import android.location.Address | ||
| import android.location.Geocoder | ||
| import android.location.Location | ||
| import android.location.LocationManager | ||
| import android.util.Log | ||
| import java.io.IOException | ||
|
|
||
| // [START android_tv_hardware_check_tv] | ||
| const val TAG = "DeviceTypeRuntimeCheck" | ||
|
|
||
| // [START_EXCLUDE silent] | ||
| class HardwareActivity : ComponentActivity() { | ||
| val TAG = "HardwareActivity" | ||
|
|
||
| fun checkTvDevice() { | ||
|
|
||
| // [END_EXCLUDE] | ||
| val isTelevision = packageManager.hasSystemFeature(PackageManager.FEATURE_LEANBACK) | ||
| if (isTelevision) { | ||
| Log.d(TAG, "Running on a TV Device") | ||
| } else { | ||
| Log.d(TAG, "Running on a non-TV Device") | ||
| } | ||
| // [END android_tv_hardware_check_tv] | ||
| } | ||
|
|
||
| fun checkHardwareFeatures() { | ||
| // [START android_tv_hardware_check_feature] | ||
| // Check whether the telephony hardware feature is available. | ||
| if (packageManager.hasSystemFeature(PackageManager.FEATURE_TELEPHONY)) { | ||
| Log.d("HardwareFeatureTest", "Device can make phone calls") | ||
| } | ||
|
|
||
| // Check whether android.hardware.touchscreen feature is available. | ||
| if (packageManager.hasSystemFeature(PackageManager.FEATURE_TOUCHSCREEN)) { | ||
| Log.d("HardwareFeatureTest", "Device has a touchscreen.") | ||
| } | ||
| // [END android_tv_hardware_check_feature] | ||
| } | ||
|
|
||
| fun checkCamera() { | ||
| // [START android_tv_hardware_check_camera] | ||
| // Check whether the camera hardware feature is available. | ||
| if (packageManager.hasSystemFeature(PackageManager.FEATURE_CAMERA)) { | ||
| Log.d("Camera test", "Camera available!") | ||
| } else { | ||
| Log.d("Camera test", "No camera available. View and edit features only.") | ||
| } | ||
| // [END android_tv_hardware_check_camera] | ||
| } | ||
|
|
||
| @SuppressLint("MissingPermission") | ||
| fun gpsStaticLocation() { | ||
| // [START android_tv_hardware_gps_static] | ||
| // Request a static location from the location manager. | ||
| val locationManager = this.getSystemService(Context.LOCATION_SERVICE) as LocationManager | ||
| val location = locationManager.getLastKnownLocation("static") | ||
| if (location == null) { | ||
| Log.e(TAG, "Location is null") | ||
| return | ||
| } | ||
|
|
||
| // Attempt to get postal code from the static location object. | ||
| val geocoder = Geocoder(this) | ||
| val address: Address? = | ||
| try { | ||
| geocoder.getFromLocation(location.latitude, location.longitude, 1)?.firstOrNull() | ||
| ?.apply { | ||
| Log.d(TAG, postalCode) | ||
| } | ||
| } catch (e: IOException) { | ||
| Log.e(TAG, "Geocoder error", e) | ||
| null | ||
| } | ||
| // [END android_tv_hardware_gps_static] | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The use of
firstNotNullOfwill throw aNoSuchElementExceptionif no matching profile is found inaudioProfiles. It is safer to usefirstNotNullOfOrNulland handle the null case or provide a default value, especially sinceaudioProfilescould be empty depending on the device's capabilities.