2 * Copyright (C) 2007 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.server;
19 import android.os.SystemClock;
21 import android.os.ConditionVariable;
24 * Utility class that you can call on with a timeout, and get called back
25 * after a given time, dealing correctly with restarting the timeout.
27 * <p>For example, this class is used by the android.os.Vibrator class.
29 abstract class ResettableTimeout
32 * Override this do what you need to do when it's starting
33 * This is called with the monitor on this method held, so be careful.
35 * @param alreadyOn is true if it's currently running
37 public abstract void on(boolean alreadyOn);
40 * Override this to do what you need to do when it's stopping.
41 * This is called with the monitor on this method held, so be careful.
43 public abstract void off();
46 * Does the following steps.
48 * <p>2. Start the timer.</p>
49 * <p>3. At the timeout, calls off()<p>
50 * <p>If you call this again, the timeout is reset to the new one</p>
52 public void go(long milliseconds)
55 mOffAt = SystemClock.uptimeMillis() + milliseconds;
59 // By starting the thread first and waiting, we ensure that if the
60 // thread to stop it can't start, we don't turn the vibrator on
61 // forever. This still isn't really sufficient, because we don't
62 // have another processor watching us. We really should have a
63 // service for this in case our process crashes.
64 if (mThread == null) {
73 // poke the thread so it gets the new timeout.
81 * Cancel the timeout and call off now.
87 if (mThread != null) {
98 private class T extends Thread
105 synchronized (this) {
106 diff = mOffAt - SystemClock.uptimeMillis();
117 catch (InterruptedException e) {
123 private ConditionVariable mLock = new ConditionVariable();
125 // turn it off at this time.
126 private volatile long mOffAt;
127 private volatile boolean mOffCalled;
129 private Thread mThread;