AI2 Component  (Version nb184)
TimerInternal.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-2012 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.util;
8 
10 
11 import android.os.Handler;
12 
17 public final class TimerInternal implements Runnable {
18 
19  // Android message handler used as a timer
20  private Handler handler;
21 
22  // Indicates whether the timer is running or not
23  private boolean enabled; // set in constructor
24 
25  // Interval between timer events in ms
26  private int interval; // set in constructor
27 
28  // Component that should be called by timer
29  private AlarmHandler component;
30 
39  public TimerInternal(AlarmHandler component, boolean enabled, int interval) {
40  this(component, enabled, interval, new Handler());
41  }
42 
55  public TimerInternal(AlarmHandler component, boolean enabled, int interval, Handler handler) {
56  this.handler = handler;
57  this.component = component;
58 
59  // Set properties to default values specified by caller.
60  this.enabled = enabled;
61  this.interval = interval;
62  if (enabled) {
63  handler.postDelayed(this, interval);
64  }
65  }
66 
72  public int Interval() {
73  return interval;
74  }
75 
81  public void Interval(int interval) {
82  this.interval = interval;
83  if (enabled) {
84  handler.removeCallbacks(this);
85  handler.postDelayed(this, interval);
86  }
87  }
88 
95  public boolean Enabled() {
96  return enabled;
97  }
98 
104  public void Enabled(boolean enabled) {
105  if (this.enabled) {
106  handler.removeCallbacks(this);
107  }
108 
109  this.enabled = enabled;
110 
111  if (enabled) {
112  handler.postDelayed(this, interval);
113  }
114  }
115 
116  // Runnable implementation
117 
118  public void run() {
119  if (enabled) {
120  component.alarm();
121 
122  // During the call to component.alarm, the enabled field may have changed.
123  // We need to make sure that enabled is still true before we call handler.postDelayed.
124  if (enabled) {
125  handler.postDelayed(this, interval);
126  }
127  }
128  }
129 }
com.google.appinventor.components.runtime.AlarmHandler
Definition: AlarmHandler.java:14
com.google.appinventor.components
com.google.appinventor.components.runtime.util.TimerInternal.run
void run()
Definition: TimerInternal.java:118
com.google.appinventor.components.runtime.util.TimerInternal.Interval
void Interval(int interval)
Definition: TimerInternal.java:81
com.google.appinventor.components.runtime.util.TimerInternal.Interval
int Interval()
Definition: TimerInternal.java:72
com.google.appinventor.components.runtime.util.TimerInternal.Enabled
boolean Enabled()
Definition: TimerInternal.java:95
com.google.appinventor.components.runtime.util.TimerInternal.TimerInternal
TimerInternal(AlarmHandler component, boolean enabled, int interval, Handler handler)
Definition: TimerInternal.java:55
com.google.appinventor.components.runtime
Copyright 2009-2011 Google, All Rights reserved.
Definition: AccelerometerSensor.java:8
com.google.appinventor.components.runtime.util.TimerInternal
Definition: TimerInternal.java:17
com.google
com
com.google.appinventor.components.runtime.util.TimerInternal.TimerInternal
TimerInternal(AlarmHandler component, boolean enabled, int interval)
Definition: TimerInternal.java:39
com.google.appinventor.components.runtime.AlarmHandler.alarm
void alarm()
com.google.appinventor.components.runtime.util.TimerInternal.Enabled
void Enabled(boolean enabled)
Definition: TimerInternal.java:104
com.google.appinventor