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
|
/*
* Copyright (C) 2017 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 android.widget;
import android.content.Context;
import android.os.LocaleList;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.util.MathUtils;
import android.view.View;
import android.view.accessibility.AccessibilityManager;
import com.android.internal.R;
/**
* View to show text input based time picker with hour and minute fields and an optional AM/PM
* spinner.
*
* @hide
*/
public class TextInputTimePickerView extends RelativeLayout {
public static final int HOURS = 0;
public static final int MINUTES = 1;
public static final int AMPM = 2;
private static final int AM = 0;
private static final int PM = 1;
private final EditText mHourEditText;
private final EditText mMinuteEditText;
private final TextView mInputSeparatorView;
private final Spinner mAmPmSpinner;
private final TextView mErrorLabel;
private final TextView mHourLabel;
private final TextView mMinuteLabel;
private boolean mIs24Hour;
private boolean mHourFormatStartsAtZero;
private OnValueTypedListener mListener;
private boolean mErrorShowing;
private boolean mTimeSet;
interface OnValueTypedListener {
void onValueChanged(int inputType, int newValue);
}
public TextInputTimePickerView(Context context) {
this(context, null);
}
public TextInputTimePickerView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public TextInputTimePickerView(Context context, AttributeSet attrs, int defStyle) {
this(context, attrs, defStyle, 0);
}
public TextInputTimePickerView(Context context, AttributeSet attrs, int defStyle,
int defStyleRes) {
super(context, attrs, defStyle, defStyleRes);
inflate(context, R.layout.time_picker_text_input_material, this);
mHourEditText = findViewById(R.id.input_hour);
mMinuteEditText = findViewById(R.id.input_minute);
mInputSeparatorView = findViewById(R.id.input_separator);
mErrorLabel = findViewById(R.id.label_error);
mHourLabel = findViewById(R.id.label_hour);
mMinuteLabel = findViewById(R.id.label_minute);
mHourEditText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {}
@Override
public void afterTextChanged(Editable editable) {
if (parseAndSetHourInternal(editable.toString()) && editable.length() > 1) {
AccessibilityManager am = (AccessibilityManager) context.getSystemService(
context.ACCESSIBILITY_SERVICE);
if (!am.isEnabled()) {
mMinuteEditText.requestFocus();
}
}
}
});
mMinuteEditText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {}
@Override
public void afterTextChanged(Editable editable) {
parseAndSetMinuteInternal(editable.toString());
}
});
mAmPmSpinner = findViewById(R.id.am_pm_spinner);
final String[] amPmStrings = TimePicker.getAmPmStrings(context);
ArrayAdapter<CharSequence> adapter =
new ArrayAdapter<CharSequence>(context, R.layout.simple_spinner_dropdown_item);
adapter.add(TimePickerClockDelegate.obtainVerbatim(amPmStrings[0]));
adapter.add(TimePickerClockDelegate.obtainVerbatim(amPmStrings[1]));
mAmPmSpinner.setAdapter(adapter);
mAmPmSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int position,
long id) {
if (position == 0) {
mListener.onValueChanged(AMPM, AM);
} else {
mListener.onValueChanged(AMPM, PM);
}
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {}
});
}
void setListener(OnValueTypedListener listener) {
mListener = listener;
}
void setHourFormat(int maxCharLength) {
mHourEditText.setFilters(new InputFilter[] {
new InputFilter.LengthFilter(maxCharLength)});
mMinuteEditText.setFilters(new InputFilter[] {
new InputFilter.LengthFilter(maxCharLength)});
final LocaleList locales = mContext.getResources().getConfiguration().getLocales();
mHourEditText.setImeHintLocales(locales);
mMinuteEditText.setImeHintLocales(locales);
}
boolean validateInput() {
final String hourText = TextUtils.isEmpty(mHourEditText.getText())
? mHourEditText.getHint().toString()
: mHourEditText.getText().toString();
final String minuteText = TextUtils.isEmpty(mMinuteEditText.getText())
? mMinuteEditText.getHint().toString()
: mMinuteEditText.getText().toString();
final boolean inputValid = parseAndSetHourInternal(hourText)
&& parseAndSetMinuteInternal(minuteText);
setError(!inputValid);
return inputValid;
}
void updateSeparator(String separatorText) {
mInputSeparatorView.setText(separatorText);
}
private void setError(boolean enabled) {
mErrorShowing = enabled;
mErrorLabel.setVisibility(enabled ? View.VISIBLE : View.INVISIBLE);
mHourLabel.setVisibility(enabled ? View.INVISIBLE : View.VISIBLE);
mMinuteLabel.setVisibility(enabled ? View.INVISIBLE : View.VISIBLE);
}
private void setTimeSet(boolean timeSet) {
mTimeSet = mTimeSet || timeSet;
}
private boolean isTimeSet() {
return mTimeSet;
}
/**
* Computes the display value and updates the text of the view.
* <p>
* This method should be called whenever the current value or display
* properties (leading zeroes, max digits) change.
*/
void updateTextInputValues(int localizedHour, int minute, int amOrPm, boolean is24Hour,
boolean hourFormatStartsAtZero) {
final String hourFormat = "%d";
final String minuteFormat = "%02d";
mIs24Hour = is24Hour;
mHourFormatStartsAtZero = hourFormatStartsAtZero;
mAmPmSpinner.setVisibility(is24Hour ? View.INVISIBLE : View.VISIBLE);
if (amOrPm == AM) {
mAmPmSpinner.setSelection(0);
} else {
mAmPmSpinner.setSelection(1);
}
if (isTimeSet()) {
mHourEditText.setText(String.format(hourFormat, localizedHour));
mMinuteEditText.setText(String.format(minuteFormat, minute));
} else {
mHourEditText.setHint(String.format(hourFormat, localizedHour));
mMinuteEditText.setHint(String.format(minuteFormat, minute));
}
if (mErrorShowing) {
validateInput();
}
}
private boolean parseAndSetHourInternal(String input) {
try {
final int hour = Integer.parseInt(input);
if (!isValidLocalizedHour(hour)) {
final int minHour = mHourFormatStartsAtZero ? 0 : 1;
final int maxHour = mIs24Hour ? 23 : 11 + minHour;
mListener.onValueChanged(HOURS, getHourOfDayFromLocalizedHour(
MathUtils.constrain(hour, minHour, maxHour)));
return false;
}
mListener.onValueChanged(HOURS, getHourOfDayFromLocalizedHour(hour));
setTimeSet(true);
return true;
} catch (NumberFormatException e) {
// Do nothing since we cannot parse the input.
return false;
}
}
private boolean parseAndSetMinuteInternal(String input) {
try {
final int minutes = Integer.parseInt(input);
if (minutes < 0 || minutes > 59) {
mListener.onValueChanged(MINUTES, MathUtils.constrain(minutes, 0, 59));
return false;
}
mListener.onValueChanged(MINUTES, minutes);
setTimeSet(true);
return true;
} catch (NumberFormatException e) {
// Do nothing since we cannot parse the input.
return false;
}
}
private boolean isValidLocalizedHour(int localizedHour) {
final int minHour = mHourFormatStartsAtZero ? 0 : 1;
final int maxHour = (mIs24Hour ? 23 : 11) + minHour;
return localizedHour >= minHour && localizedHour <= maxHour;
}
private int getHourOfDayFromLocalizedHour(int localizedHour) {
int hourOfDay = localizedHour;
if (mIs24Hour) {
if (!mHourFormatStartsAtZero && localizedHour == 24) {
hourOfDay = 0;
}
} else {
if (!mHourFormatStartsAtZero && localizedHour == 12) {
hourOfDay = 0;
}
if (mAmPmSpinner.getSelectedItemPosition() == 1) {
hourOfDay += 12;
}
}
return hourOfDay;
}
}
|