AI2 Component  (Version nb184)
TimePicker.java
Go to the documentation of this file.
1 // -*- mode: java; c-basic-offset: 2; -*-
2 // Copyright 2009-2011 Google, All Rights reserved
3 // Copyright 2011-2014 MIT, All rights reserved
4 // Released under the Apache License, Version 2.0
5 // http://www.apache.org/licenses/LICENSE-2.0
6 
7 package com.google.appinventor.components.runtime;
8 
19 
20 import android.app.Dialog;
21 import android.app.TimePickerDialog;
22 import android.text.format.DateFormat;
23 import android.os.Handler;
24 
25 import java.util.Calendar;
26 
27 
41 @DesignerComponent(version = YaVersion.TIMEPICKER_COMPONENT_VERSION,
42  category = ComponentCategory.USERINTERFACE,
43  description = "<p>A button that, when clicked on, launches a popup" +
44  " dialog to allow the user to select a time.</p>")
45 @SimpleObject
46 public class TimePicker extends ButtonBase {
47 
48  private int hour = 0;
49  private int minute = 0;
50  private TimePickerDialog time;
51  private boolean customTime = false;
52  private Form form;
53  private Calendar instant;
54  private Handler androidUIHandler;
55 
62  super(container);
63  form = container.$form();
64  final Calendar c = Calendar.getInstance();
65  hour = c.get(Calendar.HOUR_OF_DAY);
66  minute = c.get(Calendar.MINUTE);
67  time = new TimePickerDialog(this.container.$context(),
68  timePickerListener, hour, minute, DateFormat.is24HourFormat(this.container.$context()));
69 
70  instant = Dates.TimeInstant(hour, minute);
71  androidUIHandler = new Handler();
72 
73  }
74 
75 
83  description = "The hour of the last time set using the time picker." +
84  " The hour is in a 24 hour format. If the last time set was 11:53 pm" +
85  ", this property will return 23.",
86  category = PropertyCategory.APPEARANCE)
87  public int Hour() {
88  return hour;
89  }
90 
98  description = "The minute of the last time set using the time picker",
99  category = PropertyCategory.APPEARANCE)
100  public int Minute() {
101  return minute;
102  }
103 
109  description = "The instant of the last time set using the time picker",
110  category = PropertyCategory.APPEARANCE)
111  public Calendar Instant() {
112  return instant;
113  }
114 
121  @SimpleFunction(description="Set the time to be shown in the Time Picker popup. Current time is shown by default.")
122  public void SetTimeToDisplay(int hour, int minute) {
123  if ((hour < 0) || (hour > 23)) {
124  form.dispatchErrorOccurredEvent(this, "SetTimeToDisplay", ErrorMessages.ERROR_ILLEGAL_HOUR);
125  } else if ((minute < 0) || (minute > 59)) {
126  form.dispatchErrorOccurredEvent(this, "SetTimeToDisplay", ErrorMessages.ERROR_ILLEGAL_MINUTE);
127  } else {
128  time.updateTime(hour, minute);
129  instant = Dates.TimeInstant(hour, minute);
130  customTime = true;
131  }
132  }
133 
140  @SimpleFunction(description="Set the time from the instant to be shown in the Time Picker dialog. " +
141  "Current time is shown by default.")
142  public void SetTimeToDisplayFromInstant(Calendar instant) {
143  int hour = Dates.Hour(instant);
144  int minute = Dates.Minute(instant);
145  time.updateTime(hour, minute);
146  instant = Dates.TimeInstant(hour, minute);
147  customTime = true;
148  }
149 
153  @SimpleFunction(description="Launches the TimePicker dialog.")
154  public void LaunchPicker(){
155  click();
156  }
157 
158  @Override
159  public void click() {
160  if (!customTime) {
161  Calendar c = Calendar.getInstance();
162  int h = c.get(Calendar.HOUR_OF_DAY);
163  int m = c.get(Calendar.MINUTE);
164  time.updateTime(h, m);
165  instant = Dates.TimeInstant(hour, minute);
166  } else {
167  customTime = false;
168  }
169  time.show();
170  }
171 
172  private TimePickerDialog.OnTimeSetListener timePickerListener =
173  new TimePickerDialog.OnTimeSetListener() {
174  public void onTimeSet(android.widget.TimePicker view, int selectedHour,
175  int selectedMinute) {
176  if (view.isShown()) {
177  hour = selectedHour;
178  minute = selectedMinute;
179  instant = Dates.TimeInstant(hour, minute);
180  // We post an event to the Android handler to do the App Inventor
181  // event dispatch. This way it gets called outside of the context
182  // of the timepicker's event. This permits the App Inventor dispatch
183  // handler to re-launch this timepicker
184  androidUIHandler.post(new Runnable() {
185  public void run() {
186  AfterTimeSet();
187  }
188  });
189  }
190  }
191  };
192 
196  @SimpleEvent(description="This event is run when a user has set the time in the popup dialog.")
197  public void AfterTimeSet() {
198  EventDispatcher.dispatchEvent(this, "AfterTimeSet");
199  }
200 }
com.google.appinventor.components.runtime.EventDispatcher
Definition: EventDispatcher.java:22
com.google.appinventor.components.runtime.TimePicker.Minute
int Minute()
Definition: TimePicker.java:100
com.google.appinventor.components.runtime.TimePicker.SetTimeToDisplay
void SetTimeToDisplay(int hour, int minute)
Definition: TimePicker.java:122
com.google.appinventor.components.annotations.SimpleFunction
Definition: SimpleFunction.java:23
com.google.appinventor.components.runtime.util.ErrorMessages
Definition: ErrorMessages.java:17
com.google.appinventor.components.runtime.util
-*- mode: java; c-basic-offset: 2; -*-
Definition: AccountChooser.java:7
com.google.appinventor.components.runtime.TimePicker.AfterTimeSet
void AfterTimeSet()
Definition: TimePicker.java:197
com.google.appinventor.components.common.YaVersion
Definition: YaVersion.java:14
com.google.appinventor.components.runtime.util.Dates.TimeInstant
static Calendar TimeInstant(int hour, int minute)
Definition: Dates.java:275
com.google.appinventor.components
com.google.appinventor.components.annotations.DesignerComponent
Definition: DesignerComponent.java:22
com.google.appinventor.components.annotations.SimpleEvent
Definition: SimpleEvent.java:20
com.google.appinventor.components.runtime.TimePicker.SetTimeToDisplayFromInstant
void SetTimeToDisplayFromInstant(Calendar instant)
Definition: TimePicker.java:142
com.google.appinventor.components.runtime.TimePicker.TimePicker
TimePicker(ComponentContainer container)
Definition: TimePicker.java:61
com.google.appinventor.components.runtime.EventDispatcher.dispatchEvent
static boolean dispatchEvent(Component component, String eventName, Object...args)
Definition: EventDispatcher.java:188
com.google.appinventor.components.runtime.TimePicker.Hour
int Hour()
Definition: TimePicker.java:87
com.google.appinventor.components.runtime.util.ErrorMessages.ERROR_ILLEGAL_HOUR
static final int ERROR_ILLEGAL_HOUR
Definition: ErrorMessages.java:189
com.google.appinventor.components.annotations.SimpleProperty
Definition: SimpleProperty.java:23
com.google.appinventor.components.runtime.TimePicker.click
void click()
Definition: TimePicker.java:159
com.google.appinventor.components.annotations.PropertyCategory
Definition: PropertyCategory.java:13
com.google.appinventor.components.runtime.TimePicker.LaunchPicker
void LaunchPicker()
Definition: TimePicker.java:154
com.google.appinventor.components.runtime.ComponentContainer
Definition: ComponentContainer.java:16
com.google.appinventor.components.runtime.TimePicker
Definition: TimePicker.java:46
com.google.appinventor.components.runtime
Copyright 2009-2011 Google, All Rights reserved.
Definition: AccelerometerSensor.java:8
com.google.appinventor.components.runtime.util.Dates
Definition: Dates.java:24
com.google.appinventor.components.common
Definition: ComponentCategory.java:7
com.google.appinventor.components.common.ComponentCategory
Definition: ComponentCategory.java:48
com.google.appinventor.components.runtime.Form.dispatchErrorOccurredEvent
void dispatchErrorOccurredEvent(final Component component, final String functionName, final int errorNumber, final Object... messageArgs)
Definition: Form.java:1011
com.google.appinventor.components.annotations.SimpleObject
Definition: SimpleObject.java:23
com.google.appinventor.components.runtime.TimePicker.Instant
Calendar Instant()
Definition: TimePicker.java:111
com.google.appinventor.components.runtime.AndroidViewComponent.container
final ComponentContainer container
Definition: AndroidViewComponent.java:29
com.google
com.google.appinventor.components.runtime.ButtonBase
Definition: ButtonBase.java:47
com
com.google.appinventor.components.runtime.util.ErrorMessages.ERROR_ILLEGAL_MINUTE
static final int ERROR_ILLEGAL_MINUTE
Definition: ErrorMessages.java:190
com.google.appinventor.components.runtime.util.Dates.Minute
static int Minute(Calendar date)
Definition: Dates.java:305
com.google.appinventor.components.runtime.ComponentContainer.$form
Form $form()
com.google.appinventor.components.runtime.ComponentContainer.$context
Activity $context()
com.google.appinventor.components.runtime.Form
Definition: Form.java:126
com.google.appinventor.components.annotations.PropertyCategory.APPEARANCE
APPEARANCE
Definition: PropertyCategory.java:16
com.google.appinventor.components.annotations
com.google.appinventor
com.google.appinventor.components.runtime.util.Dates.Hour
static int Hour(Calendar date)
Definition: Dates.java:294