1 # This file is part of NIT (http://www.nitlanguage.org).
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
15 # Views and services to use the Android native user interface
18 # Implementation note:
20 # We cannot rely on `Activity::on_restore_instance_state` to implement
21 # `on_restore_state` is it only invoked if there is a bundled state,
22 # and we don't use the Android bundled state.
29 private import data_store
33 # The Android element used to implement `self`
34 fun native
: NATIVE is abstract
37 type NATIVE: JavaObject
41 redef var native
= app
.native_activity
.new_global_ref
43 redef type NATIVE: NativeActivity
49 # FIXME abstract the Android restriction where `content_view` must be a layout
50 assert item
isa Layout
51 native
.content_view
= item
.native
56 redef type NATIVE: NativeView
58 redef fun enabled
=(enabled
) do native
.enabled
= enabled
or else true
59 redef fun enabled
do return native
.enabled
63 redef type NATIVE: NativeViewGroup
71 # FIXME abstract the use either homogeneous or weight to balance views size in a layout
72 native
.add_view_with_weight
(item
.native
, 1.0)
75 redef fun remove
(item
)
78 if item
isa View then native
.remove_view item
.native
82 redef class HorizontalLayout
84 var layout
= new NativeLinearLayout(app
.native_activity
)
85 layout
= layout
.new_global_ref
91 redef class VerticalLayout
93 var layout
= new NativeLinearLayout(app
.native_activity
)
94 layout
= layout
.new_global_ref
100 redef class ListLayout
101 redef type NATIVE: Android_widget_ListView
104 var layout
= new Android_widget_ListView(app
.native_activity
)
105 layout
= layout
.new_global_ref
109 private var adapter
: Android_widget_ArrayAdapter do
110 var adapter
= new Android_widget_ArrayAdapter(app
.native_activity
,
111 android_r_layout_simple_list_item_1
, self)
112 native
.set_adapter adapter
113 return adapter
.new_global_ref
119 if item
isa View then adapter
.add item
.native
122 private fun create_view
(position
: Int): NativeView
124 var ctrl
= items
[position
]
130 redef class Android_widget_ArrayAdapter
131 private new (context
: NativeContext, res
: Int, sender
: ListLayout)
132 import ListLayout.create_view
in "Java" `{
133 final int final_sender_object = sender;
135 return new android.widget.ArrayAdapter(context, (int)res) {
137 public android.view.View getView(int position, android.view.View convertView, android.view.ViewGroup parent) {
138 return ListLayout_create_view(final_sender_object, position);
145 redef type NATIVE: NativeTextView
147 redef fun text
do return native
.text
.to_s
148 redef fun text
=(value
) do
149 if value
== null then value
= ""
150 native
.text
= value
.to_java_string
154 fun text_size
: Float do return native
.text_size
157 fun text_size
=(text_size
: nullable Float) do
158 if text_size
!= null then native
.text_size
= text_size
163 redef type NATIVE: NativeTextView
164 redef var native
do return (new NativeTextView(app
.native_activity
)).new_global_ref
166 init do native
.set_text_appearance
(app
.native_activity
, android_r_style_text_appearance_medium
)
169 redef class TextInput
170 redef type NATIVE: NativeEditText
171 redef var native
= (new NativeEditText(app
.native_activity
)).new_global_ref
177 redef type NATIVE: NativeButton
178 redef var native
= (new NativeButton(app
.native_activity
, self)).new_global_ref
180 private fun on_click
do notify_observers
new ButtonPressEvent(self)
182 redef fun finalize
do native
.delete_global_ref
185 redef class NativeButton
186 private new (context
: NativeActivity, sender_object
: Button)
187 import Button.on_click
in "Java" `{
188 final int final_sender_object = sender_object;
190 return new android.widget.Button(context){
192 public boolean onTouchEvent(android.view.MotionEvent event) {
193 if(event.getAction() == android.view.MotionEvent.ACTION_DOWN) {
194 Button_on_click(final_sender_object);