Chromium Code Reviews| 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.mojo.system.MojoException; | 14 import org.chromium.mojo.system.MojoException; |
| 14 import org.chromium.mojom.device.VibrationManager; | 15 import org.chromium.mojom.device.VibrationManager; |
| 15 | 16 |
| 16 /** | 17 /** |
| 17 * Android implementation of the vibration manager service defined in | 18 * Android implementation of the vibration manager service defined in |
| 18 * device/vibration/vibration_manager.mojom. | 19 * device/vibration/vibration_manager.mojom. |
| 19 */ | 20 */ |
| 20 public class VibrationManagerImpl implements VibrationManager { | 21 public class VibrationManagerImpl implements VibrationManager { |
| 21 private static final String TAG = "VibrationManagerImpl"; | 22 private static final String TAG = "VibrationManagerImpl"; |
| 22 | 23 |
| 23 private static final long MINIMUM_VIBRATION_DURATION_MS = 1; // 1 millisecon d | 24 private static final long MINIMUM_VIBRATION_DURATION_MS = 1; // 1 millisecon d |
| 24 private static final long MAXIMUM_VIBRATION_DURATION_MS = 10000; // 10 secon ds | 25 private static final long MAXIMUM_VIBRATION_DURATION_MS = 10000; // 10 secon ds |
| 25 | 26 |
| 26 private final AudioManager mAudioManager; | 27 private final AudioManager mAudioManager; |
| 27 private final Vibrator mVibrator; | |
| 28 private final boolean mHasVibratePermission; | 28 private final boolean mHasVibratePermission; |
| 29 | 29 |
| 30 private static AndroidVibratorWrapper sVibratorWrapper; | |
| 31 | |
| 32 /** | |
| 33 * Android Vibrator wrapper class provided to test code to extend. | |
| 34 */ | |
| 35 @VisibleForTesting | |
| 36 public static class AndroidVibratorWrapper { | |
|
timvolodine
2015/09/11 16:40:15
normally we try to minimize test-related code in p
leonhsl(Using Gerrit)
2015/09/22 09:56:19
As bellowing discussion.
| |
| 37 private final Vibrator mAndroidVibrator; | |
| 38 | |
| 39 protected AndroidVibratorWrapper(Vibrator vibrator) { | |
| 40 mAndroidVibrator = vibrator; | |
| 41 } | |
| 42 | |
| 43 public void vibrate(long milliseconds) { | |
| 44 mAndroidVibrator.vibrate(milliseconds); | |
| 45 } | |
| 46 | |
| 47 public void cancel() { | |
| 48 mAndroidVibrator.cancel(); | |
| 49 } | |
| 50 } | |
| 51 | |
| 52 // Test code can use this function to inject other wrapper for testing. | |
| 53 public static void setVibratorWrapperForTesting(AndroidVibratorWrapper wrapp er) { | |
|
timvolodine
2015/09/11 16:40:15
I think ideally the way to test this would be to i
leonhsl(Using Gerrit)
2015/09/22 09:56:19
ContentBrowserClient provides OverrideRenderProces
| |
| 54 sVibratorWrapper = wrapper; | |
| 55 } | |
| 56 | |
| 30 public VibrationManagerImpl(Context context) { | 57 public VibrationManagerImpl(Context context) { |
| 31 mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SE RVICE); | 58 mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SE RVICE); |
| 32 mVibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE ); | 59 if (sVibratorWrapper == null) { |
| 60 sVibratorWrapper = new AndroidVibratorWrapper( | |
| 61 (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE )); | |
| 62 } | |
| 33 mHasVibratePermission = | 63 mHasVibratePermission = |
| 34 context.checkCallingOrSelfPermission(android.Manifest.permission .VIBRATE) | 64 context.checkCallingOrSelfPermission(android.Manifest.permission .VIBRATE) |
| 35 == PackageManager.PERMISSION_GRANTED; | 65 == PackageManager.PERMISSION_GRANTED; |
| 36 if (!mHasVibratePermission) { | 66 if (!mHasVibratePermission) { |
| 37 Log.w(TAG, "Failed to use vibrate API, requires VIBRATE permission." ); | 67 Log.w(TAG, "Failed to use vibrate API, requires VIBRATE permission." ); |
| 38 } | 68 } |
| 39 } | 69 } |
| 40 | 70 |
| 41 @Override | 71 @Override |
| 42 public void close() {} | 72 public void close() {} |
| 43 | 73 |
| 44 @Override | 74 @Override |
| 45 public void onConnectionError(MojoException e) {} | 75 public void onConnectionError(MojoException e) {} |
| 46 | 76 |
| 47 @Override | 77 @Override |
| 48 public void vibrate(long milliseconds) { | 78 public void vibrate(long milliseconds) { |
| 49 // Though the Blink implementation already sanitizes vibration times, do n't | 79 // Though the Blink implementation already sanitizes vibration times, do n't |
| 50 // trust any values passed from the client. | 80 // trust any values passed from the client. |
| 51 long sanitizedMilliseconds = Math.max(MINIMUM_VIBRATION_DURATION_MS, | 81 long sanitizedMilliseconds = Math.max(MINIMUM_VIBRATION_DURATION_MS, |
| 52 Math.min(milliseconds, MAXIMUM_VIBRATION_DURATION_MS)); | 82 Math.min(milliseconds, MAXIMUM_VIBRATION_DURATION_MS)); |
| 53 | 83 |
| 54 if (mAudioManager.getRingerMode() != AudioManager.RINGER_MODE_SILENT | 84 if (mAudioManager.getRingerMode() != AudioManager.RINGER_MODE_SILENT |
| 55 && mHasVibratePermission) { | 85 && mHasVibratePermission) { |
| 56 mVibrator.vibrate(sanitizedMilliseconds); | 86 sVibratorWrapper.vibrate(sanitizedMilliseconds); |
| 57 } | 87 } |
| 58 } | 88 } |
| 59 | 89 |
| 60 @Override | 90 @Override |
| 61 public void cancel() { | 91 public void cancel() { |
| 62 if (mHasVibratePermission) mVibrator.cancel(); | 92 if (mHasVibratePermission) sVibratorWrapper.cancel(); |
| 63 } | 93 } |
| 64 } | 94 } |
| OLD | NEW |