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.VisibleForTesting; | 13 import org.chromium.base.annotations.CalledByNative; |
14 import org.chromium.base.annotations.JNINamespace; | |
14 import org.chromium.device.mojom.VibrationManager; | 15 import org.chromium.device.mojom.VibrationManager; |
15 import org.chromium.mojo.system.MojoException; | 16 import org.chromium.mojo.system.MojoException; |
16 import org.chromium.services.service_manager.InterfaceFactory; | 17 import org.chromium.services.service_manager.InterfaceFactory; |
17 | 18 |
18 /** | 19 /** |
19 * Android implementation of the vibration manager service defined in | 20 * Android implementation of the vibration manager service defined in |
20 * device/vibration/vibration_manager.mojom. | 21 * device/vibration/vibration_manager.mojom. |
21 */ | 22 */ |
23 @JNINamespace("device") | |
22 public class VibrationManagerImpl implements VibrationManager { | 24 public class VibrationManagerImpl implements VibrationManager { |
23 private static final String TAG = "VibrationManagerImpl"; | 25 private static final String TAG = "VibrationManagerImpl"; |
24 | 26 |
25 private static final long MINIMUM_VIBRATION_DURATION_MS = 1; // 1 millisecon d | 27 private static final long MINIMUM_VIBRATION_DURATION_MS = 1; // 1 millisecon d |
26 private static final long MAXIMUM_VIBRATION_DURATION_MS = 10000; // 10 secon ds | 28 private static final long MAXIMUM_VIBRATION_DURATION_MS = 10000; // 10 secon ds |
27 | 29 |
28 private final AudioManager mAudioManager; | 30 private final AudioManager mAudioManager; |
29 private final Vibrator mVibrator; | 31 private final Vibrator mVibrator; |
30 private final boolean mHasVibratePermission; | 32 private final boolean mHasVibratePermission; |
31 | 33 |
32 private static AndroidVibratorWrapper sVibratorWrapper; | 34 private static long sVibrateMilliSecondsForTesting = -1; |
33 | 35 private static boolean sVibrateCancelledForTesting = false; |
timvolodine
2017/04/05 17:16:28
Is there any particular reason for eliminating the
leonhsl(Using Gerrit)
2017/04/06 03:19:16
We were creating/setting this AndroidVibratorWrapp
| |
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 } | |
54 | 36 |
55 public VibrationManagerImpl(Context context) { | 37 public VibrationManagerImpl(Context context) { |
56 mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SE RVICE); | 38 mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SE RVICE); |
57 mVibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE ); | 39 mVibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE ); |
58 if (sVibratorWrapper == null) { | |
59 sVibratorWrapper = new AndroidVibratorWrapper(); | |
60 } | |
61 // TODO(mvanouwerkerk): What happens if permission is revoked? Handle th is better. | 40 // TODO(mvanouwerkerk): What happens if permission is revoked? Handle th is better. |
62 mHasVibratePermission = | 41 mHasVibratePermission = |
63 context.checkCallingOrSelfPermission(android.Manifest.permission .VIBRATE) | 42 context.checkCallingOrSelfPermission(android.Manifest.permission .VIBRATE) |
64 == PackageManager.PERMISSION_GRANTED; | 43 == PackageManager.PERMISSION_GRANTED; |
65 if (!mHasVibratePermission) { | 44 if (!mHasVibratePermission) { |
66 Log.w(TAG, "Failed to use vibrate API, requires VIBRATE permission." ); | 45 Log.w(TAG, "Failed to use vibrate API, requires VIBRATE permission." ); |
67 } | 46 } |
68 } | 47 } |
69 | 48 |
70 @Override | 49 @Override |
71 public void close() {} | 50 public void close() {} |
72 | 51 |
73 @Override | 52 @Override |
74 public void onConnectionError(MojoException e) {} | 53 public void onConnectionError(MojoException e) {} |
75 | 54 |
76 @Override | 55 @Override |
77 public void vibrate(long milliseconds, VibrateResponse callback) { | 56 public void vibrate(long milliseconds, VibrateResponse callback) { |
78 // Though the Blink implementation already sanitizes vibration times, do n't | 57 // Though the Blink implementation already sanitizes vibration times, do n't |
79 // trust any values passed from the client. | 58 // trust any values passed from the client. |
80 long sanitizedMilliseconds = Math.max(MINIMUM_VIBRATION_DURATION_MS, | 59 long sanitizedMilliseconds = Math.max(MINIMUM_VIBRATION_DURATION_MS, |
81 Math.min(milliseconds, MAXIMUM_VIBRATION_DURATION_MS)); | 60 Math.min(milliseconds, MAXIMUM_VIBRATION_DURATION_MS)); |
82 | 61 |
83 if (mAudioManager.getRingerMode() != AudioManager.RINGER_MODE_SILENT | 62 if (mAudioManager.getRingerMode() != AudioManager.RINGER_MODE_SILENT |
84 && mHasVibratePermission) { | 63 && mHasVibratePermission) { |
85 sVibratorWrapper.vibrate(mVibrator, sanitizedMilliseconds); | 64 mVibrator.vibrate(sanitizedMilliseconds); |
timvolodine
2017/04/07 10:39:07
Will the device actually vibrate when testing?
leonhsl(Using Gerrit)
2017/04/08 00:51:01
Nope :) The test app service_unittests has no Vibr
| |
86 } | 65 } |
66 setVibrateMilliSecondsForTesting(sanitizedMilliseconds); | |
timvolodine
2017/04/07 10:39:07
I believe we generally try to avoid mixing product
leonhsl(Using Gerrit)
2017/04/08 00:51:01
Yeah.. Having no better idea to handle such case t
| |
87 callback.call(); | 67 callback.call(); |
88 } | 68 } |
89 | 69 |
90 @Override | 70 @Override |
91 public void cancel(CancelResponse callback) { | 71 public void cancel(CancelResponse callback) { |
92 if (mHasVibratePermission) sVibratorWrapper.cancel(mVibrator); | 72 if (mHasVibratePermission) { |
73 mVibrator.cancel(); | |
74 } | |
75 setVibrateCancelledForTesting(true); | |
93 callback.call(); | 76 callback.call(); |
94 } | 77 } |
95 | 78 |
96 /** | 79 /** |
97 * A factory for implementations of the VibrationManager interface. | 80 * A factory for implementations of the VibrationManager interface. |
98 */ | 81 */ |
99 public static class Factory implements InterfaceFactory<VibrationManager> { | 82 public static class Factory implements InterfaceFactory<VibrationManager> { |
100 private Context mContext; | 83 private Context mContext; |
101 public Factory(Context context) { | 84 public Factory(Context context) { |
102 mContext = context; | 85 mContext = context; |
103 } | 86 } |
104 | 87 |
105 @Override | 88 @Override |
106 public VibrationManager createImpl() { | 89 public VibrationManager createImpl() { |
107 return new VibrationManagerImpl(mContext); | 90 return new VibrationManagerImpl(mContext); |
108 } | 91 } |
109 } | 92 } |
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 } | |
110 } | 111 } |
OLD | NEW |