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.compat.annotation.UnsupportedAppUsage; 20 import android.content.Context; 21 import android.media.AudioAttributes; 22 import android.util.Log; 23 24 /** 25 * Vibrator implementation that controls the main system vibrator. 26 * 27 * @hide 28 */ 29 public class SystemVibrator extends Vibrator { 30 private static final String TAG = "Vibrator"; 31 32 private final IVibratorService mService; 33 private final Binder mToken = new Binder(); 34 35 @UnsupportedAppUsage SystemVibrator()36 public SystemVibrator() { 37 mService = IVibratorService.Stub.asInterface(ServiceManager.getService("vibrator")); 38 } 39 40 @UnsupportedAppUsage SystemVibrator(Context context)41 public SystemVibrator(Context context) { 42 super(context); 43 mService = IVibratorService.Stub.asInterface(ServiceManager.getService("vibrator")); 44 } 45 46 @Override hasVibrator()47 public boolean hasVibrator() { 48 if (mService == null) { 49 Log.w(TAG, "Failed to vibrate; no vibrator service."); 50 return false; 51 } 52 try { 53 return mService.hasVibrator(); 54 } catch (RemoteException e) { 55 } 56 return false; 57 } 58 59 @Override hasAmplitudeControl()60 public boolean hasAmplitudeControl() { 61 if (mService == null) { 62 Log.w(TAG, "Failed to check amplitude control; no vibrator service."); 63 return false; 64 } 65 try { 66 return mService.hasAmplitudeControl(); 67 } catch (RemoteException e) { 68 } 69 return false; 70 } 71 72 @Override setAlwaysOnEffect(int uid, String opPkg, int alwaysOnId, VibrationEffect effect, AudioAttributes attributes)73 public boolean setAlwaysOnEffect(int uid, String opPkg, int alwaysOnId, VibrationEffect effect, 74 AudioAttributes attributes) { 75 if (mService == null) { 76 Log.w(TAG, "Failed to set always-on effect; no vibrator service."); 77 return false; 78 } 79 try { 80 return mService.setAlwaysOnEffect(uid, opPkg, alwaysOnId, effect, attributes); 81 } catch (RemoteException e) { 82 Log.w(TAG, "Failed to set always-on effect.", e); 83 } 84 return false; 85 } 86 87 @Override vibrate(int uid, String opPkg, VibrationEffect effect, String reason, AudioAttributes attributes)88 public void vibrate(int uid, String opPkg, VibrationEffect effect, 89 String reason, AudioAttributes attributes) { 90 if (mService == null) { 91 Log.w(TAG, "Failed to vibrate; no vibrator service."); 92 return; 93 } 94 try { 95 mService.vibrate(uid, opPkg, effect, attributes, reason, mToken); 96 } catch (RemoteException e) { 97 Log.w(TAG, "Failed to vibrate.", e); 98 } 99 } 100 101 @Override cancel()102 public void cancel() { 103 if (mService == null) { 104 return; 105 } 106 try { 107 mService.cancelVibrate(mToken); 108 } catch (RemoteException e) { 109 Log.w(TAG, "Failed to cancel vibration.", e); 110 } 111 } 112 } 113