NetGuard/app/src/main/java/eu/faircode/netguard/DownloadTask.java

167 lines
5.5 KiB
Java
Raw Normal View History

2016-02-03 10:54:17 +00:00
package eu.faircode.netguard;
2016-12-29 08:27:58 +00:00
/*
This file is part of NetGuard.
NetGuard is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
NetGuard is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with NetGuard. If not, see <http://www.gnu.org/licenses/>.
Copyright 2015-2017 by Marcel Bokhorst (M66B)
*/
2016-02-13 12:21:18 +00:00
import android.app.Activity;
2016-02-03 10:54:17 +00:00
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.AsyncTask;
import android.os.PowerManager;
import android.util.Log;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
2016-02-12 09:32:26 +00:00
import java.net.URLConnection;
2016-02-03 10:54:17 +00:00
public class DownloadTask extends AsyncTask<Object, Integer, Object> {
private static final String TAG = "NetGuard.Download";
private Context context;
private URL url;
private File file;
private Listener listener;
private ProgressDialog progressDialog;
2016-02-03 10:54:17 +00:00
private PowerManager.WakeLock wakeLock;
public interface Listener {
void onCompleted();
void onCancelled();
void onException(Throwable ex);
}
2016-02-13 12:21:18 +00:00
public DownloadTask(Activity context, URL url, File file, Listener listener) {
2016-02-03 10:54:17 +00:00
this.context = context;
this.url = url;
this.file = file;
this.listener = listener;
}
@Override
protected void onPreExecute() {
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, getClass().getName());
wakeLock.acquire();
progressDialog = new ProgressDialog(context);
2017-08-04 20:02:08 +00:00
progressDialog.setIcon(R.drawable.ic_security_color_24dp);
2016-02-03 10:54:17 +00:00
progressDialog.setTitle(R.string.app_name);
progressDialog.setMessage(context.getString(R.string.msg_downloading, url.toString()));
progressDialog.setIndeterminate(true);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setCancelable(true);
progressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
DownloadTask.this.cancel(true);
}
});
progressDialog.show();
}
@Override
protected Object doInBackground(Object... args) {
Log.i(TAG, "Downloading " + url + " into " + file);
InputStream in = null;
OutputStream out = null;
2016-02-12 09:32:26 +00:00
URLConnection connection = null;
2016-02-03 10:54:17 +00:00
try {
2016-02-12 09:32:26 +00:00
connection = url.openConnection();
2016-02-03 10:54:17 +00:00
connection.connect();
2016-02-12 09:32:26 +00:00
if (connection instanceof HttpURLConnection) {
HttpURLConnection httpConnection = (HttpURLConnection) connection;
if (httpConnection.getResponseCode() != HttpURLConnection.HTTP_OK)
throw new IOException(httpConnection.getResponseCode() + " " + httpConnection.getResponseMessage());
}
2016-02-03 10:54:17 +00:00
int contentLength = connection.getContentLength();
Log.i(TAG, "Content length=" + contentLength);
in = connection.getInputStream();
out = new FileOutputStream(file);
long size = 0;
byte buffer[] = new byte[4096];
int bytes;
while (!isCancelled() && (bytes = in.read(buffer)) != -1) {
out.write(buffer, 0, bytes);
size += bytes;
if (contentLength > 0)
publishProgress((int) (size * 100 / contentLength));
}
Log.i(TAG, "Downloaded size=" + size);
return null;
} catch (Throwable ex) {
return ex;
} finally {
try {
if (out != null)
out.close();
} catch (IOException ex) {
Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
}
try {
if (in != null)
in.close();
} catch (IOException ex) {
Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
}
2016-02-12 09:32:26 +00:00
if (connection instanceof HttpURLConnection)
((HttpURLConnection) connection).disconnect();
2016-02-03 10:54:17 +00:00
}
}
@Override
protected void onProgressUpdate(Integer... progress) {
super.onProgressUpdate(progress);
progressDialog.setIndeterminate(false);
progressDialog.setMax(100);
progressDialog.setProgress(progress[0]);
}
@Override
protected void onCancelled() {
super.onCancelled();
Log.i(TAG, "Cancelled");
listener.onCancelled();
}
@Override
protected void onPostExecute(Object result) {
wakeLock.release();
progressDialog.dismiss();
if (result instanceof Throwable) {
Log.e(TAG, result.toString() + "\n" + Log.getStackTraceString((Throwable) result));
listener.onException((Throwable) result);
} else
listener.onCompleted();
}
}