@@ -93,9 +93,6 @@ public class ChooseItineraryActivity extends Activity {
9393
9494 private SharedPreferences mPreferences ;
9595
96- // TODO fix this... move all this logic to DB
97- public boolean r7downloaded ;
98-
9996 private static final String TAG = "ChooseItineraryActivity" ;
10097
10198 private String currentLocale ;
@@ -121,11 +118,6 @@ protected void onCreate(Bundle savedInstanceState) {
121118 currentLocale = PropertyHolder .getLocale ();
122119 mPreferences = getSharedPreferences ("EruletPreferences" , MODE_PRIVATE );
123120
124- // TODO just for testing
125- // mPreferences.edit().putBoolean("r7d", false).apply();
126-
127- r7downloaded = mPreferences .getBoolean ("r7d" , false );
128-
129121 progressBar = (ProgressBar ) findViewById (R .id .pbChooseItinerary );
130122 //setUpDBIfNeeded();
131123 setUpMapIfNeeded ();
@@ -183,7 +175,6 @@ public void showItineraryOptions(){
183175 }
184176 AlertDialog .Builder builder = new AlertDialog .Builder (this );
185177 final Route r = routeTable .get (selectedMarker );
186- if (r7downloaded ){
187178 builder .setTitle (r .getName (currentLocale ));
188179 builder .setIcon (R .drawable .ic_pin_info );
189180 builder .setMessage (r .getDescription (currentLocale ));
@@ -220,24 +211,7 @@ public void onClick(DialogInterface dialogInterface, int i) {
220211 startActivity (intent ); }
221212 });
222213 }
223- } else {
224- builder .setTitle (r .getName (currentLocale ));
225- builder .setMessage (r .getDescription (currentLocale ) + "\n \n You have not yet downloaded the content for this route to your phone. Would you like to download it now?" );
226- builder .setPositiveButton ("Download" , new DialogInterface .OnClickListener () {
227- @ Override
228- public void onClick (DialogInterface dialogInterface , int i ) {
229- startDownload ();
230- }
231- });
232- builder .setNegativeButton ("Cancel" , new DialogInterface .OnClickListener () {
233- @ Override
234- public void onClick (DialogInterface dialogInterface , int i ) {
235- dialogInterface .dismiss ();
236- }
237- });
238214
239- builder .setCancelable (true );
240- }
241215 builder .show ();
242216 }
243217
@@ -452,159 +426,5 @@ private MapBoxOfflineTileProvider initTileProvider(Context context) {
452426 }
453427
454428
455- private void startDownload () {
456- // TODO unhardcode the route number and pass it instead
457- String url = "http://107.170.174.182/media/holet/zipped_routes/content_route7.zip" ;
458- new DownloadFileAsync ().execute (url );
459- }
460- @ Override
461- protected Dialog onCreateDialog (int id ) {
462- switch (id ) {
463- case DIALOG_DOWNLOAD_PROGRESS :
464- mProgressDialog = new ProgressDialog (this );
465- mProgressDialog .setMessage ("Downloading route content...please wait" );
466- mProgressDialog .setProgressStyle (ProgressDialog .STYLE_HORIZONTAL );
467- mProgressDialog .setCancelable (true );
468- mProgressDialog .show ();
469- return mProgressDialog ;
470- default :
471- return null ;
472- }
473- }
474-
475-
476- class DownloadFileAsync extends AsyncTask <String , String , String > {
477-
478- @ Override
479- protected void onPreExecute () {
480- super .onPreExecute ();
481- showDialog (DIALOG_DOWNLOAD_PROGRESS );
482- }
483-
484- @ Override
485- protected String doInBackground (String ... aurl ) {
486- int count ;
487-
488- try {
489-
490- URL url = new URL (aurl [0 ]);
491- Log .d ("ANDRO_ASYNC" , "uRL: " + url .toString ());
492-
493- HttpURLConnection conn = (HttpURLConnection ) url .openConnection ();
494- conn .setRequestMethod ("GET" );
495- conn .setDoOutput (true );
496- conn .connect ();
497-
498- int lenghtOfFile = conn .getContentLength ();
499- Log .d ("ANDRO_ASYNC" , "Lenght of file: " + lenghtOfFile );
500-
501- Log .d ("ANDRO_ASYNC" , "SD path: " + Environment .getExternalStorageDirectory ().getPath ());
502-
503- File destinationFile = new File (Environment .getExternalStorageDirectory ().getPath (), Util .baseFolder + "/route.zip" );
504- String destinationPath = destinationFile .getPath ();
505- Log .d ("ANDRO_ASYNC" , "Save path: " + destinationPath );
506-
507- InputStream input = new BufferedInputStream (url .openStream ());
508- OutputStream output = new FileOutputStream (destinationPath );
509-
510- byte data [] = new byte [1024 ];
511-
512- long total = 0 ;
513-
514- while ((count = input .read (data )) != -1 ) {
515- total += count ;
516- publishProgress ("" +(int )((total *90 )/lenghtOfFile ));
517- output .write (data , 0 , count );
518- }
519-
520- output .flush ();
521- output .close ();
522- input .close ();
523-
524-
525- // NOW UNZIP IT
526- ZipFile thisZipfile = new ZipFile (destinationPath );
527- int nEntries = thisZipfile .size ();
528- int zipCounter = 0 ;
529-
530- String zipFilePath = destinationPath ;
531- Log .d ("ANDRO_ASYNC" , "read path: " + destinationPath );
532- // TODO take out hard coded route 7
533- File target_directory = new File (Environment .getExternalStorageDirectory ().getPath (), Util .baseFolder + "/" + Util .routeMediaFolder + "/route_7" );
534- String destDirectory = target_directory .getPath ();
535- Log .d ("ANDRO_ASYNC" , "Save path: " + destDirectory );
536-
537- UnzipUtility unzipper = new UnzipUtility ();
538- try {
539- // unzipper.unzip(zipFilePath, destDirectory);
540- // publishProgress(""+100);
541-
542- final int BUFFER_SIZE = 4096 ;
543-
544- File destDir = new File (destDirectory );
545- if (!destDir .exists ()) {
546- destDir .mkdirs ();
547- }
548- ZipInputStream zipIn = new ZipInputStream (new FileInputStream (zipFilePath ));
549- ZipEntry entry = zipIn .getNextEntry ();
550- // iterates over entries in the zip file
551- while (entry != null ) {
552- String filePath = destDirectory + File .separator + entry .getName ();
553- if (!entry .isDirectory ()) {
554- // if the entry is a file, extracts it
555- // extractFile(zipIn, filePath);
556-
557- File f = new File (filePath );
558- File dir = new File (f .getParent ());
559- dir .mkdirs ();
560- BufferedOutputStream bos = new BufferedOutputStream (new FileOutputStream (filePath ));
561- byte [] bytesIn = new byte [BUFFER_SIZE ];
562- int read = 0 ;
563- while ((read = zipIn .read (bytesIn )) != -1 ) {
564- bos .write (bytesIn , 0 , read );
565-
566- }
567- bos .close ();
568-
569-
570- } else {
571- // if the entry is a directory, make the directory
572- File dir = new File (filePath );
573- dir .mkdirs ();
574- }
575-
576- publishProgress ("" +(int )(90 + ((++zipCounter *10 )/nEntries )));
577-
578- zipIn .closeEntry ();
579- entry = zipIn .getNextEntry ();
580- }
581- zipIn .close ();
582-
583-
584-
585- mPreferences .edit ().putBoolean ("r7d" , true ).apply ();
586- r7downloaded = true ;
587-
588- } catch (Exception ex ) {
589- // some errors occurred
590- ex .printStackTrace ();
591- }
592-
593- } catch (Exception e ) {}
594-
595-
596- return null ;
597-
598- }
599- protected void onProgressUpdate (String ... progress ) {
600- mProgressDialog .setProgress (Integer .parseInt (progress [0 ]));
601- }
602-
603- @ Override
604- protected void onPostExecute (String unused ) {
605- dismissDialog (DIALOG_DOWNLOAD_PROGRESS );
606- showItineraryOptions ();
607- }
608- }
609429
610430}
0 commit comments