1 /*
2  * Copyright (C) 2021 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.car.qc;
18 
19 import android.app.PendingIntent;
20 import android.os.Parcel;
21 
22 import androidx.annotation.NonNull;
23 import androidx.annotation.Nullable;
24 
25 /**
26  * Quick Control Slider included in {@link QCRow}
27  */
28 public class QCSlider extends QCItem {
29     private int mMin = 0;
30     private int mMax = 100;
31     private int mValue = 0;
32     private PendingIntent mInputAction;
33     private PendingIntent mDisabledClickAction;
34 
QCSlider(int min, int max, int value, boolean enabled, boolean clickableWhileDisabled, @Nullable PendingIntent inputAction, @Nullable PendingIntent disabledClickAction)35     public QCSlider(int min, int max, int value, boolean enabled, boolean clickableWhileDisabled,
36             @Nullable PendingIntent inputAction, @Nullable PendingIntent disabledClickAction) {
37         super(QC_TYPE_SLIDER, enabled, clickableWhileDisabled);
38         mMin = min;
39         mMax = max;
40         mValue = value;
41         mInputAction = inputAction;
42         mDisabledClickAction = disabledClickAction;
43     }
44 
QCSlider(@onNull Parcel in)45     public QCSlider(@NonNull Parcel in) {
46         super(in);
47         mMin = in.readInt();
48         mMax = in.readInt();
49         mValue = in.readInt();
50         boolean hasAction = in.readBoolean();
51         if (hasAction) {
52             mInputAction = PendingIntent.CREATOR.createFromParcel(in);
53         }
54         boolean hasDisabledClickAction = in.readBoolean();
55         if (hasDisabledClickAction) {
56             mDisabledClickAction = PendingIntent.CREATOR.createFromParcel(in);
57         }
58     }
59 
60     @Override
writeToParcel(Parcel dest, int flags)61     public void writeToParcel(Parcel dest, int flags) {
62         super.writeToParcel(dest, flags);
63         dest.writeInt(mMin);
64         dest.writeInt(mMax);
65         dest.writeInt(mValue);
66         boolean hasAction = mInputAction != null;
67         dest.writeBoolean(hasAction);
68         if (hasAction) {
69             mInputAction.writeToParcel(dest, flags);
70         }
71         boolean hasDisabledClickAction = mDisabledClickAction != null;
72         dest.writeBoolean(hasDisabledClickAction);
73         if (hasDisabledClickAction) {
74             mDisabledClickAction.writeToParcel(dest, flags);
75         }
76     }
77 
78     @Override
getPrimaryAction()79     public PendingIntent getPrimaryAction() {
80         return mInputAction;
81     }
82 
83     @Override
getDisabledClickAction()84     public PendingIntent getDisabledClickAction() {
85         return mDisabledClickAction;
86     }
87 
getMin()88     public int getMin() {
89         return mMin;
90     }
91 
getMax()92     public int getMax() {
93         return mMax;
94     }
95 
getValue()96     public int getValue() {
97         return mValue;
98     }
99 
100     public static Creator<QCSlider> CREATOR = new Creator<QCSlider>() {
101         @Override
102         public QCSlider createFromParcel(Parcel source) {
103             return new QCSlider(source);
104         }
105 
106         @Override
107         public QCSlider[] newArray(int size) {
108             return new QCSlider[size];
109         }
110     };
111 
112     /**
113      * Builder for {@link QCSlider}.
114      */
115     public static class Builder {
116         private int mMin = 0;
117         private int mMax = 100;
118         private int mValue = 0;
119         private boolean mIsEnabled = true;
120         private boolean mIsClickableWhileDisabled = false;
121         private PendingIntent mInputAction;
122         private PendingIntent mDisabledClickAction;
123 
124         /**
125          * Set the minimum allowed value for the slider input.
126          */
setMin(int min)127         public Builder setMin(int min) {
128             mMin = min;
129             return this;
130         }
131 
132         /**
133          * Set the maximum allowed value for the slider input.
134          */
setMax(int max)135         public Builder setMax(int max) {
136             mMax = max;
137             return this;
138         }
139 
140         /**
141          * Set the current value for the slider input.
142          */
setValue(int value)143         public Builder setValue(int value) {
144             mValue = value;
145             return this;
146         }
147 
148         /**
149          * Sets whether or not the slider is enabled.
150          */
setEnabled(boolean enabled)151         public Builder setEnabled(boolean enabled) {
152             mIsEnabled = enabled;
153             return this;
154         }
155 
156         /**
157          * Sets whether or not a slider should be clickable while disabled.
158          */
setClickableWhileDisabled(boolean clickable)159         public Builder setClickableWhileDisabled(boolean clickable) {
160             mIsClickableWhileDisabled = clickable;
161             return this;
162         }
163 
164 
165         /**
166          * Set the PendingIntent to be sent when the slider value is changed.
167          */
setInputAction(@ullable PendingIntent inputAction)168         public Builder setInputAction(@Nullable PendingIntent inputAction) {
169             mInputAction = inputAction;
170             return this;
171         }
172 
173         /**
174          * Sets the PendingIntent to be sent when the action item is clicked while disabled.
175          */
setDisabledClickAction(@ullable PendingIntent action)176         public Builder setDisabledClickAction(@Nullable PendingIntent action) {
177             mDisabledClickAction = action;
178             return this;
179         }
180 
181         /**
182          * Builds the final {@link QCSlider}.
183          */
build()184         public QCSlider build() {
185             return new QCSlider(mMin, mMax, mValue, mIsEnabled, mIsClickableWhileDisabled,
186                     mInputAction, mDisabledClickAction);
187         }
188     }
189 }
190