Jeff Brown | c234613 | 2012-04-13 01:55:38 -0700 | [diff] [blame] | 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 | |
Artur Satayev | afdb23a | 2019-12-10 17:47:53 +0000 | [diff] [blame] | 19 | import android.compat.annotation.UnsupportedAppUsage; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 20 | import android.content.Context; |
John Spurlock | 7b41467 | 2014-07-18 13:02:39 -0400 | [diff] [blame] | 21 | import android.media.AudioAttributes; |
Jeff Brown | c234613 | 2012-04-13 01:55:38 -0700 | [diff] [blame] | 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 | |
Andrei Onea | 24ec321 | 2019-03-15 17:35:05 +0000 | [diff] [blame] | 35 | @UnsupportedAppUsage |
Jeff Brown | c234613 | 2012-04-13 01:55:38 -0700 | [diff] [blame] | 36 | public SystemVibrator() { |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 37 | mService = IVibratorService.Stub.asInterface(ServiceManager.getService("vibrator")); |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 38 | } |
| 39 | |
Andrei Onea | 24ec321 | 2019-03-15 17:35:05 +0000 | [diff] [blame] | 40 | @UnsupportedAppUsage |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 41 | public SystemVibrator(Context context) { |
John Spurlock | 1af30c7 | 2014-03-10 08:33:35 -0400 | [diff] [blame] | 42 | super(context); |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 43 | mService = IVibratorService.Stub.asInterface(ServiceManager.getService("vibrator")); |
Jeff Brown | c234613 | 2012-04-13 01:55:38 -0700 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | @Override |
| 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 | |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 59 | @Override |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 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 |
Harpreet \"Eli\" Sangha | 45d53ee | 2019-09-25 17:43:24 +0900 | [diff] [blame] | 73 | public boolean setAlwaysOnEffect(int id, VibrationEffect effect, AudioAttributes attributes) { |
| 74 | if (mService == null) { |
| 75 | Log.w(TAG, "Failed to set always-on effect; no vibrator service."); |
| 76 | return false; |
| 77 | } |
| 78 | try { |
| 79 | return mService.setAlwaysOnEffect(id, effect, attributes); |
| 80 | } catch (RemoteException e) { |
| 81 | Log.w(TAG, "Failed to set always-on effect.", e); |
| 82 | } |
| 83 | return false; |
| 84 | } |
| 85 | |
| 86 | @Override |
Alexey Kuzmin | e1f06b8 | 2018-06-20 17:48:43 +0100 | [diff] [blame] | 87 | public void vibrate(int uid, String opPkg, VibrationEffect effect, |
| 88 | String reason, AudioAttributes attributes) { |
Jeff Brown | c234613 | 2012-04-13 01:55:38 -0700 | [diff] [blame] | 89 | if (mService == null) { |
| 90 | Log.w(TAG, "Failed to vibrate; no vibrator service."); |
| 91 | return; |
| 92 | } |
| 93 | try { |
Michael Wright | 7f37878 | 2019-06-20 19:36:57 +0100 | [diff] [blame] | 94 | mService.vibrate(uid, opPkg, effect, attributes, reason, mToken); |
Jeff Brown | c234613 | 2012-04-13 01:55:38 -0700 | [diff] [blame] | 95 | } catch (RemoteException e) { |
| 96 | Log.w(TAG, "Failed to vibrate.", e); |
| 97 | } |
| 98 | } |
| 99 | |
Jeff Brown | c234613 | 2012-04-13 01:55:38 -0700 | [diff] [blame] | 100 | @Override |
| 101 | public void cancel() { |
| 102 | if (mService == null) { |
| 103 | return; |
| 104 | } |
| 105 | try { |
| 106 | mService.cancelVibrate(mToken); |
| 107 | } catch (RemoteException e) { |
| 108 | Log.w(TAG, "Failed to cancel vibration.", e); |
| 109 | } |
| 110 | } |
| 111 | } |