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 * </p> 26 */ 27public class Vibrator 28{ 29 private static final String TAG = "Vibrator"; 30 31 IVibratorService mService; 32 private final Binder mToken = new Binder(); 33 34 /** @hide */ 35 public Vibrator() 36 { 37 mService = IVibratorService.Stub.asInterface( 38 ServiceManager.getService("vibrator")); 39 } 40 41 /** 42 * Check whether the hardware has a vibrator. Returns true if a vibrator 43 * exists, else false. 44 */ 45 public boolean hasVibrator() { 46 if (mService == null) { 47 Log.w(TAG, "Failed to vibrate; no vibrator service."); 48 return false; 49 } 50 try { 51 return mService.hasVibrator(); 52 } catch (RemoteException e) { 53 } 54 return false; 55 } 56 57 /** 58 * Turn the vibrator on. 59 * 60 * @param milliseconds The number of milliseconds to vibrate. 61 */ 62 public void vibrate(long milliseconds) 63 { 64 if (mService == null) { 65 Log.w(TAG, "Failed to vibrate; no vibrator service."); 66 return; 67 } 68 try { 69 mService.vibrate(milliseconds, mToken); 70 } catch (RemoteException e) { 71 Log.w(TAG, "Failed to vibrate.", e); 72 } 73 } 74 75 /** 76 * Vibrate with a given pattern. 77 * 78 * <p> 79 * Pass in an array of ints that are the durations for which to turn on or off 80 * the vibrator in milliseconds. The first value indicates the number of milliseconds 81 * to wait before turning the vibrator on. The next value indicates the number of milliseconds 82 * for which to keep the vibrator on before turning it off. Subsequent values alternate 83 * between durations in milliseconds to turn the vibrator off or to turn the vibrator on. 84 * </p><p> 85 * To cause the pattern to repeat, pass the index into the pattern array at which 86 * to start the repeat, or -1 to disable repeating. 87 * </p> 88 * 89 * @param pattern an array of longs of times for which to turn the vibrator on or off. 90 * @param repeat the index into pattern at which to repeat, or -1 if 91 * you don't want to repeat. 92 */ 93 public void vibrate(long[] pattern, int repeat) 94 { 95 if (mService == null) { 96 Log.w(TAG, "Failed to vibrate; no vibrator service."); 97 return; 98 } 99 // catch this here because the server will do nothing. pattern may 100 // not be null, let that be checked, because the server will drop it 101 // anyway 102 if (repeat < pattern.length) { 103 try { 104 mService.vibratePattern(pattern, repeat, mToken); 105 } catch (RemoteException e) { 106 Log.w(TAG, "Failed to vibrate.", e); 107 } 108 } else { 109 throw new ArrayIndexOutOfBoundsException(); 110 } 111 } 112 113 /** 114 * Turn the vibrator off. 115 */ 116 public void cancel() 117 { 118 if (mService == null) { 119 return; 120 } 121 try { 122 mService.cancelVibrate(mToken); 123 } catch (RemoteException e) { 124 Log.w(TAG, "Failed to cancel vibration.", e); 125 } 126 } 127} 128