From b237b1a49f30d6921d9c4465baa3418c76d5dd7e Mon Sep 17 00:00:00 2001 From: Philipp Schaefer Date: Sat, 9 Nov 2013 01:01:13 +0100 Subject: [PATCH] use android download manager instead of own download task --- .../com/cradle/iitc_mobile/IITC_Mobile.java | 53 ++++++---- .../iitc_mobile/async/DownloadIitcUpdate.java | 99 ------------------- 2 files changed, 32 insertions(+), 120 deletions(-) delete mode 100644 mobile/src/com/cradle/iitc_mobile/async/DownloadIitcUpdate.java diff --git a/mobile/src/com/cradle/iitc_mobile/IITC_Mobile.java b/mobile/src/com/cradle/iitc_mobile/IITC_Mobile.java index 186a3b94..53e53247 100644 --- a/mobile/src/com/cradle/iitc_mobile/IITC_Mobile.java +++ b/mobile/src/com/cradle/iitc_mobile/IITC_Mobile.java @@ -3,11 +3,14 @@ package com.cradle.iitc_mobile; import android.app.ActionBar; import android.app.Activity; import android.app.AlertDialog; +import android.app.DownloadManager; import android.app.ProgressDialog; import android.app.SearchManager; +import android.content.BroadcastReceiver; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; +import android.content.IntentFilter; import android.content.SharedPreferences; import android.content.SharedPreferences.OnSharedPreferenceChangeListener; import android.content.res.Configuration; @@ -30,7 +33,6 @@ import android.widget.SearchView; import android.widget.Toast; import com.cradle.iitc_mobile.IITC_NavigationHelper.Pane; -import com.cradle.iitc_mobile.async.DownloadIitcUpdate; import java.io.File; import java.io.IOException; @@ -57,7 +59,13 @@ public class IITC_Mobile extends Activity implements OnSharedPreferenceChangeLis private SharedPreferences mSharedPrefs; private IITC_NavigationHelper mNavigationHelper; private IITC_MapSettings mMapSettings; - private ProgressDialog mProgressDialog; + + private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() { + @Override + public void onReceive(Context context, Intent intent) { + ((IITC_Mobile) context).installIitcUpdate(); + } + }; // Used for custom back stack handling private final Stack mBackStack = new Stack(); @@ -111,12 +119,9 @@ public class IITC_Mobile extends Activity implements OnSharedPreferenceChangeLis // Clear the back stack mBackStack.clear(); - // init update progress dialog - mProgressDialog = new ProgressDialog(this); - mProgressDialog.setMessage("Downloading IITCm update..."); - mProgressDialog.setIndeterminate(true); - mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); - mProgressDialog.setCancelable(true); + // receive downloadManagers downloadComplete intent + // afterwards install iitc update and clean up after installation + registerReceiver(mBroadcastReceiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE)); handleIntent(getIntent(), true); } @@ -181,6 +186,7 @@ public class IITC_Mobile extends Activity implements OnSharedPreferenceChangeLis } // ------------------------------------------------------------------------ + @Override protected void onNewIntent(Intent intent) { setIntent(intent); @@ -584,7 +590,8 @@ public class IITC_Mobile extends Activity implements OnSharedPreferenceChangeLis break; case REQUEST_UPDATE_FINISHED: // clean up update apk - File file = new File(Environment.getExternalStorageDirectory().toString() + "/iitc_update.apk"); + File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + '/' + + Environment.DIRECTORY_DOWNLOADS + "/iitcUpdate.apk"); if (file != null) file.delete(); default: @@ -643,14 +650,22 @@ public class IITC_Mobile extends Activity implements OnSharedPreferenceChangeLis } public void updateIitc(String url) { - final DownloadIitcUpdate updateTask = new DownloadIitcUpdate(this); - updateTask.execute(url); - mProgressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() { - @Override - public void onCancel(DialogInterface dialog) { - updateTask.cancel(true); - } - }); + DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url)); + request.setDescription("downloading IITCm update apk..."); + request.setTitle("IITCm Update"); + request.allowScanningByMediaScanner(); + request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "iitcUpdate.apk"); + // get download service and enqueue file + DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE); + manager.enqueue(request); + } + + private void installIitcUpdate() { + String iitcUpdatePath = Environment.getExternalStorageDirectory().getAbsolutePath() + '/' + + Environment.DIRECTORY_DOWNLOADS + "/iitcUpdate.apk"; + Intent intent = new Intent(Intent.ACTION_VIEW); + intent.setDataAndType(Uri.fromFile(new File(iitcUpdatePath)), "application/vnd.android.package-archive"); + startActivityForResult(intent, REQUEST_UPDATE_FINISHED); } /** @@ -670,8 +685,4 @@ public class IITC_Mobile extends Activity implements OnSharedPreferenceChangeLis public IITC_MapSettings getMapSettings() { return mMapSettings; } - - public ProgressDialog getProgressDialog() { - return mProgressDialog; - } } diff --git a/mobile/src/com/cradle/iitc_mobile/async/DownloadIitcUpdate.java b/mobile/src/com/cradle/iitc_mobile/async/DownloadIitcUpdate.java deleted file mode 100644 index f2f64fb6..00000000 --- a/mobile/src/com/cradle/iitc_mobile/async/DownloadIitcUpdate.java +++ /dev/null @@ -1,99 +0,0 @@ -package com.cradle.iitc_mobile.async; - -import android.app.ProgressDialog; -import android.content.Intent; -import android.net.Uri; -import android.os.AsyncTask; -import android.os.Environment; -import android.util.Log; - -import com.cradle.iitc_mobile.IITC_Mobile; - -import java.io.BufferedInputStream; -import java.io.File; -import java.io.FileOutputStream; -import java.io.InputStream; -import java.io.OutputStream; -import java.net.URL; -import java.net.URLConnection; - -/** - * Background Async Task to download file - * */ -public class DownloadIitcUpdate extends AsyncTask { - - private final IITC_Mobile mIitc; - private final ProgressDialog mProgressDialog; - - public DownloadIitcUpdate(IITC_Mobile iitcm) { - mIitc = iitcm; - mProgressDialog = iitcm.getProgressDialog(); - } - - @Override - protected void onPreExecute() { - super.onPreExecute(); - mProgressDialog.show(); - } - - @Override - protected String doInBackground(String... fileUrl) { - int count; - try { - URL url = new URL(fileUrl[0]); - URLConnection connection = url.openConnection(); - connection.connect(); - int lengthOfFile = connection.getContentLength(); - - // input stream to read file - with 8k buffer - InputStream input = new BufferedInputStream(url.openStream(), 8192); - - // Output stream to write file - OutputStream output = new FileOutputStream(Environment.getExternalStorageDirectory().toString() - + "/iitc_update.apk"); - - byte data[] = new byte[8192]; - - long total = 0; - - while ((count = input.read(data)) != -1) { - total += count; - // publishing the progress.... - publishProgress((int)(total*100)/lengthOfFile); - - // writing data to file - output.write(data, 0, count); - } - - output.flush(); - - // closing streams - output.close(); - input.close(); - - } catch (Exception e) { - Log.e("iitcm:", e.getMessage()); - } - - return null; - } - - protected void onProgressUpdate(Integer... progress) { - super.onProgressUpdate(progress); - // if we get here, length is known, now set indeterminate to false - mProgressDialog.setIndeterminate(false); - mProgressDialog.setMax(100); - mProgressDialog.setProgress(progress[0]); - } - - @Override - protected void onPostExecute(String fileUrl) { - // dismiss the dialog after the file was downloaded - mProgressDialog.dismiss(); - - String iitcPath = Environment.getExternalStorageDirectory().toString() + "/iitc_update.apk"; - Intent intent = new Intent(Intent.ACTION_VIEW); - intent.setDataAndType(Uri.fromFile(new File(iitcPath)), "application/vnd.android.package-archive"); - mIitc.startActivityForResult(intent, mIitc.REQUEST_UPDATE_FINISHED); - } -} \ No newline at end of file