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 | 11 |
12 import org.chromium.base.ContextUtils; | |
12 import org.chromium.base.Log; | 13 import org.chromium.base.Log; |
13 import org.chromium.base.annotations.CalledByNative; | 14 import org.chromium.base.annotations.CalledByNative; |
14 import org.chromium.base.annotations.JNINamespace; | 15 import org.chromium.base.annotations.JNINamespace; |
15 import org.chromium.device.mojom.VibrationManager; | 16 import org.chromium.device.mojom.VibrationManager; |
16 import org.chromium.mojo.system.MojoException; | 17 import org.chromium.mojo.system.MojoException; |
17 import org.chromium.services.service_manager.InterfaceFactory; | 18 import org.chromium.services.service_manager.InterfaceFactory; |
18 | 19 |
19 /** | 20 /** |
20 * Android implementation of the VibrationManager interface defined in | 21 * Android implementation of the VibrationManager interface defined in |
21 * services/device/public/interfaces/vibration_manager.mojom. | 22 * services/device/public/interfaces/vibration_manager.mojom. |
22 */ | 23 */ |
23 @JNINamespace("device") | 24 @JNINamespace("device") |
24 public class VibrationManagerImpl implements VibrationManager { | 25 public class VibrationManagerImpl implements VibrationManager { |
25 private static final String TAG = "VibrationManagerImpl"; | 26 private static final String TAG = "VibrationManagerImpl"; |
26 | 27 |
27 private static final long MINIMUM_VIBRATION_DURATION_MS = 1; // 1 millisecon d | 28 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 private static final long MAXIMUM_VIBRATION_DURATION_MS = 10000; // 10 secon ds |
29 | 30 |
30 private final AudioManager mAudioManager; | 31 private final AudioManager mAudioManager; |
31 private final Vibrator mVibrator; | 32 private final Vibrator mVibrator; |
32 private final boolean mHasVibratePermission; | 33 private final boolean mHasVibratePermission; |
33 | 34 |
34 private static long sVibrateMilliSecondsForTesting = -1; | 35 private static long sVibrateMilliSecondsForTesting = -1; |
35 private static boolean sVibrateCancelledForTesting = false; | 36 private static boolean sVibrateCancelledForTesting = false; |
36 | 37 |
37 public VibrationManagerImpl(Context context) { | 38 public VibrationManagerImpl() { |
38 mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SE RVICE); | 39 mAudioManager = (AudioManager) ContextUtils.getApplicationContext().getS ystemService( |
39 mVibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE ); | 40 Context.AUDIO_SERVICE); |
41 mVibrator = (Vibrator) ContextUtils.getApplicationContext().getSystemSer vice( | |
42 Context.VIBRATOR_SERVICE); | |
40 // TODO(mvanouwerkerk): What happens if permission is revoked? Handle th is better. | 43 // TODO(mvanouwerkerk): What happens if permission is revoked? Handle th is better. |
41 mHasVibratePermission = | 44 mHasVibratePermission = ContextUtils.getApplicationContext().checkCallin gOrSelfPermission( |
42 context.checkCallingOrSelfPermission(android.Manifest.permission .VIBRATE) | 45 android.Manifest.permission.VIBRATE) |
43 == PackageManager.PERMISSION_GRANTED; | 46 == PackageManager.PERMISSION_GRANTED; |
44 if (!mHasVibratePermission) { | 47 if (!mHasVibratePermission) { |
45 Log.w(TAG, "Failed to use vibrate API, requires VIBRATE permission." ); | 48 Log.w(TAG, "Failed to use vibrate API, requires VIBRATE permission." ); |
46 } | 49 } |
47 } | 50 } |
48 | 51 |
49 @Override | 52 @Override |
50 public void close() {} | 53 public void close() {} |
51 | 54 |
52 @Override | 55 @Override |
(...skipping 20 matching lines...) Expand all Loading... | |
73 mVibrator.cancel(); | 76 mVibrator.cancel(); |
74 } | 77 } |
75 setVibrateCancelledForTesting(true); | 78 setVibrateCancelledForTesting(true); |
76 callback.call(); | 79 callback.call(); |
77 } | 80 } |
78 | 81 |
79 /** | 82 /** |
80 * A factory for implementations of the VibrationManager interface. | 83 * A factory for implementations of the VibrationManager interface. |
81 */ | 84 */ |
82 public static class Factory implements InterfaceFactory<VibrationManager> { | 85 public static class Factory implements InterfaceFactory<VibrationManager> { |
83 private Context mContext; | 86 public Factory() {} |
agrieve
2017/05/04 15:55:50
nit: delete.
Peter Wen
2017/05/04 17:39:26
Done.
| |
84 public Factory(Context context) { | |
85 mContext = context; | |
86 } | |
87 | 87 |
88 @Override | 88 @Override |
89 public VibrationManager createImpl() { | 89 public VibrationManager createImpl() { |
90 return new VibrationManagerImpl(mContext); | 90 return new VibrationManagerImpl(); |
91 } | 91 } |
92 } | 92 } |
93 | 93 |
94 static void setVibrateMilliSecondsForTesting(long milliseconds) { | 94 static void setVibrateMilliSecondsForTesting(long milliseconds) { |
95 sVibrateMilliSecondsForTesting = milliseconds; | 95 sVibrateMilliSecondsForTesting = milliseconds; |
96 } | 96 } |
97 | 97 |
98 static void setVibrateCancelledForTesting(boolean cancelled) { | 98 static void setVibrateCancelledForTesting(boolean cancelled) { |
99 sVibrateCancelledForTesting = cancelled; | 99 sVibrateCancelledForTesting = cancelled; |
100 } | 100 } |
101 | 101 |
102 @CalledByNative | 102 @CalledByNative |
103 static long getVibrateMilliSecondsForTesting() { | 103 static long getVibrateMilliSecondsForTesting() { |
104 return sVibrateMilliSecondsForTesting; | 104 return sVibrateMilliSecondsForTesting; |
105 } | 105 } |
106 | 106 |
107 @CalledByNative | 107 @CalledByNative |
108 static boolean getVibrateCancelledForTesting() { | 108 static boolean getVibrateCancelledForTesting() { |
109 return sVibrateCancelledForTesting; | 109 return sVibrateCancelledForTesting; |
110 } | 110 } |
111 } | 111 } |
OLD | NEW |