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.actions;
18 
19 import android.annotation.NonNull;
20 import android.annotation.Nullable;
21 import android.os.Bundle;
22 import android.service.controls.templates.RangeTemplate;
23 import android.service.controls.templates.ToggleRangeTemplate;
24 
25 /**
26  * Action sent by a {@link RangeTemplate}, {@link ToggleRangeTemplate}.
27  */
28 public final class FloatAction extends ControlAction {
29 
30     private static final @ActionType int TYPE = TYPE_FLOAT;
31     private static final String KEY_NEW_VALUE = "key_new_value";
32 
33     private final float mNewValue;
34 
35     /**
36      * @param templateId the identifier of the {@link RangeTemplate} that produced this action.
37      * @param newValue new value for the state displayed by the {@link RangeTemplate}.
38      */
FloatAction(@onNull String templateId, float newValue)39     public FloatAction(@NonNull String templateId, float newValue) {
40         this(templateId, newValue, null);
41     }
42 
43     /**
44      * @param templateId the identifier of the {@link RangeTemplate} that originated this action.
45      * @param newValue new value for the state of the {@link RangeTemplate}.
46      * @param challengeValue a value sent by the user along with the action to authenticate. {@code}
47      *                       null is sent when no authentication is needed or has not been
48      *                       requested.
49      */
50 
FloatAction(@onNull String templateId, float newValue, @Nullable String challengeValue)51     public FloatAction(@NonNull String templateId, float newValue,
52             @Nullable String challengeValue) {
53         super(templateId, challengeValue);
54         mNewValue = newValue;
55     }
56 
57     /**
58      * @param b
59      * @hide
60      */
FloatAction(Bundle b)61     FloatAction(Bundle b) {
62         super(b);
63         mNewValue = b.getFloat(KEY_NEW_VALUE);
64     }
65 
66     /**
67      * The new value set for the range in the corresponding {@link RangeTemplate}.
68      */
getNewValue()69     public float getNewValue() {
70         return mNewValue;
71     }
72 
73     /**
74      * @return {@link ControlAction#TYPE_FLOAT}
75      */
76     @Override
getActionType()77     public int getActionType() {
78         return TYPE;
79     }
80 
81     /**
82      * @return
83      * @hide
84      */
85     @Override
86     @NonNull
getDataBundle()87     Bundle getDataBundle() {
88         Bundle b = super.getDataBundle();
89         b.putFloat(KEY_NEW_VALUE, mNewValue);
90         return b;
91     }
92 }
93