Merge: doc: fixed some typos and other misc. corrections
[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 dalvik
25
26 # Handle to an Android vibrator
27 extern class Vibrator in "Java" `{ android.os.Vibrator `}
28 super JavaObject
29
30 # Vibrate for `n` miliseconds
31 fun vibrate(n: Int) in "Java" `{ self.vibrate(n); `}
32
33 # Does this devices has a vibrator
34 #
35 # TODO activate in API 11
36 #fun exists: Bool in "Java" `{ return self.hasVibrator(); `}
37
38 # Turn off the vibration
39 fun cancel in "Java" `{ self.cancel(); `}
40
41 # HACK for bug #845
42 redef fun new_global_ref import sys, Sys.jni_env `{
43 Sys sys = Vibrator_sys(self);
44 JNIEnv *env = Sys_jni_env(sys);
45 return (*env)->NewGlobalRef(env, self);
46 `}
47 end
48
49 redef class App
50 # Get the handle to this device vibrator as a global ref
51 var vibrator: Vibrator is lazy do
52 var v = vibrator_native(native_context)
53 return v.new_global_ref
54 end
55 private fun vibrator_native(context: NativeContext): Vibrator in "Java" `{
56 android.os.Vibrator v = (android.os.Vibrator)
57 context.getSystemService(android.content.Context.VIBRATOR_SERVICE);
58 return v;
59 `}
60 end