-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Expand file tree
/
Copy pathRangeDateSelector.java
More file actions
384 lines (339 loc) · 13.1 KB
/
RangeDateSelector.java
File metadata and controls
384 lines (339 loc) · 13.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/*
* Copyright 2019 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.android.material.datepicker;
import com.google.android.material.R;
import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;
import android.text.InputType;
import android.text.SpannableString;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RestrictTo;
import androidx.annotation.RestrictTo.Scope;
import androidx.core.util.Pair;
import androidx.core.util.Preconditions;
import com.google.android.material.color.MaterialColors;
import com.google.android.material.internal.ManufacturerUtils;
import com.google.android.material.resources.MaterialAttributes;
import com.google.android.material.textfield.TextInputLayout;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
/**
* A {@link DateSelector} that uses a {@link Pair} of {@link Long} objects to represent a selected
* range.
*
* @hide
*/
@RestrictTo(Scope.LIBRARY_GROUP)
public class RangeDateSelector implements DateSelector<Pair<Long, Long>> {
@Nullable private CharSequence error;
private String invalidRangeStartError;
// "" is not considered an error
private final String invalidRangeEndError = " ";
@Nullable private Long selectedStartItem = null;
@Nullable private Long selectedEndItem = null;
@Nullable private Long proposedTextStart = null;
@Nullable private Long proposedTextEnd = null;
@Nullable private SimpleDateFormat textInputFormat;
@Override
public void select(long selection) {
if (selectedStartItem == null) {
selectedStartItem = selection;
} else if (selectedEndItem == null && isValidRange(selectedStartItem, selection)) {
selectedEndItem = selection;
} else {
selectedEndItem = null;
selectedStartItem = selection;
}
}
@Override
public boolean isSelectionComplete() {
return selectedStartItem != null
&& selectedEndItem != null
&& isValidRange(selectedStartItem, selectedEndItem);
}
@Override
public void clearSelection() {
selectedStartItem = null;
selectedEndItem = null;
}
@Override
public void setSelection(@NonNull Pair<Long, Long> selection) {
if (selection.first != null && selection.second != null) {
Preconditions.checkArgument(isValidRange(selection.first, selection.second));
}
selectedStartItem =
selection.first == null ? null : UtcDates.canonicalYearMonthDay(selection.first);
selectedEndItem =
selection.second == null ? null : UtcDates.canonicalYearMonthDay(selection.second);
}
@Override
@NonNull
public Pair<Long, Long> getSelection() {
return new Pair<>(selectedStartItem, selectedEndItem);
}
@NonNull
@Override
public Collection<Pair<Long, Long>> getSelectedRanges() {
ArrayList<Pair<Long, Long>> ranges = new ArrayList<>();
Pair<Long, Long> range = new Pair<>(selectedStartItem, selectedEndItem);
ranges.add(range);
return ranges;
}
@NonNull
@Override
public Collection<Long> getSelectedDays() {
ArrayList<Long> selections = new ArrayList<>();
if (selectedStartItem != null) {
selections.add(selectedStartItem);
}
if (selectedEndItem != null) {
selections.add(selectedEndItem);
}
return selections;
}
@Override
public int getDefaultThemeResId(@NonNull Context context) {
Resources res = context.getResources();
DisplayMetrics display = res.getDisplayMetrics();
int maximumDefaultFullscreenMinorAxis =
res.getDimensionPixelSize(R.dimen.mtrl_calendar_maximum_default_fullscreen_minor_axis);
int minorAxisPx = Math.min(display.widthPixels, display.heightPixels);
int defaultThemeAttr =
minorAxisPx > maximumDefaultFullscreenMinorAxis
? R.attr.materialCalendarTheme
: R.attr.materialCalendarFullscreenTheme;
return MaterialAttributes.resolveOrThrow(
context, defaultThemeAttr, MaterialDatePicker.class.getCanonicalName());
}
@NonNull
@Override
public String getSelectionDisplayString(@NonNull Context context) {
Resources res = context.getResources();
if (selectedStartItem == null && selectedEndItem == null) {
return res.getString(R.string.mtrl_picker_range_header_unselected);
}
if (selectedEndItem == null) {
return res.getString(
R.string.mtrl_picker_range_header_only_start_selected,
DateStrings.getDateString(selectedStartItem));
}
if (selectedStartItem == null) {
return res.getString(
R.string.mtrl_picker_range_header_only_end_selected,
DateStrings.getDateString(selectedEndItem));
}
Pair<String, String> dateRangeStrings =
DateStrings.getDateRangeString(selectedStartItem, selectedEndItem);
return res.getString(
R.string.mtrl_picker_range_header_selected,
dateRangeStrings.first,
dateRangeStrings.second);
}
@NonNull
@Override
public String getSelectionContentDescription(@NonNull Context context) {
Resources res = context.getResources();
Pair<String, String> dateRangeStrings =
DateStrings.getDateRangeString(selectedStartItem, selectedEndItem);
String startPlaceholder =
dateRangeStrings.first == null
? res.getString(R.string.mtrl_picker_announce_current_selection_none)
: dateRangeStrings.first;
String endPlaceholder =
dateRangeStrings.second == null
? res.getString(R.string.mtrl_picker_announce_current_selection_none)
: dateRangeStrings.second;
return res.getString(
R.string.mtrl_picker_announce_current_range_selection, startPlaceholder, endPlaceholder);
}
@Nullable
@Override
public String getError() {
return TextUtils.isEmpty(error) ? null : error.toString();
}
@Override
public int getDefaultTitleResId() {
return R.string.mtrl_picker_range_header_title;
}
@Override
public void setTextInputFormat(@Nullable SimpleDateFormat format) {
if (format != null) {
format = (SimpleDateFormat) UtcDates.getNormalizedFormat(format);
}
this.textInputFormat = format;
}
@Override
public View onCreateTextInputView(
@NonNull LayoutInflater layoutInflater,
@Nullable ViewGroup viewGroup,
@Nullable Bundle bundle,
CalendarConstraints constraints,
final @NonNull OnSelectionChangedListener<Pair<Long, Long>> listener) {
View root =
layoutInflater.inflate(R.layout.mtrl_picker_text_input_date_range, viewGroup, false);
final TextInputLayout startTextInput =
root.findViewById(R.id.mtrl_picker_text_input_range_start);
final TextInputLayout endTextInput = root.findViewById(R.id.mtrl_picker_text_input_range_end);
EditText startEditText = startTextInput.getEditText();
EditText endEditText = endTextInput.getEditText();
Integer hintTextColor =
MaterialColors.getColorOrNull(root.getContext(), R.attr.colorOnSurfaceVariant);
if (hintTextColor != null) {
startEditText.setHintTextColor(hintTextColor);
endEditText.setHintTextColor(hintTextColor);
}
if (ManufacturerUtils.isDateInputKeyboardMissingSeparatorCharacters()) {
// Using the URI variation places the '/' and '.' in more prominent positions
startEditText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_URI);
endEditText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_URI);
}
invalidRangeStartError = root.getResources().getString(R.string.mtrl_picker_invalid_range);
boolean hasCustomFormat = textInputFormat != null;
SimpleDateFormat format =
hasCustomFormat ? textInputFormat : UtcDates.getDefaultTextInputFormat();
if (selectedStartItem != null) {
startEditText.setText(format.format(selectedStartItem));
proposedTextStart = selectedStartItem;
// Move the cursor to the end of the text field
CharSequence text = startEditText.getText();
if (text != null) {
startEditText.setSelection(text.length());
}
}
if (selectedEndItem != null) {
endEditText.setText(format.format(selectedEndItem));
proposedTextEnd = selectedEndItem;
}
String formatHint =
hasCustomFormat
? format.toPattern()
: UtcDates.getDefaultTextInputHint(root.getResources(), format);
SpannableString verbatimHint = UtcDates.getVerbatimTextInputHint(formatHint);
startTextInput.setPlaceholderText(verbatimHint);
endTextInput.setPlaceholderText(verbatimHint);
startEditText.addTextChangedListener(
new DateFormatTextWatcher(formatHint, format, startTextInput, constraints) {
@Override
void onValidDate(@Nullable Long day) {
proposedTextStart = day;
updateIfValidTextProposal(startTextInput, endTextInput, listener);
}
@Override
void onInvalidDate() {
proposedTextStart = null;
updateIfValidTextProposal(startTextInput, endTextInput, listener);
}
});
endEditText.addTextChangedListener(
new DateFormatTextWatcher(formatHint, format, endTextInput, constraints) {
@Override
void onValidDate(@Nullable Long day) {
proposedTextEnd = day;
updateIfValidTextProposal(startTextInput, endTextInput, listener);
}
@Override
void onInvalidDate() {
proposedTextEnd = null;
updateIfValidTextProposal(startTextInput, endTextInput, listener);
}
});
// only show keyboard if touch exploration is disabled
if (!DateSelector.isTouchExplorationEnabled(root.getContext())) {
DateSelector.showKeyboardWithAutoHideBehavior(startEditText, endEditText);
}
return root;
}
private boolean isValidRange(long start, long end) {
return start <= end;
}
private void updateIfValidTextProposal(
@NonNull TextInputLayout startTextInput,
@NonNull TextInputLayout endTextInput,
@NonNull OnSelectionChangedListener<Pair<Long, Long>> listener) {
if (proposedTextStart == null || proposedTextEnd == null) {
clearInvalidRange(startTextInput, endTextInput);
listener.onIncompleteSelectionChanged();
} else if (isValidRange(proposedTextStart, proposedTextEnd)) {
selectedStartItem = proposedTextStart;
selectedEndItem = proposedTextEnd;
listener.onSelectionChanged(getSelection());
} else {
setInvalidRange(startTextInput, endTextInput);
listener.onIncompleteSelectionChanged();
}
updateError(startTextInput, endTextInput);
}
private void updateError(@NonNull TextInputLayout start, @NonNull TextInputLayout end) {
if (!TextUtils.isEmpty(start.getError())) {
error = start.getError();
} else if (!TextUtils.isEmpty(end.getError())) {
error = end.getError();
} else {
error = null;
}
}
private void clearInvalidRange(@NonNull TextInputLayout start, @NonNull TextInputLayout end) {
if (start.getError() != null && invalidRangeStartError.contentEquals(start.getError())) {
start.setError(null);
}
if (end.getError() != null && invalidRangeEndError.contentEquals(end.getError())) {
end.setError(null);
}
}
private void setInvalidRange(@NonNull TextInputLayout start, @NonNull TextInputLayout end) {
start.setError(invalidRangeStartError);
end.setError(invalidRangeEndError);
}
/* Parcelable interface */
/** {@link Parcelable.Creator} */
public static final Parcelable.Creator<RangeDateSelector> CREATOR =
new Parcelable.Creator<RangeDateSelector>() {
@NonNull
@Override
public RangeDateSelector createFromParcel(@NonNull Parcel source) {
RangeDateSelector rangeDateSelector = new RangeDateSelector();
rangeDateSelector.selectedStartItem =
(Long) source.readValue(Long.class.getClassLoader());
rangeDateSelector.selectedEndItem = (Long) source.readValue(Long.class.getClassLoader());
return rangeDateSelector;
}
@NonNull
@Override
public RangeDateSelector[] newArray(int size) {
return new RangeDateSelector[size];
}
};
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(@NonNull Parcel dest, int flags) {
dest.writeValue(selectedStartItem);
dest.writeValue(selectedEndItem);
}
}