• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2008 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 
17 package com.android.cts;
18 
19 import java.util.Timer;
20 import java.util.TimerTask;
21 
22 /**
23  * Host timer.
24  * Generally, there are two use cases of this general host timer:
25  * <ul>
26  *    <li> Use it as general timer to guard host from running for
27  *         too long under some situations.
28  *    <li> Use it as special timer where host needs to run very
29  *         long to communicate with device to fetch result section
30  *         by section which requires restarting the timer.
31  * </ul>
32  */
33 public class HostTimer {
34     private final static int INIT = 0;
35     private final static int RUNNING = 1;
36     private final static int CANCELLED = 2;
37     private final static int TIMEOUT = 3;
38 
39     private boolean mIsNotified;
40     private int mStatus;
41     private int mDelay;
42     private TimerTask mTimerTask;
43     private Timer mTimer;
44 
HostTimer(TimerTask task, int delay)45     public HostTimer(TimerTask task, int delay) {
46         mDelay = delay;
47         mTimerTask = task;
48         mStatus = INIT;
49         mIsNotified = false;
50         mTimer = null;
51     }
52 
53     /**
54      * Mark notified.
55      */
setNotified()56     public void setNotified() {
57         mIsNotified = true;
58     }
59 
60     /**
61      * Get the notification status.
62      *
63      * @return The notification status.
64      */
isNotified()65     public boolean isNotified() {
66         return mIsNotified;
67     }
68 
69     /**
70      * Clear the status of notification.
71      */
resetNotified()72     public void resetNotified() {
73         mIsNotified = false;
74     }
75 
76     /**
77      * Wait on.
78      */
waitOn()79     public void waitOn() throws InterruptedException {
80         Log.d("HostTimer.waitOn(): mIsNotified=" + mIsNotified + ", this=" + this);
81         if (!mIsNotified) {
82             wait();
83         }
84         mIsNotified = false;
85     }
86 
87     /**
88      * Set the time to delay.
89      *
90      * @param delay The time to delay.
91      */
setDelay(int delay)92     public void setDelay(int delay) {
93         mDelay = delay;
94     }
95 
96     /**
97      * Set the timer task.
98      *
99      * @param task The timer task.
100      */
setTimerTask(TimerTask task)101     public void setTimerTask(TimerTask task) {
102         mTimerTask = task;
103     }
104 
105     /**
106      * Check if the watch dog timer timed out.
107      *
108      * @return If timeout, return true; else return false.
109      */
isTimeOut()110     public boolean isTimeOut() {
111         return (mStatus == TIMEOUT);
112     }
113 
114     /**
115      * Start the watch dog timer.
116      */
start()117     public void start() {
118         mTimer = new Timer();
119         mTimer.schedule(mTimerTask, mDelay);
120         mStatus = RUNNING;
121     }
122 
123     /**
124      * Restart the watch dog timer.
125      */
restart(TimerTask task, int delay)126     public void restart(TimerTask task, int delay) {
127         mTimer.cancel();
128         mTimerTask = task;
129         mDelay = delay;
130         start();
131     }
132 
133     /**
134      * Send notify to thread waiting on this object.
135      */
sendNotify()136     public void sendNotify() {
137         Log.d("HostTimer.sendNotify(): mIsNotified=" + mIsNotified + ", this=" + this);
138         mIsNotified = true;
139         notify();
140     }
141 
142     /**
143      * Cancel the timer. To keep the status info, call this
144      * cancel in stead of the one inherited from parent.
145      *
146      * @param timeout If true, the cancellation is caused by timer timing out;
147      *                If false, the cancellation is no caused by timer timing out.
148      */
cancel(boolean timeout)149     public void cancel(boolean timeout) {
150         if (mTimer != null) {
151             mTimer.cancel();
152         }
153         if (mStatus == RUNNING) {
154             if (timeout) {
155                 mStatus = TIMEOUT;
156             } else {
157                 mStatus = CANCELLED;
158             }
159         }
160     }
161 }
162