diff --git a/projects/mtg/Android/src/net/wagic/utils/ImgDownloader.java b/projects/mtg/Android/src/net/wagic/utils/ImgDownloader.java index cf113bc1d..be6ad6dec 100644 --- a/projects/mtg/Android/src/net/wagic/utils/ImgDownloader.java +++ b/projects/mtg/Android/src/net/wagic/utils/ImgDownloader.java @@ -68,17 +68,23 @@ public class ImgDownloader { static HashMap> database; - public static void loadDatabase(String path) { + public static boolean loadDatabase(String path) { database = new HashMap>(); try { String databaseurl = "https://github.com/Vitty85/wagic/releases/download/wagic-v0.21.1/CardImageLinks.csv"; URL url = new URL(databaseurl); HttpURLConnection httpcon = (HttpURLConnection) url.openConnection(); if (httpcon == null) { + System.err.println("Error: Problem downloading or initializing database file, i will use the slow method..."); database = null; - return; + return false; } httpcon.addRequestProperty("User-Agent", "Mozilla/4.76"); + httpcon.setConnectTimeout(30000); + httpcon.setReadTimeout(30000); + httpcon.setAllowUserInteraction(false); + httpcon.setDoInput(true); + httpcon.setDoOutput(false); InputStream in; try { in = new BufferedInputStream(httpcon.getInputStream()); @@ -89,20 +95,52 @@ public class ImgDownloader { try { in = new BufferedInputStream(httpcon.getInputStream()); } catch (Exception ex3) { + System.err.println("Error: Problem downloading or initializing database file, i will use the slow method..."); database = null; - return; + return false; } } } ByteArrayOutputStream out = new ByteArrayOutputStream(); byte[] buf = new byte[1024]; int n = 0; - while (-1 != (n = in.read(buf))) { + long millis = System.currentTimeMillis(); + boolean timeout = false; + while (-1 != (n = in.read(buf)) && !timeout) { out.write(buf, 0, n); + if (System.currentTimeMillis() - millis > 30000) + timeout = true; + } + if (timeout) { + System.out.println("Warning: Timeout downloading database file, i will retry 2 times more..."); + buf = new byte[1024]; + n = 0; + millis = System.currentTimeMillis(); + timeout = false; + while (-1 != (n = in.read(buf)) && !timeout) { + out.write(buf, 0, n); + if (System.currentTimeMillis() - millis > 30000) + timeout = true; + } + if (timeout) { + System.out.println("Warning: Timeout downloading database file, i will retry 1 time more..."); + buf = new byte[1024]; + n = 0; + millis = System.currentTimeMillis(); + timeout = false; + while (-1 != (n = in.read(buf)) && !timeout) { + out.write(buf, 0, n); + if (System.currentTimeMillis() - millis > 30000) + timeout = true; + } + } } out.close(); in.close(); - + if (timeout) { + System.err.println("Error: Timeout downloading database file, i will use the slow method..."); + return false; + } byte[] response = out.toByteArray(); String databasepath = path + File.separator + "CardImageLinks.csv"; FileOutputStream fos = new FileOutputStream(databasepath); @@ -120,8 +158,11 @@ public class ImgDownloader { File del = new File(databasepath); del.delete(); } catch (Exception e) { + System.err.println("Error: Problem downloading or initializing database file, i will use the slow method..."); database = null; + return false; } + return true; } public static boolean fastDownloadCard(String set, String id, String name, String imgPath, String thumbPath, int ImgX, int ImgY, int ThumbX, int ThumbY) { @@ -178,7 +219,7 @@ public class ImgDownloader { timeout = true; } if (timeout) { - System.out.println("Warning: Timeout downloading token: " + id + "t.jpg from " + imageurl + ", i will retry 2 times more..."); + System.out.println("Warning: Problem downloading card: " + name + " (" + id + ".jpg) from " + imageurl + ", i will retry 2 times more..."); buf = new byte[1024]; n = 0; millis = System.currentTimeMillis(); @@ -189,7 +230,7 @@ public class ImgDownloader { timeout = true; } if (timeout) { - System.out.println("Warning: Timeout downloading token: " + id + "t.jpg from " + imageurl + ", i will retry 1 time more..."); + System.out.println("Warning: Problem downloading card: " + name + " (" + id + ".jpg) from " + imageurl + ", i will retry 1 time more..."); buf = new byte[1024]; n = 0; millis = System.currentTimeMillis(); @@ -199,12 +240,12 @@ public class ImgDownloader { if (System.currentTimeMillis() - millis > 10000) timeout = true; } - if (timeout) { - System.out.println("Warning: Timeout downloading token: " + id + "t.jpg from " + imageurl + ", i will try with slow method..."); - return false; - } } } + if (timeout) { + System.out.println("Warning: Problem downloading card: " + name + " (" + id + ".jpg) from " + imageurl + ", i will try with slow method..."); + return false; + } out.close(); in.close(); byte[] response = out.toByteArray(); @@ -275,11 +316,43 @@ public class ImgDownloader { ByteArrayOutputStream outtoken = new ByteArrayOutputStream(); byte[] buftoken = new byte[1024]; int ntoken = 0; - while (-1 != (ntoken = intoken.read(buftoken))) { + long millis = System.currentTimeMillis(); + boolean timeout = false; + while (-1 != (ntoken = intoken.read(buftoken)) && !timeout) { outtoken.write(buftoken, 0, ntoken); + if (System.currentTimeMillis() - millis > 10000) + timeout = true; + } + if (timeout) { + System.out.println("Warning: Problem downloading token: " + id + "t.jpg from " + imageurl + ", i will retry 2 times more..."); + buftoken = new byte[1024]; + ntoken = 0; + millis = System.currentTimeMillis(); + timeout = false; + while (-1 != (ntoken = intoken.read(buftoken)) && !timeout) { + outtoken.write(buftoken, 0, ntoken); + if (System.currentTimeMillis() - millis > 10000) + timeout = true; + } + if (timeout) { + System.out.println("Warning: Problem downloading token: " + id + "t.jpg from " + imageurl + ", i will retry 1 time more..."); + buftoken = new byte[1024]; + ntoken = 0; + millis = System.currentTimeMillis(); + timeout = false; + while (-1 != (ntoken = intoken.read(buftoken)) && !timeout) { + outtoken.write(buftoken, 0, ntoken); + if (System.currentTimeMillis() - millis > 10000) + timeout = true; + } + } } outtoken.close(); intoken.close(); + if (timeout) { + System.out.println("Warning: Problem downloading token: " + id + "t.jpg from " + imageurl + ", i will try with slow method..."); + return false; + } byte[] responsetoken = outtoken.toByteArray(); String tokenimage = imgPath + File.separator + id + "t.jpg"; String tokenthumbimage = thumbPath + File.separator + id + "t.jpg"; @@ -1454,11 +1527,43 @@ public class ImgDownloader { ByteArrayOutputStream out = new ByteArrayOutputStream(); byte[] buf = new byte[1024]; int n = 0; - while (-1 != (n = in.read(buf))) { + long millis = System.currentTimeMillis(); + boolean timeout = false; + while (-1 != (n = in.read(buf)) && !timeout) { out.write(buf, 0, n); + if (System.currentTimeMillis() - millis > 10000) + timeout = true; + } + if (timeout) { + System.out.println("Warning: Problem downloading card: " + mappa.get(id) + " (" + id + ".jpg), i will retry 2 times more..."); + buf = new byte[1024]; + n = 0; + millis = System.currentTimeMillis(); + timeout = false; + while (-1 != (n = in.read(buf)) && !timeout) { + out.write(buf, 0, n); + if (System.currentTimeMillis() - millis > 10000) + timeout = true; + } + if (timeout) { + System.out.println("Warning: Problem downloading card: " + mappa.get(id) + " (" + id + ".jpg), i will retry 1 time more..."); + buf = new byte[1024]; + n = 0; + millis = System.currentTimeMillis(); + timeout = false; + while (-1 != (n = in.read(buf)) && !timeout) { + out.write(buf, 0, n); + if (System.currentTimeMillis() - millis > 10000) + timeout = true; + } + } } out.close(); in.close(); + if (timeout) { + System.err.println("Warning: Problem downloading card: " + mappa.get(id) + " (" + id + ".jpg) from, i will not retry anymore..."); + break; + } byte[] response = out.toByteArray(); String cardimage = imgPath + File.separator + id + ".jpg"; String thumbcardimage = thumbPath + File.separator + id + ".jpg"; @@ -1471,8 +1576,9 @@ public class ImgDownloader { FileOutputStream fout = new FileOutputStream(cardimage); resized.compress(Bitmap.CompressFormat.JPEG, 100, fout); } catch (Exception e) { - System.err.println("Warning: Problem resizing card: " + mappa.get(id) + " (" + id + ".jpg), image may be corrupted..."); + System.err.println("Error: Problem resizing card: " + mappa.get(id) + " (" + id + ".jpg), image may be corrupted..."); res = mappa.get(id) + " - " + set + File.separator + id + ".jpg\n" + res; + break; } try { Bitmap yourBitmapthumb = BitmapFactory.decodeFile(cardimage); @@ -1480,8 +1586,9 @@ public class ImgDownloader { FileOutputStream fout = new FileOutputStream(thumbcardimage); resizedThumb.compress(Bitmap.CompressFormat.JPEG, 100, fout); } catch (Exception e) { - System.err.println("Warning: Problem resizing card thumbnail: " + mappa.get(id) + " (" + id + ".jpg, image may be corrupted..."); + System.err.println("Error: Problem resizing card thumbnail: " + mappa.get(id) + " (" + id + ".jpg, image may be corrupted..."); res = mappa.get(id) + " - " + set + File.separator + "thumbnails" + File.separator + id + ".jpg\n" + res; + break; } continue; } @@ -1733,11 +1840,43 @@ public class ImgDownloader { ByteArrayOutputStream out = new ByteArrayOutputStream(); byte[] buf = new byte[1024]; int n = 0; - while (-1 != (n = in.read(buf))) { + long millis = System.currentTimeMillis(); + boolean timeout = false; + while (-1 != (n = in.read(buf)) && !timeout) { out.write(buf, 0, n); + if (System.currentTimeMillis() - millis > 10000) + timeout = true; + } + if (timeout) { + System.out.println("Warning: Problem downloading card: " + mappa.get(id) + " (" + id + ".jpg), i will retry 2 times more..."); + buf = new byte[1024]; + n = 0; + millis = System.currentTimeMillis(); + timeout = false; + while (-1 != (n = in.read(buf)) && !timeout) { + out.write(buf, 0, n); + if (System.currentTimeMillis() - millis > 10000) + timeout = true; + } + if (timeout) { + System.out.println("Warning: Problem downloading card: " + mappa.get(id) + " (" + id + ".jpg), i will retry 1 time more..."); + buf = new byte[1024]; + n = 0; + millis = System.currentTimeMillis(); + timeout = false; + while (-1 != (n = in.read(buf)) && !timeout) { + out.write(buf, 0, n); + if (System.currentTimeMillis() - millis > 10000) + timeout = true; + } + } } out.close(); in.close(); + if (timeout) { + System.err.println("Error: Problem downloading card: " + mappa.get(id) + " (" + id + ".jpg) from, i will not retry anymore..."); + break; + } byte[] response = out.toByteArray(); String cardimage = imgPath + File.separator + id + ".jpg"; String thumbcardimage = thumbPath + File.separator + id + ".jpg"; @@ -1750,8 +1889,9 @@ public class ImgDownloader { FileOutputStream fout = new FileOutputStream(cardimage); resized.compress(Bitmap.CompressFormat.JPEG, 100, fout); } catch (Exception e) { - System.err.println("Warning: Problem resizing card: " + mappa.get(id) + " (" + id + ".jpg), image may be corrupted..."); + System.err.println("Error: Problem resizing card: " + mappa.get(id) + " (" + id + ".jpg), image may be corrupted..."); res = mappa.get(id) + " - " + set + File.separator + id + ".jpg\n" + res; + break; } try { Bitmap yourBitmapthumb = BitmapFactory.decodeFile(cardimage); @@ -1759,8 +1899,9 @@ public class ImgDownloader { FileOutputStream fout = new FileOutputStream(thumbcardimage); resizedThumb.compress(Bitmap.CompressFormat.JPEG, 100, fout); } catch (Exception e) { - System.err.println("Warning: Problem resizing card thumbnail: " + mappa.get(id) + " (" + id + ".jpg), image may be corrupted..."); + System.err.println("Error: Problem resizing card thumbnail: " + mappa.get(id) + " (" + id + ".jpg), image may be corrupted..."); res = mappa.get(id) + " - " + set + File.separator + "thumbnails" + File.separator + id + ".jpg\n" + res; + break; } String text = ""; for (k = 0; k < divs.size(); k++) @@ -2047,11 +2188,43 @@ public class ImgDownloader { ByteArrayOutputStream outtoken = new ByteArrayOutputStream(); byte[] buftoken = new byte[1024]; int ntoken = 0; - while (-1 != (ntoken = intoken.read(buftoken))) { + millis = System.currentTimeMillis(); + timeout = false; + while (-1 != (ntoken = intoken.read(buftoken)) && !timeout) { outtoken.write(buftoken, 0, ntoken); + if (System.currentTimeMillis() - millis > 10000) + timeout = true; + } + if (timeout) { + System.out.println("Warning: Problem downloading token: " + id + "t.jpg from, i will retry 2 times more..."); + buftoken = new byte[1024]; + ntoken = 0; + millis = System.currentTimeMillis(); + timeout = false; + while (-1 != (ntoken = intoken.read(buftoken)) && !timeout) { + outtoken.write(buftoken, 0, ntoken); + if (System.currentTimeMillis() - millis > 10000) + timeout = true; + } + if (timeout) { + System.out.println("Warning: Problem downloading token: " + id + "t.jpg from, i will retry 1 time more..."); + buftoken = new byte[1024]; + ntoken = 0; + millis = System.currentTimeMillis(); + timeout = false; + while (-1 != (ntoken = intoken.read(buftoken)) && !timeout) { + outtoken.write(buftoken, 0, ntoken); + if (System.currentTimeMillis() - millis > 10000) + timeout = true; + } + } } outtoken.close(); intoken.close(); + if (timeout) { + System.err.println("Error: Problem downloading token: " + id + "t.jpg from, i will not retry anymore..."); + break; + } byte[] responsetoken = outtoken.toByteArray(); String tokenimage = imgPath + File.separator + id + "t.jpg"; String tokenthumbimage = thumbPath + File.separator + id + "t.jpg"; @@ -2068,8 +2241,9 @@ public class ImgDownloader { FileOutputStream fout = new FileOutputStream(tokenimage); resizedToken.compress(Bitmap.CompressFormat.JPEG, 100, fout); } catch (Exception e) { - System.err.println("Warning: Problem resizing token: " + id + "t.jpg, image may be corrupted..."); + System.err.println("Error: Problem resizing token: " + id + "t.jpg, image may be corrupted..."); res = nametoken + " - " + set + File.separator + "thumbnails" + File.separator + id + "t.jpg\n" + res; + break; } try { Bitmap yourBitmapTokenthumb = BitmapFactory.decodeFile(tokenimage); @@ -2077,8 +2251,9 @@ public class ImgDownloader { FileOutputStream fout = new FileOutputStream(tokenthumbimage); resizedThumbToken.compress(Bitmap.CompressFormat.JPEG, 100, fout); } catch (Exception e) { - System.err.println("Warning: Problem resizing token thumbnail: " + id + "t.jpg, image may be corrupted..."); + System.err.println("Error: Problem resizing token thumbnail: " + id + "t.jpg, image may be corrupted..."); res = nametoken + " - " + set + File.separator + "thumbnails" + File.separator + id + "t.jpg\n" + res; + break; } break; } diff --git a/projects/mtg/Android/src/org/libsdl/app/SDLActivity.java b/projects/mtg/Android/src/org/libsdl/app/SDLActivity.java index a84efe765..8b3455ef3 100644 --- a/projects/mtg/Android/src/org/libsdl/app/SDLActivity.java +++ b/projects/mtg/Android/src/org/libsdl/app/SDLActivity.java @@ -368,6 +368,7 @@ public class SDLActivity extends Activity implements OnKeyListener { boolean finished2 = false; boolean loadResInProgress = false; ProgressDialog progressBarDialogRes; + boolean fast = false; private void loadAvailableSets() { final Handler mHandler = new Handler(); @@ -429,7 +430,12 @@ public class SDLActivity extends Activity implements OnKeyListener { } } selectedSets = new ArrayList(); - downloadCardImages(); + if(!fast) { + showWarningFast(); + } + else { + downloadCardImages(); + } } }); } @@ -437,14 +443,37 @@ public class SDLActivity extends Activity implements OnKeyListener { new Thread(new Runnable() { public void run() { - ImgDownloader.loadDatabase(getSystemStorageLocation()); + fast = ImgDownloader.loadDatabase(getSystemStorageLocation()); finished2 = true; } }).start(); progressBarDialogRes.show(); + } + private void showWarningFast() { + AlertDialog.Builder infoDialog = new AlertDialog.Builder(this); + infoDialog.setTitle("Problem downloading the images database file"); + infoDialog.setMessage("The program will use the slow method (not indexed), so the images download may take really long time..."); + infoDialog.setPositiveButton("Continue", new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int id) { + downloadCardImages(); + } + }); + + infoDialog.setNegativeButton("Retry", new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int id) { + fast = ImgDownloader.loadDatabase(getSystemStorageLocation()); + if(fast){ + downloadCardImages(); + } else { + showWarningFast(); + } + } + }); + + infoDialog.create().show(); } private void downloadCardImages() {