blob: 8d9cf545496a10146e2922f86c6dd95442cf6729 [file] [log] [blame]
Jeff Brownc2346132012-04-13 01:55:38 -07001/*
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
17package android.os;
18
Dianne Hackborna06de0f2012-12-11 16:34:47 -080019import android.content.Context;
Jeff Brownc2346132012-04-13 01:55:38 -070020import android.util.Log;
21
22/**
23 * Vibrator implementation that controls the main system vibrator.
24 *
25 * @hide
26 */
27public 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 Hackborna06de0f2012-12-11 16:34:47 -080034 mService = IVibratorService.Stub.asInterface(
35 ServiceManager.getService("vibrator"));
36 }
37
38 public SystemVibrator(Context context) {
John Spurlock1af30c72014-03-10 08:33:35 -040039 super(context);
Jeff Brownc2346132012-04-13 01:55:38 -070040 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 Hackbornf265ea92013-01-31 15:00:51 -080057 /**
58 * @hide
59 */
60 @Override
Christoph Studer8fd7f1e2014-04-11 17:35:05 -040061 public void vibrate(int uid, String opPkg, long milliseconds, int streamHint) {
Jeff Brownc2346132012-04-13 01:55:38 -070062 if (mService == null) {
63 Log.w(TAG, "Failed to vibrate; no vibrator service.");
64 return;
65 }
66 try {
Christoph Studer8fd7f1e2014-04-11 17:35:05 -040067 mService.vibrate(uid, opPkg, milliseconds, streamHint, mToken);
Jeff Brownc2346132012-04-13 01:55:38 -070068 } catch (RemoteException e) {
69 Log.w(TAG, "Failed to vibrate.", e);
70 }
71 }
72
Dianne Hackbornf265ea92013-01-31 15:00:51 -080073 /**
74 * @hide
75 */
Jeff Brownc2346132012-04-13 01:55:38 -070076 @Override
Christoph Studer8fd7f1e2014-04-11 17:35:05 -040077 public void vibrate(int uid, String opPkg, long[] pattern, int repeat,
John Spurlock1af30c72014-03-10 08:33:35 -040078 int streamHint) {
Jeff Brownc2346132012-04-13 01:55:38 -070079 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 Studer8fd7f1e2014-04-11 17:35:05 -040088 mService.vibratePattern(uid, opPkg, pattern, repeat, streamHint,
John Spurlock1af30c72014-03-10 08:33:35 -040089 mToken);
Jeff Brownc2346132012-04-13 01:55:38 -070090 } 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}