• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
19 import android.annotation.UnsupportedAppUsage;
20 import android.content.Context;
21 import android.media.AudioAttributes;
22 import android.util.Log;
23 
24 /**
25  * Vibrator implementation that controls the main system vibrator.
26  *
27  * @hide
28  */
29 public 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 
35     @UnsupportedAppUsage
SystemVibrator()36     public SystemVibrator() {
37         mService = IVibratorService.Stub.asInterface(ServiceManager.getService("vibrator"));
38     }
39 
40     @UnsupportedAppUsage
SystemVibrator(Context context)41     public SystemVibrator(Context context) {
42         super(context);
43         mService = IVibratorService.Stub.asInterface(ServiceManager.getService("vibrator"));
44     }
45 
46     @Override
hasVibrator()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 
59     @Override
hasAmplitudeControl()60     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
vibrate(int uid, String opPkg, VibrationEffect effect, String reason, AudioAttributes attributes)73     public void vibrate(int uid, String opPkg, VibrationEffect effect,
74             String reason, AudioAttributes attributes) {
75         if (mService == null) {
76             Log.w(TAG, "Failed to vibrate; no vibrator service.");
77             return;
78         }
79         try {
80             mService.vibrate(uid, opPkg, effect, usageForAttributes(attributes), reason, mToken);
81         } catch (RemoteException e) {
82             Log.w(TAG, "Failed to vibrate.", e);
83         }
84     }
85 
usageForAttributes(AudioAttributes attributes)86     private static int usageForAttributes(AudioAttributes attributes) {
87         return attributes != null ? attributes.getUsage() : AudioAttributes.USAGE_UNKNOWN;
88     }
89 
90     @Override
cancel()91     public void cancel() {
92         if (mService == null) {
93             return;
94         }
95         try {
96             mService.cancelVibrate(mToken);
97         } catch (RemoteException e) {
98             Log.w(TAG, "Failed to cancel vibration.", e);
99         }
100     }
101 }
102