8000 service binder interface · chaubold/python-for-android@f0a35d6 · GitHub
[go: up one dir, main page]

Skip to content

Commit f0a35d6

Browse files
committed
service binder interface
1 parent 5a99afa commit f0a35d6

File tree

2 files changed

+42
-15
lines changed

2 files changed

+42
-15
lines changed

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

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,18 @@ public class PythonService extends Service implements Runnable {
3131
private String serviceEntrypoint;
3232
private String pythonServiceArgument;
3333

34-
protected boolean autoRestartService = false;
35-
protected boolean startForeground = true;
36-
37-
public int startType() {
34+
public int getStartType() {
3835
return START_NOT_STICKY;
3936
}
4037

38+
public boolean getStartForeground() {
39+
return false;
40+
}
41+
42+
public boolean getAutoRestart() {
43+
return false;
44+
}
45+
4146
/**
4247
* {@inheritDoc}
4348
*/
@@ -81,11 +86,11 @@ public int onStartCommand(Intent intent, int flags, int startId) {
8186
pythonThread = new Thread(this);
8287
pythonThread.start();
8388

84-
if (startForeground) {
89+
if (getStartForeground()) {
8590
doStartForeground(extras);
8691
}
8792

88-
return startType();
93+
return getStartType();
8994
}
9095

9196
protected void doStartForeground(Bundle extras) {
@@ -118,7 +123,7 @@ protected void doStartForeground(Bundle extras) {
118123
public void onDestroy() {
119124
super.onDestroy();
120125
pythonThread = null;
121-
if (autoRestartService && startIntent != null) {
126+
if (getAutoRestart() && startIntent != null) {
122127
Log.v(TAG, "Service restart requested");
123128
startService(startIntent);
124129
}
@@ -131,8 +136,8 @@ public void onDestroy() {
131136
@Override
132137
public void run() {
133138
PythonUtil.loadLibraries(getFilesDir());
134-
nativeStart(androidPrivate, androidArgument, serviceEntrypoint,
135-
pythonName, pythonHome, pythonPath, pythonServiceArgument);
139+
nativeStart(androidPrivate, androidArgument, serviceEntrypoint, pythonName, pythonHome,
140+
pythonPath, pythonServiceArgument);
136141
stopSelf();
137142
}
138143

@@ -145,8 +150,8 @@ public void run() {
145150
* @param pythonPath Python path
146151
* @param pythonServiceArgument Argument to pass to Python code
147152
*/
148-
public static native void nativeStart(String androidPrivate,
149-
String androidArgument, String serviceEntrypoint,
150-
String pythonName, String pythonHome, String pythonPath,
153+
public static native void nativeStart(String androidPrivate, String androidArgument,
154+
String serviceEntrypoint, String pythonName,
155+
String pythonHome, String pythonPath,
151156
String pythonServiceArgument);
152157
}

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

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,18 @@
44
import android.content.Context;
55
import org.kivy.android.PythonService;
66

7-
87
public class Service{{ name|capitalize }} extends PythonService {
8+
/**
9+
* Binder given to clients
10+
*/
11+
private final IBinder mBinder = new LocalBinder();
12+
913
{% if sticky %}
1014
/**
1115
* {@inheritDoc}
1216
*/
1317
@Override
14-
public int startType() {
18+
public int getStartType() {
1519
return START_STICKY;
1620
}
1721
{% endif %}
@@ -21,7 +25,7 @@ public int startType() {
2125
* {@inheritDoc}
2226
*/
2327
@Override
24-
public boolean canDisplayNotification() {
28+
public boolean getStartForeground() {
2529
return false;
2630
}
2731
{% endif %}
@@ -46,4 +50,22 @@ public static void stop(Context ctx) {
4650
ctx.stopService(intent);
4751
}
4852

53+
/**
54+
* Class used for the client Binder. Because we know this service always
55+
* runs in the same process as its clients, we don't need to deal with IPC.
56+
*/
57+
public class Service{{ name|capitalize }}Binder extends Binder {
58+
Service{{ name|capitalize }} getService() {
59+
// Return this instance of Service{{ name|capitalize }} so clients can call public methods
60+
return Service{{ name|capitalize }}.this;
61+
}
62+
}
63+
64+
/**
65+
* {@inheritDoc}
66+
*/
67+
@Override
68+
public IBinder onBind(Intent intent) {
69+
return mBinder;
70+
}
4971
}

0 commit comments

Comments
 (0)
0