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 | |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 19 | import android.content.Context; |
John Spurlock | 7b41467 | 2014-07-18 13:02:39 -0400 | [diff] [blame] | 20 | import android.media.AudioAttributes; |
Jeff Brown | c234613 | 2012-04-13 01:55:38 -0700 | [diff] [blame] | 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 IVibratorService mService; |
| 32 | private final Binder mToken = new Binder(); |
| 33 | |
| 34 | public SystemVibrator() { |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 35 | mService = IVibratorService.Stub.asInterface(ServiceManager.getService("vibrator")); |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | public SystemVibrator(Context context) { |
John Spurlock | 1af30c7 | 2014-03-10 08:33:35 -0400 | [diff] [blame] | 39 | super(context); |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 40 | mService = IVibratorService.Stub.asInterface(ServiceManager.getService("vibrator")); |
Jeff Brown | c234613 | 2012-04-13 01:55:38 -0700 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | @Override |
| 44 | public boolean hasVibrator() { |
| 45 | if (mService == null) { |
| 46 | Log.w(TAG, "Failed to vibrate; no vibrator service."); |
| 47 | return false; |
| 48 | } |
| 49 | try { |
| 50 | return mService.hasVibrator(); |
| 51 | } catch (RemoteException e) { |
| 52 | } |
| 53 | return false; |
| 54 | } |
| 55 | |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 56 | @Override |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 57 | public boolean hasAmplitudeControl() { |
| 58 | if (mService == null) { |
| 59 | Log.w(TAG, "Failed to check amplitude control; no vibrator service."); |
| 60 | return false; |
| 61 | } |
| 62 | try { |
| 63 | return mService.hasAmplitudeControl(); |
| 64 | } catch (RemoteException e) { |
| 65 | } |
| 66 | return false; |
| 67 | } |
| 68 | |
| 69 | @Override |
Alexey Kuzmin | e1f06b8 | 2018-06-20 17:48:43 +0100 | [diff] [blame] | 70 | public void vibrate(int uid, String opPkg, VibrationEffect effect, |
| 71 | String reason, AudioAttributes attributes) { |
Jeff Brown | c234613 | 2012-04-13 01:55:38 -0700 | [diff] [blame] | 72 | if (mService == null) { |
| 73 | Log.w(TAG, "Failed to vibrate; no vibrator service."); |
| 74 | return; |
| 75 | } |
| 76 | try { |
Alexey Kuzmin | e1f06b8 | 2018-06-20 17:48:43 +0100 | [diff] [blame] | 77 | mService.vibrate(uid, opPkg, effect, usageForAttributes(attributes), reason, mToken); |
Jeff Brown | c234613 | 2012-04-13 01:55:38 -0700 | [diff] [blame] | 78 | } catch (RemoteException e) { |
| 79 | Log.w(TAG, "Failed to vibrate.", e); |
| 80 | } |
| 81 | } |
| 82 | |
John Spurlock | 7b41467 | 2014-07-18 13:02:39 -0400 | [diff] [blame] | 83 | private static int usageForAttributes(AudioAttributes attributes) { |
| 84 | return attributes != null ? attributes.getUsage() : AudioAttributes.USAGE_UNKNOWN; |
| 85 | } |
| 86 | |
Jeff Brown | c234613 | 2012-04-13 01:55:38 -0700 | [diff] [blame] | 87 | @Override |
| 88 | public void cancel() { |
| 89 | if (mService == null) { |
| 90 | return; |
| 91 | } |
| 92 | try { |
| 93 | mService.cancelVibrate(mToken); |
| 94 | } catch (RemoteException e) { |
| 95 | Log.w(TAG, "Failed to cancel vibration.", e); |
| 96 | } |
| 97 | } |
| 98 | } |