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