OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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.content.browser; | 5 package org.chromium.content.browser; |
6 | 6 |
7 import android.content.Context; | 7 import android.content.Context; |
8 import android.content.pm.PackageManager; | |
8 import android.media.AudioManager; | 9 import android.media.AudioManager; |
9 import android.os.Vibrator; | 10 import android.os.Vibrator; |
11 import android.util.Log; | |
10 | 12 |
11 import org.chromium.base.CalledByNative; | 13 import org.chromium.base.CalledByNative; |
12 import org.chromium.base.JNINamespace; | 14 import org.chromium.base.JNINamespace; |
13 | 15 |
14 /** | 16 /** |
15 * This is the implementation of the C++ counterpart VibrationProvider. | 17 * This is the implementation of the C++ counterpart VibrationProvider. |
16 */ | 18 */ |
17 @JNINamespace("content") | 19 @JNINamespace("content") |
18 class VibrationProvider { | 20 class VibrationProvider { |
21 private static final String TAG = "VibrationProvider"; | |
19 | 22 |
20 private final AudioManager mAudioManager; | 23 private final AudioManager mAudioManager; |
21 private final Vibrator mVibrator; | 24 private final Vibrator mVibrator; |
25 private final boolean mHasVibratePermission; | |
22 | 26 |
23 @CalledByNative | 27 @CalledByNative |
24 private static VibrationProvider create(Context context) { | 28 private static VibrationProvider create(Context context) { |
25 return new VibrationProvider(context); | 29 return new VibrationProvider(context); |
26 } | 30 } |
27 | 31 |
28 @CalledByNative | 32 @CalledByNative |
29 private void vibrate(long milliseconds) { | 33 private void vibrate(long milliseconds) { |
30 if (mAudioManager.getRingerMode() != AudioManager.RINGER_MODE_SILENT) | 34 if (mAudioManager.getRingerMode() != AudioManager.RINGER_MODE_SILENT) { |
31 mVibrator.vibrate(milliseconds); | 35 if (mHasVibratePermission) mVibrator.vibrate(milliseconds); |
Michael van Ouwerkerk
2014/01/28 15:09:04
This condition would be cleaner if merged into the
| |
36 } | |
32 } | 37 } |
33 | 38 |
34 @CalledByNative | 39 @CalledByNative |
35 private void cancelVibration() { | 40 private void cancelVibration() { |
36 mVibrator.cancel(); | 41 if (mHasVibratePermission) mVibrator.cancel(); |
37 } | 42 } |
38 | 43 |
39 private VibrationProvider(Context context) { | 44 private VibrationProvider(Context context) { |
40 mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SE RVICE); | 45 mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SE RVICE); |
41 mVibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE ); | 46 mVibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE ); |
47 mHasVibratePermission = context.checkCallingOrSelfPermission( | |
48 android.Manifest.permission.VIBRATE) == PackageManager.PERMISSIO N_GRANTED; | |
49 if (!mHasVibratePermission) { | |
50 Log.e(TAG, "Caught security exception, requires VIBRATE permission." ); | |
Michael van Ouwerkerk
2014/01/28 15:09:04
This code did not actually catch an exception, ple
jdduke (slow)
2014/01/28 15:45:33
Also, could we make this a warning, Log.w, instead
| |
51 } | |
42 } | 52 } |
43 } | 53 } |
OLD | NEW |