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 | |
Andrei Onea | 24ec321 | 2019-03-15 17:35:05 +0000 | [diff] [blame] | 19 | import android.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 |
Alexey Kuzmin | e1f06b8 | 2018-06-20 17:48:43 +0100 | [diff] [blame] | 73 | public void vibrate(int uid, String opPkg, VibrationEffect effect, |
| 74 | String reason, AudioAttributes attributes) { |
Jeff Brown | c234613 | 2012-04-13 01:55:38 -0700 | [diff] [blame] | 75 | if (mService == null) { |
| 76 | Log.w(TAG, "Failed to vibrate; no vibrator service."); |
| 77 | return; |
| 78 | } |
| 79 | try { |
Michael Wright | 7f37878 | 2019-06-20 19:36:57 +0100 | [diff] [blame] | 80 | mService.vibrate(uid, opPkg, effect, attributes, reason, mToken); |
Jeff Brown | c234613 | 2012-04-13 01:55:38 -0700 | [diff] [blame] | 81 | } catch (RemoteException e) { |
| 82 | Log.w(TAG, "Failed to vibrate.", e); |
| 83 | } |
| 84 | } |
| 85 | |
Jeff Brown | c234613 | 2012-04-13 01:55:38 -0700 | [diff] [blame] | 86 | @Override |
| 87 | public void cancel() { |
| 88 | if (mService == null) { |
| 89 | return; |
| 90 | } |
| 91 | try { |
| 92 | mService.cancelVibrate(mToken); |
| 93 | } catch (RemoteException e) { |
| 94 | Log.w(TAG, "Failed to cancel vibration.", e); |
| 95 | } |
| 96 | } |
| 97 | } |