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 ;
16+
1517import java .io .File ;
16- import android . app . NotificationManager ;
17- import android . app . NotificationChannel ;
18- import android . graphics . Color ;
18+ import java . lang . reflect . InvocationTargetException ;
19+ import java . lang . reflect . Method ;
20+
1921import org .qtproject .qt .android .bindings .QtService ;
2022
2123public class PythonService extends QtService implements Runnable {
24+
2225 private static final String TAG = "PythonQtService" ;
2326
2427 // Thread for Python code
@@ -31,7 +34,7 @@ public class PythonService extends QtService implements Runnable {
3134 private String pythonHome ;
3235 private String pythonPath ;
3336 private String serviceEntrypoint ;
34- // Argument to pass to Python code,
37+ // Argument to pass to Python code
3538 private String pythonServiceArgument ;
3639
3740 public static PythonService mService = null ;
@@ -42,7 +45,7 @@ public class PythonService extends QtService implements Runnable {
4245 public void setEnvironmentVariable (String key , String value ) {
4346 /**
4447 * Sets an environment variable based on key/value.
45- ** /
48+ */
4649 try {
4750 android .system .Os .setenv (key , value , true );
4851 } catch (Exception e ) {
@@ -74,8 +77,8 @@ public int onStartCommand(Intent intent, int flags, int startId) {
7477 Log .v (TAG , "service exists, do not start again" );
7578 return startType ();
7679 }
77-
78- // intent is null if OS restarts a STICKY service
80+
81+ // intent is null if OS restarts a STICKY service
7982 if (intent == null ) {
8083 Context context = getApplicationContext ();
8184 intent = getThisDefaultIntent (context , "" );
@@ -89,10 +92,10 @@ public int onStartCommand(Intent intent, int flags, int startId) {
8992 pythonName = extras .getString ("pythonName" );
9093 pythonHome = extras .getString ("pythonHome" );
9194 pythonPath = extras .getString ("pythonPath" );
92- boolean serviceStartAsForeground = (
93- extras .getString ("serviceStartAsForeground" ).equals ("true" )
94- );
95+ boolean serviceStartAsForeground =
96+ extras .getString ("serviceStartAsForeground" ).equals ("true" );
9597 pythonServiceArgument = extras .getString ("pythonServiceArgument" );
98+
9699 pythonThread = new Thread (this );
97100 pythonThread .start ();
98101
@@ -116,52 +119,69 @@ protected void doStartForeground(Bundle extras) {
116119 String smallIconName = extras .getString ("smallIconName" );
117120 String contentTitle = extras .getString ("contentTitle" );
118121 String contentText = extras .getString ("contentText" );
122+
119123 Notification notification ;
120124 Context context = getApplicationContext ();
121125 Intent contextIntent = new Intent (context , PythonActivity .class );
122- PendingIntent pIntent = PendingIntent .getActivity (context , 0 , contextIntent ,
123- PendingIntent .FLAG_IMMUTABLE | PendingIntent .FLAG_UPDATE_CURRENT );
126+ PendingIntent pIntent =
127+ PendingIntent .getActivity (
128+ context ,
129+ 0 ,
130+ contextIntent ,
131+ PendingIntent .FLAG_IMMUTABLE | PendingIntent .FLAG_UPDATE_CURRENT );
124132
125133 // Unspecified icon uses default.
126134 int smallIconId = context .getApplicationInfo ().icon ;
127-
135+
128136 if (smallIconName != null ) {
129- if (!smallIconName .equals ("" )){
130- int resId = getResources ().getIdentifier (smallIconName , "mipmap" ,
131- getPackageName ());
132- if (resId ==0 ) {
133- resId = getResources ().getIdentifier (smallIconName , "drawable" ,
134- getPackageName ());
137+ if (!smallIconName .isEmpty ()) {
138+ int resId =
139+ getResources ()
140+ .getIdentifier (smallIconName , "mipmap" , getPackageName ());
141+ if (resId == 0 ) {
142+ resId =
143+ getResources ()
144+ .getIdentifier (smallIconName , "drawable" , getPackageName ());
135145 }
136- if (resId !=0 ) {
137- smallIconId = resId ;
146+ if (resId != 0 ) {
147+ smallIconId = resId ;
138148 }
139149 }
140150 }
141151
142- if (Build .VERSION .SDK_INT < Build .VERSION_CODES .O ) {
152+ if (Build .VERSION .SDK_INT < Build .VERSION_CODES .O ) {
143153 // This constructor is deprecated
144- notification = new Notification (
145- smallIconId , serviceTitle , System .currentTimeMillis ());
154+ notification = new Notification (smallIconId , serviceTitle , System .currentTimeMillis ());
146155 try {
147156 // prevent using NotificationCompat, this saves 100kb on apk
148- Method func = notification .getClass ().getMethod (
149- "setLatestEventInfo" , Context .class , CharSequence .class ,
150- CharSequence .class , PendingIntent .class );
157+ Method func =
158+ notification
159+ .getClass ()
160+ .getMethod (
161+ "setLatestEventInfo" ,
162+ Context .class ,
163+ CharSequence .class ,
164+ CharSequence .class ,
165+ PendingIntent .class );
151166 func .invoke (notification , context , contentTitle , contentText , pIntent );
152- } catch (NoSuchMethodException | IllegalAccessException |
153- IllegalArgumentException | InvocationTargetException e ) {
167+ } catch (NoSuchMethodException
168+ | IllegalAccessException
169+ | IllegalArgumentException
170+ | InvocationTargetException e ) {
171+ // ignored
154172 }
155173 } else {
156174 // for android 8+ we need to create our own channel
157- // https://stackoverflow.com/questions/47531742/startforeground-fail-after-upgrade-to-android-8-1
158175 String NOTIFICATION_CHANNEL_ID = "org.kivy.p4a" + getServiceId ();
159176 String channelName = "Background Service" + getServiceId ();
160- NotificationChannel chan = new NotificationChannel (NOTIFICATION_CHANNEL_ID , channelName , NotificationManager .IMPORTANCE_NONE );
177+ NotificationChannel chan =
178+ new NotificationChannel (
179+ NOTIFICATION_CHANNEL_ID , channelName , NotificationManager .IMPORTANCE_NONE );
161180
162181 chan .setLightColor (Color .BLUE );
163182 chan .setLockscreenVisibility (Notification .VISIBILITY_PRIVATE );
164- NotificationManager manager = (NotificationManager ) getSystemService (Context .NOTIFICATION_SERVICE );
183+ NotificationManager manager =
184+ (NotificationManager ) getSystemService (Context .NOTIFICATION_SERVICE );
165185 manager .createNotificationChannel (chan );
166186
167187 Notification .Builder builder = new Notification .Builder (context , NOTIFICATION_CHANNEL_ID );
@@ -187,24 +207,25 @@ public void onDestroy() {
187207 }
188208
189209 /**
190- * Stops the task gracefully when killed.
191- * Calling stopSelf() will trigger a onDestroy() call from the system.
210+ * Stops the task gracefully when killed. Calling stopSelf() will trigger a onDestroy() call
211+ * from the system.
192212 */
193213 @ Override
194214 public void onTaskRemoved (Intent rootIntent ) {
195215 super .onTaskRemoved (rootIntent );
196- //sticky service runtime/restart is managed by the OS. leave it running when app is closed
216+ // sticky service runtime/restart is managed by the OS. leave it running when app is closed
197217 if (startType () != START_STICKY ) {
198218 stopSelf ();
199219 }
200220 }
201221
202222 @ Override
203- public void run (){
204- String app_root = getFilesDir ().getAbsolutePath () + "/app" ;
223+ public void run () {
224+ String app_root = getFilesDir ().getAbsolutePath () + "/app" ;
205225 File app_root_file = new File (app_root );
206- PythonUtil .loadLibraries (app_root_file ,
207- new File (getApplicationInfo ().nativeLibraryDir ));
226+
227+ PythonUtil .loadLibraries (
228+ app_root_file , new File (getApplicationInfo ().nativeLibraryDir ));
208229 this .mService = this ;
209230
210231 Log .v (TAG , "Setting env vars for start.c and Python to use" );
@@ -224,8 +245,11 @@ public void run(){
224245
225246 // Native part
226247 public static native void nativeStart (
227- String androidPrivate , String androidArgument ,
228- String serviceEntrypoint , String pythonName ,
229- String pythonHome , String pythonPath ,
248+ String androidPrivate ,
249+ String androidArgument ,
250+ String serviceEntrypoint ,
251+ String pythonName ,
252+ String pythonHome ,
253+ String pythonPath ,
230254 String pythonServiceArgument );
231- }
255+ }
0 commit comments