nit: Added link to `CONTRIBUTING.md` from the README
[nit.git] / contrib / benitlux / src / client / android.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 # Android variant improved with platform specific services
16 module android is
17 android_manifest_activity """android:theme="@android:style/Theme.DeviceDefault" """
18 android_api_min 16 # For BigTextStyle
19 android_api_target 16
20 end
21
22 import ::android::portrait
23 import ::android::toast
24 import ::android::wifi
25 import ::android::service::at_boot
26
27 import client
28 import push
29 import checkins
30
31 redef class App
32
33 redef fun on_create
34 do
35 super
36
37 # Launch service with app, if it wasn't already launched at boot
38 start_service
39 end
40
41 # Use Android toasts if there is an activity, otherwise fallback on the log
42 redef fun feedback(text)
43 do
44 if activities.not_empty then
45 app.toast(text.to_s, false)
46 else super
47 end
48
49 # Register to callback `async_wifi_scan_available` when a wifi scan is available
50 private fun notify_on_wifi_scan(context: NativeContext)
51 import async_wifi_scan_available in "Java" `{
52
53 android.content.IntentFilter filter = new android.content.IntentFilter();
54 filter.addAction(android.net.wifi.WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
55 final int final_self = self;
56
57 context.registerReceiver(
58 new android.content.BroadcastReceiver() {
59 @Override
60 public void onReceive(android.content.Context context, android.content.Intent intent) {
61 if (intent.getAction().equals(android.net.wifi.WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) {
62 App_async_wifi_scan_available(final_self);
63 }
64 }
65 }, filter);
66 `}
67
68 private fun async_wifi_scan_available do run_on_ui_thread task_on_wifi_scan_available
69
70 private var task_on_wifi_scan_available = new WifiScanAvailable is lazy
71 end
72
73 redef class Service
74 redef fun on_start_command(intent, flags, id)
75 do
76 app.notify_on_wifi_scan native
77
78 # Check token validity
79 (new PushHttpRequest("push/check_token?token={app.token}")).start
80
81 return start_sticky
82 end
83 end
84
85 # Task ran on the UI thread when a wifi scan is available
86 private class WifiScanAvailable
87 super Task
88
89 redef fun main
90 do
91 jni_env.push_local_frame 4
92 var manager = app.native_context.wifi_manager
93 var networks = manager.get_scan_results
94 var found_ben = false
95 for i in networks.length.times do
96 jni_env.push_local_frame 4
97 var net = networks[i]
98 var ssid = net.ssid.to_s
99
100 # TODO use BSSID instead
101 #var bssid = net.bssid.to_s
102 var target_ssids = ["Benelux"]
103 if target_ssids.has(ssid) then # and bssid == "C8:F7:33:81:B0:E6" then
104 found_ben = true
105 break
106 end
107 jni_env.pop_local_frame
108 end
109 jni_env.pop_local_frame
110
111 if found_ben then
112 app.on_check_in
113 else app.on_check_out
114 end
115 end
116
117 redef class SectionTitle
118 init do set_text_style(native, app.native_context)
119
120 private fun set_text_style(view: NativeTextView, context: NativeContext) in "Java" `{
121 view.setTextAppearance(context, android.R.style.TextAppearance_Large);
122 `}
123 end
124
125 redef class ItemView
126 init do set_backgroud(native, app.native_context)
127
128 private fun set_backgroud(view: NativeView, context: NativeContext) in "Java" `{
129 int color = context.getResources().getIdentifier("item_background", "color", context.getPackageName());
130 view.setBackgroundResource(color);
131 `}
132 end
133
134 # Use Android notifications
135 redef fun notify(title, content, id)
136 do
137 var service = app.service
138 assert service != null
139 native_notify(service.native, id, title.to_java_string, content.to_java_string)
140 end
141
142 private fun native_notify(context: NativeService, id: Int, title, content: JavaString)
143 in "Java" `{
144 int icon = context.getResources().getIdentifier(
145 "notif", "drawable", context.getPackageName());
146
147 android.app.Notification.BigTextStyle style =
148 new android.app.Notification.BigTextStyle();
149 style.bigText(content);
150
151 android.content.Intent intent = new android.content.Intent(
152 context, nit.app.NitActivity.class);
153 android.app.PendingIntent pendingIntent = android.app.PendingIntent.getActivity(
154 context, 0, intent, android.app.PendingIntent.FLAG_UPDATE_CURRENT);
155
156 android.app.Notification notif = new android.app.Notification.Builder(context)
157 .setContentTitle(title)
158 .setContentText(content)
159 .setSmallIcon(icon)
160 .setAutoCancel(true)
161 .setOngoing(false)
162 .setStyle(style)
163 .setContentIntent(pendingIntent)
164 .setDefaults(android.app.Notification.DEFAULT_SOUND |
165 android.app.Notification.DEFAULT_LIGHTS)
166 .build();
167
168 android.app.NotificationManager notificationManager =
169 (android.app.NotificationManager)context.getSystemService(android.content.Context.NOTIFICATION_SERVICE);
170
171 notificationManager.notify((int)id, notif);
172 `}
173
174
175 # Use `RatingBar` as the beer rating control
176 redef class BeerView
177 redef fun setup_stars(rating)
178 do
179 var title = "Review %0".t.format(beer_info.beer.name).to_java_string
180 native_setup_stars(app.native_context, top_line_layout.native, rating, title, app.user != null)
181 end
182
183 private fun native_setup_stars(context: NativeContext, layout: NativeViewGroup, rating: Int, title: JavaString, loggedin: Bool)
184 import on_review in "Java" `{
185 // Set an indicator/non-interactive display
186 final android.widget.RatingBar view = new android.widget.RatingBar(
187 context, null, android.R.attr.ratingBarStyleIndicator);
188 view.setNumStars(5);
189 view.setRating(rating);
190 view.setIsIndicator(true);
191
192 final android.view.ViewGroup.MarginLayoutParams params = new android.view.ViewGroup.MarginLayoutParams(
193 android.widget.LinearLayout.LayoutParams.WRAP_CONTENT,
194 android.widget.LinearLayout.LayoutParams.FILL_PARENT);
195 layout.addView(view, params);
196
197 // Make some variables final to used in anonymous class and delayed methods
198 final android.content.Context final_context = context;
199 final long final_rating = rating;
200 final String final_title = title;
201 final boolean final_loggedin = loggedin;
202
203 final int final_self = self;
204 BeerView_incr_ref(self); // Nit GC
205
206 view.setOnTouchListener(new android.view.View.OnTouchListener() {
207 @Override
208 public boolean onTouch(android.view.View v, android.view.MotionEvent event) {
209 if (event.getAction() != android.view.MotionEvent.ACTION_UP) return true;
210
211 // Don't show dialog if not logged in
212 if (!final_loggedin) {
213 android.widget.Toast toast = android.widget.Toast.makeText(
214 final_context, "You must login first to post reviews",
215 android.widget.Toast.LENGTH_SHORT);
216 toast.show();
217 return true;
218 }
219
220 // Build dialog with a simple interactive RatingBar
221 final android.app.AlertDialog.Builder dialog_builder = new android.app.AlertDialog.Builder(final_context);
222 final android.widget.RatingBar rating = new android.widget.RatingBar(final_context);
223 rating.setNumStars(5);
224 rating.setStepSize(1.0f);
225 rating.setRating(final_rating);
226
227 // Header bar
228 int icon = final_context.getResources().getIdentifier("notif", "drawable", final_context.getPackageName());
229 dialog_builder.setIcon(icon);
230 dialog_builder.setTitle(final_title);
231
232 // Rating control
233 android.widget.LinearLayout l = new android.widget.LinearLayout(final_context);
234 l.addView(rating, params);
235 l.setHorizontalGravity(android.view.Gravity.CENTER_HORIZONTAL);
236 dialog_builder.setView(l);
237
238 // OK button
239 dialog_builder.setPositiveButton(android.R.string.ok,
240 new android.content.DialogInterface.OnClickListener() {
241 public void onClick(android.content.DialogInterface dialog, int which) {
242 dialog.dismiss();
243
244 long r = (long)rating.getRating();
245 view.setRating(r); // Update static control
246 view.invalidate(); // For not refreshing bug
247
248 BeerView_on_review(final_self, r); // Callback
249 BeerView_decr_ref(final_self); // Nit GC
250 }
251 });
252
253 // Cancel button
254 dialog_builder.setNegativeButton(android.R.string.cancel,
255 new android.content.DialogInterface.OnClickListener() {
256 public void onClick(android.content.DialogInterface dialog, int id) {
257 dialog.cancel();
258 BeerView_decr_ref(final_self); // Nit GC
259 }
260 });
261
262 dialog_builder.create();
263 dialog_builder.show();
264 return true;
265 }
266 });
267 `}
268 end