This commit is contained in:
Renan LE CARO 2025-04-20 09:54:49 +02:00
parent 66952cb1ca
commit 70f3c2307a

View file

@ -21,6 +21,9 @@ import android.webkit.WebChromeClient
import android.webkit.WebView import android.webkit.WebView
import android.widget.Toast import android.widget.Toast
import java.io.File import java.io.File
import java.net.URLDecoder
import java.nio.charset.Charset
import java.nio.charset.StandardCharsets
import java.text.SimpleDateFormat import java.text.SimpleDateFormat
import java.util.Date import java.util.Date
import java.util.jar.Manifest import java.util.jar.Manifest
@ -50,37 +53,27 @@ class MainActivity : android.app.Activity() {
private fun downloadFile(url: String) { private fun downloadFile(url: String) {
try { try {
if (!url.startsWith("data:")) { if (!url.startsWith("data:application/json;charset=utf-8,")) {
Log.w("DL", "url ignored because it does not start with data:") Log.w("DL", "url ignored because it does not start with data:")
return return
} }
val sdf = SimpleDateFormat("yyyy-M-dd-hh-mm") val sdf = SimpleDateFormat("yyyy-M-dd-hh-mm")
val currentDate = sdf.format(Date()) val currentDate = sdf.format(Date())
val base64Data = url.substringAfterLast(',') val urlEncoded = url.substring("data:application/json;charset=utf-8,".length)
val decodedBytes = android.util.Base64.decode(base64Data, android.util.Base64.DEFAULT) val str =URLDecoder.decode(urlEncoded, StandardCharsets.UTF_8.name())
if (url.startsWith("data:application/json;base64,")) { writeFile(str, "breakout-71-save-$currentDate.json", "application/json")
writeFile(decodedBytes, "breakout-71-save-$currentDate.json", "application/json")
} else if (url.startsWith("data:video/webm;base64,")) {
writeFile(decodedBytes, "breakout-71-gameplay-capture-$currentDate.webm", "video/webm")
} else {
Log.w("DL", "unexpected type " + url)
}
} catch (e: Exception) { } catch (e: Exception) {
Log.e("DL", "Error ${e.message}") Log.e("DL", "Error ${e.message}")
Toast.makeText(this, "Error ${e.message}", Toast.LENGTH_LONG).show() Toast.makeText(this, "Error ${e.message}", Toast.LENGTH_LONG).show()
} }
} }
fun writeFile(decodedBytes:ByteArray,fileName:String, mime:String){ fun writeFile(jsonData:String,fileName:String, mime:String){
val jsonData = String(decodedBytes);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
// android 10
val contentValues = ContentValues().apply { val contentValues = ContentValues().apply {
put(MediaStore.Downloads.DISPLAY_NAME, fileName) put(MediaStore.Downloads.DISPLAY_NAME, fileName)
put(MediaStore.Downloads.MIME_TYPE,mime ) put(MediaStore.Downloads.MIME_TYPE,mime )
@ -92,13 +85,12 @@ class MainActivity : android.app.Activity() {
) )
uri?.let { uri?.let {
contentResolver.openOutputStream(it)?.use { outputStream -> contentResolver.openOutputStream(it)?.use { outputStream ->
outputStream.write(decodedBytes) outputStream.write(jsonData.toByteArray())
} }
} }
val shareIntent: Intent = Intent().apply { val shareIntent: Intent = Intent().apply {
action = Intent.ACTION_SEND action = Intent.ACTION_SEND
// Example: content://com.google.android.apps.photos.contentprovider/...
putExtra(Intent.EXTRA_STREAM, uri) putExtra(Intent.EXTRA_STREAM, uri)
type = mime type = mime
} }
@ -155,13 +147,18 @@ class MainActivity : android.app.Activity() {
} catch (e: Exception) { } catch (e: Exception) {
Log.e("DL", "Error ${e.message}") Log.e("DL", "Error ${e.message}")
Toast.makeText(activity, "Error ${e.message}", Toast.LENGTH_LONG).show() Toast.makeText(activity, "Error ${e.message}", Toast.LENGTH_LONG).show()
return false return false
} }
} }
} }
webView.setDownloadListener(DownloadListener { url, userAgent, contentDisposition, mimetype, contentLength -> webView.setDownloadListener(DownloadListener { url, userAgent, contentDisposition, mimetype, contentLength ->
Log.d("DL", "url: ${url}")
Log.d("DL", "userAgent: ${userAgent}")
Log.d("DL", "contentDisposition: ${contentDisposition}")
Log.d("DL", "mimetype: ${mimetype}")
Log.d("DL", "contentLength: ${contentLength}")
downloadFile(url) downloadFile(url)
}) })