1 /* 2 * Copyright (C) 2020 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.test.silkfx.common 18 19 import android.content.Context 20 import android.content.pm.ActivityInfo 21 import android.util.AttributeSet 22 import android.util.Log 23 import android.view.Display 24 import android.view.View 25 import android.view.Window 26 import android.widget.Button 27 import android.widget.LinearLayout 28 import android.widget.TextView 29 import com.android.test.silkfx.R 30 import com.android.test.silkfx.app.WindowObserver 31 import java.util.function.Consumer 32 33 class ColorModeControls : LinearLayout, WindowObserver { 34 constructor(context: Context) : this(context, null) 35 constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) 36 37 private var window: Window? = null 38 private var currentModeDisplay: TextView? = null 39 40 override fun onFinishInflate() { 41 super.onFinishInflate() 42 val window = window ?: throw IllegalStateException("Failed to attach window") 43 44 currentModeDisplay = findViewById(R.id.current_mode)!! 45 setColorMode(window.colorMode) 46 47 findViewById<Button>(R.id.mode_default)!!.setOnClickListener { 48 setColorMode(ActivityInfo.COLOR_MODE_DEFAULT) 49 } 50 findViewById<Button>(R.id.mode_wide)!!.setOnClickListener { 51 setColorMode(ActivityInfo.COLOR_MODE_WIDE_COLOR_GAMUT) 52 } 53 findViewById<Button>(R.id.mode_hdr)!!.setOnClickListener { 54 setColorMode(ActivityInfo.COLOR_MODE_HDR) 55 } 56 findViewById<Button>(R.id.mode_hdr10)!!.setOnClickListener { 57 setColorMode(ActivityInfo.COLOR_MODE_HDR10) 58 } 59 } 60 61 private val hdrSdrListener = Consumer<Display> { display -> 62 Log.d("SilkFX", "HDR/SDR changed ${display.hdrSdrRatio}") 63 post { 64 updateModeInfoDisplay() 65 } 66 } 67 68 override fun onAttachedToWindow() { 69 super.onAttachedToWindow() 70 val hdrVis = if (display.isHdrSdrRatioAvailable) { 71 display.registerHdrSdrRatioChangedListener({ it.run() }, hdrSdrListener) 72 View.VISIBLE 73 } else { 74 View.GONE 75 } 76 findViewById<Button>(R.id.mode_hdr)!!.visibility = hdrVis 77 findViewById<Button>(R.id.mode_hdr10)!!.visibility = hdrVis 78 } 79 80 override fun onDetachedFromWindow() { 81 super.onDetachedFromWindow() 82 display.unregisterHdrSdrRatioChangedListener(hdrSdrListener) 83 } 84 85 private fun setColorMode(newMode: Int) { 86 window!!.colorMode = newMode 87 updateModeInfoDisplay() 88 } 89 90 override fun setWindow(window: Window) { 91 this.window = window 92 } 93 94 private fun updateModeInfoDisplay() { 95 val sdrHdrRatio = display?.hdrSdrRatio ?: 1.0f 96 val colormode = window!!.colorMode 97 currentModeDisplay?.run { 98 text = "Current Mode: " + when (colormode) { 99 ActivityInfo.COLOR_MODE_DEFAULT -> "Default/SRGB" 100 ActivityInfo.COLOR_MODE_WIDE_COLOR_GAMUT -> "Wide Gamut" 101 ActivityInfo.COLOR_MODE_HDR -> "HDR (sdr/hdr ratio $sdrHdrRatio)" 102 ActivityInfo.COLOR_MODE_HDR10 -> "HDR10 (sdr/hdr ratio $sdrHdrRatio)" 103 else -> "Unknown" 104 } 105 } 106 } 107 } 108