Merge: Prepare for the android UI module
[nit.git] / lib / android / vibration.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2014 Alexis Laferrière <alexis.laf@xymus.net>
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 # Vibration services for Android
18 #
19 # Importing this module will trigger the use of the VIBRATE permission
20 module vibration is
21 android_manifest("""<uses-permission android:name="android.permission.VIBRATE" />""")
22 end
23
24 import native_app_glue
25
26 # Handle to an Android vibrator
27 extern class Vibrator in "Java" `{ android.os.Vibrator `}
28 super JavaObject
29 redef type SELF: Vibrator
30
31 # Vibrate for `n` miliseconds
32 fun vibrate(n: Int) in "Java" `{ recv.vibrate(n); `}
33
34 # Does this devices has a vibrator
35 #
36 # TODO activate in API 11
37 #fun exists: Bool in "Java" `{ return recv.hasVibrator(); `}
38
39 # Turn off the vibration
40 fun cancel in "Java" `{ recv.cancel(); `}
41
42 # HACK for bug #845
43 redef fun new_global_ref import sys, Sys.jni_env `{
44 Sys sys = Vibrator_sys(recv);
45 JNIEnv *env = Sys_jni_env(sys);
46 return (*env)->NewGlobalRef(env, recv);
47 `}
48 end
49
50 redef class App
51 # Get the handle to this device vibrator as a global ref
52 fun vibrator: Vibrator is cached do
53 var v = vibrator_native(native_activity)
54 return v.new_global_ref
55 end
56 private fun vibrator_native(context: NativeActivity): Vibrator in "Java" `{
57 android.os.Vibrator v = (android.os.Vibrator)
58 context.getSystemService(android.content.Context.VIBRATOR_SERVICE);
59 return v;
60 `}
61 end