Improved Dowloader: Set Async thread and displayed a progress bar for resource loading and card image downloading.

This commit is contained in:
valfieri
2019-08-25 22:29:24 +02:00
parent a5c9f877f7
commit 39884d9711
2 changed files with 323 additions and 156 deletions
@@ -27,6 +27,7 @@ import java.util.HashMap;
import java.util.stream.Stream; import java.util.stream.Stream;
import android.graphics.*; import android.graphics.*;
import android.app.ProgressDialog;
public class ImgDownloader { public class ImgDownloader {
@@ -143,6 +144,7 @@ public class ImgDownloader {
public static String getSpecialCardUrl(String id) { public static String getSpecialCardUrl(String id) {
String cardurl = ""; String cardurl = "";
if (id.equals("15208711")) if (id.equals("15208711"))
cardurl = "https://img.scryfall.com/cards/large/front/9/c/9c138bf9-8be6-4f1a-a82c-a84938ab84f5.jpg?1562279137"; cardurl = "https://img.scryfall.com/cards/large/front/9/c/9c138bf9-8be6-4f1a-a82c-a84938ab84f5.jpg?1562279137";
else if (id.equals("15208712")) else if (id.equals("15208712"))
@@ -153,14 +155,52 @@ public class ImgDownloader {
cardurl = "https://crystal-cdn4.crystalcommerce.com/photos/504053/ooze_token_b.jpg"; cardurl = "https://crystal-cdn4.crystalcommerce.com/photos/504053/ooze_token_b.jpg";
else if (id.equals("22010012")) else if (id.equals("22010012"))
cardurl = "https://img.scryfall.com/cards/normal/front/8/4/84dc847c-7a37-4c7f-b02c-30b3e4c91fb6.jpg?1561757490"; cardurl = "https://img.scryfall.com/cards/normal/front/8/4/84dc847c-7a37-4c7f-b02c-30b3e4c91fb6.jpg?1561757490";
else if (id.equals("8759611"))
cardurl = "https://img.scryfall.com/cards/large/front/4/1/41004bdf-8e09-4b2c-9e9c-26c25eac9854.jpg?1562493483";
else if (id.equals("8759911"))
cardurl = "https://img.scryfall.com/cards/large/front/0/b/0b61d772-2d8b-4acf-9dd2-b2e8b03538c8.jpg?1562492461";
else if (id.equals("8759511"))
cardurl = "https://img.scryfall.com/cards/large/front/d/2/d224c50f-8146-4c91-9401-04e5bd306d02.jpg?1562496100";
else if (id.equals("8471611"))
cardurl = "https://img.scryfall.com/cards/png/front/8/4/84920a21-ee2a-41ac-a369-347633d10371.png?1562494702";
else if (id.equals("8760011"))
cardurl = "https://img.scryfall.com/cards/large/front/4/2/42ba0e13-d20f-47f9-9c86-2b0b13c39ada.jpg?1562493487";
else if (id.equals("401721"))
cardurl = "https://deckmaster.info/images/cards/DDP/401721-hr.jpg";
else if (id.equals("401722"))
cardurl = "https://deckmaster.info/images/cards/DDP/401722-hr.jpg";
else if (id.equals("19784311"))
cardurl = "https://deckmaster.info/images/cards/AKH/-4173-hr.jpg";
else if (id.equals("19784312"))
cardurl = "https://deckmaster.info/images/cards/BNG/-10-hr.jpg";
else if (id.equals("19784313"))
cardurl = "https://deckmaster.info/images/cards/DDD/201843-hr.jpg";
else if (id.equals("20787512"))
cardurl = "https://deckmaster.info/images/cards/SOM/-227-hr.jpg";
else if (id.equals("20787511"))
cardurl = "https://deckmaster.info/images/cards/SOM/-226-hr.jpg";
else if (id.equals("11492111"))
cardurl = "https://deckmaster.info/images/cards/TSP/-2841-hr.jpg";
else if (id.equals("11492112"))
cardurl = "https://deckmaster.info/images/cards/TSP/-2840-hr.jpg";
else if (id.equals("11492113"))
cardurl = "https://img.scryfall.com/cards/large/front/5/b/5b9f471a-1822-4981-95a9-8923d83ddcbf.jpg?1562702075";
else if (id.equals("11492114"))
cardurl = "https://deckmaster.info/images/cards/DDN/386322-hr.jpg";
else if (id.equals("11492115"))
cardurl = "https://deckmaster.info/images/cards/DDE/209162-hr.jpg";
return cardurl; return cardurl;
} }
public static String getSpecialTokenUrl(String id) { public static String getSpecialTokenUrl(String id) {
String tokenurl = ""; String tokenurl = "";
if (id.equals("75291t")) if (id.equals("75291t"))
tokenurl = "http://4.bp.blogspot.com/-y5Fanm3qvrU/Vmd4gGnl2DI/AAAAAAAAAWY/FCrS9FTgOJk/s1600/Tatsumasa%2BToken.jpg"; tokenurl = "http://4.bp.blogspot.com/-y5Fanm3qvrU/Vmd4gGnl2DI/AAAAAAAAAWY/FCrS9FTgOJk/s1600/Tatsumasa%2BToken.jpg";
else if(id.equals("202474t")) else if (id.equals("435411t") || id.equals("435410t"))
tokenurl = "https://deckmaster.info/images/cards/XLN/-5173-hr.jpg";
else if (id.equals("202474t") || id.equals("1098t"))
tokenurl = "https://deckmaster.info/images/cards/AST/-884-hr.jpg"; tokenurl = "https://deckmaster.info/images/cards/AST/-884-hr.jpg";
else if (id.equals("202590t")) else if (id.equals("202590t"))
tokenurl = "https://deckmaster.info/images/cards/AST/-892-hr.jpg"; tokenurl = "https://deckmaster.info/images/cards/AST/-892-hr.jpg";
@@ -230,6 +270,45 @@ public class ImgDownloader {
tokenurl = "https://media.mtgsalvation.com/attachments/102/31/635032498723573408.jpg"; tokenurl = "https://media.mtgsalvation.com/attachments/102/31/635032498723573408.jpg";
else if (id.equals("24624t")) else if (id.equals("24624t"))
tokenurl = "https://www.mtg.onl/static/6d717cba653ea9e3f6bd1419741671cb/4d406/PROXY_Minion_B_1_1.jpg"; tokenurl = "https://www.mtg.onl/static/6d717cba653ea9e3f6bd1419741671cb/4d406/PROXY_Minion_B_1_1.jpg";
else if (id.equals("409810t") || id.equals("409805t") || id.equals("409953t") || id.equals("409997t") || id.equals("410032t"))
tokenurl = "https://deckmaster.info/images/cards/SOI/-2404-hr.jpg";
else if (id.equals("74492t"))
tokenurl = "https://media.mtgsalvation.com/attachments/94/295/635032496473215708.jpg";
else if (id.equals("88973t"))
tokenurl = "https://deckmaster.info/images/cards/DDQ/409655-hr.jpg";
else if (id.equals("89051t"))
tokenurl = "https://www.mtg.onl/static/b7625a256e10bcec251a1a0abbf17bd4/4d406/PROXY_Horror_B_4_4.jpg";
else if (id.equals("5261t"))
tokenurl = "https://static.cardmarket.com/img/5a0199344cad68eebeefca6fa24e52c3/items/1/MH1/376905.jpg";
else if (id.equals("116384t"))
tokenurl = "https://deckmaster.info/images/cards/TSP/-114916-hr.jpg";
else if (id.equals("116383t"))
tokenurl = "https://i.imgur.com/wRMebWg.jpg";
else if (id.equals("114917t"))
tokenurl = "https://deckmaster.info/images/cards/JOU/-43-hr.jpg";
else if (id.equals("5610t"))
tokenurl = "https://deckmaster.info/images/cards/DDE/207998-hr.jpg";
else if (id.equals("185704t"))
tokenurl = "https://deckmaster.info/images/cards/ZEN/-277-hr.jpg";
else if (id.equals("461099t"))
tokenurl = "https://img.scryfall.com/cards/large/front/d/e/de7ba875-f77b-404f-8b75-4ba6f81da410.jpg?1557575978";
else if (id.equals("9667t"))
tokenurl = "https://www.mtg.onl/static/abe5178af8ebbe84f5504493a1b5f154/4d406/PROXY_Giant_Chicken_R_4_4.jpg";
else if (id.equals("368549t"))
tokenurl = "https://deckmaster.info/images/cards/DDQ/409655-hr.jpg";
else if (id.equals("73953t"))
tokenurl = "https://www.mtg.onl/static/98da67c454fe6cde61ae45af4f9c3150/4d406/PROXY_Giant_Teddy_Bear_5_5.jpg";
else if (id.equals("74265t"))
tokenurl = "https://deckmaster.info/images/cards/UNH/-2064-hr.jpg";
else if (id.equals("27634t"))
tokenurl = "https://www.mtg.onl/static/8b684bdea239d594e296a134f5ec1783/4d406/PROXY_Hippo_G_1_1.jpg";
else if (id.equals("111046t"))
tokenurl = "https://media.mtgsalvation.com/attachments/32/354/635032480299772645.jpg";
else if (id.equals("4771t"))
tokenurl = "https://www.mtg.onl/static/b8060dffbaf67ef987c6324c1523d3e4/4d406/PROXY_Hound_G_1_1.jpg";
else if (id.equals("3591t"))
tokenurl = "https://i.pinimg.com/564x/6e/8d/fe/6e8dfeee2919a3efff210df56ab7b85d.jpg";
return tokenurl; return tokenurl;
} }
@@ -238,7 +317,10 @@ public class ImgDownloader {
id.equals("417759") || id.equals("386476") || id.equals("456371") || id.equals("456360") || id.equals("391958") || id.equals("466959") || id.equals("417759") || id.equals("386476") || id.equals("456371") || id.equals("456360") || id.equals("391958") || id.equals("466959") ||
id.equals("466813") || id.equals("201176") || id.equals("202483") || id.equals("3546") || id.equals("425949") || id.equals("426027") || id.equals("466813") || id.equals("201176") || id.equals("202483") || id.equals("3546") || id.equals("425949") || id.equals("426027") ||
id.equals("425853") || id.equals("425846") || id.equals("426036") || id.equals("370387") || id.equals("29955") || id.equals("29989") || id.equals("425853") || id.equals("425846") || id.equals("426036") || id.equals("370387") || id.equals("29955") || id.equals("29989") ||
id.equals("19741") || id.equals("19722") || id.equals("19706") || id.equals("24597") || id.equals("24617") || id.equals("24563")) id.equals("19741") || id.equals("19722") || id.equals("19706") || id.equals("24597") || id.equals("24617") || id.equals("24563") ||
id.equals("253539") || id.equals("277995") || id.equals("265415") || id.equals("289225") || id.equals("289215") || id.equals("253529") ||
id.equals("253641") || id.equals("270957") || id.equals("401685") || id.equals("89116") || id.equals("5183") || id.equals("5177") ||
id.equals("209289") || id.equals("198171") || id.equals("10419") || id.equals("470542") || id.equals("29992"))
return false; return false;
return true; return true;
} }
@@ -368,7 +450,7 @@ public class ImgDownloader {
throw new Exception(); throw new Exception();
} }
public static String DownloadCardImages(String set, String[] availableSets, String targetres, String basePath, String destinationPath) throws IOException { public static String DownloadCardImages(String set, String[] availableSets, String targetres, String basePath, String destinationPath, ProgressDialog progressBarDialog) throws IOException {
String res = ""; String res = "";
String baseurl = "https://gatherer.wizards.com/Pages/Card/Details.aspx?multiverseid="; String baseurl = "https://gatherer.wizards.com/Pages/Card/Details.aspx?multiverseid=";
@@ -472,7 +554,18 @@ public class ImgDownloader {
lines = lines.substring(c + 8); lines = lines.substring(c + 8);
if (primitive != null && id != null && !id.equalsIgnoreCase("null")) if (primitive != null && id != null && !id.equalsIgnoreCase("null"))
mappa.put(id, primitive); mappa.put(id, primitive);
if (id.equals("114921")) {
mappa.put("11492111", "Citizen");
mappa.put("11492112", "Camarid");
mappa.put("11492113", "Thrull");
mappa.put("11492114", "Goblin");
mappa.put("11492115", "Saproling");
} }
}
progressBarDialog.setProgress(0);
progressBarDialog.setMax(totalcards);
progressBarDialog.setTitle("Downloading set: " + set);
File imgPath = new File(destinationPath + set + "/"); File imgPath = new File(destinationPath + set + "/");
if (!imgPath.exists()) { if (!imgPath.exists()) {
@@ -625,6 +718,11 @@ public class ImgDownloader {
byte[] response = out.toByteArray(); byte[] response = out.toByteArray();
String cardimage = imgPath + "/" + id + ".jpg"; String cardimage = imgPath + "/" + id + ".jpg";
String thumbcardimage = thumbPath + "/" + id + ".jpg"; String thumbcardimage = thumbPath + "/" + id + ".jpg";
if (id.equals("11492111") || id.equals("11492112") || id.equals("11492113") ||
id.equals("11492114") || id.equals("11492115")) {
cardimage = imgPath + File.separator + id + "t.jpg";
thumbcardimage = thumbPath + File.separator + id + "t.jpg";
}
FileOutputStream fos = new FileOutputStream(cardimage); FileOutputStream fos = new FileOutputStream(cardimage);
fos.write(response); fos.write(response);
fos.close(); fos.close();
@@ -644,7 +742,7 @@ public class ImgDownloader {
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
progressBarDialog.incrementProgressBy((int) (1));
continue; continue;
} }
if (doc == null) { if (doc == null) {
@@ -942,7 +1040,7 @@ public class ImgDownloader {
} }
} }
} }
progressBarDialog.incrementProgressBy((int) (1));
break; break;
} }
} }
@@ -358,30 +358,24 @@ public class SDLActivity extends Activity implements OnKeyListener {
} }
} }
private void downloadCardInfo() {
AlertDialog.Builder cardDownloader = new AlertDialog.Builder(this);
cardDownloader.setTitle("Image Downloader");
cardDownloader.setMessage("IMPORTANT: After you press OK you may have to wait a bit while Downloader will evaluate all the sets available in this game version.");
cardDownloader.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
downloadCardImages();
}
});
cardDownloader.create().show();
}
String set = ""; String set = "";
String[] availableSets; String[] availableSets;
Integer totalset = 0; Integer totalset = 0;
boolean finished = false;
boolean loadResInProgress = false;
ProgressDialog progressBarDialogRes;
private void downloadCardImages() { private void loadAvailableSets() {
AlertDialog.Builder cardDownloader = new AlertDialog.Builder(this); final Handler mHandler = new Handler();
cardDownloader.setTitle("Which Set would you like to download?"); progressBarDialogRes = new ProgressDialog(this);
progressBarDialogRes.setTitle("Loading all available sets...");
progressBarDialogRes.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressBarDialogRes.setProgress(0);
new Thread(new Runnable() {
public void run() {
ArrayList<String> sets = new ArrayList<String>(); ArrayList<String> sets = new ArrayList<String>();
if (availableSets == null) { if (availableSets == null) {
loadResInProgress = true;
File baseFolder = new File(getSystemStorageLocation()); File baseFolder = new File(getSystemStorageLocation());
File[] listOfFiles = baseFolder.listFiles(); File[] listOfFiles = baseFolder.listFiles();
ZipFile zipFile = null; ZipFile zipFile = null;
@@ -412,10 +406,39 @@ public class SDLActivity extends Activity implements OnKeyListener {
availableSets = new String[sets.size() + 1]; availableSets = new String[sets.size() + 1];
availableSets[0] = "*.* - All Wagic sets (thousands of cards)"; availableSets[0] = "*.* - All Wagic sets (thousands of cards)";
progressBarDialogRes.setMax(sets.size());
for (int i = 1; i < availableSets.length; i++) { for (int i = 1; i < availableSets.length; i++) {
availableSets[i] = sets.get(i - 1) + " - " + ImgDownloader.getSetInfo(sets.get(i - 1), true, getSystemStorageLocation()); availableSets[i] = sets.get(i - 1) + " - " + ImgDownloader.getSetInfo(sets.get(i - 1), true, getSystemStorageLocation());
progressBarDialogRes.incrementProgressBy((int) (1));
} }
} }
finished = true;
loadResInProgress = false;
progressBarDialogRes.dismiss();
mHandler.post(new Runnable() {
public void run() {
while (!finished) {
try {
Thread.sleep(5000);
} catch (Exception e) {
}
}
downloadCardImages();
}
});
}
}).start();
progressBarDialogRes.show();
}
private void downloadCardImages() {
AlertDialog.Builder cardDownloader = new AlertDialog.Builder(this);
cardDownloader.setTitle("Which Set would you like to download?");
cardDownloader.setSingleChoiceItems(availableSets, -1, new DialogInterface.OnClickListener() { cardDownloader.setSingleChoiceItems(availableSets, -1, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) { public void onClick(DialogInterface dialog, int item) {
set = availableSets[item].split(" - ")[0]; set = availableSets[item].split(" - ")[0];
@@ -431,33 +454,71 @@ public class SDLActivity extends Activity implements OnKeyListener {
cardDownloader.create().show(); cardDownloader.create().show();
} }
private void downloadCardImagesStart() {
AlertDialog.Builder cardDownloader = new AlertDialog.Builder(this);
cardDownloader.setTitle("Download of: " + set);
cardDownloader.setMessage("IMPORTANT: After you press OK don't turn off phone or wi-fi/data connection and don't close Wagic.\nThe download process can take several minutes according to the number of images contained in the selected set.\nNOTE: if you choose *.* you may have to wait several hours!!!");
cardDownloader.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
boolean error = false; boolean error = false;
String res = ""; String res = "";
boolean dowloadInProgress = false;
ProgressDialog cardDownloader;
private void downloadCardImagesStart() {
final Handler mHandler = new Handler();
cardDownloader = new ProgressDialog(this);
cardDownloader.setTitle("Downloading set: " + set);
cardDownloader.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
cardDownloader.setProgress(0);
cardDownloader.setMessage("Don't turn off phone or wi-fi/data connection and don't quit Wagic. The download could take several minutes, but you can hide this window and continue to play, a pop-up will notify the completion of process.");
new Thread(new Runnable() {
public void run() {
try { try {
res = ImgDownloader.DownloadCardImages(set, availableSets, "HI", getSystemStorageLocation(), getUserStorageLocation() + "sets/"); dowloadInProgress = true;
res = ImgDownloader.DownloadCardImages(set, availableSets, "HI", getSystemStorageLocation(), getUserStorageLocation() + "sets/", cardDownloader);
} catch (Exception e) { } catch (Exception e) {
res = e.getMessage(); res = e.getMessage();
error = true; error = true;
} }
mHandler.post(new Runnable() {
public void run() {
downloadCardCompleted(error, res, set); downloadCardCompleted(error, res, set);
dowloadInProgress = false;
cardDownloader.dismiss();
}
});
}
}).start();
cardDownloader.setButton(DialogInterface.BUTTON_POSITIVE, "Hide", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
cardDownloader.hide();
} }
}); });
cardDownloader.setNegativeButton("Back", new DialogInterface.OnClickListener() { cardDownloader.setButton(DialogInterface.BUTTON_NEGATIVE, "Stop Download", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
mHandler.post(new Runnable() {
public void run() {
downloadCardInterruped(set, cardDownloader.getProgress(), cardDownloader.getMax());
dowloadInProgress = false;
cardDownloader.dismiss();
}
});
}
});
cardDownloader.show();
}
private void downloadCardInterruped(String set, int cardsDownloaded, int total) {
AlertDialog.Builder infoDialog = new AlertDialog.Builder(this);
infoDialog.setTitle("Download of " + set + " has been interrupted!");
infoDialog.setMessage("WARNING: Only " + cardsDownloaded + " of " + total + " total cards have been downloaded and zip archive (" + set + ".zip) has not been created. You have to start the download again in order to complete the entire set.");
infoDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
downloadCardImages(); downloadCardImages();
} }
}); });
cardDownloader.create().show(); infoDialog.create().show();
} }
private void downloadCardCompleted(boolean error, String res, String set) { private void downloadCardCompleted(boolean error, String res, String set) {
@@ -496,9 +557,17 @@ public class SDLActivity extends Activity implements OnKeyListener {
importDeckOptions(); importDeckOptions();
} else if (itemId == 3) { } else if (itemId == 3) {
if (availableSets == null) if (availableSets == null)
downloadCardInfo(); loadAvailableSets();
else else {
if (loadResInProgress) {
progressBarDialogRes.show();
progressBarDialogRes.show();
} else if (dowloadInProgress) {
cardDownloader.show();
cardDownloader.show();
} else
downloadCardImages(); downloadCardImages();
}
} else if (itemId == 4) { } else if (itemId == 4) {
// display some info about the app // display some info about the app
AlertDialog.Builder infoDialog = new AlertDialog.Builder(this); AlertDialog.Builder infoDialog = new AlertDialog.Builder(this);