Skip to content

Commit

Permalink
more crash resistant
Browse files Browse the repository at this point in the history
  • Loading branch information
ManApart committed Jul 15, 2022
1 parent 0a3f0c7 commit 5002873
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 17 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ android {
minSdk 25
targetSdk 32
versionCode 11
versionName "2.2.1"
versionName "2.2.2"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand All @@ -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()
}
}

Expand All @@ -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)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import kotlin.concurrent.thread

class JsonDownloader(
private val image: ImageInfo,
private val downloading: MutableSet<ImageInfo>,
private val listener: (ImageInfo, String) -> Unit
) {
private val imageUrl = "http://www.effectgames.com/demos/canvascycle/image.php?file="
Expand All @@ -21,49 +22,55 @@ 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 {
cleanImageJson(json)
}
}

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)
}
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)
Expand Down

0 comments on commit 5002873

Please sign in to comment.