OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.device.vibration; | 5 package org.chromium.device.vibration; |
6 | 6 |
7 import android.content.Context; | 7 import android.content.Context; |
8 import android.content.pm.PackageManager; | 8 import android.content.pm.PackageManager; |
9 import android.media.AudioManager; | 9 import android.media.AudioManager; |
10 import android.os.Vibrator; | 10 import android.os.Vibrator; |
11 import android.util.Log; | 11 import android.util.Log; |
12 | 12 |
13 import org.chromium.base.annotations.CalledByNative; | 13 import org.chromium.base.VisibleForTesting; |
14 import org.chromium.base.annotations.JNINamespace; | |
15 import org.chromium.device.mojom.VibrationManager; | 14 import org.chromium.device.mojom.VibrationManager; |
16 import org.chromium.mojo.system.MojoException; | 15 import org.chromium.mojo.system.MojoException; |
17 import org.chromium.services.service_manager.InterfaceFactory; | 16 import org.chromium.services.service_manager.InterfaceFactory; |
18 | 17 |
19 /** | 18 /** |
20 * Android implementation of the vibration manager service defined in | 19 * Android implementation of the vibration manager service defined in |
21 * device/vibration/vibration_manager.mojom. | 20 * device/vibration/vibration_manager.mojom. |
22 */ | 21 */ |
23 @JNINamespace("device") | |
24 public class VibrationManagerImpl implements VibrationManager { | 22 public class VibrationManagerImpl implements VibrationManager { |
25 private static final String TAG = "VibrationManagerImpl"; | 23 private static final String TAG = "VibrationManagerImpl"; |
26 | 24 |
27 private static final long MINIMUM_VIBRATION_DURATION_MS = 1; // 1 millisecon
d | 25 private static final long MINIMUM_VIBRATION_DURATION_MS = 1; // 1 millisecon
d |
28 private static final long MAXIMUM_VIBRATION_DURATION_MS = 10000; // 10 secon
ds | 26 private static final long MAXIMUM_VIBRATION_DURATION_MS = 10000; // 10 secon
ds |
29 | 27 |
30 private final AudioManager mAudioManager; | 28 private final AudioManager mAudioManager; |
31 private final Vibrator mVibrator; | 29 private final Vibrator mVibrator; |
32 private final boolean mHasVibratePermission; | 30 private final boolean mHasVibratePermission; |
33 | 31 |
34 private static long sVibrateMilliSecondsForTesting = -1; | 32 private static AndroidVibratorWrapper sVibratorWrapper; |
35 private static boolean sVibrateCancelledForTesting = false; | 33 |
| 34 /** |
| 35 * Android Vibrator wrapper class provided to test code to extend. |
| 36 */ |
| 37 @VisibleForTesting |
| 38 public static class AndroidVibratorWrapper { |
| 39 protected AndroidVibratorWrapper() {} |
| 40 |
| 41 public void vibrate(Vibrator vibrator, long milliseconds) { |
| 42 vibrator.vibrate(milliseconds); |
| 43 } |
| 44 |
| 45 public void cancel(Vibrator vibrator) { |
| 46 vibrator.cancel(); |
| 47 } |
| 48 } |
| 49 |
| 50 // Test code can use this function to inject other wrapper for testing. |
| 51 public static void setVibratorWrapperForTesting(AndroidVibratorWrapper wrapp
er) { |
| 52 sVibratorWrapper = wrapper; |
| 53 } |
36 | 54 |
37 public VibrationManagerImpl(Context context) { | 55 public VibrationManagerImpl(Context context) { |
38 mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SE
RVICE); | 56 mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SE
RVICE); |
39 mVibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE
); | 57 mVibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE
); |
| 58 if (sVibratorWrapper == null) { |
| 59 sVibratorWrapper = new AndroidVibratorWrapper(); |
| 60 } |
40 // TODO(mvanouwerkerk): What happens if permission is revoked? Handle th
is better. | 61 // TODO(mvanouwerkerk): What happens if permission is revoked? Handle th
is better. |
41 mHasVibratePermission = | 62 mHasVibratePermission = |
42 context.checkCallingOrSelfPermission(android.Manifest.permission
.VIBRATE) | 63 context.checkCallingOrSelfPermission(android.Manifest.permission
.VIBRATE) |
43 == PackageManager.PERMISSION_GRANTED; | 64 == PackageManager.PERMISSION_GRANTED; |
44 if (!mHasVibratePermission) { | 65 if (!mHasVibratePermission) { |
45 Log.w(TAG, "Failed to use vibrate API, requires VIBRATE permission."
); | 66 Log.w(TAG, "Failed to use vibrate API, requires VIBRATE permission."
); |
46 } | 67 } |
47 } | 68 } |
48 | 69 |
49 @Override | 70 @Override |
50 public void close() {} | 71 public void close() {} |
51 | 72 |
52 @Override | 73 @Override |
53 public void onConnectionError(MojoException e) {} | 74 public void onConnectionError(MojoException e) {} |
54 | 75 |
55 @Override | 76 @Override |
56 public void vibrate(long milliseconds, VibrateResponse callback) { | 77 public void vibrate(long milliseconds, VibrateResponse callback) { |
57 // Though the Blink implementation already sanitizes vibration times, do
n't | 78 // Though the Blink implementation already sanitizes vibration times, do
n't |
58 // trust any values passed from the client. | 79 // trust any values passed from the client. |
59 long sanitizedMilliseconds = Math.max(MINIMUM_VIBRATION_DURATION_MS, | 80 long sanitizedMilliseconds = Math.max(MINIMUM_VIBRATION_DURATION_MS, |
60 Math.min(milliseconds, MAXIMUM_VIBRATION_DURATION_MS)); | 81 Math.min(milliseconds, MAXIMUM_VIBRATION_DURATION_MS)); |
61 | 82 |
62 if (mAudioManager.getRingerMode() != AudioManager.RINGER_MODE_SILENT | 83 if (mAudioManager.getRingerMode() != AudioManager.RINGER_MODE_SILENT |
63 && mHasVibratePermission) { | 84 && mHasVibratePermission) { |
64 mVibrator.vibrate(sanitizedMilliseconds); | 85 sVibratorWrapper.vibrate(mVibrator, sanitizedMilliseconds); |
65 } | 86 } |
66 setVibrateMilliSecondsForTesting(sanitizedMilliseconds); | |
67 callback.call(); | 87 callback.call(); |
68 } | 88 } |
69 | 89 |
70 @Override | 90 @Override |
71 public void cancel(CancelResponse callback) { | 91 public void cancel(CancelResponse callback) { |
72 if (mHasVibratePermission) { | 92 if (mHasVibratePermission) sVibratorWrapper.cancel(mVibrator); |
73 mVibrator.cancel(); | |
74 } | |
75 setVibrateCancelledForTesting(true); | |
76 callback.call(); | 93 callback.call(); |
77 } | 94 } |
78 | 95 |
79 /** | 96 /** |
80 * A factory for implementations of the VibrationManager interface. | 97 * A factory for implementations of the VibrationManager interface. |
81 */ | 98 */ |
82 public static class Factory implements InterfaceFactory<VibrationManager> { | 99 public static class Factory implements InterfaceFactory<VibrationManager> { |
83 private Context mContext; | 100 private Context mContext; |
84 public Factory(Context context) { | 101 public Factory(Context context) { |
85 mContext = context; | 102 mContext = context; |
86 } | 103 } |
87 | 104 |
88 @Override | 105 @Override |
89 public VibrationManager createImpl() { | 106 public VibrationManager createImpl() { |
90 return new VibrationManagerImpl(mContext); | 107 return new VibrationManagerImpl(mContext); |
91 } | 108 } |
92 } | 109 } |
93 | |
94 static void setVibrateMilliSecondsForTesting(long milliseconds) { | |
95 sVibrateMilliSecondsForTesting = milliseconds; | |
96 } | |
97 | |
98 static void setVibrateCancelledForTesting(boolean cancelled) { | |
99 sVibrateCancelledForTesting = cancelled; | |
100 } | |
101 | |
102 @CalledByNative | |
103 static long getVibrateMilliSecondsForTesting() { | |
104 return sVibrateMilliSecondsForTesting; | |
105 } | |
106 | |
107 @CalledByNative | |
108 static boolean getVibrateCancelledForTesting() { | |
109 return sVibrateCancelledForTesting; | |
110 } | |
111 } | 110 } |
OLD | NEW |