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() { |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 35 | mService = IVibratorService.Stub.asInterface( |
| 36 | ServiceManager.getService("vibrator")); |
| 37 | } |
| 38 | |
| 39 | public SystemVibrator(Context context) { |
John Spurlock | 1af30c7 | 2014-03-10 08:33:35 -0400 | [diff] [blame] | 40 | super(context); |
Jeff Brown | c234613 | 2012-04-13 01:55:38 -0700 | [diff] [blame] | 41 | mService = IVibratorService.Stub.asInterface( |
| 42 | ServiceManager.getService("vibrator")); |
| 43 | } |
| 44 | |
| 45 | @Override |
| 46 | public boolean hasVibrator() { |
| 47 | if (mService == null) { |
| 48 | Log.w(TAG, "Failed to vibrate; no vibrator service."); |
| 49 | return false; |
| 50 | } |
| 51 | try { |
| 52 | return mService.hasVibrator(); |
| 53 | } catch (RemoteException e) { |
| 54 | } |
| 55 | return false; |
| 56 | } |
| 57 | |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 58 | /** |
| 59 | * @hide |
| 60 | */ |
| 61 | @Override |
John Spurlock | 7b41467 | 2014-07-18 13:02:39 -0400 | [diff] [blame] | 62 | public void vibrate(int uid, String opPkg, long milliseconds, AudioAttributes attributes) { |
Jeff Brown | c234613 | 2012-04-13 01:55:38 -0700 | [diff] [blame] | 63 | if (mService == null) { |
| 64 | Log.w(TAG, "Failed to vibrate; no vibrator service."); |
| 65 | return; |
| 66 | } |
| 67 | try { |
John Spurlock | 7b41467 | 2014-07-18 13:02:39 -0400 | [diff] [blame] | 68 | mService.vibrate(uid, opPkg, milliseconds, usageForAttributes(attributes), mToken); |
Jeff Brown | c234613 | 2012-04-13 01:55:38 -0700 | [diff] [blame] | 69 | } catch (RemoteException e) { |
| 70 | Log.w(TAG, "Failed to vibrate.", e); |
| 71 | } |
| 72 | } |
| 73 | |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 74 | /** |
| 75 | * @hide |
| 76 | */ |
Jeff Brown | c234613 | 2012-04-13 01:55:38 -0700 | [diff] [blame] | 77 | @Override |
Christoph Studer | 8fd7f1e | 2014-04-11 17:35:05 -0400 | [diff] [blame] | 78 | public void vibrate(int uid, String opPkg, long[] pattern, int repeat, |
John Spurlock | 7b41467 | 2014-07-18 13:02:39 -0400 | [diff] [blame] | 79 | AudioAttributes attributes) { |
Jeff Brown | c234613 | 2012-04-13 01:55:38 -0700 | [diff] [blame] | 80 | if (mService == null) { |
| 81 | Log.w(TAG, "Failed to vibrate; no vibrator service."); |
| 82 | return; |
| 83 | } |
| 84 | // catch this here because the server will do nothing. pattern may |
| 85 | // not be null, let that be checked, because the server will drop it |
| 86 | // anyway |
| 87 | if (repeat < pattern.length) { |
| 88 | try { |
John Spurlock | 7b41467 | 2014-07-18 13:02:39 -0400 | [diff] [blame] | 89 | mService.vibratePattern(uid, opPkg, pattern, repeat, usageForAttributes(attributes), |
John Spurlock | 1af30c7 | 2014-03-10 08:33:35 -0400 | [diff] [blame] | 90 | mToken); |
Jeff Brown | c234613 | 2012-04-13 01:55:38 -0700 | [diff] [blame] | 91 | } catch (RemoteException e) { |
| 92 | Log.w(TAG, "Failed to vibrate.", e); |
| 93 | } |
| 94 | } else { |
| 95 | throw new ArrayIndexOutOfBoundsException(); |
| 96 | } |
| 97 | } |
| 98 | |
John Spurlock | 7b41467 | 2014-07-18 13:02:39 -0400 | [diff] [blame] | 99 | private static int usageForAttributes(AudioAttributes attributes) { |
| 100 | return attributes != null ? attributes.getUsage() : AudioAttributes.USAGE_UNKNOWN; |
| 101 | } |
| 102 | |
Jeff Brown | c234613 | 2012-04-13 01:55:38 -0700 | [diff] [blame] | 103 | @Override |
| 104 | public void cancel() { |
| 105 | if (mService == null) { |
| 106 | return; |
| 107 | } |
| 108 | try { |
| 109 | mService.cancelVibrate(mToken); |
| 110 | } catch (RemoteException e) { |
| 111 | Log.w(TAG, "Failed to cancel vibration.", e); |
| 112 | } |
| 113 | } |
| 114 | } |