11package org .kivy .android ;
22
3- import android .os .Build ;
4- import java .lang .reflect .Method ;
5- import java .lang .reflect .InvocationTargetException ;
6- import android .app .Service ;
7- import android .os .IBinder ;
8- import android .os .Bundle ;
9- import android .content .Intent ;
10- import android .content .Context ;
11- import android .util .Log ;
123import android .app .Notification ;
4+ import android .app .NotificationChannel ;
5+ import android .app .NotificationManager ;
136import android .app .PendingIntent ;
7+ import android .app .Service ;
8+ import android .content .Context ;
9+ import android .content .Intent ;
10+ import android .graphics .Color ;
11+ import android .os .Build ;
12+ import android .os .Bundle ;
13+ import android .os .IBinder ;
1414import android .os .Process ;
15+ import android .util .Log ;
1516import java .io .File ;
16-
17- //imports for channel definition
18- import android .app .NotificationManager ;
19- import android .app .NotificationChannel ;
20- import android .graphics .Color ;
17+ import java .lang .reflect .InvocationTargetException ;
18+ import java .lang .reflect .Method ;
2119
2220public class PythonService extends Service implements Runnable {
2321
@@ -34,7 +32,6 @@ public class PythonService extends Service implements Runnable {
3432 // Argument to pass to Python code,
3533 private String pythonServiceArgument ;
3634
37-
3835 public static PythonService mService = null ;
3936 private Intent startIntent = null ;
4037
@@ -64,7 +61,7 @@ public int onStartCommand(Intent intent, int flags, int startId) {
6461 Log .v ("python service" , "service exists, do not start again" );
6562 return startType ();
6663 }
67- // intent is null if OS restarts a STICKY service
64+ // intent is null if OS restarts a STICKY service
6865 if (intent == null ) {
6966 Context context = getApplicationContext ();
7067 intent = getThisDefaultIntent (context , "" );
@@ -78,9 +75,8 @@ public int onStartCommand(Intent intent, int flags, int startId) {
7875 pythonName = extras .getString ("pythonName" );
7976 pythonHome = extras .getString ("pythonHome" );
8077 pythonPath = extras .getString ("pythonPath" );
81- boolean serviceStartAsForeground = (
82- extras .getString ("serviceStartAsForeground" ).equals ("true" )
83- );
78+ boolean serviceStartAsForeground =
79+ (extras .getString ("serviceStartAsForeground" ).equals ("true" ));
8480 pythonServiceArgument = extras .getString ("pythonServiceArgument" );
8581 pythonThread = new Thread (this );
8682 pythonThread .start ();
@@ -108,51 +104,68 @@ protected void doStartForeground(Bundle extras) {
108104 Notification notification ;
109105 Context context = getApplicationContext ();
110106 Intent contextIntent = new Intent (context , PythonActivity .class );
111- PendingIntent pIntent = PendingIntent .getActivity (context , 0 , contextIntent ,
112- PendingIntent .FLAG_IMMUTABLE | PendingIntent .FLAG_UPDATE_CURRENT );
113-
114- // Unspecified icon uses default.
115- int smallIconId = context .getApplicationInfo ().icon ;
116- if (smallIconName != null ) {
117- if (!smallIconName .equals ("" )){
118- int resId = getResources ().getIdentifier (smallIconName , "mipmap" ,
119- getPackageName ());
120- if (resId ==0 ) {
121- resId = getResources ().getIdentifier (smallIconName , "drawable" ,
122- getPackageName ());
123- }
124- if (resId !=0 ) {
125- smallIconId = resId ;
107+ PendingIntent pIntent =
108+ PendingIntent .getActivity (
109+ context ,
110+ 0 ,
111+ contextIntent ,
112+ PendingIntent .FLAG_IMMUTABLE | PendingIntent .FLAG_UPDATE_CURRENT );
113+
114+ // Unspecified icon uses default.
115+ int smallIconId = context .getApplicationInfo ().icon ;
116+ if (smallIconName != null ) {
117+ if (!smallIconName .equals ("" )) {
118+ int resId = getResources ().getIdentifier (smallIconName , "mipmap" , getPackageName ());
119+ if (resId == 0 ) {
120+ resId =
121+ getResources ()
122+ .getIdentifier (smallIconName , "drawable" , getPackageName ());
123+ }
124+ if (resId != 0 ) {
125+ smallIconId = resId ;
126+ }
126127 }
127128 }
128- }
129129
130- if (Build .VERSION .SDK_INT < Build .VERSION_CODES .O ) {
131- // This constructor is deprecated
132- notification = new Notification (
133- smallIconId , serviceTitle , System .currentTimeMillis ());
130+ if (Build .VERSION .SDK_INT < Build .VERSION_CODES .O ) {
131+ // This constructor is deprecated
132+ notification = new Notification (smallIconId , serviceTitle , System .currentTimeMillis ());
134133 try {
135134 // prevent using NotificationCompat, this saves 100kb on apk
136- Method func = notification .getClass ().getMethod (
137- "setLatestEventInfo" , Context .class , CharSequence .class ,
138- CharSequence .class , PendingIntent .class );
135+ Method func =
136+ notification
137+ .getClass ()
138+ .getMethod (
139+ "setLatestEventInfo" ,
140+ Context .class ,
141+ CharSequence .class ,
142+ CharSequence .class ,
143+ PendingIntent .class );
139144 func .invoke (notification , context , contentTitle , contentText , pIntent );
140- } catch (NoSuchMethodException | IllegalAccessException |
141- IllegalArgumentException | InvocationTargetException e ) {
145+ } catch (NoSuchMethodException
146+ | IllegalAccessException
147+ | IllegalArgumentException
148+ | InvocationTargetException e ) {
142149 }
143150 } else {
144151 // for android 8+ we need to create our own channel
145152 // https://stackoverflow.com/questions/47531742/startforeground-fail-after-upgrade-to-android-8-1
146153 String NOTIFICATION_CHANNEL_ID = "org.kivy.p4a" + getServiceId ();
147154 String channelName = "Background Service" + getServiceId ();
148- NotificationChannel chan = new NotificationChannel (NOTIFICATION_CHANNEL_ID , channelName , NotificationManager .IMPORTANCE_NONE );
155+ NotificationChannel chan =
156+ new NotificationChannel (
157+ NOTIFICATION_CHANNEL_ID ,
158+ channelName ,
159+ NotificationManager .IMPORTANCE_NONE );
149160
150161 chan .setLightColor (Color .BLUE );
151162 chan .setLockscreenVisibility (Notification .VISIBILITY_PRIVATE );
152- NotificationManager manager = (NotificationManager ) getSystemService (Context .NOTIFICATION_SERVICE );
163+ NotificationManager manager =
164+ (NotificationManager ) getSystemService (Context .NOTIFICATION_SERVICE );
153165 manager .createNotificationChannel (chan );
154166
155- Notification .Builder builder = new Notification .Builder (context , NOTIFICATION_CHANNEL_ID );
167+ Notification .Builder builder =
168+ new Notification .Builder (context , NOTIFICATION_CHANNEL_ID );
156169 builder .setContentTitle (contentTitle );
157170 builder .setContentText (contentText );
158171 builder .setContentIntent (pIntent );
@@ -174,37 +187,42 @@ public void onDestroy() {
174187 }
175188
176189 /**
177- * Stops the task gracefully when killed.
178- * Calling stopSelf() will trigger a onDestroy() call from the system.
190+ * Stops the task gracefully when killed. Calling stopSelf() will trigger a onDestroy() call
191+ * from the system.
179192 */
180193 @ Override
181194 public void onTaskRemoved (Intent rootIntent ) {
182195 super .onTaskRemoved (rootIntent );
183- //sticky service runtime/restart is managed by the OS. leave it running when app is closed
196+ // sticky service runtime/restart is managed by the OS. leave it running when app is closed
184197 if (startType () != START_STICKY ) {
185198 stopSelf ();
186199 }
187200 }
188201
189202 @ Override
190- public void run (){
191- String app_root = getFilesDir ().getAbsolutePath () + "/app" ;
203+ public void run () {
204+ String app_root = getFilesDir ().getAbsolutePath () + "/app" ;
192205 File app_root_file = new File (app_root );
193- PythonUtil .loadLibraries (app_root_file ,
194- new File (getApplicationInfo ().nativeLibraryDir ));
206+ PythonUtil .loadLibraries (app_root_file , new File (getApplicationInfo ().nativeLibraryDir ));
195207 this .mService = this ;
196208 nativeStart (
197- androidPrivate , androidArgument ,
198- serviceEntrypoint , pythonName ,
199- pythonHome , pythonPath ,
200- pythonServiceArgument );
209+ androidPrivate ,
210+ androidArgument ,
211+ serviceEntrypoint ,
212+ pythonName ,
213+ pythonHome ,
214+ pythonPath ,
215+ pythonServiceArgument );
201216 stopSelf ();
202217 }
203218
204219 // Native part
205220 public static native void nativeStart (
206- String androidPrivate , String androidArgument ,
207- String serviceEntrypoint , String pythonName ,
208- String pythonHome , String pythonPath ,
221+ String androidPrivate ,
222+ String androidArgument ,
223+ String serviceEntrypoint ,
224+ String pythonName ,
225+ String pythonHome ,
226+ String pythonPath ,
209227 String pythonServiceArgument );
210228}
0 commit comments