8000 Merge pull request #628 from kived/service-foreground · mixedCase/python-for-android@b59e2dc · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit b59e2dc

Browse files
committed
Merge pull request kivy#628 from kived/service-foreground
add foreground option to segregated services
2 parents 452afaa + 9c5415b commit b59e2dc

File tree

3 files changed

+37
-13
lines changed

3 files changed

+37
-13
lines changed

pythonforandroid/bootstraps/sdl2/build/build.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -305,15 +305,25 @@ def make_package(args):
305305
service = True
306306

307307
service_names = []
308-
for entrypoint in args.services:
309-
name, entrypoint = entrypoint.split(":", 1)
308+
for sid, spec in enumerate(args.services):
309+
spec = spec.split(':')
310+
name = spec[0]
311+
entrypoint = spec[1]
312+
options = spec[2:]
313+
314+
foreground = False
315+
if 'foreground' in options:
316+
foreground = True
317+
310318
service_names.append(name)
311319
render(
312320
'Service.tmpl.java',
313321
'src/{}/Service{}.java'.format(args.package.replace(".", "/"), name.capitalize()),
314322
name=name,
315323
entrypoint=entrypoint,
316-
args=args
324+
args=args,
325+
foreground=foreground,
326+
service_id=sid + 1,
317327
)
318328

319329
render(
@@ -427,7 +437,8 @@ def parse_args(args=None):
427437
ap.add_argument('--with-billing', dest='billing_pubkey',
428438
help='If set, the billing service will be added (not implemented)')
429439
ap.add_argument('--service', dest='services', action='append',
430-
help='Declare a new service entrypoint: NAME:PATH_TO_PY')
440+
help='Declare a new service entrypoint: '
441+
'NAME:PATH_TO_PY[:foreground]')
431442
ap.add_argument('--add-source', dest='extra_source_dirs', action='append',
432443
help='Include additional source dirs in Java build')
433444

pythonforandroid/bootstraps/sdl2/build/src/org/kivy/android/PythonService.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,22 +64,25 @@ public int onStartCommand(Intent intent, int flags, int startId) {
6464
pythonThread = new Thread(this);
6565
pythonThread.start();
6666

67-
if (this.canDisplayNotification()) {
67+
doStartForeground(extras);
68+
69+
return START_NOT_STICKY;
70+
}
71+
72+
protected void doStartForeground(Bundle extras) {
73+
if (canDisplayNotification()) {
6874
String serviceTitle = extras.getString("serviceTitle");
6975
String serviceDescription = extras.getString("serviceDescription");
7076

7177
Context context = getApplicationContext();
7278
Notification notification = new Notification(context.getApplicationInfo().icon,
73-
serviceTitle,
74-
System.currentTimeMillis());
79+
serviceTitle, System.currentTimeMillis());
7580
Intent contextIntent = new Intent(context, PythonActivity.class);
7681
PendingIntent pIntent = PendingIntent.getActivity(context, 0, contextIntent,
77-
PendingIntent.FLAG_UPDATE_CURRENT);
82+
PendingIntent.FLAG_UPDATE_CURRENT);
7883
notification.setLatestEventInfo(context, serviceTitle, serviceDescription, pIntent);
7984
startForeground(1, notification);
8085
}
81-
82-
return START_NOT_STICKY;
8386
}
8487

8588
@Override
@@ -91,7 +94,7 @@ public void onDestroy() {
9194

9295
@Override
9396
public void run(){
94-
PythonUtil.loadLibraries(getFilesDir());
97+
PythonUtil.loadLibraries(getFilesDir());
9598
this.mService = this;
9699
nativeStart(
97100
androidPrivate, androidArgument,

pythonforandroid/bootstraps/sdl2/build/templates/Service.tmpl.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,24 @@
22

33
import android.content.Intent;
44
import android.content.Context;
5+
import android.app.Notification;
6+
import android.app.PendingIntent;
7+
import android.os.Bundle;
58
import org.kivy.android.PythonService;
69
import org.kivy.android.PythonActivity;
710

811

912
public class Service{{ name|capitalize }} extends PythonService {
1013
@Override
11-
public boolean canDisplayNotification() {
12-
return false;
14+
protected void doStartForeground(Bundle extras) {
15+
Context context = getApplicationContext();
16+
Notification notification = new Notification(context.getApplicationInfo().icon,
17+
"{{ args.name }}", System.currentTimeMillis());
18+
Intent contextIntent = new Intent(context, PythonActivity.class);
19+
PendingIntent pIntent = PendingIntent.getActivity(context, 0, contextIntent,
20+
PendingIntent.FLAG_UPDATE_CURRENT);
21+
notification.setLatestEventInfo(context, "{{ args.name }}", "{{ name| capitalize }}", pIntent);
22+
startForeground({{ service_id }}, notification);
1323
}
1424

1525
static public void start(Context ctx, String pythonServiceArgument) {

0 commit comments

Comments
 (0)
0