SystemVibrator.java revision 8fd7f1ed7c11d35b3f2a97878e68ee38a551dd15
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
17package android.os;
18
19import android.content.Context;
20import 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() {
34        mService = IVibratorService.Stub.asInterface(
35                ServiceManager.getService("vibrator"));
36    }
37
38    public SystemVibrator(Context context) {
39        super(context);
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
57    /**
58     * @hide
59     */
60    @Override
61    public void vibrate(int uid, String opPkg, long milliseconds, int streamHint) {
62        if (mService == null) {
63            Log.w(TAG, "Failed to vibrate; no vibrator service.");
64            return;
65        }
66        try {
67            mService.vibrate(uid, opPkg, milliseconds, streamHint, mToken);
68        } catch (RemoteException e) {
69            Log.w(TAG, "Failed to vibrate.", e);
70        }
71    }
72
73    /**
74     * @hide
75     */
76    @Override
77    public void vibrate(int uid, String opPkg, long[] pattern, int repeat,
78            int streamHint) {
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 {
88                mService.vibratePattern(uid, opPkg, pattern, repeat, streamHint,
89                        mToken);
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}
110