SystemVibrator.java revision d8e1dbb6bc1fbaf4f2e38c3ba92ced94270deaac
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 String mPackageName;
31    private final IVibratorService mService;
32    private final Binder mToken = new Binder();
33
34    public SystemVibrator() {
35        mPackageName = null;
36        mService = IVibratorService.Stub.asInterface(
37                ServiceManager.getService("vibrator"));
38    }
39
40    public SystemVibrator(Context context) {
41        mPackageName = context.getBasePackageName();
42        mService = IVibratorService.Stub.asInterface(
43                ServiceManager.getService("vibrator"));
44    }
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
59    @Override
60    public void vibrate(long milliseconds) {
61        if (mService == null) {
62            Log.w(TAG, "Failed to vibrate; no vibrator service.");
63            return;
64        }
65        try {
66            mService.vibrate(mPackageName, milliseconds, mToken);
67        } catch (RemoteException e) {
68            Log.w(TAG, "Failed to vibrate.", e);
69        }
70    }
71
72    @Override
73    public void vibrate(long[] pattern, int repeat) {
74        if (mService == null) {
75            Log.w(TAG, "Failed to vibrate; no vibrator service.");
76            return;
77        }
78        // catch this here because the server will do nothing.  pattern may
79        // not be null, let that be checked, because the server will drop it
80        // anyway
81        if (repeat < pattern.length) {
82            try {
83                mService.vibratePattern(mPackageName, pattern, repeat, mToken);
84            } catch (RemoteException e) {
85                Log.w(TAG, "Failed to vibrate.", e);
86            }
87        } else {
88            throw new ArrayIndexOutOfBoundsException();
89        }
90    }
91
92    @Override
93    public void cancel() {
94        if (mService == null) {
95            return;
96        }
97        try {
98            mService.cancelVibrate(mToken);
99        } catch (RemoteException e) {
100            Log.w(TAG, "Failed to cancel vibration.", e);
101        }
102    }
103}
104