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.annotation.CallbackExecutor; 20 import android.annotation.NonNull; 21 import android.compat.annotation.UnsupportedAppUsage; 22 import android.content.Context; 23 import android.media.AudioAttributes; 24 import android.util.ArrayMap; 25 import android.util.Log; 26 27 import com.android.internal.annotations.GuardedBy; 28 29 import java.util.Objects; 30 import java.util.concurrent.Executor; 31 32 /** 33 * Vibrator implementation that controls the main system vibrator. 34 * 35 * @hide 36 */ 37 public class SystemVibrator extends Vibrator { 38 private static final String TAG = "Vibrator"; 39 40 private final IVibratorService mService; 41 private final Binder mToken = new Binder(); 42 private final Context mContext; 43 44 @GuardedBy("mDelegates") 45 private final ArrayMap<OnVibratorStateChangedListener, 46 OnVibratorStateChangedListenerDelegate> mDelegates = new ArrayMap<>(); 47 48 @UnsupportedAppUsage SystemVibrator()49 public SystemVibrator() { 50 mContext = null; 51 mService = IVibratorService.Stub.asInterface(ServiceManager.getService("vibrator")); 52 } 53 54 @UnsupportedAppUsage SystemVibrator(Context context)55 public SystemVibrator(Context context) { 56 super(context); 57 mContext = context; 58 mService = IVibratorService.Stub.asInterface(ServiceManager.getService("vibrator")); 59 } 60 61 @Override hasVibrator()62 public boolean hasVibrator() { 63 if (mService == null) { 64 Log.w(TAG, "Failed to vibrate; no vibrator service."); 65 return false; 66 } 67 try { 68 return mService.hasVibrator(); 69 } catch (RemoteException e) { 70 } 71 return false; 72 } 73 74 /** 75 * Check whether the vibrator is vibrating. 76 * 77 * @return True if the hardware is vibrating, otherwise false. 78 */ 79 @Override isVibrating()80 public boolean isVibrating() { 81 if (mService == null) { 82 Log.w(TAG, "Failed to vibrate; no vibrator service."); 83 return false; 84 } 85 try { 86 return mService.isVibrating(); 87 } catch (RemoteException e) { 88 e.rethrowFromSystemServer(); 89 } 90 return false; 91 } 92 93 private class OnVibratorStateChangedListenerDelegate extends 94 IVibratorStateListener.Stub { 95 private final Executor mExecutor; 96 private final OnVibratorStateChangedListener mListener; 97 OnVibratorStateChangedListenerDelegate(@onNull OnVibratorStateChangedListener listener, @NonNull Executor executor)98 OnVibratorStateChangedListenerDelegate(@NonNull OnVibratorStateChangedListener listener, 99 @NonNull Executor executor) { 100 mExecutor = executor; 101 mListener = listener; 102 } 103 104 @Override onVibrating(boolean isVibrating)105 public void onVibrating(boolean isVibrating) { 106 mExecutor.execute(() -> mListener.onVibratorStateChanged(isVibrating)); 107 } 108 } 109 110 /** 111 * Adds a listener for vibrator state change. If the listener was previously added and not 112 * removed, this call will be ignored. 113 * 114 * @param listener Listener to be added. 115 * @param executor The {@link Executor} on which the listener's callbacks will be executed on. 116 */ 117 @Override addVibratorStateListener( @onNull @allbackExecutor Executor executor, @NonNull OnVibratorStateChangedListener listener)118 public void addVibratorStateListener( 119 @NonNull @CallbackExecutor Executor executor, 120 @NonNull OnVibratorStateChangedListener listener) { 121 Objects.requireNonNull(listener); 122 Objects.requireNonNull(executor); 123 if (mService == null) { 124 Log.w(TAG, "Failed to add vibrate state listener; no vibrator service."); 125 return; 126 } 127 128 synchronized (mDelegates) { 129 // If listener is already registered, reject and return. 130 if (mDelegates.containsKey(listener)) { 131 Log.w(TAG, "Listener already registered."); 132 return; 133 } 134 try { 135 final OnVibratorStateChangedListenerDelegate delegate = 136 new OnVibratorStateChangedListenerDelegate(listener, executor); 137 if (!mService.registerVibratorStateListener(delegate)) { 138 Log.w(TAG, "Failed to register vibrate state listener"); 139 return; 140 } 141 mDelegates.put(listener, delegate); 142 } catch (RemoteException e) { 143 throw e.rethrowFromSystemServer(); 144 } 145 } 146 } 147 148 /** 149 * Adds a listener for vibrator state changes. Callbacks will be executed on the main thread. 150 * If the listener was previously added and not removed, this call will be ignored. 151 * 152 * @param listener listener to be added 153 */ 154 @Override addVibratorStateListener(@onNull OnVibratorStateChangedListener listener)155 public void addVibratorStateListener(@NonNull OnVibratorStateChangedListener listener) { 156 Objects.requireNonNull(listener); 157 if (mContext == null) { 158 Log.w(TAG, "Failed to add vibrate state listener; no vibrator context."); 159 return; 160 } 161 addVibratorStateListener(mContext.getMainExecutor(), listener); 162 } 163 164 /** 165 * Removes the listener for vibrator state changes. If the listener was not previously 166 * registered, this call will do nothing. 167 * 168 * @param listener Listener to be removed. 169 */ 170 @Override removeVibratorStateListener(@onNull OnVibratorStateChangedListener listener)171 public void removeVibratorStateListener(@NonNull OnVibratorStateChangedListener listener) { 172 Objects.requireNonNull(listener); 173 if (mService == null) { 174 Log.w(TAG, "Failed to remove vibrate state listener; no vibrator service."); 175 return; 176 } 177 synchronized (mDelegates) { 178 // Check if the listener is registered, otherwise will return. 179 if (mDelegates.containsKey(listener)) { 180 final OnVibratorStateChangedListenerDelegate delegate = mDelegates.get(listener); 181 try { 182 if (!mService.unregisterVibratorStateListener(delegate)) { 183 Log.w(TAG, "Failed to unregister vibrate state listener"); 184 return; 185 } 186 mDelegates.remove(listener); 187 } catch (RemoteException e) { 188 throw e.rethrowFromSystemServer(); 189 } 190 } 191 } 192 } 193 194 @Override hasAmplitudeControl()195 public boolean hasAmplitudeControl() { 196 if (mService == null) { 197 Log.w(TAG, "Failed to check amplitude control; no vibrator service."); 198 return false; 199 } 200 try { 201 return mService.hasAmplitudeControl(); 202 } catch (RemoteException e) { 203 } 204 return false; 205 } 206 207 @Override setAlwaysOnEffect(int uid, String opPkg, int alwaysOnId, VibrationEffect effect, AudioAttributes attributes)208 public boolean setAlwaysOnEffect(int uid, String opPkg, int alwaysOnId, VibrationEffect effect, 209 AudioAttributes attributes) { 210 if (mService == null) { 211 Log.w(TAG, "Failed to set always-on effect; no vibrator service."); 212 return false; 213 } 214 try { 215 VibrationAttributes atr = new VibrationAttributes.Builder(attributes, effect).build(); 216 return mService.setAlwaysOnEffect(uid, opPkg, alwaysOnId, effect, atr); 217 } catch (RemoteException e) { 218 Log.w(TAG, "Failed to set always-on effect.", e); 219 } 220 return false; 221 } 222 223 @Override vibrate(int uid, String opPkg, VibrationEffect effect, String reason, AudioAttributes attributes)224 public void vibrate(int uid, String opPkg, VibrationEffect effect, 225 String reason, AudioAttributes attributes) { 226 if (mService == null) { 227 Log.w(TAG, "Failed to vibrate; no vibrator service."); 228 return; 229 } 230 try { 231 if (attributes == null) { 232 attributes = new AudioAttributes.Builder().build(); 233 } 234 VibrationAttributes atr = new VibrationAttributes.Builder(attributes, effect).build(); 235 mService.vibrate(uid, opPkg, effect, atr, reason, mToken); 236 } catch (RemoteException e) { 237 Log.w(TAG, "Failed to vibrate.", e); 238 } 239 } 240 241 @Override areEffectsSupported(@ibrationEffect.EffectType int... effectIds)242 public int[] areEffectsSupported(@VibrationEffect.EffectType int... effectIds) { 243 try { 244 return mService.areEffectsSupported(effectIds); 245 } catch (RemoteException e) { 246 Log.w(TAG, "Failed to query effect support"); 247 throw e.rethrowAsRuntimeException(); 248 } 249 } 250 251 @Override arePrimitivesSupported( @onNull @ibrationEffect.Composition.Primitive int... primitiveIds)252 public boolean[] arePrimitivesSupported( 253 @NonNull @VibrationEffect.Composition.Primitive int... primitiveIds) { 254 try { 255 return mService.arePrimitivesSupported(primitiveIds); 256 } catch (RemoteException e) { 257 Log.w(TAG, "Failed to query effect support"); 258 throw e.rethrowAsRuntimeException(); 259 } 260 } 261 262 263 @Override cancel()264 public void cancel() { 265 if (mService == null) { 266 return; 267 } 268 try { 269 mService.cancelVibrate(mToken); 270 } catch (RemoteException e) { 271 Log.w(TAG, "Failed to cancel vibration.", e); 272 } 273 } 274 } 275