From 500287338e0f2b1b7a0a06fe32edd12fbee7ce27 Mon Sep 17 00:00:00 2001 From: ManApart Date: Fri, 15 Jul 2022 08:01:42 -0400 Subject: [PATCH] more crash resistant --- app/build.gradle | 2 +- .../cycling/jsonLoading/ImageLoader.kt | 6 ++-- .../cycling/jsonLoading/JsonDownloader.kt | 33 +++++++++++-------- 3 files changed, 24 insertions(+), 17 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 2a68c92..d6eadab 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -11,7 +11,7 @@ android { minSdk 25 targetSdk 32 versionCode 11 - versionName "2.2.1" + versionName "2.2.2" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } diff --git a/app/src/main/java/rak/pixellwp/cycling/jsonLoading/ImageLoader.kt b/app/src/main/java/rak/pixellwp/cycling/jsonLoading/ImageLoader.kt index 9e4633b..f394185 100644 --- a/app/src/main/java/rak/pixellwp/cycling/jsonLoading/ImageLoader.kt +++ b/app/src/main/java/rak/pixellwp/cycling/jsonLoading/ImageLoader.kt @@ -107,7 +107,7 @@ class ImageLoader(private val context: Context) { Log.d(logTag, "Unable to find ${image.name} locally, downloading using id ${image.id}") downloading.add(image) thread { - JsonDownloader(image, ::saveAndLoadImage).download() + JsonDownloader(image, downloading, ::saveAndLoadImage).download() } Toast.makeText(context, "Unable to find ${image.name} locally. I'll change the image as soon as it's downloaded", Toast.LENGTH_LONG).show() } @@ -133,7 +133,7 @@ class ImageLoader(private val context: Context) { private fun downloadImageWithoutChanging(image: ImageInfo) { if (!downloading.contains(image)) { downloading.add(image) - JsonDownloader(image, ::saveImageWithoutLoading).download() + JsonDownloader(image, downloading, ::saveImageWithoutLoading).download() } } @@ -142,11 +142,11 @@ class ImageLoader(private val context: Context) { val stream = OutputStreamWriter(context.openFileOutput(image.getFileName(), Context.MODE_PRIVATE)) stream.write(json) stream.close() + Log.d(logTag, "saved ${image.name} as ${image.getFileName()}") } catch (e: Exception) { Log.e(logTag, "Unable to save image") e.printStackTrace() } - Log.d(logTag, "saved ${image.name} as ${image.getFileName()}") downloading.remove(image) } diff --git a/app/src/main/java/rak/pixellwp/cycling/jsonLoading/JsonDownloader.kt b/app/src/main/java/rak/pixellwp/cycling/jsonLoading/JsonDownloader.kt index 0268b36..6502b5f 100644 --- a/app/src/main/java/rak/pixellwp/cycling/jsonLoading/JsonDownloader.kt +++ b/app/src/main/java/rak/pixellwp/cycling/jsonLoading/JsonDownloader.kt @@ -11,6 +11,7 @@ import kotlin.concurrent.thread class JsonDownloader( private val image: ImageInfo, + private val downloading: MutableSet, private val listener: (ImageInfo, String) -> Unit ) { private val imageUrl = "http://www.effectgames.com/demos/canvascycle/image.php?file=" @@ -21,31 +22,39 @@ class JsonDownloader( completeDownload(downloadImage()) } - private fun downloadImage(): String { - var json = "" + private fun downloadImage(): String? { try { val inputStream = URL(getFullUrl(image)).openStream() val s = java.util.Scanner(inputStream).useDelimiter("\\A") - json = if (s.hasNext()) s.next() else "" + val json = if (s.hasNext()) s.next() else "" inputStream.close() return json } catch (e: Exception) { Log.e(logTag, "Unable to download image from ${getFullUrl(image)}") e.printStackTrace() + downloading.remove(image) } - return json + return null } private fun completeDownload(result: String?) { - val json = cleanJson(result) - val jsonSample = json.getSample() - Log.d(logTag, "downloaded json for ${image.name} from ${getFullUrl(image)} to ${image.getFileName()}: $jsonSample") - listener(image, json) + if (result != null) { + try { + val json = cleanJson(result) + val jsonSample = json.getSample() + Log.d(logTag, "downloaded json for ${image.name} from ${getFullUrl(image)} to ${image.getFileName()}: $jsonSample") + listener(image, json) + } catch (e: Exception) { + Log.e(logTag, "Unable to download image from ${getFullUrl(image)}") + e.printStackTrace() + downloading.remove(image) + } + } } - private fun cleanJson(json: String?): String { + private fun cleanJson(json: String): String { return if (image.isTimeline) { cleanTimelineImageJson(json) } else { @@ -53,8 +62,7 @@ class JsonDownloader( } } - private fun cleanImageJson(json: String?): String { - if (json == null) return "" + private fun cleanImageJson(json: String): String { if (json.length > 25) { val start = json.indexOf("{filename") return json.substring(start, json.length - 4) @@ -62,8 +70,7 @@ class JsonDownloader( return json } - private fun cleanTimelineImageJson(json: String?): String { - if (json == null) return "" + private fun cleanTimelineImageJson(json: String): String { if (json.length > 25) { val start = json.indexOf("{base") return json.substring(start, json.length - 3)