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.utils;
18 
19 import android.content.res.Resources;
20 import android.content.res.TypedArray;
21 import android.util.TypedValue;
22 
23 public class AmbientFilterFactory {
24     /**
25      * Creates a temporal filter which functions as a weighted moving average buffer for recent
26      * sensor values.
27      * @param tag
28      *      The tag used for dumping and logging.
29      * @param horizon
30      *      How long ambient value changes are kept and taken into consideration.
31      * @param intercept
32      *      Recent changes are prioritised by integrating their duration over y = x + intercept
33      *      (the higher it is, the less prioritised recent changes are).
34      *
35      * @return
36      *      An AmbientFiler.
37      *
38      * @throws IllegalArgumentException
39      *      - Horizon is not positive.
40      *      - Intercept not configured.
41      */
createAmbientFilter(String tag, int horizon, float intercept)42     public static AmbientFilter createAmbientFilter(String tag, int horizon, float intercept) {
43         if (!Float.isNaN(intercept)) {
44             return new AmbientFilter.WeightedMovingAverageAmbientFilter(tag, horizon, intercept);
45         }
46         throw new IllegalArgumentException("missing configurations: "
47                 + "expected config_displayWhiteBalanceBrightnessFilterIntercept");
48     }
49 
50     /**
51      * Helper to create a default BrightnessFilter which has configuration in the resource file.
52      * @param tag
53      *      The tag used for dumping and logging.
54      * @param resources
55      *      The resources used to configure the various components.
56      *
57      * @return
58      *      An AmbientFilter.
59      */
createBrightnessFilter(String tag, Resources resources)60     public static AmbientFilter createBrightnessFilter(String tag, Resources resources) {
61         final int horizon = resources.getInteger(
62                 com.android.internal.R.integer.config_displayWhiteBalanceBrightnessFilterHorizon);
63         final float intercept = getFloat(resources,
64                 com.android.internal.R.dimen.config_displayWhiteBalanceBrightnessFilterIntercept);
65 
66         return createAmbientFilter(tag, horizon, intercept);
67     }
68 
69     /**
70      * Helper to creates a default ColorTemperatureFilter which has configuration in the resource
71      * file.
72      * @param tag
73      *      The tag used for dumping and logging.
74      * @param resources
75      *      The resources used to configure the various components.
76      *
77      * @return
78      *      An AmbientFilter.
79      */
createColorTemperatureFilter(String tag, Resources resources)80     public static AmbientFilter createColorTemperatureFilter(String tag, Resources resources) {
81         final int horizon = resources.getInteger(
82                 com.android.internal.R.integer
83                 .config_displayWhiteBalanceColorTemperatureFilterHorizon);
84         final float intercept = getFloat(resources,
85                 com.android.internal.R.dimen
86                 .config_displayWhiteBalanceColorTemperatureFilterIntercept);
87 
88         return createAmbientFilter(tag, horizon, intercept);
89     }
90 
91     // Instantiation is disabled.
AmbientFilterFactory()92     private AmbientFilterFactory() { }
93 
getFloat(Resources resources, int id)94     private static float getFloat(Resources resources, int id) {
95         TypedValue value = new TypedValue();
96 
97         resources.getValue(id, value, true /* resolveRefs */);
98         if (value.type != TypedValue.TYPE_FLOAT) {
99             return Float.NaN;
100         }
101 
102         return value.getFloat();
103     }
104 }
105 
106