android: trigger click event on button release
[nit.git] / lib / android / ui / ui.nit
1 # This file is part of NIT (http://www.nitlanguage.org).
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 # Views and services to use the Android native user interface
16 module ui is
17 # `adjustPan` allows to use EditText in a ListLayout
18 android_manifest_activity """android:windowSoftInputMode="adjustPan""""
19 end
20
21 # Implementation note:
22 #
23 # We cannot rely on `Activity::on_restore_instance_state` to implement
24 # `on_restore_state` is it only invoked if there is a bundled state,
25 # and we don't use the Android bundled state.
26
27 import native_ui
28 import log
29 import nit_activity
30
31 import app::ui
32 private import data_store
33 import assets
34
35 redef class Control
36 # The Android element used to implement `self`
37 fun native: NATIVE is abstract
38
39 # Type of `native`
40 type NATIVE: JavaObject
41 end
42
43 redef class NativeActivity
44
45 private fun remove_title_bar in "Java" `{
46 self.requestWindowFeature(android.view.Window.FEATURE_NO_TITLE);
47 `}
48
49 # Insert a single layout as the root of the activity window
50 private fun insert_root_layout(root_layout_id: Int)
51 in "Java" `{
52 android.widget.FrameLayout layout = new android.widget.FrameLayout(self);
53 layout.setId((int)root_layout_id);
54 self.setContentView(layout);
55 `}
56
57 # Replace the currently visible fragment, if any, with `native_fragment`
58 private fun show_fragment(root_layout_id: Int, native_fragment: Android_app_Fragment)
59 in "Java" `{
60 android.app.FragmentTransaction transaction = self.getFragmentManager().beginTransaction();
61 transaction.replace((int)root_layout_id, native_fragment);
62 transaction.commit();
63 `}
64 end
65
66 redef class App
67 redef fun on_create
68 do
69 app.native_activity.remove_title_bar
70 native_activity.insert_root_layout(root_layout_id)
71 super
72 end
73
74 # Identifier of the container holding the fragments
75 private var root_layout_id = 0xFFFF
76
77 redef fun window=(window)
78 do
79 native_activity.show_fragment(root_layout_id, window.native)
80 super
81 end
82
83 redef fun on_start do window.on_start
84
85 redef fun on_destroy do window.on_destroy
86 end
87
88 redef class CompositeControl
89 redef fun on_start do for i in items do i.on_start
90
91 redef fun on_destroy do for i in items do i.on_destroy
92 end
93
94 redef class Activity
95 redef fun on_back_pressed
96 do
97 var window = app.window
98 if window.enable_back_button then
99 window.on_back_button
100 return true
101 end
102
103 return false
104 end
105 end
106
107 # On Android, a window is implemented with the fragment `native`
108 redef class Window
109 redef var native = (new Android_app_Fragment(self)).new_global_ref
110
111 redef type NATIVE: Android_app_Fragment
112
113 # Root high-level view of this window
114 var view: nullable View = null
115
116 redef fun add(item)
117 do
118 if item isa View then view = item
119 super
120 end
121
122 private fun on_create_fragment: NativeView
123 do
124 on_create
125
126 var view = view
127 assert view != null else print_error "{class_name} needs a `view` after `Window::on_create` returns"
128 return view.native
129 end
130 end
131
132 redef class View
133 redef type NATIVE: NativeView
134
135 redef fun enabled=(enabled) do native.enabled = enabled or else true
136 redef fun enabled do return native.enabled
137 end
138
139 redef class Layout
140 redef type NATIVE: NativeViewGroup
141
142 redef fun add(item)
143 do
144 super
145
146 assert item isa View
147
148 # FIXME abstract the use either homogeneous or weight to balance views size in a layout
149 native.add_view_with_weight(item.native, 1.0)
150 end
151
152 redef fun remove(item)
153 do
154 super
155 if item isa View then native.remove_view item.native
156 end
157 end
158
159 redef class HorizontalLayout
160 redef var native do
161 var layout = new NativeLinearLayout(app.native_activity)
162 layout = layout.new_global_ref
163 layout.set_horizontal
164 return layout
165 end
166 end
167
168 redef class VerticalLayout
169 redef var native do
170 var layout = new NativeLinearLayout(app.native_activity)
171 layout = layout.new_global_ref
172 layout.set_vertical
173 return layout
174 end
175 end
176
177 redef class ListLayout
178 redef type NATIVE: Android_widget_ListView
179
180 redef var native do
181 var layout = new Android_widget_ListView(app.native_activity)
182 layout = layout.new_global_ref
183 return layout
184 end
185
186 private var adapter: Android_widget_ArrayAdapter do
187 var adapter = new Android_widget_ArrayAdapter(app.native_activity,
188 android_r_layout_simple_list_item_1, self)
189 native.set_adapter adapter
190 return adapter.new_global_ref
191 end
192
193 redef fun add(item)
194 do
195 super
196 if item isa View then adapter.add item.native
197 end
198
199 private fun create_view(position: Int): NativeView
200 do
201 var ctrl = items[position]
202 assert ctrl isa View
203 return ctrl.native
204 end
205 end
206
207 redef class Android_widget_ArrayAdapter
208 private new (context: NativeContext, res: Int, sender: ListLayout)
209 import ListLayout.create_view in "Java" `{
210 final int final_sender_object = sender;
211
212 return new android.widget.ArrayAdapter(context, (int)res) {
213 @Override
214 public android.view.View getView(int position, android.view.View convertView, android.view.ViewGroup parent) {
215 return ListLayout_create_view(final_sender_object, position);
216 }
217 };
218 `}
219 end
220
221 redef class TextView
222 redef type NATIVE: NativeTextView
223
224 redef fun text do return native.text.to_s
225 redef fun text=(value) do
226 if value == null then value = ""
227 native.text = value.to_java_string
228 end
229
230 redef fun size=(size) do set_size_native(app.native_activity, native, size or else 1.0)
231
232 private fun set_size_native(context: NativeContext, view: NativeTextView, size: Float)
233 in "Java" `{
234 int s;
235 if (size == 1.0d)
236 s = android.R.style.TextAppearance_Medium;
237 else if (size < 1.0d)
238 s = android.R.style.TextAppearance_Small;
239 else // if (size > 1.0d)
240 s = android.R.style.TextAppearance_Large;
241
242 view.setTextAppearance(context, s);
243 `}
244
245 redef fun align=(align) do set_align_native(native, align or else 0.0)
246
247 private fun set_align_native(view: NativeTextView, align: Float)
248 in "Java" `{
249 int g;
250 if (align == 0.5d)
251 g = android.view.Gravity.CENTER_HORIZONTAL;
252 else if (align < 0.5d)
253 g = android.view.Gravity.LEFT;
254 else // if (align > 0.5d)
255 g = android.view.Gravity.RIGHT;
256
257 view.setGravity(g | android.view.Gravity.CENTER_VERTICAL);
258 `}
259 end
260
261 redef class Label
262 redef type NATIVE: NativeTextView
263 redef var native do return (new NativeTextView(app.native_activity)).new_global_ref
264 end
265
266 redef class CheckBox
267 redef type NATIVE: Android_widget_CompoundButton
268 redef var native do return (new Android_widget_CheckBox(app.native_activity)).new_global_ref
269 init do set_callback_on_toggle(native)
270
271 redef fun is_checked do return native.is_checked
272 redef fun is_checked=(value) do native.set_checked(value)
273
274 private fun on_toggle do notify_observers new ToggleEvent(self)
275
276 private fun set_callback_on_toggle(view: NATIVE)
277 import on_toggle in "Java" `{
278 final int final_sender_object = self;
279 CheckBox_incr_ref(final_sender_object);
280
281 view.setOnCheckedChangeListener(
282 new android.widget.CompoundButton.OnCheckedChangeListener() {
283 @Override
284 public void onCheckedChanged(android.widget.CompoundButton buttonView, boolean isChecked) {
285 CheckBox_on_toggle(final_sender_object);
286 }
287 });
288 `}
289 end
290
291 redef class TextInput
292 redef type NATIVE: NativeEditText
293 redef var native = (new NativeEditText(app.native_activity)).new_global_ref
294
295 redef fun is_password=(value)
296 do
297 native.is_password = value or else false
298 super
299 end
300 end
301
302 redef class NativeEditText
303
304 # Configure this view to hide passwords
305 fun is_password=(value: Bool) in "Java" `{
306 if (value) {
307 self.setInputType(android.text.InputType.TYPE_CLASS_TEXT | android.text.InputType.TYPE_TEXT_VARIATION_PASSWORD);
308 self.setTransformationMethod(android.text.method.PasswordTransformationMethod.getInstance());
309 } else {
310 self.setInputType(android.text.InputType.TYPE_CLASS_TEXT);
311 self.setTransformationMethod(null);
312 }
313 `}
314 end
315
316 redef class Button
317 super Finalizable
318
319 redef type NATIVE: NativeButton
320 redef var native = (new NativeButton(app.native_activity, self)).new_global_ref
321
322 private fun on_click do notify_observers new ButtonPressEvent(self)
323
324 redef fun finalize do native.delete_global_ref
325 end
326
327 redef class NativeButton
328 private new (context: NativeActivity, sender_object: Button)
329 import Button.on_click in "Java" `{
330 final int final_sender_object = sender_object;
331 Button_incr_ref(final_sender_object);
332
333 return new android.widget.Button(context) {
334 @Override
335 public boolean onTouchEvent(android.view.MotionEvent event) {
336 if (event.getAction() == android.view.MotionEvent.ACTION_UP) {
337 Button_on_click(final_sender_object);
338 return true;
339 } else if (event.getAction() == android.view.MotionEvent.ACTION_DOWN) {
340 return true;
341 }
342 return false;
343 }
344 };
345 `}
346 end
347
348 redef class Android_app_Fragment
349 private new (nit_window: Window)
350 import Window.on_create_fragment in "Java" `{
351 final int final_nit_window = nit_window;
352
353 return new android.app.Fragment(){
354 @Override
355 public android.view.View onCreateView(android.view.LayoutInflater inflater,
356 android.view.ViewGroup container, android.os.Bundle state) {
357
358 return Window_on_create_fragment(final_nit_window);
359 }
360 };
361 `}
362 end
363
364 redef class Text
365 redef fun open_in_browser
366 do to_java_string.native_open_in_browser(app.native_activity)
367 end
368
369 redef class JavaString
370 private fun native_open_in_browser(context: NativeContext)
371 in "Java" `{
372 android.content.Intent intent = new android.content.Intent(
373 android.content.Intent.ACTION_VIEW,
374 android.net.Uri.parse(self));
375 context.startActivity(intent);
376 `}
377 end