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