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 android.service.controls.templates;
18 
19 import android.annotation.NonNull;
20 import android.annotation.Nullable;
21 import android.os.Bundle;
22 import android.service.controls.Control;
23 import android.service.controls.actions.FloatAction;
24 
25 /**
26  * A template for a {@link Control} with inputs in a "continuous" range of values.
27  *
28  * @see FloatAction
29  */
30 public final class RangeTemplate extends ControlTemplate {
31 
32     private static final @TemplateType int TYPE = TYPE_RANGE;
33     private static final String KEY_MIN_VALUE = "key_min_value";
34     private static final String KEY_MAX_VALUE = "key_max_value";
35     private static final String KEY_CURRENT_VALUE = "key_current_value";
36     private static final String KEY_STEP_VALUE = "key_step_value";
37     private static final String KEY_FORMAT_STRING = "key_format_string";
38 
39     private final float mMinValue;
40     private final float mMaxValue;
41     private final float mCurrentValue;
42     private final float mStepValue;
43     private final @NonNull CharSequence mFormatString;
44 
45     /**
46      * Construct a new {@link RangeTemplate}.
47      *
48      * The range must be valid, meaning:
49      * <ul>
50      *     <li> {@code minValue} < {@code maxValue}
51      *     <li> {@code minValue} < {@code currentValue}
52      *     <li> {@code currentValue} < {@code maxValue}
53      *     <li> 0 < {@code stepValue}
54      * </ul>
55      * <p>
56      * The current value of the Control will be formatted accordingly.
57      *
58      * @param templateId the identifier for this template object
59      * @param minValue minimum value for the input
60      * @param maxValue maximum value for the input
61      * @param currentValue the current value of the {@link Control} containing this object.
62      * @param stepValue minimum value of increments/decrements when interacting with this control.
63      * @param formatString a formatting string as per {@link String#format} used to display the
64      *                    {@code currentValue}. If {@code null} is passed, the "%.1f" is used.
65      * @throws IllegalArgumentException if the parameters passed do not make a valid range.
66      */
RangeTemplate(@onNull String templateId, float minValue, float maxValue, float currentValue, float stepValue, @Nullable CharSequence formatString)67     public RangeTemplate(@NonNull String templateId,
68             float minValue,
69             float maxValue,
70             float currentValue,
71             float stepValue,
72             @Nullable CharSequence formatString) {
73         super(templateId);
74         mMinValue = minValue;
75         mMaxValue = maxValue;
76         mCurrentValue = currentValue;
77         mStepValue = stepValue;
78         if (formatString != null) {
79             mFormatString = formatString;
80         } else {
81             mFormatString = "%.1f";
82         }
83         validate();
84     }
85 
86     /**
87      * Construct a new {@link RangeTemplate} from a {@link Bundle}.
88      *
89      * @throws IllegalArgumentException if the parameters passed do not make a valid range
90      * @see RangeTemplate#RangeTemplate(String, float, float, float, float, CharSequence)
91      * @hide
92      */
RangeTemplate(Bundle b)93     RangeTemplate(Bundle b) {
94         super(b);
95         mMinValue = b.getFloat(KEY_MIN_VALUE);
96         mMaxValue = b.getFloat(KEY_MAX_VALUE);
97         mCurrentValue = b.getFloat(KEY_CURRENT_VALUE);
98         mStepValue = b.getFloat(KEY_STEP_VALUE);
99         mFormatString = b.getCharSequence(KEY_FORMAT_STRING, "%.1f");
100         validate();
101     }
102 
103     /**
104      * The minimum value for this range.
105      */
getMinValue()106     public float getMinValue() {
107         return mMinValue;
108     }
109 
110     /**
111      * The maximum value for this range.
112      */
getMaxValue()113     public float getMaxValue() {
114         return mMaxValue;
115     }
116 
117     /**
118      * The current value for this range.
119      */
getCurrentValue()120     public float getCurrentValue() {
121         return mCurrentValue;
122     }
123 
124     /**
125      * The value of the smallest increment or decrement that can be performed on this range.
126      */
getStepValue()127     public float getStepValue() {
128         return mStepValue;
129     }
130 
131     /**
132      * Formatter for generating a user visible {@link String} representing the value
133      *         returned by {@link RangeTemplate#getCurrentValue}.
134      * @return a formatting string as specified in {@link String#format}
135      */
136     @NonNull
getFormatString()137     public CharSequence getFormatString() {
138         return mFormatString;
139     }
140 
141     /**
142      * @return {@link ControlTemplate#TYPE_RANGE}
143      */
144     @Override
getTemplateType()145     public int getTemplateType() {
146         return TYPE;
147     }
148 
149     /**
150      * @return
151      * @hide
152      */
153     @Override
154     @NonNull
getDataBundle()155     Bundle getDataBundle() {
156         Bundle b = super.getDataBundle();
157         b.putFloat(KEY_MIN_VALUE, mMinValue);
158         b.putFloat(KEY_MAX_VALUE, mMaxValue);
159         b.putFloat(KEY_CURRENT_VALUE, mCurrentValue);
160         b.putFloat(KEY_STEP_VALUE, mStepValue);
161         b.putCharSequence(KEY_FORMAT_STRING, mFormatString);
162         return b;
163     }
164 
165     /**
166      * Validate constructor parameters
167      *
168      * @throws IllegalArgumentException if the parameters passed do not make a valid range
169      */
validate()170     private void validate() {
171         if (Float.compare(mMinValue, mMaxValue) > 0) {
172             throw new IllegalArgumentException(
173                     String.format("minValue=%f > maxValue=%f", mMinValue, mMaxValue));
174         }
175         if (Float.compare(mMinValue, mCurrentValue) > 0) {
176             throw new IllegalArgumentException(
177                     String.format("minValue=%f > currentValue=%f", mMinValue, mCurrentValue));
178         }
179         if (Float.compare(mCurrentValue, mMaxValue) > 0) {
180             throw new IllegalArgumentException(
181                     String.format("currentValue=%f > maxValue=%f", mCurrentValue, mMaxValue));
182         }
183         if (mStepValue <= 0) {
184             throw new IllegalArgumentException(String.format("stepValue=%f <= 0", mStepValue));
185         }
186     }
187 }
188