Skip to content

Commit

Permalink
Refactor archive compression for WebView
Browse files Browse the repository at this point in the history
  • Loading branch information
AbandonedCart committed Nov 1, 2023
1 parent 72d0638 commit 96f1636
Show file tree
Hide file tree
Showing 6 changed files with 388 additions and 96 deletions.
24 changes: 14 additions & 10 deletions app/src/main/java/com/hiddenramblings/tagmo/BrowserActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -1878,16 +1878,20 @@ class BrowserActivity : AppCompatActivity(), BrowserSettingsListener,
val binFile = resources.getStringArray(R.array.mimetype_bin)
if (binFile.contains(intent.type)) {
val length = getUriFIleSize(uri)
if (length >= NfcByte.TAG_DATA_SIZE) {
val data = TagReader.readTagDocument(uri)
updateAmiiboView(data, AmiiboFile(
uri.path?.let { File(it) }, Amiibo.dataToId(data), data
))
} else if (getUriFIleSize(uri) > NfcByte.KEY_FILE_SIZE) {
onLoadSettingsFragment()
fragmentSettings?.validateKeys(intent.parcelable(Intent.EXTRA_STREAM) as Uri?)
} else {
Toasty(this@BrowserActivity).Short(R.string.error_uri_size)
when {
length >= NfcByte.TAG_DATA_SIZE -> {
val data = TagReader.readTagDocument(uri)
updateAmiiboView(data, AmiiboFile(
uri.path?.let { File(it) }, Amiibo.dataToId(data), data
))
}
length >= NfcByte.KEY_FILE_SIZE -> {
onLoadSettingsFragment()
fragmentSettings?.validateKeys(intent.parcelable(Intent.EXTRA_STREAM) as Uri?)
}
else -> {
Toasty(this@BrowserActivity).Short(R.string.error_uri_size)
}
}
} else if (intent.type == getString(R.string.mimetype_zip)) {
decompressArchive(uri)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -578,7 +578,9 @@ class GattService : Service() {
if (emptyAdapater || address == null) return false

// Previously connected device. Try to reconnect.
if (address == mBluetoothDeviceAddress) mBluetoothGatt?.let { return it.connect() }
mBluetoothGatt?.let {
if (address == mBluetoothDeviceAddress && it.connect()) return true
}
val device = mBluetoothAdapter?.getRemoteDevice(address) ?: return false
// We want to directly connect to the device, so we are setting the autoConnect
// parameter to false.
Expand Down
107 changes: 107 additions & 0 deletions app/src/main/java/com/hiddenramblings/tagmo/eightbit/util/UnZip.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package com.hiddenramblings.tagmo.eightbit.util

import android.content.Context
import android.os.Build
import android.os.Handler
import android.os.Looper
import androidx.annotation.RequiresApi
import com.hiddenramblings.tagmo.R
import com.hiddenramblings.tagmo.eightbit.io.Debug
import com.hiddenramblings.tagmo.eightbit.os.Version
import com.hiddenramblings.tagmo.eightbit.widget.ProgressAlert
import java.io.File
import java.io.FileOutputStream
import java.io.IOException
import java.nio.file.Files
import java.nio.file.Paths
import java.util.zip.ZipFile

class UnZip(var context: Context, var archive: File, var outputDir: File) : Runnable {
private val zipHandler = Handler(Looper.getMainLooper())
private var dialog: ProgressAlert? = null

@RequiresApi(Build.VERSION_CODES.KITKAT)
private fun decompress() {
ZipFile(archive).use { zipFile ->
val entries = zipFile.entries()
while (entries.hasMoreElements()) {
// get the zip entry
val finalEntry = entries.nextElement()
zipHandler.post {
dialog?.setMessage(context.getString(R.string.unzip_item, finalEntry.name))
}
if (finalEntry.isDirectory) {
val dir = File(
outputDir, finalEntry.name.replace(File.separator, "")
)
if (!dir.exists() && !dir.mkdirs())
throw RuntimeException(context.getString(R.string.mkdir_failed, dir.name))
} else {
zipFile.getInputStream(finalEntry).use { zipInStream ->
if (Version.isOreo) {
Files.copy(
zipInStream,
Paths.get(outputDir.absolutePath, finalEntry.name)
)
} else {
FileOutputStream(File(outputDir, finalEntry.name)).use { fileOut ->
val buffer = ByteArray(8192)
var len: Int
while (zipInStream.read(buffer).also { len = it } != -1)
fileOut.write(buffer, 0, len)
}
}
}
}
}
}
}
@Throws(IOException::class)
private fun decompressLegacy() {
val zipFile = ZipFile(archive)
val entries = zipFile.entries()
while (entries.hasMoreElements()) {
// get the zip entry
val finalEntry = entries.nextElement()
zipHandler.post {
dialog?.setMessage(context.getString(R.string.unzip_item, finalEntry.name))
}
if (finalEntry.isDirectory) {
val dir = File(
outputDir, finalEntry.name.replace(File.separator, "")
)
if (!dir.exists() && !dir.mkdirs())
throw RuntimeException(context.getString(R.string.mkdir_failed, dir.name))
} else {
val zipInStream = zipFile.getInputStream(finalEntry)
if (Version.isOreo) {
Files.copy(zipInStream, Paths.get(outputDir.absolutePath, finalEntry.name))
} else {
val fileOut = FileOutputStream(File(outputDir, finalEntry.name))
val buffer = ByteArray(8192)
var len: Int
while (zipInStream.read(buffer).also { len = it } != -1)
fileOut.write(buffer, 0, len)
fileOut.close()
}
zipInStream.close()
}
}
zipFile.close()
}

override fun run() {
zipHandler.post { dialog = ProgressAlert.show(context, "") }
try {
if (Version.isKitKat)
decompress()
else
decompressLegacy()
} catch (e: IOException) {
Debug.warn(e)
} finally {
zipHandler.post { dialog?.dismiss() }
archive.delete()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import android.view.View
import android.view.ViewGroup
import android.webkit.*
import android.widget.EditText
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AlertDialog
import androidx.fragment.app.Fragment
import androidx.webkit.*
Expand All @@ -21,6 +22,7 @@ import com.hiddenramblings.tagmo.amiibo.AmiiboManager.getAmiiboManager
import com.hiddenramblings.tagmo.eightbit.io.Debug
import com.hiddenramblings.tagmo.eightbit.os.Storage
import com.hiddenramblings.tagmo.eightbit.os.Version
import com.hiddenramblings.tagmo.eightbit.util.UnZip
import com.hiddenramblings.tagmo.eightbit.widget.ProgressAlert
import com.hiddenramblings.tagmo.nfctech.TagArray
import com.hiddenramblings.tagmo.security.SecurityHandler
Expand All @@ -38,7 +40,6 @@ import java.util.zip.ZipFile
class WebsiteFragment : Fragment() {
private val webHandler = Handler(Looper.getMainLooper())
private var mWebView: WebView? = null
private var dialog: ProgressAlert? = null

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
Expand Down Expand Up @@ -165,58 +166,6 @@ class WebsiteFragment : Fragment() {
}
}

private inner class UnZip(var archive: File, var outputDir: File) : Runnable {
@Throws(IOException::class)
private fun decompress() {
val zipFile = ZipFile(archive)
val entries = zipFile.entries()
while (entries.hasMoreElements()) {
// get the zip entry
val finalEntry = entries.nextElement()
webHandler.post {
dialog?.setMessage(getString(R.string.unzip_item, finalEntry.name))
}
if (finalEntry.isDirectory) {
val dir = File(
outputDir, finalEntry.name.replace(File.separator, "")
)
if (!dir.exists() && !dir.mkdirs())
throw RuntimeException(getString(R.string.mkdir_failed, dir.name))
} else {
val zipInStream = zipFile.getInputStream(finalEntry)
if (Version.isOreo) {
Files.copy(zipInStream, Paths.get(outputDir.absolutePath, finalEntry.name))
} else {
val fileOut = FileOutputStream(
File(outputDir, finalEntry.name)
)
val buffer = ByteArray(8192)
var len: Int
while (zipInStream.read(buffer).also { len = it } != -1) fileOut.write(
buffer,
0,
len
)
fileOut.close()
}
zipInStream.close()
}
}
zipFile.close()
}

override fun run() {
try {
decompress()
} catch (e: IOException) {
Debug.warn(e)
} finally {
dialog?.dismiss()
archive.delete()
}
}
}

private fun saveBinFile(tagData: ByteArray, name: String) {
try {
val filePath = File(
Expand Down Expand Up @@ -295,9 +244,10 @@ class WebsiteFragment : Fragment() {
), 0))
it.flush()
}
webHandler.post { dialog = ProgressAlert.show(requireContext(), "") }
Thread(UnZip(
filePath, Storage.getDownloadDir("TagMo", "Downloads")
requireContext(),
filePath,
Storage.getDownloadDir("TagMo", "Downloads")
)).start()
} else {
resources.getStringArray(R.array.mimetype_bin).find { binType ->
Expand Down
12 changes: 6 additions & 6 deletions app/src/main/res/raw/amiibo.json
Original file line number Diff line number Diff line change
Expand Up @@ -7462,7 +7462,7 @@
}
},
"0x3802000103951702": {
"name": "Daijobu",
"name": "Yabe",
"release": {
"au": null,
"eu": null,
Expand All @@ -7480,7 +7480,7 @@
}
},
"0x3804000103971702": {
"name": "Yabe",
"name": "Ganda",
"release": {
"au": null,
"eu": null,
Expand All @@ -7489,7 +7489,7 @@
}
},
"0x3805000103981702": {
"name": "Ganda",
"name": "Daijobu",
"release": {
"au": null,
"eu": null,
Expand Down Expand Up @@ -8237,10 +8237,10 @@
"0x37c1": "Richter",
"0x3800": "Pawapuro",
"0x3801": "Ikari",
"0x3802": "Daijobu",
"0x3802": "Yabe",
"0x3803": "Hayakawa",
"0x3804": "Yabe",
"0x3805": "Ganda",
"0x3804": "Ganda",
"0x3805": "Daijobu",
"0x3840": "Yuga Ohdo",
"0x3841": "Tatsuhisa \u201cLuke\u201d Kamij\u014d",
"0x3842": "Gakuto S\u014dgetsu",
Expand Down
Loading

0 comments on commit 96f1636

Please sign in to comment.