use of com.ichi2.async.Connection in project Anki-Android by Ramblurr.
the class Connection method doInBackgroundDownloadMissingMedia.
/**
* Downloads any missing media files according to the mediaURL deckvar.
*
* @param data
* @return The return type contains data.resultType and an array of Integer in data.data. data.data[0] is the number
* of total missing media, data.data[1] is the number of downloaded ones.
*/
private Payload doInBackgroundDownloadMissingMedia(Payload data) {
Log.i(AnkiDroidApp.TAG, "DownloadMissingMedia");
HashMap<String, String> missingPaths = new HashMap<String, String>();
HashMap<String, String> missingSums = new HashMap<String, String>();
Decks deck = (Decks) data.data[0];
// pass it to the return object so we close the deck in the deck picker
data.result = deck;
// deck.getDeckName();
String syncName = "";
data.success = false;
data.data = new Object[] { 0, 0, 0 };
// if (!deck.hasKey("mediaURL")) {
// data.success = true;
// return data;
// }
// deck.getVar("mediaURL");
String urlbase = "";
if (urlbase.equals("")) {
data.success = true;
return data;
}
// deck.mediaDir(true);
String mdir = "";
int totalMissing = 0;
int missing = 0;
int grabbed = 0;
Cursor cursor = null;
try {
// deck.getDB().getDatabase().rawQuery("SELECT filename, originalPath FROM media", null);
cursor = null;
String path = null;
String f = null;
while (cursor.moveToNext()) {
f = cursor.getString(0);
path = mdir + "/" + f;
File file = new File(path);
if (!file.exists()) {
missingPaths.put(f, path);
missingSums.put(f, cursor.getString(1));
Log.i(AnkiDroidApp.TAG, "Missing file: " + f);
}
}
} finally {
if (cursor != null) {
cursor.close();
}
}
totalMissing = missingPaths.size();
data.data[0] = new Integer(totalMissing);
if (totalMissing == 0) {
data.success = true;
return data;
}
publishProgress(Boolean.FALSE, new Integer(totalMissing), new Integer(0), syncName);
URL url = null;
HttpURLConnection connection = null;
String path = null;
String sum = null;
int readbytes = 0;
byte[] buf = new byte[4096];
for (String file : missingPaths.keySet()) {
try {
android.net.Uri uri = android.net.Uri.parse(Uri.encode(urlbase, ":/@%") + Uri.encode(file));
url = new URI(uri.toString()).toURL();
connection = (HttpURLConnection) url.openConnection();
connection.connect();
if (connection.getResponseCode() == 200) {
path = missingPaths.get(file);
InputStream is = connection.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is, 4096);
FileOutputStream fos = new FileOutputStream(path);
while ((readbytes = bis.read(buf, 0, 4096)) != -1) {
fos.write(buf, 0, readbytes);
Log.i(AnkiDroidApp.TAG, "Downloaded " + readbytes + " file: " + path);
}
fos.close();
// Verify with checksum
sum = missingSums.get(file);
if (true) {
// sum.equals("") || sum.equals(Utils.fileChecksum(path))) {
grabbed++;
} else {
// Download corrupted, delete file
Log.i(AnkiDroidApp.TAG, "Downloaded media file " + path + " failed checksum.");
File f = new File(path);
f.delete();
missing++;
}
} else {
Log.e(AnkiDroidApp.TAG, "Connection error (" + connection.getResponseCode() + ") while retrieving media file " + urlbase + file);
Log.e(AnkiDroidApp.TAG, "Connection message: " + connection.getResponseMessage());
if (missingSums.get(file).equals("")) {
// Ignore and keep going
missing++;
} else {
data.success = false;
data.data = new Object[] { file };
return data;
}
}
connection.disconnect();
} catch (URISyntaxException e) {
Log.e(AnkiDroidApp.TAG, Log.getStackTraceString(e));
} catch (MalformedURLException e) {
Log.e(AnkiDroidApp.TAG, Log.getStackTraceString(e));
Log.e(AnkiDroidApp.TAG, "MalformedURLException while download media file " + path);
if (missingSums.get(file).equals("")) {
// Ignore and keep going
missing++;
} else {
data.success = false;
data.data = new Object[] { file };
return data;
}
} catch (IOException e) {
Log.e(AnkiDroidApp.TAG, Log.getStackTraceString(e));
Log.e(AnkiDroidApp.TAG, "IOException while download media file " + path);
if (missingSums.get(file).equals("")) {
// Ignore and keep going
missing++;
} else {
data.success = false;
data.data = new Object[] { file };
return data;
}
} finally {
if (connection != null) {
connection.disconnect();
}
}
publishProgress(Boolean.TRUE, new Integer(totalMissing), new Integer(grabbed + missing), syncName);
}
data.data[1] = new Integer(grabbed);
data.data[2] = new Integer(missing);
data.success = true;
return data;
}
Aggregations