Merge: Bundle: Nit API wrapping Bundle class
[nit.git] / examples / mnit_simple / src / simple_android.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2012-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 module simple_android is
18 java_package("org.nitlanguage.simple")
19 android_manifest("""<uses-permission android:name="android.permission.VIBRATE" />""")
20 end
21
22 import simple
23 import mnit_android
24 import android::bundle
25 import android::shared_preferences
26 import android::assets_and_resources
27 import android::audio
28
29 in "Java" `{
30 import android.content.Context;
31 import android.widget.Toast;
32 `}
33
34 redef class App
35 var soundsp: Sound
36 var soundmp: Sound
37
38 redef fun init_window
39 do
40 super
41 manage_audio_mode
42 # retrieve sound
43 soundsp = load_sound("sound.ogg")
44 soundmp = load_music("xylofon.ogg")
45 default_mediaplayer.looping = true
46 default_mediaplayer.prepare
47 soundmp.play
48 end
49
50 redef fun input( ie )
51 do
52 if ie isa PointerEvent and ie.depressed then
53 do_java_stuff
54 test_bundle
55 test_shared_preferences
56 soundsp.play
57 test_assets
58 test_resources
59 end
60 return super
61 end
62
63 #testing the assets manager
64 fun test_assets
65 do
66 assert asset_manager.bitmap("fighter.png") != null
67 end
68
69 #testing the resources manager
70 fun test_resources do
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
75 end
76
77 fun test_bundle
78 do
79 var bundle = new Bundle(self)
80
81 bundle["anInt"] = 1
82 bundle["aFloat"] = 1.1
83 bundle["aString"] = "A string"
84 bundle["aBool"] = true
85
86 var int_array = new Array[Int]
87 var bool_array = new Array[Bool]
88
89 var value = true
90
91 for i in [0..5] do
92 int_array.add(i)
93 bool_array.add(value)
94 value = not value
95 end
96
97 bundle["anArrayOfInt"] = int_array
98 bundle["anArrayOfBool"] = bool_array
99
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
108
109 var int_array_test = bundle.array_of_int("anArrayOfInt")
110 var bool_array_test = bundle.array_of_bool("anArrayOfBool")
111
112 value = true
113
114 for i in [0..5] do
115 assert int_array_test[i] == i
116 assert bool_array_test[i] == value
117 value = not value
118 end
119
120 assert bundle.size == 6
121 assert bundle.has("aBool")
122 assert not bundle.is_empty
123
124 bundle.remove("aString")
125 bundle.remove("anArrayOfBool")
126
127 assert bundle.string("aString") == null
128 assert bundle.array_of_bool("anArrayOfBool") == null
129
130 # Serializable tests
131 var p1 = new Point(10, 10)
132 bundle["aPoint"] = p1
133 var p2 = bundle.deserialize("aPoint")
134
135 assert p1.to_s == p2.to_s
136
137 var point_array = new Array[Point]
138
139 for i in [0..5] do point_array.add(new Point(i, i))
140
141 bundle["anArrayOfPoint"] = point_array
142
143 var deserialized_point_array = bundle.deserialize_array("anArrayOfPoint")
144
145 for i in [0..5] do
146 var point = new Point(i, i)
147 assert deserialized_point_array[i].to_s == point.to_s
148 end
149
150 bundle.clear
151
152 assert bundle.keys.is_empty
153 assert bundle.is_empty
154 end
155
156 fun test_shared_preferences
157 do
158 # Private mode tests
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")
169
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
175
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
190
191 sp.clear
192 assert sp.all == null
193
194 sp.destroy
195 end
196
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!!!");
200
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);
204
205 // Vibration
206 android.os.Vibrator v = (android.os.Vibrator)
207 context.getSystemService(android.content.Context.VIBRATOR_SERVICE);
208 v.vibrate(500);
209
210 // - UI thread needed from now on
211 context.runOnUiThread(new Runnable() {
212 @Override
213 public void run() {
214 // + Toast
215 CharSequence text = "Java within Nit!";
216 int duration = Toast.LENGTH_SHORT;
217 Toast toast = Toast.makeText(context, text, duration);
218 toast.show();
219 }
220 });
221 `}
222 end
223
224 class Point
225 auto_serializable
226 super Serializable
227
228 var x: Int
229 var y: Int
230
231 init(x, y: Int)
232 do
233 self.x = x
234 self.y = y
235 end
236
237 redef fun to_s do return "({x}, {y})"
238 end