Improved Android Downloader and added the multiple set selection in the download option menu.

This commit is contained in:
valfieri
2019-08-26 00:44:12 +02:00
parent 39884d9711
commit 29f1420abc
2 changed files with 565 additions and 526 deletions
@@ -87,9 +87,9 @@ public class ImgDownloader {
while (e.hasMoreElements()) { while (e.hasMoreElements()) {
ZipEntry entry = e.nextElement(); ZipEntry entry = e.nextElement();
String entryName = entry.getName(); String entryName = entry.getName();
if (entryName.contains("sets/")) { if (entryName.contains("sets" + File.separator)) {
if (entryName.contains("_cards.dat")) { if (entryName.contains("_cards.dat")) {
String[] names = entryName.split("/"); String[] names = entryName.split(File.separator);
if (setName.equalsIgnoreCase(names[1])) { if (setName.equalsIgnoreCase(names[1])) {
stream = zipFile.getInputStream(entry); stream = zipFile.getInputStream(entry);
byte[] buffer = new byte[1]; byte[] buffer = new byte[1];
@@ -413,7 +413,7 @@ public class ImgDownloader {
} }
} }
System.out.println("Warning: Token " + name + " has not been found in https://deckmaster.info so i will search for it between any other set in " + imageurl + " (it can take long time)"); System.out.println("Warning: Token " + name + " has not been found in https://deckmaster.info so i will search for it between any other set in " + imageurl + " (it can take long time)");
for (int i = 1; i < availableSets.length; i++) { for (int i = 0; i < availableSets.length; i++) {
String currentSet = availableSets[i].toLowerCase().split(" - ")[0]; String currentSet = availableSets[i].toLowerCase().split(" - ")[0];
if (!currentSet.equalsIgnoreCase(set)) { if (!currentSet.equalsIgnoreCase(set)) {
try { try {
@@ -461,40 +461,39 @@ public class ImgDownloader {
Integer ThumbX = 0; Integer ThumbX = 0;
Integer ThumbY = 0; Integer ThumbY = 0;
if (targetres.equals("HI")) { if (targetres.equals("High")) {
ImgX = 488; ImgX = 488;
ImgY = 680; ImgY = 680;
ThumbX = 90; ThumbX = 90;
ThumbY = 128; ThumbY = 128;
} else if (targetres.equals("LOW")) { } else if (targetres.equals("Medium")) {
ImgX = 244; ImgX = 244;
ImgY = 340; ImgY = 340;
ThumbX = 45; ThumbX = 45;
ThumbY = 64; ThumbY = 64;
} else if (targetres.equals("Low")) {
ImgX = 180;
ImgY = 255;
ThumbX = 45;
ThumbY = 64;
} }
File baseFolder = new File(basePath); File baseFolder = new File(basePath);
File[] listOfFiles = baseFolder.listFiles(); File[] listOfFiles = baseFolder.listFiles();
String currentSet = "";
for (int f = 1; f < availableSets.length; f++) {
if (set.equalsIgnoreCase("*.*"))
currentSet = availableSets[f];
else
currentSet = set;
Map<String, String> mappa = new HashMap<String, String>(); Map<String, String> mappa = new HashMap<String, String>();
ZipFile zipFile = null; ZipFile zipFile = null;
InputStream stream = null; InputStream stream = null;
java.nio.file.Path filePath = null; java.nio.file.Path filePath = null;
try { try {
zipFile = new ZipFile(basePath + "/" + listOfFiles[0].getName()); zipFile = new ZipFile(basePath + File.separator + listOfFiles[0].getName());
Enumeration<? extends ZipEntry> e = zipFile.entries(); Enumeration<? extends ZipEntry> e = zipFile.entries();
while (e.hasMoreElements()) { while (e.hasMoreElements()) {
ZipEntry entry = e.nextElement(); ZipEntry entry = e.nextElement();
String entryName = entry.getName(); String entryName = entry.getName();
if (entryName != null && entryName.contains("sets/")) { if (entryName != null && entryName.contains("sets" + File.separator)) {
if (entryName.contains("_cards.dat")) { if (entryName.contains("_cards.dat")) {
String[] names = entryName.split("/"); String[] names = entryName.split(File.separator);
if (currentSet.equalsIgnoreCase(names[1])) { if (set.equalsIgnoreCase(names[1])) {
stream = zipFile.getInputStream(entry); stream = zipFile.getInputStream(entry);
byte[] buffer = new byte[1]; byte[] buffer = new byte[1];
java.nio.file.Path outDir = Paths.get(basePath); java.nio.file.Path outDir = Paths.get(basePath);
@@ -511,8 +510,6 @@ public class ImgDownloader {
} catch (Exception ex) { } catch (Exception ex) {
System.out.println("Error extracting zip file" + ex); System.out.println("Error extracting zip file" + ex);
} }
if (!set.equalsIgnoreCase("*.*"))
f = availableSets.length;
break; break;
} }
} }
@@ -565,9 +562,8 @@ public class ImgDownloader {
progressBarDialog.setProgress(0); progressBarDialog.setProgress(0);
progressBarDialog.setMax(totalcards); progressBarDialog.setMax(totalcards);
progressBarDialog.setTitle("Downloading set: " + set);
File imgPath = new File(destinationPath + set + "/"); File imgPath = new File(destinationPath + set + File.separator);
if (!imgPath.exists()) { if (!imgPath.exists()) {
System.out.println("creating directory: " + imgPath.getName()); System.out.println("creating directory: " + imgPath.getName());
boolean result = false; boolean result = false;
@@ -583,7 +579,7 @@ public class ImgDownloader {
} }
} }
File thumbPath = new File(destinationPath + set + "/thumbnails/"); File thumbPath = new File(destinationPath + set + File.separator + "thumbnails" + File.separator);
if (!thumbPath.exists()) { if (!thumbPath.exists()) {
System.out.println("creating directory: " + thumbPath.getName()); System.out.println("creating directory: " + thumbPath.getName());
boolean result = false; boolean result = false;
@@ -627,7 +623,7 @@ public class ImgDownloader {
System.err.println("Error: Problem reading card (" + mappa.get(id) + ") infos from: " + baseurl + id + ", i will not retry anymore..."); System.err.println("Error: Problem reading card (" + mappa.get(id) + ") infos from: " + baseurl + id + ", i will not retry anymore...");
continue; continue;
} }
String scryset = currentSet; String scryset = set;
if (scryset.equalsIgnoreCase("MRQ")) if (scryset.equalsIgnoreCase("MRQ"))
scryset = "MMQ"; scryset = "MMQ";
else if (scryset.equalsIgnoreCase("AVN")) else if (scryset.equalsIgnoreCase("AVN"))
@@ -682,7 +678,7 @@ public class ImgDownloader {
doc = Jsoup.connect(imageurl + scryset.toLowerCase()).get(); doc = Jsoup.connect(imageurl + scryset.toLowerCase()).get();
} catch (Exception e3) { } catch (Exception e3) {
System.err.println("Error: Problem downloading card: " + mappa.get(id) + "-" + id + " from " + scryset + " on ScryFall, i will not retry anymore..."); System.err.println("Error: Problem downloading card: " + mappa.get(id) + "-" + id + " from " + scryset + " on ScryFall, i will not retry anymore...");
res = mappa.get(id) + " - " + currentSet + "/" + id + ".jpg\n" + res; res = mappa.get(id) + " - " + set + File.separator + id + ".jpg\n" + res;
continue; continue;
} }
} }
@@ -716,8 +712,8 @@ public class ImgDownloader {
out.close(); out.close();
in.close(); in.close();
byte[] response = out.toByteArray(); byte[] response = out.toByteArray();
String cardimage = imgPath + "/" + id + ".jpg"; String cardimage = imgPath + File.separator + id + ".jpg";
String thumbcardimage = thumbPath + "/" + id + ".jpg"; String thumbcardimage = thumbPath + File.separator + id + ".jpg";
if (id.equals("11492111") || id.equals("11492112") || id.equals("11492113") || if (id.equals("11492111") || id.equals("11492112") || id.equals("11492113") ||
id.equals("11492114") || id.equals("11492115")) { id.equals("11492114") || id.equals("11492115")) {
cardimage = imgPath + File.separator + id + "t.jpg"; cardimage = imgPath + File.separator + id + "t.jpg";
@@ -795,8 +791,8 @@ public class ImgDownloader {
out.close(); out.close();
in.close(); in.close();
byte[] response = out.toByteArray(); byte[] response = out.toByteArray();
String cardimage = imgPath + "/" + id + ".jpg"; String cardimage = imgPath + File.separator + id + ".jpg";
String thumbcardimage = thumbPath + "/" + id + ".jpg"; String thumbcardimage = thumbPath + File.separator + id + ".jpg";
FileOutputStream fos = new FileOutputStream(cardimage); FileOutputStream fos = new FileOutputStream(cardimage);
fos.write(response); fos.write(response);
fos.close(); fos.close();
@@ -1047,15 +1043,15 @@ public class ImgDownloader {
} }
try { try {
try { try {
File oldzip = new File(destinationPath + "/" + set + "/" + set + ".zip"); File oldzip = new File(destinationPath + File.separator + set + File.separator + set + ".zip");
oldzip.delete(); oldzip.delete();
} catch (Exception e) { } catch (Exception e) {
} }
ZipParameters zipParameters = new ZipParameters(); ZipParameters zipParameters = new ZipParameters();
zipParameters.setCompressionMethod(CompressionMethod.STORE); zipParameters.setCompressionMethod(CompressionMethod.STORE);
File folder = new File(destinationPath + set + "/"); File folder = new File(destinationPath + set + File.separator);
File[] listOfFile = folder.listFiles(); File[] listOfFile = folder.listFiles();
net.lingala.zip4j.ZipFile zipped = new net.lingala.zip4j.ZipFile(destinationPath + "/" + set + "/" + set + ".zip"); net.lingala.zip4j.ZipFile zipped = new net.lingala.zip4j.ZipFile(destinationPath + File.separator + set + File.separator + set + ".zip");
for (int i = 0; i < listOfFile.length; i++) { for (int i = 0; i < listOfFile.length; i++) {
if (listOfFile[i].isDirectory()) { if (listOfFile[i].isDirectory()) {
zipped.addFolder(listOfFile[i], zipParameters); zipped.addFolder(listOfFile[i], zipParameters);
@@ -1063,7 +1059,7 @@ public class ImgDownloader {
zipped.addFile(listOfFile[i], zipParameters); zipped.addFile(listOfFile[i], zipParameters);
} }
} }
File destFolder = new File(destinationPath + set + "/"); File destFolder = new File(destinationPath + set + File.separator);
listOfFiles = destFolder.listFiles(); listOfFiles = destFolder.listFiles();
for (int u = 0; u < listOfFiles.length; u++) { for (int u = 0; u < listOfFiles.length; u++) {
if (!listOfFiles[u].getName().contains(".zip")) { if (!listOfFiles[u].getName().contains(".zip")) {
@@ -1078,7 +1074,7 @@ public class ImgDownloader {
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
}
return res; return res;
} }
} }
@@ -31,6 +31,7 @@ import net.wagic.utils.DeckImporter;
import net.wagic.utils.ImgDownloader; import net.wagic.utils.ImgDownloader;
import android.app.Activity; import android.app.Activity;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.widget.ListView;
import android.app.Dialog; import android.app.Dialog;
import android.app.ProgressDialog; import android.app.ProgressDialog;
import android.content.Context; import android.content.Context;
@@ -360,6 +361,8 @@ public class SDLActivity extends Activity implements OnKeyListener {
String set = ""; String set = "";
String[] availableSets; String[] availableSets;
ArrayList<String> selectedSets;
boolean[] checkedSet;
Integer totalset = 0; Integer totalset = 0;
boolean finished = false; boolean finished = false;
boolean loadResInProgress = false; boolean loadResInProgress = false;
@@ -404,11 +407,12 @@ public class SDLActivity extends Activity implements OnKeyListener {
} }
} }
availableSets = new String[sets.size() + 1]; availableSets = new String[sets.size()];
availableSets[0] = "*.* - All Wagic sets (thousands of cards)"; checkedSet = new boolean[sets.size()];
progressBarDialogRes.setMax(sets.size()); progressBarDialogRes.setMax(sets.size());
for (int i = 1; i < availableSets.length; i++) { for (int i = 0; i < availableSets.length; i++) {
availableSets[i] = sets.get(i - 1) + " - " + ImgDownloader.getSetInfo(sets.get(i - 1), true, getSystemStorageLocation()); availableSets[i] = sets.get(i) + " - " + ImgDownloader.getSetInfo(sets.get(i), true, getSystemStorageLocation());
checkedSet[i] = false;
progressBarDialogRes.incrementProgressBy((int) (1)); progressBarDialogRes.incrementProgressBy((int) (1));
} }
} }
@@ -437,20 +441,38 @@ public class SDLActivity extends Activity implements OnKeyListener {
private void downloadCardImages() { private void downloadCardImages() {
AlertDialog.Builder cardDownloader = new AlertDialog.Builder(this); AlertDialog.Builder cardDownloader = new AlertDialog.Builder(this);
cardDownloader.setTitle("Which Set would you like to download?"); cardDownloader.setTitle("Which Sets would you like to download?");
selectedSets = new ArrayList<String>();
cardDownloader.setSingleChoiceItems(availableSets, -1, new DialogInterface.OnClickListener() { cardDownloader.setMultiChoiceItems(availableSets, checkedSet, new DialogInterface.OnMultiChoiceClickListener() {
public void onClick(DialogInterface dialog, int item) { public void onClick(DialogInterface dialog, int which, boolean isChecked) {
set = availableSets[item].split(" - ")[0]; checkedSet[which] = isChecked;
if (checkedSet[which])
selectedSets.add(availableSets[which].split(" - ")[0]);
else
selectedSets.remove(availableSets[which].split(" - ")[0]);
} }
}); });
cardDownloader.setPositiveButton("Confirm Selection", new DialogInterface.OnClickListener() { cardDownloader.setNeutralButton("Download All", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int id) {
selectedSets.clear();
for (int i = 0; i < availableSets.length; i++) {
selectedSets.add(availableSets[i].split(" - ")[0]);
}
downloadCardImagesStart(); downloadCardImagesStart();
} }
}); });
cardDownloader.setPositiveButton("Download Selected", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
if (selectedSets.size() > 0)
downloadCardImagesStart();
else
downloadCardImages();
}
});
cardDownloader.create().show(); cardDownloader.create().show();
} }
@@ -462,28 +484,47 @@ public class SDLActivity extends Activity implements OnKeyListener {
private void downloadCardImagesStart() { private void downloadCardImagesStart() {
final Handler mHandler = new Handler(); final Handler mHandler = new Handler();
cardDownloader = new ProgressDialog(this); cardDownloader = new ProgressDialog(this);
cardDownloader.setTitle("Downloading set: " + set); cardDownloader.setTitle("Downloading now set: " + set);
cardDownloader.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); cardDownloader.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
cardDownloader.setProgress(0); 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."); if (selectedSets.size() == 1)
cardDownloader.setMessage("You choose to download just 1 set: Please don't quit Wagic or turn off Internet connection, you can hide this window and continue to play, a pop-up will notify the completion of download process.");
else
cardDownloader.setMessage("You choose to download " + selectedSets.size() + " sets: Please don't quit Wagic or turn off Internet connection, you can hide this window and continue to play, a pop-up will notify the completion of download process.");
new Thread(new Runnable() { new Thread(new Runnable() {
public void run() { public void run() {
try {
dowloadInProgress = true; dowloadInProgress = true;
res = ImgDownloader.DownloadCardImages(set, availableSets, "HI", getSystemStorageLocation(), getUserStorageLocation() + "sets/", cardDownloader); if (selectedSets != null) {
for (int i = 0; i < selectedSets.size(); i++) {
try {
set = selectedSets.get(i);
mHandler.post(new Runnable() {
public void run() {
cardDownloader.setTitle("Downloading set: " + set);
}
});
String details = ImgDownloader.DownloadCardImages(set, availableSets, "High", getSystemStorageLocation(), getUserStorageLocation() + "sets/", cardDownloader);
if (!details.isEmpty()) {
if (!res.isEmpty())
res = res + "\nSET " + set + ":\n" + details;
else
res = "SET " + set + ":\n" + details;
}
} catch (Exception e) { } catch (Exception e) {
res = e.getMessage(); res = res + "\n" + e.getMessage();
error = true; error = true;
} }
}
mHandler.post(new Runnable() { mHandler.post(new Runnable() {
public void run() { public void run() {
downloadCardCompleted(error, res, set); downloadSelectedSetsCompleted(error, res);
dowloadInProgress = false; dowloadInProgress = false;
cardDownloader.dismiss(); cardDownloader.dismiss();
} }
}); });
} }
}
}).start(); }).start();
cardDownloader.setButton(DialogInterface.BUTTON_POSITIVE, "Hide", new DialogInterface.OnClickListener() { cardDownloader.setButton(DialogInterface.BUTTON_POSITIVE, "Hide", new DialogInterface.OnClickListener() {
@@ -521,19 +562,21 @@ public class SDLActivity extends Activity implements OnKeyListener {
infoDialog.create().show(); infoDialog.create().show();
} }
private void downloadCardCompleted(boolean error, String res, String set) { private void downloadSelectedSetsCompleted(boolean error, String res) {
AlertDialog.Builder infoDialog = new AlertDialog.Builder(this); AlertDialog.Builder infoDialog = new AlertDialog.Builder(this);
if (!error) { if (!error) {
infoDialog.setTitle("Download Completed: " + set); infoDialog.setTitle("The download process has completed without any error");
if (!res.isEmpty()) if (!res.isEmpty())
infoDialog.setMessage("Warning: Following cards could not be downloaded:\n" + res); infoDialog.setMessage("Following cards could not be downloaded:\n" + res);
else
infoDialog.setMessage("All the cards have been successfully downloaded");
} else { } else {
infoDialog.setTitle("Error downloading: " + set + ", please try again..."); infoDialog.setTitle("Some errors occurred during the process!");
infoDialog.setMessage(res); infoDialog.setMessage(res);
} }
infoDialog.create().show(); infoDialog.create().show();
res = "";
set = "";
selectedSets = new ArrayList<String>();
error = false;
} }
@Override @Override