Skip to content

Commit c234613

Browse files
author
Jeff Brown
committed
Extract Vibrator implementation from interface.
Moved the core logic of Vibrator into SystemVibrator, potentially allowing for the creation of other Vibrator subclasses. Fixed several places where we were creating new Vibrator instances unnecessarily instead of getting it from the Context. It is safe to make Vibrator abstract because its constructor was hidden from the SDK so it was not possible to subclass it. Bug: 6334179 Change-Id: I18ece6544c26a7efb2d5099f8346a10aef8a5e18
1 parent 069c4f3 commit c234613

File tree

11 files changed

+133
-88
lines changed

11 files changed

+133
-88
lines changed

api/16.txt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15190,11 +15190,11 @@ package android.os {
1519015190
method public abstract void released();
1519115191
}
1519215192

15193-
public class Vibrator {
15194-
method public void cancel();
15195-
method public boolean hasVibrator();
15196-
method public void vibrate(long);
15197-
method public void vibrate(long[], int);
15193+
public abstract class Vibrator {
15194+
method public abstract void cancel();
15195+
method public abstract boolean hasVibrator();
15196+
method public abstract void vibrate(long);
15197+
method public abstract void vibrate(long[], int);
1519815198
}
1519915199

1520015200
public class WorkSource implements android.os.Parcelable {

api/current.txt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15481,11 +15481,11 @@ package android.os {
1548115481
ctor public TransactionTooLargeException();
1548215482
}
1548315483

15484-
public class Vibrator {
15485-
method public void cancel();
15486-
method public boolean hasVibrator();
15487-
method public void vibrate(long);
15488-
method public void vibrate(long[], int);
15484+
public abstract class Vibrator {
15485+
method public abstract void cancel();
15486+
method public abstract boolean hasVibrator();
15487+
method public abstract void vibrate(long);
15488+
method public abstract void vibrate(long[], int);
1548915489
}
1549015490

1549115491
public class WorkSource implements android.os.Parcelable {

core/java/android/app/ContextImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@
8282
import android.os.RemoteException;
8383
import android.os.ServiceManager;
8484
import android.os.UserId;
85-
import android.os.Vibrator;
85+
import android.os.SystemVibrator;
8686
import android.os.storage.StorageManager;
8787
import android.telephony.TelephonyManager;
8888
import android.content.ClipboardManager;
@@ -455,7 +455,7 @@ public Object createService(ContextImpl ctx) {
455455

456456
registerService(VIBRATOR_SERVICE, new ServiceFetcher() {
457457
public Object createService(ContextImpl ctx) {
458-
return new Vibrator();
458+
return new SystemVibrator();
459459
}});
460460

461461
registerService(WALLPAPER_SERVICE, WALLPAPER_FETCHER);
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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.util.Log;
20+
21+
/**
22+
* Vibrator implementation that controls the main system vibrator.
23+
*
24+
* @hide
25+
*/
26+
public class SystemVibrator extends Vibrator {
27+
private static final String TAG = "Vibrator";
28+
29+
private final IVibratorService mService;
30+
private final Binder mToken = new Binder();
31+
32+
public SystemVibrator() {
33+
mService = IVibratorService.Stub.asInterface(
34+
ServiceManager.getService("vibrator"));
35+
}
36+
37+
@Override
38+
public boolean hasVibrator() {
39+
if (mService == null) {
40+
Log.w(TAG, "Failed to vibrate; no vibrator service.");
41+
return false;
42+
}
43+
try {
44+
return mService.hasVibrator();
45+
} catch (RemoteException e) {
46+
}
47+
return false;
48+
}
49+
50+
@Override
51+
public void vibrate(long milliseconds) {
52+
if (mService == null) {
53+
Log.w(TAG, "Failed to vibrate; no vibrator service.");
54+
return;
55+
}
56+
try {
57+
mService.vibrate(milliseconds, mToken);
58+
} catch (RemoteException e) {
59+
Log.w(TAG, "Failed to vibrate.", e);
60+
}
61+
}
62+
63+
@Override
64+
public void vibrate(long[] pattern, int repeat) {
65+
if (mService == null) {
66+
Log.w(TAG, "Failed to vibrate; no vibrator service.");
67+
return;
68+
}
69+
// catch this here because the server will do nothing. pattern may
70+
// not be null, let that be checked, because the server will drop it
71+
// anyway
72+
if (repeat < pattern.length) {
73+
try {
74+
mService.vibratePattern(pattern, repeat, mToken);
75+
} catch (RemoteException e) {
76+
Log.w(TAG, "Failed to vibrate.", e);
77+
}
78+
} else {
79+
throw new ArrayIndexOutOfBoundsException();
80+
}
81+
}
82+
83+
@Override
84+
public void cancel() {
85+
if (mService == null) {
86+
return;
87+
}
88+
try {
89+
mService.cancelVibrate(mToken);
90+
} catch (RemoteException e) {
91+
Log.w(TAG, "Failed to cancel vibration.", e);
92+
}
93+
}
94+
}

core/java/android/os/Vibrator.java

Lines changed: 17 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -16,61 +16,37 @@
1616

1717
package android.os;
1818

19-
import android.util.Log;
19+
import android.content.Context;
2020

2121
/**
2222
* Class that operates the vibrator on the device.
2323
* <p>
2424
* If your process exits, any vibration you started with will stop.
2525
* </p>
26+
*
27+
* To obtain an instance of the system vibrator, call
28+
* {@link Context#getSystemService} with {@link Context#VIBRATOR_SERVICE} as argument.
2629
*/
27-
public class Vibrator
28-
{
29-
private static final String TAG = "Vibrator";
30-
31-
IVibratorService mService;
32-
private final Binder mToken = new Binder();
33-
34-
/** @hide */
35-
public Vibrator()
36-
{
37-
mService = IVibratorService.Stub.asInterface(
38-
ServiceManager.getService("vibrator"));
30+
public abstract class Vibrator {
31+
/**
32+
* @hide to prevent subclassing from outside of the framework
33+
*/
34+
public Vibrator() {
3935
}
4036

4137
/**
42-
* Check whether the hardware has a vibrator. Returns true if a vibrator
43-
* exists, else false.
38+
* Check whether the hardware has a vibrator.
39+
*
40+
* @return True if the hardware has a vibrator, else false.
4441
*/
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-
}
42+
public abstract boolean hasVibrator();
5643

5744
/**
58-
* Turn the vibrator on.
45+
* Vibrate constantly for the specified period of time.
5946
*
6047
* @param milliseconds The number of milliseconds to vibrate.
6148
*/
62-
public void vibrate(long milliseconds)
63-
{
64-
if (mService == null) {
65-
Log.w(TAG, "Failed to vibrate; no vibrator service.");
66-
return;
67-
}
68-
try {
69-
mService.vibrate(milliseconds, mToken);
70-
} catch (RemoteException e) {
71-
Log.w(TAG, "Failed to vibrate.", e);
72-
}
73-
}
49+
public abstract void vibrate(long milliseconds);
7450

7551
/**
7652
* Vibrate with a given pattern.
@@ -90,38 +66,10 @@ public void vibrate(long milliseconds)
9066
* @param repeat the index into pattern at which to repeat, or -1 if
9167
* you don't want to repeat.
9268
*/
93-
public void vibrate(long[] pattern, int repeat)
94-
{
95-
if (mService == null) {
96-
Log.w(TAG, "Failed to vibrate; no vibrator service.");
97-
return;
98-
}
99-
// catch this here because the server will do nothing. pattern may
100-
// not be null, let that be checked, because the server will drop it
101-
// anyway
102-
if (repeat < pattern.length) {
103-
try {
104-
mService.vibratePattern(pattern, repeat, mToken);
105-
} catch (RemoteException e) {
106-
Log.w(TAG, "Failed to vibrate.", e);
107-
}
108-
} else {
109-
throw new ArrayIndexOutOfBoundsException();
110-
}
111-
}
69+
public abstract void vibrate(long[] pattern, int repeat);
11270

11371
/**
11472
* Turn the vibrator off.
11573
*/
116-
public void cancel()
117-
{
118-
if (mService == null) {
119-
return;
120-
}
121-
try {
122-
mService.cancelVibrate(mToken);
123-
} catch (RemoteException e) {
124-
Log.w(TAG, "Failed to cancel vibration.", e);
125-
}
126-
}
74+
public abstract void cancel();
12775
}

core/java/android/view/VolumePanel.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ public void onDismiss(DialogInterface dialog) {
263263
| LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH);
264264

265265
mToneGenerators = new ToneGenerator[AudioSystem.getNumStreamTypes()];
266-
mVibrator = new Vibrator();
266+
mVibrator = (Vibrator)context.getSystemService(Context.VIBRATOR_SERVICE);
267267

268268
mVoiceCapable = context.getResources().getBoolean(R.bool.config_voice_capable);
269269
mShowCombinedVolumes = !mVoiceCapable && !useMasterVolume;

core/java/com/android/internal/app/PlatLogoActivity.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@
2929
import android.widget.Toast;
3030

3131
public class PlatLogoActivity extends Activity {
32+
Vibrator mZzz;
3233
Toast mToast;
3334
ImageView mContent;
34-
Vibrator mZzz = new Vibrator();
3535
int mCount;
3636
final Handler mHandler = new Handler();
3737

@@ -63,7 +63,8 @@ public void run() {
6363
@Override
6464
protected void onCreate(Bundle savedInstanceState) {
6565
super.onCreate(savedInstanceState);
66-
66+
67+
mZzz = (Vibrator)getSystemService(VIBRATOR_SERVICE);
6768
mToast = Toast.makeText(this, "Android 4.0: Ice Cream Sandwich", Toast.LENGTH_SHORT);
6869

6970
mContent = new ImageView(this);

core/java/com/android/internal/app/ShutdownThread.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import android.os.SystemClock;
3838
import android.os.SystemProperties;
3939
import android.os.Vibrator;
40+
import android.os.SystemVibrator;
4041
import android.os.storage.IMountService;
4142
import android.os.storage.IMountShutdownObserver;
4243

@@ -399,7 +400,7 @@ public static void rebootOrShutdown(boolean reboot, String reason) {
399400
}
400401
} else if (SHUTDOWN_VIBRATE_MS > 0) {
401402
// vibrate before shutting down
402-
Vibrator vibrator = new Vibrator();
403+
Vibrator vibrator = new SystemVibrator();
403404
try {
404405
vibrator.vibrate(SHUTDOWN_VIBRATE_MS);
405406
} catch (Exception e) {

policy/src/com/android/internal/policy/impl/PhoneWindowManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -909,7 +909,7 @@ public void init(Context context, IWindowManager windowManager,
909909
mPluggedIn = (0 != intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0));
910910
}
911911

912-
mVibrator = new Vibrator();
912+
mVibrator = (Vibrator)context.getSystemService(Context.VIBRATOR_SERVICE);
913913
mLongPressVibePattern = getLongIntArray(mContext.getResources(),
914914
com.android.internal.R.array.config_longPressVibePattern);
915915
mVirtualKeyVibePattern = getLongIntArray(mContext.getResources(),

services/java/com/android/server/NotificationManagerService.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public class NotificationManagerService extends INotificationManager.Stub
100100
private int mDisabledNotifications;
101101

102102
private NotificationRecord mVibrateNotification;
103-
private Vibrator mVibrator = new Vibrator();
103+
private Vibrator mVibrator;
104104

105105
// for enabling and disabling notification pulse behavior
106106
private boolean mScreenOn = true;
@@ -398,6 +398,7 @@ public void update() {
398398
{
399399
super();
400400
mContext = context;
401+
mVibrator = (Vibrator)context.getSystemService(Context.VIBRATOR_SERVICE);
401402
mAm = ActivityManagerNative.getDefault();
402403
mSound = new NotificationPlayer(TAG);
403404
mSound.setUsesWakeLock(context);

0 commit comments

Comments
 (0)