Merge: Use more intern methods and add intern factories for NativeString and NativeArray
[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::shared_preferences
25
26 in "Java" `{
27 import android.content.Context;
28 import android.widget.Toast;
29 `}
30
31 redef class App
32 redef fun input( ie )
33 do
34 if ie isa PointerEvent and ie.depressed then
35 do_java_stuff
36 test_shared_preferences
37 end
38 return super
39 end
40
41 fun test_shared_preferences
42 do
43 # Private mode tests
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")
54
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
60
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
75
76 sp.clear
77 assert sp.all == null
78
79 sp.destroy
80 end
81
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!!!");
85
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);
89
90 // Vibration
91 android.os.Vibrator v = (android.os.Vibrator)
92 context.getSystemService(android.content.Context.VIBRATOR_SERVICE);
93 v.vibrate(500);
94
95 // - UI thread needed from now on
96 context.runOnUiThread(new Runnable() {
97 @Override
98 public void run() {
99 // + Toast
100 CharSequence text = "Java within Nit!";
101 int duration = Toast.LENGTH_SHORT;
102 Toast toast = Toast.makeText(context, text, duration);
103 toast.show();
104 }
105 });
106 `}
107 end
108
109 class Point
110 auto_serializable
111 super Serializable
112
113 var x: Int
114 var y: Int
115
116 init(x, y: Int)
117 do
118 self.x = x
119 self.y = y
120 end
121
122 redef fun to_s do return "({x}, {y})"
123 end
124