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 
getTransitionDurationMilliseconds(boolean direction)78     public long getTransitionDurationMilliseconds(boolean direction) {
79         return TRANSITION_DURATION;
80     }
81 
82     /**
83      * Dump debug information.
84      */
dump(PrintWriter pw)85     public void dump(PrintWriter pw) {
86     }
87 
88     /**
89      * Set up any constants needed for computing the matrix.
90      */
setUp(Context context, boolean needsLinear)91     public abstract void setUp(Context context, boolean needsLinear);
92 
93     /**
94      * Sets the 4x4 matrix to apply.
95      */
setMatrix(int value)96     public abstract void setMatrix(int value);
97 
98     /**
99      * Get the 4x4 matrix to apply.
100      */
getMatrix()101     public abstract float[] getMatrix();
102 
103     /**
104      * Get the color transform level to apply the matrix.
105      */
getLevel()106     public abstract int getLevel();
107 
108     /**
109      * Returns whether or not this transform type is available on this device.
110      */
isAvailable(Context context)111     public abstract boolean isAvailable(Context context);
112 
113     /**
114      * Format a given matrix into a string.
115      *
116      * @param matrix the matrix to format
117      * @param columns number of columns in the matrix
118      */
matrixToString(float[] matrix, int columns)119     static String matrixToString(float[] matrix, int columns) {
120         if (matrix == null || columns <= 0) {
121             Slog.e(ColorDisplayService.TAG, "Invalid arguments when formatting matrix to string,"
122                     + " matrix is null: " + (matrix == null)
123                     + " columns: " + columns);
124             return "";
125         }
126 
127         final StringBuilder sb = new StringBuilder("");
128         for (int i = 0; i < matrix.length; i++) {
129             if (i % columns == 0) {
130                 sb.append("\n      ");
131             }
132             sb.append(String.format("%9.6f", matrix[i]));
133         }
134         return sb.toString();
135     }
136 
137 }
138