22
33import android .app .Service ;
44import android .content .Intent ;
5- import android .content .pm .PackageInfo ;
6- import android .content .pm .PackageManager ;
75import android .os .Bundle ;
86import android .os .IBinder ;
97import android .os .Process ;
108import android .util .Log ;
119
12- import org .renpy .android .AssetExtract ;
13-
14- import java .io .File ;
15- import java .io .FileInputStream ;
16- import java .io .FileOutputStream ;
17-
1810public class PythonService extends Service implements Runnable {
19- private static String TAG = "PythonService" ;
11+ private static String TAG = PythonService .class .getSimpleName ();
12+
13+ public static PythonService mService = null ;
14+ /**
15+ * Intent that started the service
16+ */
17+ private Intent startIntent = null ;
2018
21- // Thread for Python code
2219 private Thread pythonThread = null ;
2320
2421 // Python environment variables
@@ -28,47 +25,44 @@ public class PythonService extends Service implements Runnable {
2825 private String pythonHome ;
2926 private String pythonPath ;
3027 private String serviceEntrypoint ;
31-
32- // Argument to pass to Python code,
3328 private String pythonServiceArgument ;
34- public static PythonService mService = null ;
35- private Intent startIntent = null ;
3629
3730 private boolean autoRestartService = false ;
3831
3932 public void setAutoRestartService (boolean restart ) {
4033 autoRestartService = restart ;
4134 }
4235
43- public boolean canDisplayNotification () {
44- return true ;
45- }
46-
47- public int startType () {
48- return START_NOT_STICKY ;
49- }
50-
36+ /**
37+ * {@inheritDoc}
38+ */
5139 @ Override
52- public IBinder onBind (Intent arg0 ) {
40+ public IBinder onBind (Intent intent ) {
5341 return null ;
5442 }
5543
44+ /**
45+ * {@inheritDoc}
46+ */
5647 @ Override
5748 public void onCreate () {
5849 Log .v (TAG , "Device: " + android .os .Build .DEVICE );
5950 Log .v (TAG , "Model: " + android .os .Build .MODEL );
60- unpackData ( "private" , getFilesDir ());
51+ AssetExtract . extractAsset ( getApplicationContext (), "private.mp3 " , getFilesDir ());
6152 super .onCreate ();
6253 }
6354
55+ /**
56+ * {@inheritDoc}
57+ */
6458 @ Override
6559 public int onStartCommand (Intent intent , int flags , int startId ) {
6660 if (pythonThread != null ) {
6761 Log .v (TAG , "Service exists, do not start again" );
6862 return START_NOT_STICKY ;
6963 }
70-
7164 startIntent = intent ;
65+
7266 Bundle extras = intent .getExtras ();
7367 androidPrivate = extras .getString ("androidPrivate" );
7468 androidArgument = extras .getString ("androidArgument" );
@@ -78,6 +72,7 @@ public int onStartCommand(Intent intent, int flags, int startId) {
7872 pythonPath = extras .getString ("pythonPath" );
7973 pythonServiceArgument = extras .getString ("pythonServiceArgument" );
8074
75+ Log .v (TAG , "Starting Python thread" );
8176 pythonThread = new Thread (this );
8277 pythonThread .start ();
8378
@@ -104,6 +99,9 @@ protected void doStartForeground(Bundle extras) {
10499 startForeground (1 , notification );
105100 }
106101
102+ /**
103+ * {@inheritDoc}
104+ */
107105 @ Override
108106 public void onDestroy () {
109107 super .onDestroy ();
@@ -115,6 +113,9 @@ public void onDestroy() {
115113 Process .killProcess (Process .myPid ());
116114 }
117115
116+ /**
117+ * {@inheritDoc}
118+ */
118119 @ Override
119120 public void run () {
120121 PythonUtil .loadLibraries (getFilesDir ());
@@ -124,80 +125,15 @@ public void run() {
124125 stopSelf ();
125126 }
126127
127- public void recursiveDelete (File f ) {
128- if (f .isDirectory ()) {
129- for (File r : f .listFiles ()) {
130- recursiveDelete (r );
131- }
132- }
133- f .delete ();
134- }
135-
136- public void unpackData (final String resource , File target ) {
137-
138- Log .v (TAG , "UNPACKING!!! " + resource + " " + target .getName ());
139-
140- // The version of data in memory and on disk.
141- String data_version = null ;
142- String disk_version = null ;
143-
144- try {
145- PackageManager manager = this .getPackageManager ();
146- PackageInfo info = manager .getPackageInfo (this .getPackageName (), 0 );
147- data_version = info .versionName ;
148-
149- Log .v (TAG , "Data version is " + data_version );
150- } catch (PackageManager .NameNotFoundException e ) {
151- Log .w (TAG , "Data version not found of " + resource + " data." );
152- }
153-
154- // If no version, no unpacking is necessary.
155- if (data_version == null ) {
156- return ;
157- }
158-
159- // Check the current disk version, if any.
160- String filesDir = target .getAbsolutePath ();
161- String disk_version_fn = filesDir + "/" + resource + ".version" ;
162-
163- try {
164- byte buf [] = new byte [64 ];
165- FileInputStream is = new FileInputStream (disk_version_fn );
166- int len = is .read (buf );
167- disk_version = new String (buf , 0 , len );
168- is .close ();
169- } catch (Exception e ) {
170- disk_version = "" ;
171- }
172-
173- // If the disk data is out of date, extract it and write the version
174- // file.
175- if (!data_version .equals (disk_version )) {
176- Log .v (TAG , "Extracting " + resource + " assets." );
177-
178- recursiveDelete (target );
179- target .mkdirs ();
180-
181- AssetExtract ae = new AssetExtract (this );
182- if (!ae .extractTar (resource + ".mp3" , target .getAbsolutePath ())) {
183- Log .e (TAG , "Could not extract " + resource + " data." );
184- }
185-
186- try {
187- // Write .nomedia.
188- new File (target , ".nomedia" ).createNewFile ();
189-
190- // Write version file.
191- FileOutputStream os = new FileOutputStream (disk_version_fn );
192- os .write (data_version .getBytes ());
193- os .close ();
194- } catch (Exception e ) {
195- Log .w ("python" , e );
196- }
197- }
198- }
199-
200- // Native part
128+ /**
129+ * @param androidPrivate Directory for private files
130+ * @param androidArgument Android path
131+ * @param serviceEntrypoint Python file to execute first
132+ * @param pythonName Python name
133+ * @param pythonHome Python home
134+ * @param pythonPath Python path
135+ * @param pythonServiceArgument Argument to pass to Python code
136+ */
201137 public static native void nativeStart (String androidPrivate ,
202138 String androidArgument , String serviceEntrypoint ,
203139 String pythonName , String pythonHome , String pythonPath ,
0 commit comments