package com.cafejeunesse.android.filemanager;
import android.content.Context;
import android.os.AsyncTask;
import android.os.PowerManager;
import com.cafejeunesse.android.fragment.Refreshable;
import com.cafejeunesse.android.navigationdrawer.R;
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;
import java.util.Date;
// usually, subclasses of AsyncTask are declared inside the activity class.
// that way, you can easily modify the UI thread from here
public class DownloadTask extends AsyncTask<String, Integer, String> {
private Context context;
private PowerManager.WakeLock mWakeLock;
private Refreshable mRefresh;
public DownloadTask(Context context, Refreshable target) {
this.context = context;
this.mRefresh = target;
}
@Override
protected String doInBackground(String... sUrl) {
InputStream input = null;
OutputStream output = null;
HttpURLConnection connection = null;
try {
URL url = new URL(sUrl[0]);
connection = (HttpURLConnection) url.openConnection();
connection.connect();
// expect HTTP 200 OK, so we don't mistakenly save error report
// instead of the file
if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
return context.getString(R.string.server_returned_http) + connection.getResponseCode()
+ " " + connection.getResponseMessage();
}
// this will be useful to display download percentage
// might be -1: server did not report the length
int fileLength = connection.getContentLength();
// download the file
input = connection.getInputStream();
String path = sUrl[1];
output = new FileOutputStream(path);
byte data[] = new byte[4096];
long total = 0;
int count;
while ((count = input.read(data)) != -1) {
// allow canceling with back button
if (isCancelled()) {
input.close();
return null;
}
total += count;
// publishing the progress....
if (fileLength > 0) // only if total length is known
publishProgress((int) (total * 100 / fileLength));
output.write(data, 0, count);
}
} catch (Exception e) {
return e.toString();
} finally {
try {
if (output != null)
output.close();
if (input != null)
input.close();
} catch (IOException ignored) {
}
if (connection != null)
connection.disconnect();
}
return null;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
// take CPU lock to prevent CPU from going off if the user
// presses the power button during download
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, getClass().getName());
mWakeLock.acquire();
}
@Override
protected void onPostExecute(String result) {
mWakeLock.release();
if (result == null)
mRefresh.refresh();
}
public static void testAndDownload(String filepath, String url, Long timeBetweenDownloads, Context context, Refreshable refreshTarget) {
// Téléchargement (si besoin) des calendriers
File f = new File(filepath);
// Si le fichier n'est pas présent ou qu'il est trop vieux
if (!f.exists() || !f.isFile() || (new Date().getTime() - f.lastModified()) > timeBetweenDownloads)
startDownload(url, filepath, context, refreshTarget);
}
public static void startDownload(String url, String fileName, Context context, Refreshable refreshTarget) {
// execute this when the downloader must be fired
final DownloadTask downloadTask = new DownloadTask(context, refreshTarget);
downloadTask.execute(url, fileName);
}
}