1/* 2 * Copyright (C) 2006 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 android.os; 18 19import android.util.Log; 20 21/** 22 * Class that operates the vibrator on the device. 23 * <p> 24 * If your process exits, any vibration you started with will stop. 25 */ 26public class Vibrator 27{ 28 private static final String TAG = "Vibrator"; 29 30 IVibratorService mService; 31 private final Binder mToken = new Binder(); 32 33 /** @hide */ 34 public Vibrator() 35 { 36 mService = IVibratorService.Stub.asInterface( 37 ServiceManager.getService("vibrator")); 38 } 39 40 /** 41 * Turn the vibrator on. 42 * 43 * @param milliseconds How long to vibrate for. 44 */ 45 public void vibrate(long milliseconds) 46 { 47 if (mService == null) { 48 Log.w(TAG, "Failed to vibrate; no vibrator service."); 49 return; 50 } 51 try { 52 mService.vibrate(milliseconds, mToken); 53 } catch (RemoteException e) { 54 Log.w(TAG, "Failed to vibrate.", e); 55 } 56 } 57 58 /** 59 * Vibrate with a given pattern. 60 * 61 * <p> 62 * Pass in an array of ints that are the times at which to turn on or off 63 * the vibrator. The first one is how long to wait before turning it on, 64 * and then after that it alternates. If you want to repeat, pass the 65 * index into the pattern at which to start the repeat. 66 * 67 * @param pattern an array of longs of times to turn the vibrator on or off. 68 * @param repeat the index into pattern at which to repeat, or -1 if 69 * you don't want to repeat. 70 */ 71 public void vibrate(long[] pattern, int repeat) 72 { 73 if (mService == null) { 74 Log.w(TAG, "Failed to vibrate; no vibrator service."); 75 return; 76 } 77 // catch this here because the server will do nothing. pattern may 78 // not be null, let that be checked, because the server will drop it 79 // anyway 80 if (repeat < pattern.length) { 81 try { 82 mService.vibratePattern(pattern, repeat, mToken); 83 } catch (RemoteException e) { 84 Log.w(TAG, "Failed to vibrate.", e); 85 } 86 } else { 87 throw new ArrayIndexOutOfBoundsException(); 88 } 89 } 90 91 /** 92 * Turn the vibrator off. 93 */ 94 public void cancel() 95 { 96 if (mService == null) { 97 return; 98 } 99 try { 100 mService.cancelVibrate(mToken); 101 } catch (RemoteException e) { 102 Log.w(TAG, "Failed to cancel vibration.", e); 103 } 104 } 105} 106