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 # Android variant improved with platform specific services
17 android_manifest_activity
"""android:theme="@android:style/Theme.DeviceDefault" """
18 android_api_min
16 # For BigTextStyle
22 import ::android
::portrait
23 import ::android
::toast
24 import ::android
::wifi
25 import ::android
::service
::at_boot
37 # Launch service with app, if it wasn't already launched at boot
41 # Use Android toasts if there is an activity, otherwise fallback on the log
42 redef fun feedback
(text
)
44 if activities
.not_empty
then
45 app
.toast
(text
.to_s
, false)
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" `{
53 android.content.IntentFilter filter = new android.content.IntentFilter();
54 filter.addAction(android.net.wifi.WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
55 final nit.app.NitObject final_self = self;
56 App_incr_ref(final_self);
58 context.registerReceiver(
59 new android.content.BroadcastReceiver() {
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);
69 private fun async_wifi_scan_available
do run_on_ui_thread task_on_wifi_scan_available
71 private var task_on_wifi_scan_available
= new WifiScanAvailable is lazy
75 redef fun on_start_command
(intent
, flags
, id
)
77 app
.notify_on_wifi_scan native
79 # Check token validity
80 (new PushHttpRequest("push/check_token?token={app.token}")).start
86 # Task ran on the UI thread when a wifi scan is available
87 private class WifiScanAvailable
92 jni_env
.push_local_frame
4
93 var manager
= app
.native_context
.wifi_manager
94 var networks
= manager
.get_scan_results
96 for i
in networks
.length
.times
do
97 jni_env
.push_local_frame
4
99 var ssid
= net
.ssid
.to_s
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
108 jni_env
.pop_local_frame
110 jni_env
.pop_local_frame
114 else app
.on_check_out
118 redef class SectionTitle
119 init do set_text_style
(native
, app
.native_context
)
121 private fun set_text_style
(view
: NativeTextView, context
: NativeContext) in "Java" `{
122 view.setTextAppearance(context, android.R.style.TextAppearance_Large);
127 init do set_background
(native
, app
.native_context
)
129 private fun set_background
(view
: NativeView, context
: NativeContext) in "Java" `{
130 view.setBackgroundResource(R.color.item_background);
134 # Use Android notifications
135 redef fun notify
(title
, content
, id
)
137 var service
= app
.service
138 assert service
!= null
139 native_notify
(service
.native
, id
, title
.to_java_string
, content
.to_java_string
)
142 private fun native_notify
(context
: NativeService, id
: Int, title
, content
: JavaString)
144 android.app.Notification.BigTextStyle style =
145 new android.app.Notification.BigTextStyle();
146 style.bigText(content);
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);
153 android.app.Notification notif = new android.app.Notification.Builder(context)
154 .setContentTitle(title)
155 .setContentText(content)
156 .setSmallIcon(R.drawable.notif)
160 .setContentIntent(pendingIntent)
161 .setDefaults(android.app.Notification.DEFAULT_SOUND |
162 android.app.Notification.DEFAULT_LIGHTS)
165 android.app.NotificationManager notificationManager =
166 (android.app.NotificationManager)context.getSystemService(android.content.Context.NOTIFICATION_SERVICE);
168 notificationManager.notify((int)id, notif);
172 # Use `RatingBar` as the beer rating control
174 redef fun setup_stars
(rating
)
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)
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);
186 view.setRating(rating);
187 view.setIsIndicator(true);
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);
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;
200 final nit.app.NitObject final_self = self;
201 BeerView_incr_ref(self); // Nit GC
203 view.setOnTouchListener(new android.view.View.OnTouchListener() {
205 public boolean onTouch(android.view.View v, android.view.MotionEvent event) {
206 if (event.getAction() != android.view.MotionEvent.ACTION_UP) return true;
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);
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);
225 dialog_builder.setIcon(R.drawable.notif);
226 dialog_builder.setTitle(final_title);
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);
235 dialog_builder.setPositiveButton(android.R.string.ok,
236 new android.content.DialogInterface.OnClickListener() {
237 public void onClick(android.content.DialogInterface dialog, int which) {
240 long r = (long)rating.getRating();
241 view.setRating(r); // Update static control
242 view.invalidate(); // For not refreshing bug
244 BeerView_on_review(final_self, r); // Callback
245 BeerView_decr_ref(final_self); // Nit GC
250 dialog_builder.setNegativeButton(android.R.string.cancel,
251 new android.content.DialogInterface.OnClickListener() {
252 public void onClick(android.content.DialogInterface dialog, int id) {
254 BeerView_decr_ref(final_self); // Nit GC
258 dialog_builder.create();
259 dialog_builder.show();