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
::bundle
25 import android
::shared_preferences
26 import android
::assets_and_resources
30 import android.content.Context;
31 import android.widget.Toast;
43 soundsp
= load_sound
("sound.ogg")
44 soundmp
= load_music
("xylofon.ogg")
45 default_mediaplayer
.looping
= true
46 default_mediaplayer
.prepare
52 if ie
isa PointerEvent and ie
.depressed
then
55 test_shared_preferences
63 #testing the assets manager
66 assert asset_manager
.bitmap
("fighter.png") != null
69 #testing the resources manager
71 assert resource_manager
.string
("string_test") == "string test"
72 assert resource_manager
.boolean
("test_bool") == true
73 assert resource_manager
.dimension
("test_dimen_1") != null
74 assert resource_manager
.dimension
("test_dimen_2") != null
79 var bundle
= new Bundle(self)
82 bundle
["aFloat"] = 1.1
83 bundle
["aString"] = "A string"
84 bundle
["aBool"] = true
86 var int_array
= new Array[Int]
87 var bool_array
= new Array[Bool]
97 bundle
["anArrayOfInt"] = int_array
98 bundle
["anArrayOfBool"] = bool_array
100 assert bundle
.int
("anInt", 0) == 1
101 assert bundle
.int
("wrongInt", 0) == 0
102 assert bundle
.float
("aFloat", 0.0) == 1.1
103 assert bundle
.float
("wrongFloat", 0.0) == 0.0
104 assert bundle
.string
("aString") == "A string"
105 assert bundle
.string
("wrongString") == null
106 assert bundle
.bool
("aBool", false)
107 assert bundle
.bool
("wrongBool", false) == false
109 var int_array_test
= bundle
.array_of_int
("anArrayOfInt")
110 var bool_array_test
= bundle
.array_of_bool
("anArrayOfBool")
115 assert int_array_test
[i
] == i
116 assert bool_array_test
[i
] == value
120 assert bundle
.size
== 6
121 assert bundle
.has
("aBool")
122 assert not bundle
.is_empty
124 bundle
.remove
("aString")
125 bundle
.remove
("anArrayOfBool")
127 assert bundle
.string
("aString") == null
128 assert bundle
.array_of_bool
("anArrayOfBool") == null
131 var p1
= new Point(10, 10)
132 bundle
["aPoint"] = p1
133 var p2
= bundle
.deserialize
("aPoint")
135 assert p1
.to_s
== p2
.to_s
137 var point_array
= new Array[Point]
139 for i
in [0..5] do point_array
.add
(new Point(i
, i
))
141 bundle
["anArrayOfPoint"] = point_array
143 var deserialized_point_array
= bundle
.deserialize_array
("anArrayOfPoint")
146 var point
= new Point(i
, i
)
147 assert deserialized_point_array
[i
].to_s
== point
.to_s
152 assert bundle
.keys
.is_empty
153 assert bundle
.is_empty
156 fun test_shared_preferences
159 var sp
= new SharedPreferences.privately
(self, "test")
160 sp
.add_bool
("a_boolean", true)
161 sp
.add_float
("a_float", 66.6)
162 sp
.add_int
("an_int", 666)
163 sp
.add_int
("a_second_int", 666777)
164 sp
.add_long
("a_long", 6666666666)
165 sp
.add_string
("a_string", "A string")
166 sp
["another_int"] = 85
167 sp
["yet_another_string"] = "Another string"
168 sp
.remove
("a_second_int")
170 # Serialized object test
171 var my_point
= new Point(10, 10)
172 sp
["a_point"] = my_point
173 var my_deserialized_point
= sp
.deserialize
("a_point")
174 assert my_point
.to_s
== my_deserialized_point
.to_s
176 assert sp
.bool
("a_boolean", false) == true
177 assert sp
.bool
("wrong_boolean", false) == false
178 assert sp
.float
("a_float", 0.0) != 0.0
179 assert sp
.float
("wrong_float", 0.0) == 0.0
180 assert sp
.int
("an_int", 0) == 666
181 assert sp
.int
("a_second_int", 0) == 0
182 assert sp
.long
("a_long", 0) == 6666666666
183 assert sp
.long
("wrong_long", 0) == 0
184 assert sp
.string
("a_string", "ERROR!") == "A string"
185 assert sp
.string
("wrong_string", "ERROR!") == "ERROR!"
186 assert sp
.long
("another_int", 0) == 85
187 assert sp
.string
("yet_another_string", "ERROR!") == "Another string"
188 assert sp
.has
("an_int") == true
189 assert sp
.has
("a_second_int") == false
192 assert sp
.all
== null
197 fun do_java_stuff
import native_activity
in "Java" `{
198 // + Log (no context needed)
199 android.util.Log.d("mnit_simple", "Java within NIT!!!");
201 // - Context needed from now on
202 // NativeActivity is a Java sub-class of Context
203 final android.app.NativeActivity context = App_native_activity(recv);
206 android.os.Vibrator v = (android.os.Vibrator)
207 context.getSystemService(android.content.Context.VIBRATOR_SERVICE);
210 // - UI thread needed from now on
211 context.runOnUiThread(new Runnable() {
215 CharSequence text = "Java within Nit!";
216 int duration = Toast.LENGTH_SHORT;
217 Toast toast = Toast.makeText(context, text, duration);
237 redef fun to_s
do return "({x}, {y})"