Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add sticky services, auto-restart services, fix foreground option #643

Merged
merged 1 commit into from
Feb 16, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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