Skip to content

Commit

Permalink
add sticky services, auto-restart services, fix foreground option
Browse files Browse the repository at this point in the history
  • Loading branch information
kived committed Feb 15, 2016
1 parent 1664b8a commit 7ad96b2
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 19 deletions.
6 changes: 3 additions & 3 deletions pythonforandroid/bootstraps/sdl2/build/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,9 +311,8 @@ def make_package(args):
entrypoint = spec[1]
options = spec[2:]

foreground = False
if 'foreground' in options:
foreground = True
foreground = 'foreground' in options
sticky = 'sticky' in options

service_names.append(name)
render(
Expand All @@ -323,6 +322,7 @@ def make_package(args):
entrypoint=entrypoint,
args=args,
foreground=foreground,
sticky=sticky,
service_id=sid + 1,
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,23 @@ public class PythonService extends Service implements Runnable {
private String serviceEntrypoint;
// Argument to pass to Python code,
private String pythonServiceArgument;
public static Service mService = null;
public static PythonService mService = null;
private Intent startIntent = null;

private boolean autoRestartService = false;

public void setAutoRestartService(boolean restart) {
autoRestartService = restart;
}

public boolean canDisplayNotification() {
return true;
}

public int startType() {
return START_NOT_STICKY;
}

@Override
public IBinder onBind(Intent arg0) {
return null;
Expand All @@ -52,6 +63,7 @@ public int onStartCommand(Intent intent, int flags, int startId) {
return START_NOT_STICKY;
}

startIntent = intent;
Bundle extras = intent.getExtras();
androidPrivate = extras.getString("androidPrivate");
androidArgument = extras.getString("androidArgument");
Expand All @@ -64,31 +76,35 @@ public int onStartCommand(Intent intent, int flags, int startId) {
pythonThread = new Thread(this);
pythonThread.start();

doStartForeground(extras);
if (canDisplayNotification()) {
doStartForeground(extras);
}

return START_NOT_STICKY;
return startType();
}

protected void doStartForeground(Bundle extras) {
if (canDisplayNotification()) {
String serviceTitle = extras.getString("serviceTitle");
String serviceDescription = extras.getString("serviceDescription");

Context context = getApplicationContext();
Notification notification = new Notification(context.getApplicationInfo().icon,
serviceTitle, System.currentTimeMillis());
Intent contextIntent = new Intent(context, PythonActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(context, 0, contextIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(context, serviceTitle, serviceDescription, pIntent);
startForeground(1, notification);
}
String serviceTitle = extras.getString("serviceTitle");
String serviceDescription = extras.getString("serviceDescription");

Context context = getApplicationContext();
Notification notification = new Notification(context.getApplicationInfo().icon,
serviceTitle, System.currentTimeMillis());
Intent contextIntent = new Intent(context, PythonActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(context, 0, contextIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(context, serviceTitle, serviceDescription, pIntent);
startForeground(1, notification);
}

@Override
public void onDestroy() {
super.onDestroy();
pythonThread = null;
if (autoRestartService && startIntent != null) {
Log.v("python service", "service restart requested");
startService(startIntent);
}
Process.killProcess(Process.myPid());
}

Expand Down
14 changes: 14 additions & 0 deletions pythonforandroid/bootstraps/sdl2/build/templates/Service.tmpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,20 @@


public class Service{{ name|capitalize }} extends PythonService {
{% if sticky %}
@Override
public int startType() {
return START_STICKY;
}
{% endif %}

{% if not foreground %}
@Override
public boolean canDisplayNotification() {
return false;
}
{% endif %}

@Override
protected void doStartForeground(Bundle extras) {
Context context = getApplicationContext();
Expand Down

0 comments on commit 7ad96b2

Please sign in to comment.