diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index d272be11..8a5b6b18 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -129,6 +129,8 @@ androidx-dynamicanimation = "androidx.dynamicanimation:dynamicanimation-ktx:1.0. androidx-media3-exoplayer = { module = "androidx.media3:media3-exoplayer", version.ref = "media3" } androidx-media3-ui = { module = "androidx.media3:media3-ui", version.ref = "media3" } +glide = "com.github.bumptech.glide:glide:4.15.1" + mdc = "com.google.android.material:material:1.9.0" [plugins] diff --git a/samples/README.md b/samples/README.md index 0be8e2a8..fee5adc7 100644 --- a/samples/README.md +++ b/samples/README.md @@ -22,6 +22,8 @@ Shows how to create a GATT server and communicate with the GATT client Demonstrates how to implement data access auditing for your app to identify - [Displaying UltraHDR](graphics/ultrahdr/src/main/java/com/example/platform/graphics/ultrahdr/display/DisplayingUltraHDR.kt): This sample demonstrates displaying an UltraHDR image. +- [Displaying UltraHDR (Glide)](graphics/ultrahdr/src/main/java/com/example/platform/graphics/ultrahdr/display/DisplayingUltraHDRUsingGlide.kt): +This sample demonstrates using the Glide image loading library to detect the - [Downloadable Fonts](user-interface/text/src/main/java/com/example/platform/ui/text/DownloadableFonts.kt): Download fonts instead of bundling them in the app resources. - [Drag and Drop](user-interface/draganddrop/src/main/java/com/example/platform/ui/draganddrop/DragAndDrop.kt): diff --git a/samples/graphics/ultrahdr/build.gradle.kts b/samples/graphics/ultrahdr/build.gradle.kts index 8522c392..2848b663 100644 --- a/samples/graphics/ultrahdr/build.gradle.kts +++ b/samples/graphics/ultrahdr/build.gradle.kts @@ -26,5 +26,6 @@ android { } dependencies { - // Add samples specific dependencies + // Glide + implementation(libs.glide) } \ No newline at end of file diff --git a/samples/graphics/ultrahdr/src/main/assets/gainmaps/night_highrise.jpg b/samples/graphics/ultrahdr/src/main/assets/gainmaps/night_highrise.jpg new file mode 100755 index 00000000..a48db8d9 Binary files /dev/null and b/samples/graphics/ultrahdr/src/main/assets/gainmaps/night_highrise.jpg differ diff --git a/samples/graphics/ultrahdr/src/main/assets/sdr/night_highrise.jpg b/samples/graphics/ultrahdr/src/main/assets/sdr/night_highrise.jpg new file mode 100644 index 00000000..fa3a5846 Binary files /dev/null and b/samples/graphics/ultrahdr/src/main/assets/sdr/night_highrise.jpg differ diff --git a/samples/graphics/ultrahdr/src/main/java/com/example/platform/graphics/ultrahdr/common/ColorModeControls.kt b/samples/graphics/ultrahdr/src/main/java/com/example/platform/graphics/ultrahdr/common/ColorModeControls.kt index 58a5c464..9e9d7f99 100644 --- a/samples/graphics/ultrahdr/src/main/java/com/example/platform/graphics/ultrahdr/common/ColorModeControls.kt +++ b/samples/graphics/ultrahdr/src/main/java/com/example/platform/graphics/ultrahdr/common/ColorModeControls.kt @@ -43,7 +43,7 @@ class ColorModeControls : LinearLayout, WindowObserver { * Android ViewBinding. */ private var _binding: ColorModeControlsBinding? = null - private val binding get() = _binding!! + val binding get() = _binding!! /** * Reference to [Window]. This should come from the currently active activity. diff --git a/samples/graphics/ultrahdr/src/main/java/com/example/platform/graphics/ultrahdr/display/DisplayingUltraHDRUsingGlide.kt b/samples/graphics/ultrahdr/src/main/java/com/example/platform/graphics/ultrahdr/display/DisplayingUltraHDRUsingGlide.kt new file mode 100644 index 00000000..7447dbc7 --- /dev/null +++ b/samples/graphics/ultrahdr/src/main/java/com/example/platform/graphics/ultrahdr/display/DisplayingUltraHDRUsingGlide.kt @@ -0,0 +1,117 @@ +/* + * Copyright 2023 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.platform.graphics.ultrahdr.display + +import android.content.pm.ActivityInfo +import android.graphics.Bitmap +import android.graphics.drawable.Drawable +import android.net.Uri +import android.os.Build.VERSION_CODES +import android.os.Bundle +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import androidx.annotation.RequiresApi +import androidx.fragment.app.Fragment +import com.bumptech.glide.Glide +import com.bumptech.glide.request.target.CustomTarget +import com.bumptech.glide.request.target.CustomViewTarget +import com.bumptech.glide.request.transition.Transition +import com.example.platform.graphics.ultrahdr.databinding.DisplayingUltrahdrUsingGlideBinding +import com.google.android.catalog.framework.annotations.Sample + +@Sample( + name = "Displaying UltraHDR (Glide)", + description = "This sample demonstrates using the Glide image loading library to detect the" + + " presence of a gainmap to enable HDR mode when displaying an image using this library.", + documentation = "https://github.com/bumptech/glide", + tags = ["UltraHDR"], +) +@RequiresApi(VERSION_CODES.UPSIDE_DOWN_CAKE) +class DisplayingUltraHDRUsingGlide : Fragment() { + /** + * Android ViewBinding. + */ + private var _binding: DisplayingUltrahdrUsingGlideBinding? = null + private val binding get() = _binding!! + + /** + * Using [Glide]s [CustomTarget] class, we can access the given [Bitmap] to check for the + * presence of a gainmap, indicating that we should enable the HDR color mode. + * + * The same could be done with [CustomViewTarget] as well. + */ + private val target: CustomTarget = object : CustomTarget() { + override fun onResourceReady(resource: Bitmap, transition: Transition?) { + binding.imageContainer.setImageBitmap(resource) + + // Set color mode of the activity to the correct color mode. + requireActivity().window.colorMode = when (resource.hasGainmap()) { + true -> ActivityInfo.COLOR_MODE_HDR + else -> ActivityInfo.COLOR_MODE_DEFAULT + } + } + + override fun onLoadCleared(placeholder: Drawable?) { + // clear resources if need be. + } + } + + override fun onCreateView( + inflater: LayoutInflater, + container: ViewGroup?, + savedInstanceState: Bundle?, + ): View { + _binding = DisplayingUltrahdrUsingGlideBinding.inflate(inflater, container, false) + return binding.root + } + + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) + binding.colorModeControls.setWindow(requireActivity().window) + loadImageWithGlide(SDR_IMAGE) + + // Disable color mode controls to demonstrate glide enabling hdr mode when a gainmap is + // is detected. + binding.colorModeControls.binding.ultrahdrColorModeSdr.isEnabled = false + binding.colorModeControls.binding.ultrahdrColorModeHdr.isEnabled = false + + binding.optionSdrImage.setOnClickListener { loadImageWithGlide(SDR_IMAGE) } + binding.optionUltrahdrImage.setOnClickListener { loadImageWithGlide(ULTRAHDR_IMAGE) } + } + + /** + * Load an image using [Glide]. + */ + private fun loadImageWithGlide(path: String) = + Glide.with(this) + .asBitmap() + .load(Uri.parse(path)) + .into(target) + + override fun onDetach() { + super.onDetach() + binding.colorModeControls.detach() + } + + companion object { + /** + * Sample UltraHDR images paths + */ + private const val SDR_IMAGE = "file:///android_asset/sdr/night_highrise.jpg" + private const val ULTRAHDR_IMAGE = "file:///android_asset/gainmaps/night_highrise.jpg" + } +} \ No newline at end of file diff --git a/samples/graphics/ultrahdr/src/main/res/layout/displaying_ultrahdr_using_glide.xml b/samples/graphics/ultrahdr/src/main/res/layout/displaying_ultrahdr_using_glide.xml new file mode 100644 index 00000000..66a4e6cb --- /dev/null +++ b/samples/graphics/ultrahdr/src/main/res/layout/displaying_ultrahdr_using_glide.xml @@ -0,0 +1,63 @@ + + + + + + + +