1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2012-2014 Alexis Laferrière <alexis.laf@xymus.net>
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
9 # http://www.apache.org/licenses/LICENSE-2.0
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.
17 module simple_android
is
18 java_package
("org.nitlanguage.simple")
19 android_manifest
("""<uses-permission android:name="android.permission.VIBRATE" />""")
24 import android
::shared_preferences
27 import android.content.Context;
28 import android.widget.Toast;
34 if ie
isa PointerEvent and ie
.depressed
then
36 test_shared_preferences
41 fun test_shared_preferences
44 var sp
= new SharedPreferences.privately
(self, "test")
45 sp
.add_bool
("a_boolean", true)
46 sp
.add_float
("a_float", 66.6)
47 sp
.add_int
("an_int", 666)
48 sp
.add_int
("a_second_int", 666777)
49 sp
.add_long
("a_long", 6666666666)
50 sp
.add_string
("a_string", "A string")
51 sp
["another_int"] = 85
52 sp
["yet_another_string"] = "Another string"
53 sp
.remove
("a_second_int")
55 # Serialized object test
56 var my_point
= new Point(10, 10)
57 sp
["a_point"] = my_point
58 var my_deserialized_point
= sp
.deserialize
("a_point")
59 assert my_point
.to_s
== my_deserialized_point
.to_s
61 assert sp
.bool
("a_boolean", false) == true
62 assert sp
.bool
("wrong_boolean", false) == false
63 assert sp
.float
("a_float", 0.0) != 0.0
64 assert sp
.float
("wrong_float", 0.0) == 0.0
65 assert sp
.int
("an_int", 0) == 666
66 assert sp
.int
("a_second_int", 0) == 0
67 assert sp
.long
("a_long", 0) == 6666666666
68 assert sp
.long
("wrong_long", 0) == 0
69 assert sp
.string
("a_string", "ERROR!") == "A string"
70 assert sp
.string
("wrong_string", "ERROR!") == "ERROR!"
71 assert sp
.long
("another_int", 0) == 85
72 assert sp
.string
("yet_another_string", "ERROR!") == "Another string"
73 assert sp
.has
("an_int") == true
74 assert sp
.has
("a_second_int") == false
82 fun do_java_stuff
import native_activity
in "Java" `{
83 // + Log (no context needed)
84 android.util.Log.d("mnit_simple", "Java within NIT!!!");
86 // - Context needed from now on
87 // NativeActivity is a Java sub-class of Context
88 final android.app.NativeActivity context = App_native_activity(recv);
91 android.os.Vibrator v = (android.os.Vibrator)
92 context.getSystemService(android.content.Context.VIBRATOR_SERVICE);
95 // - UI thread needed from now on
96 context.runOnUiThread(new Runnable() {
100 CharSequence text = "Java within Nit!";
101 int duration = Toast.LENGTH_SHORT;
102 Toast toast = Toast.makeText(context, text, duration);
122 redef fun to_s
do return "({x}, {y})"