1 /* 2 * Copyright (C) 2019 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.server.display.color; 18 19 import android.animation.ValueAnimator; 20 import android.content.Context; 21 import android.util.Slog; 22 import com.android.internal.annotations.GuardedBy; 23 24 import java.io.PrintWriter; 25 26 abstract class TintController { 27 28 /** 29 * The default transition time, in milliseconds, for color transforms to turn on/off. 30 */ 31 private static final long TRANSITION_DURATION = 3000L; 32 33 private final Object mLock = new Object(); 34 35 @GuardedBy("mLock") 36 private ValueAnimator mAnimator; 37 @GuardedBy("mLock") 38 private Boolean mIsActivated; 39 getAnimator()40 public ValueAnimator getAnimator() { 41 synchronized (mLock) { 42 return mAnimator; 43 } 44 } 45 setAnimator(ValueAnimator animator)46 public void setAnimator(ValueAnimator animator) { 47 synchronized (mLock) { 48 mAnimator = animator; 49 } 50 } 51 52 /** 53 * Cancel the animator if it's still running. 54 */ cancelAnimator()55 public void cancelAnimator() { 56 synchronized (mLock) { 57 if (mAnimator != null) { 58 mAnimator.cancel(); 59 } 60 } 61 } 62 63 /** 64 * End the animator if it's still running, jumping to the end state. 65 */ endAnimator()66 public void endAnimator() { 67 synchronized (mLock) { 68 if (mAnimator != null) { 69 mAnimator.end(); 70 mAnimator = null; 71 } 72 } 73 } 74 setActivated(Boolean isActivated)75 public void setActivated(Boolean isActivated) { 76 synchronized (mLock) { 77 mIsActivated = isActivated; 78 } 79 } 80 isActivated()81 public boolean isActivated() { 82 synchronized (mLock) { 83 return mIsActivated != null && mIsActivated; 84 } 85 } 86 isActivatedStateNotSet()87 public boolean isActivatedStateNotSet() { 88 synchronized (mLock) { 89 return mIsActivated == null; 90 } 91 } 92 getTransitionDurationMilliseconds()93 public long getTransitionDurationMilliseconds() { 94 return TRANSITION_DURATION; 95 } 96 getTransitionDurationMilliseconds(boolean direction)97 public long getTransitionDurationMilliseconds(boolean direction) { 98 return TRANSITION_DURATION; 99 } 100 101 /** 102 * Dump debug information. 103 */ dump(PrintWriter pw)104 public void dump(PrintWriter pw) { 105 } 106 107 /** 108 * Set up any constants needed for computing the matrix. 109 */ setUp(Context context, boolean needsLinear)110 public abstract void setUp(Context context, boolean needsLinear); 111 112 /** 113 * Sets the 4x4 matrix to apply. 114 */ setMatrix(int value)115 public abstract void setMatrix(int value); 116 117 /** 118 * Get the 4x4 matrix to apply. 119 */ getMatrix()120 public abstract float[] getMatrix(); 121 122 /** 123 * Get the color transform level to apply the matrix. 124 */ getLevel()125 public abstract int getLevel(); 126 127 /** 128 * Returns whether or not this transform type is available on this device. 129 */ isAvailable(Context context)130 public abstract boolean isAvailable(Context context); 131 132 /** 133 * Format a given matrix into a string. 134 * 135 * @param matrix the matrix to format 136 * @param columns number of columns in the matrix 137 */ matrixToString(float[] matrix, int columns)138 static String matrixToString(float[] matrix, int columns) { 139 if (matrix == null || columns <= 0) { 140 Slog.e(ColorDisplayService.TAG, "Invalid arguments when formatting matrix to string," 141 + " matrix is null: " + (matrix == null) 142 + " columns: " + columns); 143 return ""; 144 } 145 146 final StringBuilder sb = new StringBuilder(""); 147 for (int i = 0; i < matrix.length; i++) { 148 if (i % columns == 0) { 149 sb.append("\n "); 150 } 151 sb.append(String.format("%9.6f", matrix[i])); 152 } 153 return sb.toString(); 154 } 155 156 } 157