use android download manager instead of own download task

This commit is contained in:
Philipp Schaefer
2013-11-09 01:01:13 +01:00
parent 32a13b46ae
commit b237b1a49f
2 changed files with 32 additions and 120 deletions

View File

@ -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<Pane> mBackStack = new Stack<IITC_NavigationHelper.Pane>();
@ -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;
}
}