| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 package org.chromium.device.vibration; | |
| 6 | |
| 7 import android.content.Context; | |
| 8 import android.content.pm.PackageManager; | |
| 9 import android.media.AudioManager; | |
| 10 import android.os.Vibrator; | |
| 11 import android.util.Log; | |
| 12 | |
| 13 import org.chromium.base.annotations.CalledByNative; | |
| 14 import org.chromium.base.annotations.JNINamespace; | |
| 15 import org.chromium.device.mojom.VibrationManager; | |
| 16 import org.chromium.mojo.system.MojoException; | |
| 17 import org.chromium.services.service_manager.InterfaceFactory; | |
| 18 | |
| 19 /** | |
| 20 * Android implementation of the vibration manager service defined in | |
| 21 * device/vibration/vibration_manager.mojom. | |
| 22 */ | |
| 23 @JNINamespace("device") | |
| 24 public class VibrationManagerImpl implements VibrationManager { | |
| 25 private static final String TAG = "VibrationManagerImpl"; | |
| 26 | |
| 27 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 | |
| 29 | |
| 30 private final AudioManager mAudioManager; | |
| 31 private final Vibrator mVibrator; | |
| 32 private final boolean mHasVibratePermission; | |
| 33 | |
| 34 private static long sVibrateMilliSecondsForTesting = -1; | |
| 35 private static boolean sVibrateCancelledForTesting = false; | |
| 36 | |
| 37 public VibrationManagerImpl(Context context) { | |
| 38 mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SE
RVICE); | |
| 39 mVibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE
); | |
| 40 // TODO(mvanouwerkerk): What happens if permission is revoked? Handle th
is better. | |
| 41 mHasVibratePermission = | |
| 42 context.checkCallingOrSelfPermission(android.Manifest.permission
.VIBRATE) | |
| 43 == PackageManager.PERMISSION_GRANTED; | |
| 44 if (!mHasVibratePermission) { | |
| 45 Log.w(TAG, "Failed to use vibrate API, requires VIBRATE permission."
); | |
| 46 } | |
| 47 } | |
| 48 | |
| 49 @Override | |
| 50 public void close() {} | |
| 51 | |
| 52 @Override | |
| 53 public void onConnectionError(MojoException e) {} | |
| 54 | |
| 55 @Override | |
| 56 public void vibrate(long milliseconds, VibrateResponse callback) { | |
| 57 // Though the Blink implementation already sanitizes vibration times, do
n't | |
| 58 // trust any values passed from the client. | |
| 59 long sanitizedMilliseconds = Math.max(MINIMUM_VIBRATION_DURATION_MS, | |
| 60 Math.min(milliseconds, MAXIMUM_VIBRATION_DURATION_MS)); | |
| 61 | |
| 62 if (mAudioManager.getRingerMode() != AudioManager.RINGER_MODE_SILENT | |
| 63 && mHasVibratePermission) { | |
| 64 mVibrator.vibrate(sanitizedMilliseconds); | |
| 65 } | |
| 66 setVibrateMilliSecondsForTesting(sanitizedMilliseconds); | |
| 67 callback.call(); | |
| 68 } | |
| 69 | |
| 70 @Override | |
| 71 public void cancel(CancelResponse callback) { | |
| 72 if (mHasVibratePermission) { | |
| 73 mVibrator.cancel(); | |
| 74 } | |
| 75 setVibrateCancelledForTesting(true); | |
| 76 callback.call(); | |
| 77 } | |
| 78 | |
| 79 /** | |
| 80 * A factory for implementations of the VibrationManager interface. | |
| 81 */ | |
| 82 public static class Factory implements InterfaceFactory<VibrationManager> { | |
| 83 private Context mContext; | |
| 84 public Factory(Context context) { | |
| 85 mContext = context; | |
| 86 } | |
| 87 | |
| 88 @Override | |
| 89 public VibrationManager createImpl() { | |
| 90 return new VibrationManagerImpl(mContext); | |
| 91 } | |
| 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 } | |
| 111 } | |
| OLD | NEW |