44import android .content .Context ;
55import android .content .DialogInterface ;
66import android .content .Intent ;
7+ import android .content .res .AssetManager ;
78import android .net .ConnectivityManager ;
89import android .net .Uri ;
910import android .os .AsyncTask ;
3637import java .io .File ;
3738import java .io .FileOutputStream ;
3839import java .io .IOException ;
40+ import java .io .InputStream ;
3941import java .util .Enumeration ;
4042import java .util .Locale ;
4143import java .util .zip .ZipEntry ;
@@ -66,21 +68,24 @@ protected void onCreate(Bundle savedInstanceState) {
6668 setTitle (R .string .install );
6769
6870 mPreferences = new AppSetting (this );
69- mProgressBar = ( ProgressBar ) findViewById (R .id .progress_bar );
70- mInfo = ( TextView ) findViewById (R .id .txt_info );
71- mInstallButton = ( Button ) findViewById (R .id .btn_install );
71+ mProgressBar = findViewById (R .id .progress_bar );
72+ mInfo = findViewById (R .id .txt_info );
73+ mInstallButton = findViewById (R .id .btn_install );
7274
7375// mTxtVersion = (TextView) findViewById(R.id.txt_version);
7476// String version = getString(R.string.system_version) + mPreferences.getSystemVersion();
7577// mTxtVersion.setText(version);
7678 findViewById (R .id .btn_install ).setOnClickListener (this );
7779 findViewById (R .id .btn_select_file ).setOnClickListener (this );
7880 findViewById (R .id .down_load_from_github ).setOnClickListener (this );
81+
82+ extractFileFromAsset ();
7983 }
8084
85+
8186 @ Override
8287 public void onClick (View v ) {
83- if (v .getId () == R .id .btn_install ) {
88+ /* if (v.getId() == R.id.btn_install) {
8489 if (isConnected()) {
8590 downloadFile();
8691 } else {
@@ -90,7 +95,11 @@ public void onClick(View v) {
9095 selectFile();
9196 } else if (v.getId() == R.id.down_load_from_github) {
9297 downloadFromGit();
93- }
98+ }*/
99+ }
100+
101+ private void extractFileFromAsset () {
102+ new CopyFromAssetTask (this ).execute ();
94103 }
95104
96105 private void downloadFromGit () {
@@ -226,6 +235,10 @@ private void showDialogDownload() {
226235 builder .setTitle ("Download system" );
227236 }
228237
238+ private void installFailed () {
239+ Toast .makeText (this , "Install failed" , Toast .LENGTH_SHORT ).show ();
240+ }
241+
229242 private class InstallTask extends AsyncTask <File , String , Boolean > {
230243 private Exception error = null ;
231244 private Context context ;
@@ -256,77 +269,10 @@ protected Boolean doInBackground(File... params) {
256269 throw new RuntimeException ("Install failed, Not a classes.zip file" );
257270 }
258271
259- // File tmp = new File(home, "tmp");
260- // if (!tmp.exists()) tmp.mkdirs();
261- //
262- // File worker = new File(tmp, "WORK_" + System.currentTimeMillis());
263- // if (!worker.exists()) worker.mkdirs();
264- //
265- // File busytar = new File(worker, "busybox");
266- // if (busytar.exists()) busytar.delete();
267- //
268- // publishProgress("Extract busybox");
269- // FileManager.extractAsset(context, "busybox.mp3", busytar);
270- //
271- // String[] env = new String[2];
272- // env[0] = "PATH=/sbin" + ":/vendor/bin" + ":/system/sbin" + ":/system/bin" +
273- // ":/system/xbin";
274- //
275- // env[1] = "LD_LIBRARY_PATH=" + "/vendor/lib" + ":/vendor/lib64" + ":/system/lib" +
276- // ":/system/lib64";
277- //
278- // busytar.setReadable(true, true);
279- // busytar.setWritable(true, true);
280- // busytar.setExecutable(true, false);
281- //
282- // publishProgress("Extract system file");
283- // File systar = new File(worker, "system.tar.gz");
284- // org.apache.commons.io.FileUtils.copyFile(params[0], systar);
285- // publishProgress("Extracted system");
286- //
287- // Process pp;
288- // publishProgress("Removing Old System");
289- //
290- // File systemFolder = new File(home, "system");
291- // FileManager.deleteFolder(systemFolder);
292- //
293- // publishProgress("Config system file");
294- // String busyboxCmd_ = busytar.getPath() + " ";
295- // pp = Runtime.getRuntime().exec(busyboxCmd_ + "tar -C " + home.getPath() + " -xzf " + systar.getPath(), env, home);
296- // pp.waitFor();
297- //
298- // File bindir = new File(systemFolder, "bin");
299- // File bbindir = new File(bindir, "bbdir");
300- // if (!bbindir.exists()) bbindir.mkdirs();
301- //
302- // File busybox = new File(bindir, "busybox");
303- // String command = busybox.getPath() + " --install -s " + bbindir.getPath();
304- // pp = Runtime.getRuntime().exec(command, env, home);
305- // pp.waitFor();
306- //
307- // //Now delete the SU link.. too much confusion..
308- // File su = new File(bbindir, "su");
309- // su.delete();
310- //
311- // publishProgress("Copy config file");
312- // copyFileConfig(home, systemFolder, busyboxCmd_, env);
313- //
314- // publishProgress("Create local file");
315- // createLocalFile(home);
316- //
317- // publishProgress("Cleaning up...");
318- // FileManager.deleteFolder(worker);
319- // FileManager.deleteFolder(params[0]);
320- //
321- // mPreferences.put("system_installed", true);
322- // mPreferences.put("system_version", SYSTEM_VERSION);
323272
324273 } catch (Exception e ) {
325274 publishProgress ("Error when install system" );
326275
327- // mPreferences.put("system_installed", false);
328- // mPreferences.put("system_version", "");
329-
330276 e .printStackTrace ();
331277 error = e ;
332278 return false ;
@@ -337,92 +283,6 @@ protected Boolean doInBackground(File... params) {
337283 return true ;
338284 }
339285
340- private void copyFileConfig (File home , File systemFolder , String busyboxCmd_ , String [] env )
341- throws IOException , InterruptedException {
342- boolean override = true ;
343- Process pp ;
344- File bashrc = new File (systemFolder , "bashrc" );
345- File bashrcu = new File (home , ".bashrc" );
346- if (!bashrcu .exists () || override ) {
347- pp = Runtime .getRuntime ().exec (busyboxCmd_ + "cp -f " + bashrc .getPath () + " " + bashrcu .getPath (), env , home );
348- pp .waitFor ();
349- }
350-
351- File nanorc = new File (systemFolder , "nanorc" );
352- File nanorcu = new File (home , ".nanorc" );
353- if (!nanorcu .exists () || override ) {
354- pp = Runtime .getRuntime ().exec (busyboxCmd_ + "cp -f " + nanorc .getPath () + " " + nanorcu .getPath (), env , home );
355- pp .waitFor ();
356- }
357-
358- File tmuxrc = new File (systemFolder , "tmux.conf" );
359- File tmuxrcu = new File (home , ".tmux.conf" );
360- if (!tmuxrcu .exists () || override ) {
361- pp = Runtime .getRuntime ().exec (busyboxCmd_ + "cp -f " + tmuxrc .getPath () + " " + tmuxrcu .getPath (), env , home );
362- pp .waitFor ();
363- }
364-
365-
366- File ini = new File (systemFolder , "mc.ini" );
367- File conf = new File (home , ".config" );
368- File confmc = new File (conf , "mc" );
369- if (!confmc .exists ()) confmc .mkdirs ();
370- File mcini = new File (confmc , "ini" );
371- if (!mcini .exists () || override ) {
372- pp = Runtime .getRuntime ().exec (busyboxCmd_ + "cp -f " + ini .getPath () + " " + mcini .getPath (), env , home );
373- pp .waitFor ();
374- }
375-
376- File inputrc = new File (systemFolder , "inputrc" );
377- File inputrcu = new File (home , ".inputrc" );
378- pp = Runtime .getRuntime ().exec (busyboxCmd_ + "cp -f " + inputrc .getPath () + " " + inputrcu .getPath (), env , home );
379- pp .waitFor ();
380-
381- //config vim
382- File vimrc = new File (systemFolder , "vimrc" );
383- File vimrcu = new File (home , ".vimrc" );
384- pp = Runtime .getRuntime ().exec (busyboxCmd_ + "cp -f " + vimrc .getPath () + " " + vimrcu .getPath (), env , home );
385- pp .waitFor ();
386-
387- //Check the home vim folder
388- File vimh = new File (systemFolder , "etc/default_vim" );
389- File vimhu = new File (home , ".vim" );
390- pp = Runtime .getRuntime ().exec (busyboxCmd_ + "cp -rf " + vimh .getPath () + " " + vimhu .getPath (), env , home );
391- pp .waitFor ();
392-
393- //Create a link to the sdcard
394- File sdcard = Environment .getExternalStorageDirectory ();
395- File lnsdcard = new File (home , "sdcard" );
396- pp = Runtime .getRuntime ().exec (busyboxCmd_ + "ln -s " + sdcard .getPath () + " " + lnsdcard .getPath (), env , home );
397- pp .waitFor ();
398-
399- }
400-
401- /**
402- * create local dir
403- *
404- * @param home
405- */
406- private void createLocalFile (File home ) {
407- File local = new File (home , "local" );
408- if (!local .exists ()) local .mkdirs ();
409-
410- File bin = new File (local , "bin" );
411- if (!bin .exists ()) bin .mkdirs ();
412-
413- bin = new File (local , "lib" );
414- if (!bin .exists ()) bin .mkdirs ();
415-
416- bin = new File (local , "include" );
417- if (!bin .exists ()) bin .mkdirs ();
418-
419- bin = new File (home , "tmp" );
420- if (!bin .exists ()) bin .mkdirs ();
421-
422- bin = new File (home , "projects" );
423- if (!bin .exists ()) bin .mkdirs ();
424- }
425-
426286 @ Override
427287 protected void onProgressUpdate (String ... values ) {
428288 super .onProgressUpdate (values );
@@ -483,5 +343,48 @@ private void createDir(File dir) {
483343
484344 }
485345
346+ private class CopyFromAssetTask extends AsyncTask <File , String , File > {
347+ private Context context ;
348+
349+ public CopyFromAssetTask (Context context ) {
350+ this .context = context ;
351+ }
352+
353+ @ Override
354+ protected void onPreExecute () {
355+ super .onPreExecute ();
356+ mInfo .setText (R .string .start_install_system );
357+ mProgressBar .setIndeterminate (true );
358+ mInstallButton .setEnabled (false );
359+ installing = true ;
360+ }
361+
362+ @ Override
363+ protected File doInBackground (File ... params ) {
364+ try {
365+ AssetManager assets = context .getAssets ();
366+ InputStream open = assets .open ("classes/classes.zip" );
367+ File outFile = new File (getFilesDir (), "classes.zip" );
368+ FileOutputStream fileOutputStream = new FileOutputStream (outFile );
369+ FileManager .copyStream (open , fileOutputStream );
370+ fileOutputStream .close ();
371+ return outFile ;
372+ } catch (IOException e ) {
373+ e .printStackTrace ();
374+ }
375+ return null ;
376+ }
377+
378+ @ Override
379+ protected void onPostExecute (File file ) {
380+ super .onPostExecute (file );
381+ if (file != null ) {
382+ new InstallTask (context ).execute (file );
383+ } else {
384+ installFailed ();
385+ }
386+ }
387+ }
388+
486389
487390}
0 commit comments