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.os.Bundle;
21 import android.service.controls.Control;
22 
23 import com.android.internal.util.Preconditions;
24 
25 /**
26  * A template for a {@link Control} supporting toggling and a range.
27  *
28  * @see ToggleTemplate
29  * @see RangeTemplate
30  */
31 public final class ToggleRangeTemplate extends ControlTemplate {
32 
33     private static final @TemplateType int TYPE = TYPE_TOGGLE_RANGE;
34     private static final String KEY_BUTTON = "key_button";
35     private static final String KEY_RANGE = "key_range";
36 
37     private @NonNull final ControlButton mControlButton;
38     private @NonNull final RangeTemplate mRangeTemplate;
39 
40     /**
41      * @param b
42      * @hide
43      */
ToggleRangeTemplate(@onNull Bundle b)44     ToggleRangeTemplate(@NonNull Bundle b) {
45         super(b);
46         mControlButton = b.getParcelable(KEY_BUTTON, android.service.controls.templates.ControlButton.class);
47         mRangeTemplate = new RangeTemplate(b.getBundle(KEY_RANGE));
48     }
49 
50     /**
51      * Constructs a new {@link ToggleRangeTemplate}.
52      * @param templateId the identifier for this template.
53      * @param button a {@link ControlButton} to use for the toggle interface
54      * @param range a {@link RangeTemplate} to use for the range interface
55      */
ToggleRangeTemplate(@onNull String templateId, @NonNull ControlButton button, @NonNull RangeTemplate range)56     public ToggleRangeTemplate(@NonNull String templateId,
57             @NonNull ControlButton button,
58             @NonNull RangeTemplate range) {
59         super(templateId);
60         Preconditions.checkNotNull(button);
61         Preconditions.checkNotNull(range);
62         mControlButton = button;
63         mRangeTemplate = range;
64     }
65 
66     /**
67      * Constructs a new {@link ToggleRangeTemplate}.
68      * @param templateId the identifier for this template.
69      * @param checked true if the toggle should be rendered as active.
70      * @param actionDescription action description for the button.
71      * @param range  {@link RangeTemplate} to use for the range interface
72      * @see ControlButton
73      */
ToggleRangeTemplate(@onNull String templateId, boolean checked, @NonNull CharSequence actionDescription, @NonNull RangeTemplate range)74     public ToggleRangeTemplate(@NonNull String templateId,
75             boolean checked,
76             @NonNull CharSequence actionDescription,
77             @NonNull RangeTemplate range) {
78         this(templateId,
79                 new ControlButton(checked, actionDescription),
80                 range);
81     }
82 
83     /**
84      * @return
85      * @hide
86      */
87     @Override
88     @NonNull
getDataBundle()89     Bundle getDataBundle() {
90         Bundle b = super.getDataBundle();
91         b.putParcelable(KEY_BUTTON, mControlButton);
92         b.putBundle(KEY_RANGE, mRangeTemplate.getDataBundle());
93         return b;
94     }
95 
96     @NonNull
getRange()97     public RangeTemplate getRange() {
98         return mRangeTemplate;
99     }
100 
isChecked()101     public boolean isChecked() {
102         return mControlButton.isChecked();
103     }
104 
105     @NonNull
getActionDescription()106     public CharSequence getActionDescription() {
107         return mControlButton.getActionDescription();
108     }
109 
110     /**
111      * @return {@link ControlTemplate#TYPE_TOGGLE_RANGE}
112      */
113     @Override
getTemplateType()114     public int getTemplateType() {
115         return TYPE;
116     }
117 }
118