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 17 package android.os; 18 19 import android.app.ActivityThread; 20 import android.content.Context; 21 import android.util.Log; 22 23 /** 24 * Vibrator implementation that controls the main system vibrator. 25 * 26 * @hide 27 */ 28 public class SystemVibrator extends Vibrator { 29 private static final String TAG = "Vibrator"; 30 31 private final String mPackageName; 32 private final IVibratorService mService; 33 private final Binder mToken = new Binder(); 34 35 public SystemVibrator() { 36 mPackageName = ActivityThread.currentPackageName(); 37 mService = IVibratorService.Stub.asInterface( 38 ServiceManager.getService("vibrator")); 39 } 40 41 public SystemVibrator(Context context) { 42 mPackageName = context.getBasePackageName(); 43 mService = IVibratorService.Stub.asInterface( 44 ServiceManager.getService("vibrator")); 45 } 46 47 @Override 48 public boolean hasVibrator() { 49 if (mService == null) { 50 Log.w(TAG, "Failed to vibrate; no vibrator service."); 51 return false; 52 } 53 try { 54 return mService.hasVibrator(); 55 } catch (RemoteException e) { 56 } 57 return false; 58 } 59 60 @Override 61 public void vibrate(long milliseconds) { 62 vibrate(Process.myUid(), mPackageName, milliseconds); 63 } 64 65 @Override 66 public void vibrate(long[] pattern, int repeat) { 67 vibrate(Process.myUid(), mPackageName, pattern, repeat); 68 } 69 70 /** 71 * @hide 72 */ 73 @Override 74 public void vibrate(int owningUid, String owningPackage, long milliseconds) { 75 if (mService == null) { 76 Log.w(TAG, "Failed to vibrate; no vibrator service."); 77 return; 78 } 79 try { 80 mService.vibrate(owningUid, owningPackage, milliseconds, mToken); 81 } catch (RemoteException e) { 82 Log.w(TAG, "Failed to vibrate.", e); 83 } 84 } 85 86 /** 87 * @hide 88 */ 89 @Override 90 public void vibrate(int owningUid, String owningPackage, long[] pattern, int repeat) { 91 if (mService == null) { 92 Log.w(TAG, "Failed to vibrate; no vibrator service."); 93 return; 94 } 95 // catch this here because the server will do nothing. pattern may 96 // not be null, let that be checked, because the server will drop it 97 // anyway 98 if (repeat < pattern.length) { 99 try { 100 mService.vibratePattern(owningUid, owningPackage, pattern, repeat, mToken); 101 } catch (RemoteException e) { 102 Log.w(TAG, "Failed to vibrate.", e); 103 } 104 } else { 105 throw new ArrayIndexOutOfBoundsException(); 106 } 107 } 108 109 @Override 110 public void cancel() { 111 if (mService == null) { 112 return; 113 } 114 try { 115 mService.cancelVibrate(mToken); 116 } catch (RemoteException e) { 117 Log.w(TAG, "Failed to cancel vibration.", e); 118 } 119 } 120 } 121