| 1 |
/* |
| 2 |
* Copyright (C) 2015 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 |
package android.databinding.adapters; |
| 17 |
|
| 18 |
import android.databinding.BindingAdapter; |
| 19 |
import android.databinding.InverseBindingAdapter; |
| 20 |
import android.databinding.InverseBindingListener; |
| 21 |
import android.os.Build.VERSION; |
| 22 |
import android.os.Build.VERSION_CODES; |
| 23 |
import android.widget.TimePicker; |
| 24 |
import android.widget.TimePicker.OnTimeChangedListener; |
| 25 |
|
| 26 |
public class TimePickerBindingAdapter { |
| 27 |
|
| 28 |
@SuppressWarnings("deprecation") |
| 29 |
@BindingAdapter("android:hour") |
| 30 |
public static void setHour(TimePicker view, int hour) { |
| 31 |
if (VERSION.SDK_INT >= VERSION_CODES.M) { |
| 32 |
if (view.getHour() != hour) { |
| 33 |
view.setHour(hour); |
| 34 |
} |
| 35 |
} else { |
| 36 |
if (view.getCurrentHour() != hour) { |
| 37 |
view.setCurrentHour(hour); |
| 38 |
} |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
@SuppressWarnings("deprecation") |
| 43 |
@BindingAdapter("android:minute") |
| 44 |
public static void setMinute(TimePicker view, int minute) { |
| 45 |
if (VERSION.SDK_INT >= VERSION_CODES.M) { |
| 46 |
if (view.getMinute() != minute) { |
| 47 |
view.setMinute(minute); |
| 48 |
} |
| 49 |
} else { |
| 50 |
if (view.getCurrentMinute() != minute) { |
| 51 |
view.setCurrentHour(minute); |
| 52 |
} |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
@InverseBindingAdapter(attribute = "android:hour") |
| 57 |
public static int getHour(TimePicker view) { |
| 58 |
if (VERSION.SDK_INT >= VERSION_CODES.M) { |
| 59 |
return view.getHour(); |
| 60 |
} else { |
| 61 |
@SuppressWarnings("deprecation") |
| 62 |
Integer hour = view.getCurrentHour(); |
| 63 |
if (hour == null) { |
| 64 |
return 0; |
| 65 |
} else { |
| 66 |
return hour; |
| 67 |
} |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
@InverseBindingAdapter(attribute = "android:minute") |
| 72 |
public static int getMinute(TimePicker view) { |
| 73 |
if (VERSION.SDK_INT >= VERSION_CODES.M) { |
| 74 |
return view.getMinute(); |
| 75 |
} else { |
| 76 |
@SuppressWarnings("deprecation") |
| 77 |
Integer minute = view.getCurrentMinute(); |
| 78 |
if (minute == null) { |
| 79 |
return 0; |
| 80 |
} else { |
| 81 |
return minute; |
| 82 |
} |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
@BindingAdapter(value = {"android:onTimeChanged", "android:hourAttrChanged", |
| 87 |
"android:minuteAttrChanged"}, requireAll = false) |
| 88 |
public static void setListeners(TimePicker view, final OnTimeChangedListener listener, |
| 89 |
final InverseBindingListener hourChange, final InverseBindingListener minuteChange) { |
| 90 |
if (hourChange == null && minuteChange == null) { |
| 91 |
view.setOnTimeChangedListener(listener); |
| 92 |
} else { |
| 93 |
view.setOnTimeChangedListener(new OnTimeChangedListener() { |
| 94 |
@Override |
| 95 |
public void onTimeChanged(TimePicker view, int hourOfDay, int minute) { |
| 96 |
if (listener != null) { |
| 97 |
listener.onTimeChanged(view, hourOfDay, minute); |
| 98 |
} |
| 99 |
if (hourChange != null) { |
| 100 |
hourChange.onChange(); |
| 101 |
} |
| 102 |
if (minuteChange != null) { |
| 103 |
minuteChange.onChange(); |
| 104 |
} |
| 105 |
} |
| 106 |
}); |
| 107 |
} |
| 108 |
} |
| 109 |
} |