blob: 2fcdb5d5502f7b13b237e6179641a6c4f904da11 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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
17package com.android.server;
18
19import android.content.BroadcastReceiver;
20import android.content.Context;
21import android.content.Intent;
22import android.content.IntentFilter;
23import android.content.pm.PackageManager;
Joe Onorato95e4f702009-03-24 19:29:09 -070024import android.os.Handler;
Mike Lockwood3a322132009-11-24 00:30:52 -050025import android.os.IVibratorService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026import android.os.PowerManager;
27import android.os.Process;
28import android.os.RemoteException;
29import android.os.IBinder;
30import android.os.Binder;
31import android.os.SystemClock;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -070032import android.os.WorkSource;
Joe Onorato8a9b2202010-02-26 18:56:32 -080033import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034
Patrick Scott18dd5f02009-07-02 11:31:12 -040035import java.util.LinkedList;
36import java.util.ListIterator;
37
Mike Lockwood3a322132009-11-24 00:30:52 -050038public class VibratorService extends IVibratorService.Stub {
39 private static final String TAG = "VibratorService";
Mike Lockwoodcc9a63d2009-11-10 07:50:28 -050040
Patrick Scott18dd5f02009-07-02 11:31:12 -040041 private final LinkedList<Vibration> mVibrations;
42 private Vibration mCurrentVibration;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -070043 private final WorkSource mTmpWorkSource = new WorkSource();
Patrick Scott18dd5f02009-07-02 11:31:12 -040044
Patrick Scott18dd5f02009-07-02 11:31:12 -040045 private class Vibration implements IBinder.DeathRecipient {
46 private final IBinder mToken;
47 private final long mTimeout;
48 private final long mStartTime;
49 private final long[] mPattern;
50 private final int mRepeat;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -070051 private final int mUid;
Patrick Scott18dd5f02009-07-02 11:31:12 -040052
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -070053 Vibration(IBinder token, long millis, int uid) {
54 this(token, millis, null, 0, uid);
Patrick Scott18dd5f02009-07-02 11:31:12 -040055 }
56
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -070057 Vibration(IBinder token, long[] pattern, int repeat, int uid) {
58 this(token, 0, pattern, repeat, uid);
Patrick Scott18dd5f02009-07-02 11:31:12 -040059 }
60
61 private Vibration(IBinder token, long millis, long[] pattern,
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -070062 int repeat, int uid) {
Patrick Scott18dd5f02009-07-02 11:31:12 -040063 mToken = token;
64 mTimeout = millis;
65 mStartTime = SystemClock.uptimeMillis();
66 mPattern = pattern;
67 mRepeat = repeat;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -070068 mUid = uid;
Patrick Scott18dd5f02009-07-02 11:31:12 -040069 }
70
71 public void binderDied() {
72 synchronized (mVibrations) {
73 mVibrations.remove(this);
74 if (this == mCurrentVibration) {
75 doCancelVibrateLocked();
76 startNextVibrationLocked();
77 }
78 }
79 }
80
81 public boolean hasLongerTimeout(long millis) {
82 if (mTimeout == 0) {
83 // This is a pattern, return false to play the simple
84 // vibration.
85 return false;
86 }
87 if ((mStartTime + mTimeout)
88 < (SystemClock.uptimeMillis() + millis)) {
89 // If this vibration will end before the time passed in, let
90 // the new vibration play.
91 return false;
92 }
93 return true;
94 }
95 }
96
Mike Lockwood3a322132009-11-24 00:30:52 -050097 VibratorService(Context context) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098 // Reset the hardware to a default state, in case this is a runtime
99 // restart instead of a fresh boot.
100 vibratorOff();
101
102 mContext = context;
103 PowerManager pm = (PowerManager)context.getSystemService(
104 Context.POWER_SERVICE);
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700105 mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*vibrator*");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106 mWakeLock.setReferenceCounted(true);
107
Patrick Scott18dd5f02009-07-02 11:31:12 -0400108 mVibrations = new LinkedList<Vibration>();
109
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800110 IntentFilter filter = new IntentFilter();
111 filter.addAction(Intent.ACTION_SCREEN_OFF);
112 context.registerReceiver(mIntentReceiver, filter);
113 }
114
Dianne Hackbornea9020e2010-11-04 11:39:12 -0700115 public boolean hasVibrator() {
116 return vibratorExists();
117 }
118
Patrick Scott18dd5f02009-07-02 11:31:12 -0400119 public void vibrate(long milliseconds, IBinder token) {
The Android Open Source Project10592532009-03-18 17:39:46 -0700120 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.VIBRATE)
121 != PackageManager.PERMISSION_GRANTED) {
122 throw new SecurityException("Requires VIBRATE permission");
123 }
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700124 int uid = Binder.getCallingUid();
Patrick Scott24f10762009-08-19 09:03:56 -0400125 // We're running in the system server so we cannot crash. Check for a
126 // timeout of 0 or negative. This will ensure that a vibration has
127 // either a timeout of > 0 or a non-null pattern.
128 if (milliseconds <= 0 || (mCurrentVibration != null
129 && mCurrentVibration.hasLongerTimeout(milliseconds))) {
Patrick Scott18dd5f02009-07-02 11:31:12 -0400130 // Ignore this vibration since the current vibration will play for
131 // longer than milliseconds.
132 return;
133 }
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700134 Vibration vib = new Vibration(token, milliseconds, uid);
Patrick Scott18dd5f02009-07-02 11:31:12 -0400135 synchronized (mVibrations) {
136 removeVibrationLocked(token);
137 doCancelVibrateLocked();
138 mCurrentVibration = vib;
139 startVibrationLocked(vib);
140 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800141 }
142
143 private boolean isAll0(long[] pattern) {
144 int N = pattern.length;
145 for (int i = 0; i < N; i++) {
146 if (pattern[i] != 0) {
147 return false;
148 }
149 }
150 return true;
151 }
152
153 public void vibratePattern(long[] pattern, int repeat, IBinder token) {
154 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.VIBRATE)
155 != PackageManager.PERMISSION_GRANTED) {
156 throw new SecurityException("Requires VIBRATE permission");
157 }
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700158 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800159 // so wakelock calls will succeed
160 long identity = Binder.clearCallingIdentity();
161 try {
162 if (false) {
163 String s = "";
164 int N = pattern.length;
165 for (int i=0; i<N; i++) {
166 s += " " + pattern[i];
167 }
Joe Onorato8a9b2202010-02-26 18:56:32 -0800168 Slog.i(TAG, "vibrating with pattern: " + s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800169 }
170
171 // we're running in the server so we can't fail
172 if (pattern == null || pattern.length == 0
173 || isAll0(pattern)
174 || repeat >= pattern.length || token == null) {
175 return;
176 }
177
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700178 Vibration vib = new Vibration(token, pattern, repeat, uid);
Patrick Scott18dd5f02009-07-02 11:31:12 -0400179 try {
180 token.linkToDeath(vib, 0);
181 } catch (RemoteException e) {
182 return;
183 }
184
185 synchronized (mVibrations) {
186 removeVibrationLocked(token);
187 doCancelVibrateLocked();
188 if (repeat >= 0) {
189 mVibrations.addFirst(vib);
190 startNextVibrationLocked();
191 } else {
192 // A negative repeat means that this pattern is not meant
193 // to repeat. Treat it like a simple vibration.
194 mCurrentVibration = vib;
195 startVibrationLocked(vib);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800196 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800197 }
198 }
199 finally {
200 Binder.restoreCallingIdentity(identity);
201 }
202 }
203
Patrick Scott18dd5f02009-07-02 11:31:12 -0400204 public void cancelVibrate(IBinder token) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800205 mContext.enforceCallingOrSelfPermission(
206 android.Manifest.permission.VIBRATE,
207 "cancelVibrate");
208
209 // so wakelock calls will succeed
210 long identity = Binder.clearCallingIdentity();
211 try {
Patrick Scott18dd5f02009-07-02 11:31:12 -0400212 synchronized (mVibrations) {
213 final Vibration vib = removeVibrationLocked(token);
214 if (vib == mCurrentVibration) {
215 doCancelVibrateLocked();
216 startNextVibrationLocked();
217 }
218 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800219 }
220 finally {
221 Binder.restoreCallingIdentity(identity);
222 }
223 }
Eric Olsenf42f15c2009-10-29 16:42:03 -0700224
Patrick Scott18dd5f02009-07-02 11:31:12 -0400225 private final Runnable mVibrationRunnable = new Runnable() {
226 public void run() {
227 synchronized (mVibrations) {
228 doCancelVibrateLocked();
229 startNextVibrationLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800230 }
Patrick Scott18dd5f02009-07-02 11:31:12 -0400231 }
232 };
233
234 // Lock held on mVibrations
235 private void doCancelVibrateLocked() {
236 if (mThread != null) {
237 synchronized (mThread) {
238 mThread.mDone = true;
239 mThread.notify();
240 }
241 mThread = null;
242 }
243 vibratorOff();
244 mH.removeCallbacks(mVibrationRunnable);
245 }
246
247 // Lock held on mVibrations
248 private void startNextVibrationLocked() {
249 if (mVibrations.size() <= 0) {
250 return;
251 }
252 mCurrentVibration = mVibrations.getFirst();
253 startVibrationLocked(mCurrentVibration);
254 }
255
256 // Lock held on mVibrations
257 private void startVibrationLocked(final Vibration vib) {
258 if (vib.mTimeout != 0) {
259 vibratorOn(vib.mTimeout);
260 mH.postDelayed(mVibrationRunnable, vib.mTimeout);
261 } else {
262 // mThread better be null here. doCancelVibrate should always be
263 // called before startNextVibrationLocked or startVibrationLocked.
264 mThread = new VibrateThread(vib);
265 mThread.start();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800266 }
267 }
268
Patrick Scott18dd5f02009-07-02 11:31:12 -0400269 // Lock held on mVibrations
270 private Vibration removeVibrationLocked(IBinder token) {
271 ListIterator<Vibration> iter = mVibrations.listIterator(0);
272 while (iter.hasNext()) {
273 Vibration vib = iter.next();
274 if (vib.mToken == token) {
275 iter.remove();
276 return vib;
277 }
278 }
279 // We might be looking for a simple vibration which is only stored in
280 // mCurrentVibration.
281 if (mCurrentVibration != null && mCurrentVibration.mToken == token) {
282 return mCurrentVibration;
283 }
284 return null;
285 }
286
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800287 private class VibrateThread extends Thread {
Patrick Scott18dd5f02009-07-02 11:31:12 -0400288 final Vibration mVibration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800289 boolean mDone;
Eric Olsenf42f15c2009-10-29 16:42:03 -0700290
Patrick Scott18dd5f02009-07-02 11:31:12 -0400291 VibrateThread(Vibration vib) {
292 mVibration = vib;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700293 mTmpWorkSource.set(vib.mUid);
294 mWakeLock.setWorkSource(mTmpWorkSource);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800295 mWakeLock.acquire();
296 }
297
298 private void delay(long duration) {
299 if (duration > 0) {
300 long bedtime = SystemClock.uptimeMillis();
301 do {
302 try {
303 this.wait(duration);
304 }
305 catch (InterruptedException e) {
306 }
307 if (mDone) {
308 break;
309 }
310 duration = duration
311 - SystemClock.uptimeMillis() - bedtime;
312 } while (duration > 0);
313 }
314 }
315
316 public void run() {
317 Process.setThreadPriority(Process.THREAD_PRIORITY_URGENT_DISPLAY);
318 synchronized (this) {
319 int index = 0;
Patrick Scott18dd5f02009-07-02 11:31:12 -0400320 long[] pattern = mVibration.mPattern;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800321 int len = pattern.length;
Patrick Scott18dd5f02009-07-02 11:31:12 -0400322 int repeat = mVibration.mRepeat;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800323 long duration = 0;
324
325 while (!mDone) {
Eric Olsenf42f15c2009-10-29 16:42:03 -0700326 // add off-time duration to any accumulated on-time duration
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800327 if (index < len) {
328 duration += pattern[index++];
329 }
330
331 // sleep until it is time to start the vibrator
332 delay(duration);
333 if (mDone) {
334 break;
335 }
336
337 if (index < len) {
338 // read on-time duration and start the vibrator
339 // duration is saved for delay() at top of loop
340 duration = pattern[index++];
341 if (duration > 0) {
Mike Lockwood3a322132009-11-24 00:30:52 -0500342 VibratorService.this.vibratorOn(duration);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800343 }
344 } else {
Patrick Scott18dd5f02009-07-02 11:31:12 -0400345 if (repeat < 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800346 break;
347 } else {
Patrick Scott18dd5f02009-07-02 11:31:12 -0400348 index = repeat;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800349 duration = 0;
350 }
351 }
352 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800353 mWakeLock.release();
354 }
Patrick Scott18dd5f02009-07-02 11:31:12 -0400355 synchronized (mVibrations) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800356 if (mThread == this) {
357 mThread = null;
358 }
Patrick Scott18dd5f02009-07-02 11:31:12 -0400359 if (!mDone) {
360 // If this vibration finished naturally, start the next
361 // vibration.
362 mVibrations.remove(mVibration);
363 startNextVibrationLocked();
364 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800365 }
366 }
367 };
368
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800369 BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
370 public void onReceive(Context context, Intent intent) {
371 if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
Patrick Scott18dd5f02009-07-02 11:31:12 -0400372 synchronized (mVibrations) {
373 doCancelVibrateLocked();
374 mVibrations.clear();
375 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800376 }
377 }
378 };
Eric Olsenf42f15c2009-10-29 16:42:03 -0700379
Mike Lockwood3a322132009-11-24 00:30:52 -0500380 private Handler mH = new Handler();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800381
Dianne Hackbornbed30e12009-03-31 14:46:20 -0700382 private final Context mContext;
383 private final PowerManager.WakeLock mWakeLock;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800384
385 volatile VibrateThread mThread;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800386
Dianne Hackbornea9020e2010-11-04 11:39:12 -0700387 native static boolean vibratorExists();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800388 native static void vibratorOn(long milliseconds);
389 native static void vibratorOff();
390}