lib & examples: update users of Android API version annotations
[nit.git] / lib / android / ui / native_ui.nit
1 # This file is part of NIT (http://www.nitlanguage.org).
2 #
3 # Copyright 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 # Native services from the `android.view` and `android.widget` namespaces
18 module native_ui is android_api_min 14
19
20 import nit_activity
21
22 in "Java" `{
23 import android.app.Activity;
24
25 import android.view.Gravity;
26 import android.view.ViewGroup;
27 import android.view.ViewGroup.MarginLayoutParams;
28
29 import android.widget.Button;
30 import android.widget.LinearLayout;
31 import android.widget.GridLayout;
32 import android.widget.PopupWindow;
33 import android.widget.TextView;
34
35 import java.lang.*;
36 import java.util.*;
37 `}
38
39 redef extern class NativeActivity
40
41 # Set the main layout of this activity
42 fun content_view=(layout: NativeViewGroup) in "Java" `{
43 final ViewGroup final_layout = layout;
44 final Activity final_recv = recv;
45
46 recv.runOnUiThread(new Runnable() {
47 @Override
48 public void run() {
49 final_recv.setContentView(final_layout);
50
51 final_layout.requestFocus();
52 }
53 });
54 `}
55 end
56
57 # A `View` for Android
58 extern class NativeView in "Java" `{ android.view.View `}
59 super JavaObject
60
61 fun minimum_width=(val: Int) in "Java" `{ recv.setMinimumWidth((int)val); `}
62 fun minimum_height=(val: Int) in "Java" `{ recv.setMinimumHeight((int)val); `}
63 end
64
65 # A collection of `NativeView`
66 extern class NativeViewGroup in "Java" `{ android.view.ViewGroup `}
67 super NativeView
68
69 fun add_view(view: NativeView) in "Java" `{ recv.addView(view); `}
70 end
71
72 # A `NativeViewGroup` organized in a line
73 extern class NativeLinearLayout in "Java" `{ android.widget.LinearLayout `}
74 super NativeViewGroup
75
76 new(context: NativeActivity) in "Java" `{ return new LinearLayout(context); `}
77
78 fun set_vertical in "Java" `{ recv.setOrientation(LinearLayout.VERTICAL); `}
79 fun set_horizontal in "Java" `{ recv.setOrientation(LinearLayout.HORIZONTAL); `}
80
81 redef fun add_view(view) in "Java"
82 `{
83 MarginLayoutParams params = new MarginLayoutParams(
84 LinearLayout.LayoutParams.MATCH_PARENT,
85 LinearLayout.LayoutParams.WRAP_CONTENT);
86 recv.addView(view, params);
87 `}
88
89 fun add_view_with_weight(view: NativeView, weight: Float)
90 in "Java" `{
91 recv.addView(view, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, (float)weight));
92 `}
93 end
94
95 # A `NativeViewGroup` organized as a grid
96 extern class NativeGridLayout in "Java" `{ android.widget.GridLayout `}
97 super NativeViewGroup
98
99 new(context: NativeActivity) in "Java" `{ return new android.widget.GridLayout(context); `}
100
101 fun row_count=(val: Int) in "Java" `{ recv.setRowCount((int)val); `}
102
103 fun column_count=(val: Int) in "Java" `{ recv.setColumnCount((int)val); `}
104
105 redef fun add_view(view) in "Java" `{ recv.addView(view); `}
106 end
107
108 extern class NativePopupWindow in "Java" `{ android.widget.PopupWindow `}
109 super NativeView
110
111 new (context: NativeActivity) in "Java" `{
112 PopupWindow recv = new PopupWindow(context);
113 recv.setWindowLayoutMode(LinearLayout.LayoutParams.MATCH_PARENT,
114 LinearLayout.LayoutParams.MATCH_PARENT);
115 recv.setClippingEnabled(false);
116 return recv;
117 `}
118
119 fun content_view=(layout: NativeViewGroup) in "Java" `{ recv.setContentView(layout); `}
120 end
121
122 extern class NativeTextView in "Java" `{ android.widget.TextView `}
123 super NativeView
124
125 new (context: NativeActivity) in "Java" `{ return new TextView(context); `}
126
127 fun text: JavaString in "Java" `{ return recv.getText().toString(); `}
128
129 fun text=(value: JavaString) in "Java" `{
130
131 final TextView final_recv = recv;
132 final String final_value = value;
133
134 ((Activity)recv.getContext()).runOnUiThread(new Runnable() {
135 @Override
136 public void run() {
137 final_recv.setText(final_value);
138 }
139 });
140 `}
141
142 fun enabled: Bool in "Java" `{ return recv.isEnabled(); `}
143 fun enabled=(value: Bool) in "Java" `{
144 final TextView final_recv = recv;
145 final boolean final_value = value;
146
147 ((Activity)recv.getContext()).runOnUiThread(new Runnable() {
148 @Override
149 public void run() {
150 final_recv.setEnabled(final_value);
151 }
152 });
153 `}
154
155 fun gravity_center in "Java" `{
156 recv.setGravity(Gravity.CENTER);
157 `}
158
159 fun text_size=(dpi: Float) in "Java" `{
160 recv.setTextSize(android.util.TypedValue.COMPLEX_UNIT_DIP, (float)dpi);
161 `}
162 end
163
164 extern class NativeEditText in "Java" `{ android.widget.EditText `}
165 super NativeTextView
166
167 redef type SELF: NativeEditText
168
169 new (context: NativeActivity) in "Java" `{ return new android.widget.EditText(context); `}
170
171 fun width=(val: Int) in "Java" `{ recv.setWidth((int)val); `}
172
173 fun input_type_text in "Java" `{ recv.setInputType(android.text.InputType.TYPE_CLASS_TEXT); `}
174
175 redef fun new_global_ref: SELF import sys, Sys.jni_env `{
176 Sys sys = NativeEditText_sys(recv);
177 JNIEnv *env = Sys_jni_env(sys);
178 return (*env)->NewGlobalRef(env, recv);
179 `}
180 end
181
182 extern class NativeButton in "Java" `{ android.widget.Button `}
183 super NativeTextView
184
185 redef type SELF: NativeButton
186
187 redef fun new_global_ref: SELF import sys, Sys.jni_env `{
188 Sys sys = NativeButton_sys(recv);
189 JNIEnv *env = Sys_jni_env(sys);
190 return (*env)->NewGlobalRef(env, recv);
191 `}
192 end