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