The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2006 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 | |
John Spurlock | 1af30c7 | 2014-03-10 08:33:35 -0400 | [diff] [blame] | 19 | import android.app.ActivityThread; |
Jeff Brown | c234613 | 2012-04-13 01:55:38 -0700 | [diff] [blame] | 20 | import android.content.Context; |
John Spurlock | 7b41467 | 2014-07-18 13:02:39 -0400 | [diff] [blame] | 21 | import android.media.AudioAttributes; |
Brad Fitzpatrick | e331644 | 2010-10-14 19:40:56 -0700 | [diff] [blame] | 22 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 23 | /** |
| 24 | * Class that operates the vibrator on the device. |
| 25 | * <p> |
John Spurlock | 0f49c28 | 2014-03-10 11:29:35 -0400 | [diff] [blame] | 26 | * If your process exits, any vibration you started will stop. |
Jeff Brown | d10bfe1 | 2010-12-14 13:23:10 -0800 | [diff] [blame] | 27 | * </p> |
Jeff Brown | c234613 | 2012-04-13 01:55:38 -0700 | [diff] [blame] | 28 | * |
| 29 | * To obtain an instance of the system vibrator, call |
John Spurlock | 0f49c28 | 2014-03-10 11:29:35 -0400 | [diff] [blame] | 30 | * {@link Context#getSystemService} with {@link Context#VIBRATOR_SERVICE} as the argument. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 31 | */ |
Jeff Brown | c234613 | 2012-04-13 01:55:38 -0700 | [diff] [blame] | 32 | public abstract class Vibrator { |
John Spurlock | 1af30c7 | 2014-03-10 08:33:35 -0400 | [diff] [blame] | 33 | |
| 34 | private final String mPackageName; |
| 35 | |
Jeff Brown | c234613 | 2012-04-13 01:55:38 -0700 | [diff] [blame] | 36 | /** |
| 37 | * @hide to prevent subclassing from outside of the framework |
| 38 | */ |
| 39 | public Vibrator() { |
John Spurlock | 1af30c7 | 2014-03-10 08:33:35 -0400 | [diff] [blame] | 40 | mPackageName = ActivityThread.currentPackageName(); |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * @hide to prevent subclassing from outside of the framework |
| 45 | */ |
| 46 | protected Vibrator(Context context) { |
| 47 | mPackageName = context.getOpPackageName(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | /** |
Jeff Brown | c234613 | 2012-04-13 01:55:38 -0700 | [diff] [blame] | 51 | * Check whether the hardware has a vibrator. |
| 52 | * |
| 53 | * @return True if the hardware has a vibrator, else false. |
Dianne Hackborn | ea9020e | 2010-11-04 11:39:12 -0700 | [diff] [blame] | 54 | */ |
Jeff Brown | c234613 | 2012-04-13 01:55:38 -0700 | [diff] [blame] | 55 | public abstract boolean hasVibrator(); |
John Spurlock | 1af30c7 | 2014-03-10 08:33:35 -0400 | [diff] [blame] | 56 | |
Dianne Hackborn | ea9020e | 2010-11-04 11:39:12 -0700 | [diff] [blame] | 57 | /** |
Jeff Brown | c234613 | 2012-04-13 01:55:38 -0700 | [diff] [blame] | 58 | * Vibrate constantly for the specified period of time. |
Nicolas Falliere | 9530e3a | 2012-06-18 17:21:06 -0700 | [diff] [blame] | 59 | * <p>This method requires the caller to hold the permission |
| 60 | * {@link android.Manifest.permission#VIBRATE}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 61 | * |
Jeff Brown | d10bfe1 | 2010-12-14 13:23:10 -0800 | [diff] [blame] | 62 | * @param milliseconds The number of milliseconds to vibrate. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 63 | */ |
John Spurlock | 1af30c7 | 2014-03-10 08:33:35 -0400 | [diff] [blame] | 64 | public void vibrate(long milliseconds) { |
John Spurlock | 7b41467 | 2014-07-18 13:02:39 -0400 | [diff] [blame] | 65 | vibrate(milliseconds, null); |
John Spurlock | 1af30c7 | 2014-03-10 08:33:35 -0400 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Vibrate constantly for the specified period of time. |
| 70 | * <p>This method requires the caller to hold the permission |
| 71 | * {@link android.Manifest.permission#VIBRATE}. |
| 72 | * |
| 73 | * @param milliseconds The number of milliseconds to vibrate. |
John Spurlock | 7b41467 | 2014-07-18 13:02:39 -0400 | [diff] [blame] | 74 | * @param attributes {@link AudioAttributes} corresponding to the vibration. For example, |
| 75 | * specify {@link AudioAttributes#USAGE_ALARM} for alarm vibrations or |
Jean-Michel Trivi | 89c3b29 | 2014-07-20 11:41:02 -0700 | [diff] [blame] | 76 | * {@link AudioAttributes#USAGE_NOTIFICATION_RINGTONE} for |
John Spurlock | 7b41467 | 2014-07-18 13:02:39 -0400 | [diff] [blame] | 77 | * vibrations associated with incoming calls. |
John Spurlock | 1af30c7 | 2014-03-10 08:33:35 -0400 | [diff] [blame] | 78 | */ |
John Spurlock | 7b41467 | 2014-07-18 13:02:39 -0400 | [diff] [blame] | 79 | public void vibrate(long milliseconds, AudioAttributes attributes) { |
| 80 | vibrate(Process.myUid(), mPackageName, milliseconds, attributes); |
John Spurlock | 1af30c7 | 2014-03-10 08:33:35 -0400 | [diff] [blame] | 81 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 82 | |
| 83 | /** |
| 84 | * Vibrate with a given pattern. |
| 85 | * |
| 86 | * <p> |
Jeff Brown | d10bfe1 | 2010-12-14 13:23:10 -0800 | [diff] [blame] | 87 | * Pass in an array of ints that are the durations for which to turn on or off |
| 88 | * the vibrator in milliseconds. The first value indicates the number of milliseconds |
| 89 | * to wait before turning the vibrator on. The next value indicates the number of milliseconds |
| 90 | * for which to keep the vibrator on before turning it off. Subsequent values alternate |
| 91 | * between durations in milliseconds to turn the vibrator off or to turn the vibrator on. |
| 92 | * </p><p> |
| 93 | * To cause the pattern to repeat, pass the index into the pattern array at which |
| 94 | * to start the repeat, or -1 to disable repeating. |
| 95 | * </p> |
Nicolas Falliere | 9530e3a | 2012-06-18 17:21:06 -0700 | [diff] [blame] | 96 | * <p>This method requires the caller to hold the permission |
| 97 | * {@link android.Manifest.permission#VIBRATE}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 98 | * |
Jeff Brown | d10bfe1 | 2010-12-14 13:23:10 -0800 | [diff] [blame] | 99 | * @param pattern an array of longs of times for which to turn the vibrator on or off. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 100 | * @param repeat the index into pattern at which to repeat, or -1 if |
| 101 | * you don't want to repeat. |
| 102 | */ |
John Spurlock | 1af30c7 | 2014-03-10 08:33:35 -0400 | [diff] [blame] | 103 | public void vibrate(long[] pattern, int repeat) { |
John Spurlock | 7b41467 | 2014-07-18 13:02:39 -0400 | [diff] [blame] | 104 | vibrate(pattern, repeat, null); |
John Spurlock | 1af30c7 | 2014-03-10 08:33:35 -0400 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Vibrate with a given pattern. |
| 109 | * |
| 110 | * <p> |
| 111 | * Pass in an array of ints that are the durations for which to turn on or off |
| 112 | * the vibrator in milliseconds. The first value indicates the number of milliseconds |
| 113 | * to wait before turning the vibrator on. The next value indicates the number of milliseconds |
| 114 | * for which to keep the vibrator on before turning it off. Subsequent values alternate |
| 115 | * between durations in milliseconds to turn the vibrator off or to turn the vibrator on. |
| 116 | * </p><p> |
| 117 | * To cause the pattern to repeat, pass the index into the pattern array at which |
| 118 | * to start the repeat, or -1 to disable repeating. |
| 119 | * </p> |
| 120 | * <p>This method requires the caller to hold the permission |
| 121 | * {@link android.Manifest.permission#VIBRATE}. |
| 122 | * |
| 123 | * @param pattern an array of longs of times for which to turn the vibrator on or off. |
| 124 | * @param repeat the index into pattern at which to repeat, or -1 if |
| 125 | * you don't want to repeat. |
John Spurlock | 7b41467 | 2014-07-18 13:02:39 -0400 | [diff] [blame] | 126 | * @param attributes {@link AudioAttributes} corresponding to the vibration. For example, |
| 127 | * specify {@link AudioAttributes#USAGE_ALARM} for alarm vibrations or |
Jean-Michel Trivi | 89c3b29 | 2014-07-20 11:41:02 -0700 | [diff] [blame] | 128 | * {@link AudioAttributes#USAGE_NOTIFICATION_RINGTONE} for |
John Spurlock | 7b41467 | 2014-07-18 13:02:39 -0400 | [diff] [blame] | 129 | * vibrations associated with incoming calls. |
John Spurlock | 1af30c7 | 2014-03-10 08:33:35 -0400 | [diff] [blame] | 130 | */ |
John Spurlock | 7b41467 | 2014-07-18 13:02:39 -0400 | [diff] [blame] | 131 | public void vibrate(long[] pattern, int repeat, AudioAttributes attributes) { |
| 132 | vibrate(Process.myUid(), mPackageName, pattern, repeat, attributes); |
John Spurlock | 1af30c7 | 2014-03-10 08:33:35 -0400 | [diff] [blame] | 133 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 134 | |
| 135 | /** |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 136 | * @hide |
John Spurlock | 7b41467 | 2014-07-18 13:02:39 -0400 | [diff] [blame] | 137 | * Like {@link #vibrate(long, AudioAttributes)}, but allowing the caller to specify that |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 138 | * the vibration is owned by someone else. |
| 139 | */ |
John Spurlock | 7b41467 | 2014-07-18 13:02:39 -0400 | [diff] [blame] | 140 | public abstract void vibrate(int uid, String opPkg, long milliseconds, |
| 141 | AudioAttributes attributes); |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 142 | |
| 143 | /** |
| 144 | * @hide |
John Spurlock | 7b41467 | 2014-07-18 13:02:39 -0400 | [diff] [blame] | 145 | * Like {@link #vibrate(long[], int, AudioAttributes)}, but allowing the caller to specify that |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 146 | * the vibration is owned by someone else. |
| 147 | */ |
John Spurlock | 7b41467 | 2014-07-18 13:02:39 -0400 | [diff] [blame] | 148 | public abstract void vibrate(int uid, String opPkg, long[] pattern, int repeat, |
| 149 | AudioAttributes attributes); |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 150 | |
| 151 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 152 | * Turn the vibrator off. |
Nicolas Falliere | 9530e3a | 2012-06-18 17:21:06 -0700 | [diff] [blame] | 153 | * <p>This method requires the caller to hold the permission |
| 154 | * {@link android.Manifest.permission#VIBRATE}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 155 | */ |
Jeff Brown | c234613 | 2012-04-13 01:55:38 -0700 | [diff] [blame] | 156 | public abstract void cancel(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 157 | } |