# limitations under the License.
# Views and services to use the Android native user interface
-module ui
+module ui is
+ # `adjustPan` allows to use EditText in a ListLayout
+ android_manifest_activity """android:windowSoftInputMode="adjustPan""""
+end
# Implementation note:
#
type NATIVE: JavaObject
end
+redef class NativeActivity
+
+ private fun remove_title_bar in "Java" `{
+ self.requestWindowFeature(android.view.Window.FEATURE_NO_TITLE);
+ `}
+
+ # Insert a single layout as the root of the activity window
+ private fun insert_root_layout(root_layout_id: Int)
+ in "Java" `{
+ android.widget.FrameLayout layout = new android.widget.FrameLayout(self);
+ layout.setId((int)root_layout_id);
+ self.setContentView(layout);
+ `}
+
+ # Replace the currently visible fragment, if any, with `native_fragment`
+ private fun show_fragment(root_layout_id: Int, native_fragment: Android_app_Fragment)
+ in "Java" `{
+ android.app.FragmentTransaction transaction = self.getFragmentManager().beginTransaction();
+ transaction.replace((int)root_layout_id, native_fragment);
+ transaction.commit();
+ `}
+end
+
+redef class App
+ redef fun on_create
+ do
+ app.native_activity.remove_title_bar
+ native_activity.insert_root_layout(root_layout_id)
+ super
+ end
+
+ # Identifier of the container holding the fragments
+ private var root_layout_id = 0xFFFF
+
+ redef fun window=(window)
+ do
+ native_activity.show_fragment(root_layout_id, window.native)
+ super
+ end
+end
+
+redef class Activity
+ redef fun on_back_pressed
+ do
+ var window = app.window
+ if window.enable_back_button then
+ window.on_back_button
+ return true
+ end
+
+ return false
+ end
+end
+
+# On Android, a window is implemented with the fragment `native`
redef class Window
- redef var native = app.native_activity.new_global_ref
+ redef var native = (new Android_app_Fragment(self)).new_global_ref
+
+ redef type NATIVE: Android_app_Fragment
- redef type NATIVE: NativeActivity
+ # Root high-level view of this window
+ var view: nullable View = null
redef fun add(item)
do
+ if item isa View then view = item
super
+ end
+
+ private fun on_create_fragment: NativeView
+ do
+ on_create
- # FIXME abstract the Android restriction where `content_view` must be a layout
- assert item isa Layout
- native.content_view = item.native
+ var view = view
+ assert view != null else print_error "{class_name} needs a `view` after `Window::on_create` returns"
+ return view.native
end
end
native.text = value.to_java_string
end
- # Size of the text
- fun text_size: Float do return native.text_size
+ redef fun size=(size) do set_size_native(app.native_activity, native, size or else 1.0)
- # Size of the text
- fun text_size=(text_size: nullable Float) do
- if text_size != null then native.text_size = text_size
- end
+ private fun set_size_native(context: NativeContext, view: NativeTextView, size: Float)
+ in "Java" `{
+ int s;
+ if (size == 1.0d)
+ s = android.R.style.TextAppearance_Medium;
+ else if (size < 1.0d)
+ s = android.R.style.TextAppearance_Small;
+ else // if (size > 1.0d)
+ s = android.R.style.TextAppearance_Large;
+
+ view.setTextAppearance(context, s);
+ `}
+
+ redef fun align=(align) do set_align_native(native, align or else 0.0)
+
+ private fun set_align_native(view: NativeTextView, align: Float)
+ in "Java" `{
+ int g;
+ if (align == 0.5d)
+ g = android.view.Gravity.CENTER_HORIZONTAL;
+ else if (align < 0.5d)
+ g = android.view.Gravity.LEFT;
+ else // if (align > 0.5d)
+ g = android.view.Gravity.RIGHT;
+
+ view.setGravity(g);
+ `}
end
redef class Label
redef type NATIVE: NativeTextView
redef var native do return (new NativeTextView(app.native_activity)).new_global_ref
-
- init do native.set_text_appearance(app.native_activity, android_r_style_text_appearance_medium)
end
redef class CheckBox
};
`}
end
+
+redef class Android_app_Fragment
+ private new (nit_window: Window)
+ import Window.on_create_fragment in "Java" `{
+ final int final_nit_window = nit_window;
+
+ return new android.app.Fragment(){
+ @Override
+ public android.view.View onCreateView(android.view.LayoutInflater inflater,
+ android.view.ViewGroup container, android.os.Bundle state) {
+
+ return Window_on_create_fragment(final_nit_window);
+ }
+ };
+ `}
+end