1/* 2 * Copyright (C) 2012 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 * Vibrator implementation that controls the main system vibrator. 23 * 24 * @hide 25 */ 26public class SystemVibrator extends Vibrator { 27 private static final String TAG = "Vibrator"; 28 29 private final IVibratorService mService; 30 private final Binder mToken = new Binder(); 31 32 public SystemVibrator() { 33 mService = IVibratorService.Stub.asInterface( 34 ServiceManager.getService("vibrator")); 35 } 36 37 @Override 38 public boolean hasVibrator() { 39 if (mService == null) { 40 Log.w(TAG, "Failed to vibrate; no vibrator service."); 41 return false; 42 } 43 try { 44 return mService.hasVibrator(); 45 } catch (RemoteException e) { 46 } 47 return false; 48 } 49 50 @Override 51 public void vibrate(long milliseconds) { 52 if (mService == null) { 53 Log.w(TAG, "Failed to vibrate; no vibrator service."); 54 return; 55 } 56 try { 57 mService.vibrate(milliseconds, mToken); 58 } catch (RemoteException e) { 59 Log.w(TAG, "Failed to vibrate.", e); 60 } 61 } 62 63 @Override 64 public void vibrate(long[] pattern, int repeat) { 65 if (mService == null) { 66 Log.w(TAG, "Failed to vibrate; no vibrator service."); 67 return; 68 } 69 // catch this here because the server will do nothing. pattern may 70 // not be null, let that be checked, because the server will drop it 71 // anyway 72 if (repeat < pattern.length) { 73 try { 74 mService.vibratePattern(pattern, repeat, mToken); 75 } catch (RemoteException e) { 76 Log.w(TAG, "Failed to vibrate.", e); 77 } 78 } else { 79 throw new ArrayIndexOutOfBoundsException(); 80 } 81 } 82 83 @Override 84 public void cancel() { 85 if (mService == null) { 86 return; 87 } 88 try { 89 mService.cancelVibrate(mToken); 90 } catch (RemoteException e) { 91 Log.w(TAG, "Failed to cancel vibration.", e); 92 } 93 } 94} 95