blob: a5188e7cd58d08b32a6e465fd9f380aeec936d4f [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
Andrei Onea24ec3212019-03-15 17:35:05 +000019import android.annotation.UnsupportedAppUsage;
Dianne Hackborna06de0f2012-12-11 16:34:47 -080020import android.content.Context;
John Spurlock7b414672014-07-18 13:02:39 -040021import android.media.AudioAttributes;
Jeff Brownc2346132012-04-13 01:55:38 -070022import android.util.Log;
23
24/**
25 * Vibrator implementation that controls the main system vibrator.
26 *
27 * @hide
28 */
29public 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 Onea24ec3212019-03-15 17:35:05 +000035 @UnsupportedAppUsage
Jeff Brownc2346132012-04-13 01:55:38 -070036 public SystemVibrator() {
Michael Wright71216972017-01-31 18:33:54 +000037 mService = IVibratorService.Stub.asInterface(ServiceManager.getService("vibrator"));
Dianne Hackborna06de0f2012-12-11 16:34:47 -080038 }
39
Andrei Onea24ec3212019-03-15 17:35:05 +000040 @UnsupportedAppUsage
Dianne Hackborna06de0f2012-12-11 16:34:47 -080041 public SystemVibrator(Context context) {
John Spurlock1af30c72014-03-10 08:33:35 -040042 super(context);
Michael Wright71216972017-01-31 18:33:54 +000043 mService = IVibratorService.Stub.asInterface(ServiceManager.getService("vibrator"));
Jeff Brownc2346132012-04-13 01:55:38 -070044 }
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 Hackbornf265ea92013-01-31 15:00:51 -080059 @Override
Michael Wright71216972017-01-31 18:33:54 +000060 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 Kuzmine1f06b82018-06-20 17:48:43 +010073 public void vibrate(int uid, String opPkg, VibrationEffect effect,
74 String reason, AudioAttributes attributes) {
Jeff Brownc2346132012-04-13 01:55:38 -070075 if (mService == null) {
76 Log.w(TAG, "Failed to vibrate; no vibrator service.");
77 return;
78 }
79 try {
Michael Wright7f378782019-06-20 19:36:57 +010080 mService.vibrate(uid, opPkg, effect, attributes, reason, mToken);
Jeff Brownc2346132012-04-13 01:55:38 -070081 } catch (RemoteException e) {
82 Log.w(TAG, "Failed to vibrate.", e);
83 }
84 }
85
Jeff Brownc2346132012-04-13 01:55:38 -070086 @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}