| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 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 |
|
| 27 |
|
| 28 |
|
| 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 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 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 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 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 |
|
| 105 |
|
| 106 |
public float getMinValue() { |
| 107 |
return mMinValue; |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
public float getMaxValue() { |
| 114 |
return mMaxValue; |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
public float getCurrentValue() { |
| 121 |
return mCurrentValue; |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
public float getStepValue() { |
| 128 |
return mStepValue; |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
@NonNull |
| 137 |
public CharSequence getFormatString() { |
| 138 |
return mFormatString; |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
@Override |
| 145 |
public int getTemplateType() { |
| 146 |
return TYPE; |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
@Override |
| 154 |
@NonNull |
| 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 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 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 |
} |