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
17 # `adjustPan` allows to use EditText in a ListLayout
18 android_manifest_activity
"""android:windowSoftInputMode="adjustPan""""
21 # Implementation note:
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.
32 private import data_store
36 # The Android element used to implement `self`
37 fun native: NATIVE is abstract
40 type NATIVE: JavaObject
43 redef class NativeActivity
45 private fun remove_title_bar in "Java" `{
46 self.requestWindowFeature(android.view.Window.FEATURE_NO_TITLE);
49 # Insert a single layout as the root of the activity window
50 private fun insert_root_layout(root_layout_id: Int)
52 android.widget.FrameLayout layout = new android.widget.FrameLayout(self);
53 layout.setId((int)root_layout_id);
54 self.setContentView(layout);
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)
60 android.app.FragmentTransaction transaction = self.getFragmentManager().beginTransaction();
61 transaction.replace((int)root_layout_id, native_fragment);
69 app.native_activity.remove_title_bar
70 native_activity.insert_root_layout(root_layout_id)
74 # Identifier of the container holding the fragments
75 private var root_layout_id = 0xFFFF
77 redef fun window=(window)
79 native_activity.show_fragment(root_layout_id, window.native)
85 redef fun on_back_pressed
87 var window = app.window
88 if window.enable_back_button then
97 # On Android, a window is implemented with the fragment `native`
99 redef var native = (new Android_app_Fragment(self)).new_global_ref
101 redef type NATIVE: Android_app_Fragment
103 # Root high-level view of this window
104 var view: nullable View = null
108 if item isa View then view = item
112 private fun on_create_fragment: NativeView
117 assert view != null else print_error "{class_name} needs a
`view` after
`Window::on_create` returns
"
123 redef type NATIVE: NativeView
125 redef fun enabled=(enabled) do native.enabled = enabled or else true
126 redef fun enabled do return native.enabled
130 redef type NATIVE: NativeViewGroup
138 # FIXME abstract the use either homogeneous or weight to balance views size in a layout
139 native.add_view_with_weight(item.native, 1.0)
142 redef fun remove(item)
145 if item isa View then native.remove_view item.native
149 redef class HorizontalLayout
151 var layout = new NativeLinearLayout(app.native_activity)
152 layout = layout.new_global_ref
153 layout.set_horizontal
158 redef class VerticalLayout
160 var layout = new NativeLinearLayout(app.native_activity)
161 layout = layout.new_global_ref
167 redef class ListLayout
168 redef type NATIVE: Android_widget_ListView
171 var layout = new Android_widget_ListView(app.native_activity)
172 layout = layout.new_global_ref
176 private var adapter: Android_widget_ArrayAdapter do
177 var adapter = new Android_widget_ArrayAdapter(app.native_activity,
178 android_r_layout_simple_list_item_1, self)
179 native.set_adapter adapter
180 return adapter.new_global_ref
186 if item isa View then adapter.add item.native
189 private fun create_view(position: Int): NativeView
191 var ctrl = items[position]
197 redef class Android_widget_ArrayAdapter
198 private new (context: NativeContext, res: Int, sender: ListLayout)
199 import ListLayout.create_view in "Java" `{
200 final int final_sender_object = sender;
202 return new android.widget.ArrayAdapter(context, (int)res) {
204 public android.view.View getView(int position, android.view.View convertView, android.view.ViewGroup parent) {
205 return ListLayout_create_view(final_sender_object, position);
212 redef type NATIVE: NativeTextView
214 redef fun text do return native.text.to_s
215 redef fun text=(value) do
216 if value == null then value = ""
217 native.text = value.to_java_string
220 redef fun size=(size) do set_size_native(app.native_activity, native, size or else 1.0)
222 private fun set_size_native(context: NativeContext, view: NativeTextView, size: Float)
226 s = android.R.style.TextAppearance_Medium;
227 else if (size < 1.0d)
228 s = android.R.style.TextAppearance_Small;
229 else // if (size > 1.0d)
230 s = android.R.style.TextAppearance_Large;
232 view.setTextAppearance(context, s);
235 redef fun align=(align) do set_align_native(native, align or else 0.0)
237 private fun set_align_native(view: NativeTextView, align: Float)
241 g = android.view.Gravity.CENTER_HORIZONTAL;
242 else if (align < 0.5d)
243 g = android.view.Gravity.LEFT;
244 else // if (align > 0.5d)
245 g = android.view.Gravity.RIGHT;
247 view.setGravity(g | android.view.Gravity.CENTER_VERTICAL);
252 redef type NATIVE: NativeTextView
253 redef var native do return (new NativeTextView(app.native_activity)).new_global_ref
257 redef type NATIVE: Android_widget_CompoundButton
258 redef var native do return (new Android_widget_CheckBox(app.native_activity)).new_global_ref
259 init do set_callback_on_toggle(native)
261 redef fun is_checked do return native.is_checked
262 redef fun is_checked=(value) do native.set_checked(value)
264 private fun on_toggle do notify_observers new ToggleEvent(self)
266 private fun set_callback_on_toggle(view: NATIVE)
267 import on_toggle in "Java" `{
268 final int final_sender_object = self;
269 CheckBox_incr_ref(final_sender_object);
271 view.setOnCheckedChangeListener(
272 new android.widget.CompoundButton.OnCheckedChangeListener() {
274 public void onCheckedChanged(android.widget.CompoundButton buttonView, boolean isChecked) {
275 CheckBox_on_toggle(final_sender_object);
281 redef class TextInput
282 redef type NATIVE: NativeEditText
283 redef var native = (new NativeEditText(app.native_activity)).new_global_ref
285 redef fun is_password=(value)
287 native.is_password = value or else false
292 redef class NativeEditText
294 # Configure this view to hide passwords
295 fun is_password=(value: Bool) in "Java" `{
297 self.setInputType(android.text.InputType.TYPE_CLASS_TEXT | android.text.InputType.TYPE_TEXT_VARIATION_PASSWORD);
298 self.setTransformationMethod(android.text.method.PasswordTransformationMethod.getInstance());
300 self.setInputType(android.text.InputType.TYPE_CLASS_TEXT);
301 self.setTransformationMethod(null);
309 redef type NATIVE: NativeButton
310 redef var native = (new NativeButton(app.native_activity, self)).new_global_ref
312 private fun on_click do notify_observers new ButtonPressEvent(self)
314 redef fun finalize do native.delete_global_ref
317 redef class NativeButton
318 private new (context: NativeActivity, sender_object: Button)
319 import Button.on_click in "Java" `{
320 final int final_sender_object = sender_object;
321 Button_incr_ref(final_sender_object);
323 return new android.widget.Button(context){
325 public boolean onTouchEvent(android.view.MotionEvent event) {
326 if(event.getAction() == android.view.MotionEvent.ACTION_DOWN) {
327 Button_on_click(final_sender_object);
336 redef class Android_app_Fragment
337 private new (nit_window: Window)
338 import Window.on_create_fragment in "Java" `{
339 final int final_nit_window = nit_window;
341 return new android.app.Fragment(){
343 public android.view.View onCreateView(android.view.LayoutInflater inflater,
344 android.view.ViewGroup container, android.os.Bundle state) {
346 return Window_on_create_fragment(final_nit_window);