1
1
package org .kivy .android ;
2
2
3
- import android .app .Activity ;
4
3
import android .app .PendingIntent ;
5
4
import android .app .Service ;
6
5
import android .content .Context ;
7
6
import android .content .Intent ;
7
+ import android .content .pm .ApplicationInfo ;
8
8
import android .os .Bundle ;
9
- import android .os .IBinder ;
10
9
import android .os .Process ;
11
10
import android .support .v4 .app .NotificationCompat ;
12
11
import android .util .Log ;
13
12
14
- public class PythonService extends Service implements Runnable {
13
+ public abstract class PythonService extends Service implements Runnable {
15
14
private static String TAG = PythonService .class .getSimpleName ();
16
15
17
- public static PythonService mService = null ;
18
16
/**
19
17
* Intent that started the service
20
18
*/
@@ -31,26 +29,16 @@ public class PythonService extends Service implements Runnable {
31
29
private String serviceEntrypoint ;
32
30
private String pythonServiceArgument ;
33
31
34
- private boolean autoRestartService = false ;
35
-
36
- public void setAutoRestartService (boolean restart ) {
37
- autoRestartService = restart ;
38
- }
39
-
40
- public boolean canDisplayNotification () {
41
- return true ;
32
+ public int getStartType () {
33
+ return START_NOT_STICKY ;
42
34
}
43
35
44
- public int startType () {
45
- return START_NOT_STICKY ;
36
+ public boolean getStartForeground () {
37
+ return false ;
46
38
}
47
39
48
- /**
49
- * {@inheritDoc}
50
- */
51
- @ Override
52
- public IBinder onBind (Intent intent ) {
53
- return null ;
40
+ public boolean getAutoRestart () {
41
+ return false ;
54
42
}
55
43
56
44
/**
@@ -88,28 +76,30 @@ public int onStartCommand(Intent intent, int flags, int startId) {
88
76
pythonThread = new Thread (this );
89
77
pythonThread .start ();
90
78
91
- if (canDisplayNotification ()) {
79
+ if (getStartForeground ()) {
92
80
doStartForeground (extras );
93
81
}
94
82
95
- return startType ();
83
+ return getStartType ();
96
84
}
97
85
98
86
protected void doStartForeground (Bundle extras ) {
99
- String serviceTitle = extras . getString ( "serviceTitle" );
100
- String serviceDescription = extras . getString ( "serviceDescription" );
87
+ Context appContext = getApplicationContext ( );
88
+ ApplicationInfo appInfo = appContext . getApplicationInfo ( );
101
89
102
- Context context = getApplicationContext ();
90
+ String serviceTitle = extras .getString ("serviceTitle" , TAG );
91
+ String serviceDescription = extras .getString ("serviceDescription" , "" );
92
+ int serviceIconId = extras .getInt ("serviceIconId" , appInfo .icon );
103
93
104
94
NotificationCompat .Builder builder =
105
95
new NotificationCompat .Builder (this )
106
- .setSmallIcon (context . getApplicationInfo (). icon )
96
+ .setSmallIcon (serviceIconId )
107
97
.setContentTitle (serviceTitle )
108
98
.setContentText (serviceDescription );
109
99
110
100
int NOTIFICATION_ID = 1 ;
111
101
112
- Intent targetIntent = new Intent (this , Activity .class );
102
+ Intent targetIntent = new Intent (this , MainActivity .class );
113
103
PendingIntent contentIntent = PendingIntent .getActivity (this , 0 , targetIntent , PendingIntent .FLAG_UPDATE_CURRENT );
114
104
builder .setContentIntent (contentIntent );
115
105
@@ -123,7 +113,7 @@ protected void doStartForeground(Bundle extras) {
123
113
public void onDestroy () {
124
114
super .onDestroy ();
125
115
pythonThread = null ;
126
- if (autoRestartService && startIntent != null ) {
116
+ if (getAutoRestart () && startIntent != null ) {
127
117
Log .v (TAG , "Service restart requested" );
128
118
startService (startIntent );
129
119
}
@@ -136,9 +126,8 @@ public void onDestroy() {
136
126
@ Override
137
127
public void run () {
138
128
PythonUtil .loadLibraries (getFilesDir ());
139
- mService = this ;
140
- nativeStart (androidPrivate , androidArgument , serviceEntrypoint ,
141
- pythonName , pythonHome , pythonPath , pythonServiceArgument );
129
+ nativeStart (androidPrivate , androidArgument , serviceEntrypoint , pythonName , pythonHome ,
130
+ pythonPath , pythonServiceArgument );
142
131
stopSelf ();
143
132
}
144
133
@@ -151,8 +140,8 @@ public void run() {
151
140
* @param pythonPath Python path
152
141
* @param pythonServiceArgument Argument to pass to Python code
153
142
*/
154
- public static native void nativeStart (String androidPrivate ,
155
- String androidArgument , String serviceEntrypoint ,
156
- String pythonName , String pythonHome , String pythonPath ,
143
+ public static native void nativeStart (String androidPrivate , String androidArgument ,
144
+ String serviceEntrypoint , String pythonName ,
145
+ String pythonHome , String pythonPath ,
157
146
String pythonServiceArgument );
158
147
}
0 commit comments