breakout71/app/src/main/java/me/lecaro/breakout/MainActivity.kt

162 lines
5.9 KiB
Kotlin
Raw Normal View History

package me.lecaro.breakout
2025-03-18 14:16:12 +01:00
2025-03-17 11:50:13 +01:00
import android.app.Activity
import android.app.DownloadManager
import android.content.Context
import android.content.Intent
2025-03-18 14:16:12 +01:00
import android.content.pm.PackageManager
2025-03-17 11:50:13 +01:00
import android.net.Uri
import android.os.Bundle
2025-03-17 11:50:13 +01:00
import android.os.Environment
import android.util.Log
import android.view.Window
import android.view.WindowManager
import android.webkit.ConsoleMessage
2025-03-17 11:50:13 +01:00
import android.webkit.DownloadListener
import android.webkit.ValueCallback
import android.webkit.WebChromeClient
import android.webkit.WebView
2025-03-17 11:50:13 +01:00
import android.widget.Toast
import java.io.File
import java.text.SimpleDateFormat
import java.util.Date
2025-03-18 14:16:12 +01:00
import java.util.jar.Manifest
2025-02-20 11:34:11 +01:00
2025-03-17 11:50:13 +01:00
const val CHOOSE_FILE_REQUEST_CODE = 548459
2025-03-18 14:16:12 +01:00
const val PERM_REQUEST_CODE = 66622635
class MainActivity : android.app.Activity() {
2025-03-17 11:50:13 +01:00
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
when (requestCode) {
CHOOSE_FILE_REQUEST_CODE -> {
if (resultCode == RESULT_OK) {
2025-03-18 14:16:12 +01:00
filePathCallback?.onReceiveValue(
WebChromeClient.FileChooserParams.parseResult(
resultCode,
data
)
)
2025-03-17 11:50:13 +01:00
filePathCallback = null
}
}
}
}
2025-03-18 14:16:12 +01:00
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
when (requestCode) {
PERM_REQUEST_CODE -> {
if ((grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED)) {
downloadFile()
} else {
Toast.makeText(this, "We cant make a save file without that permission", Toast.LENGTH_SHORT).show()
}
return
}
}
}
2025-03-17 11:50:13 +01:00
var filePathCallback: ValueCallback<Array<Uri>>? = null
2025-03-18 14:16:12 +01:00
var fileToDownload:String? = null
fun downloadFile(){
val url = fileToDownload ?: return
try{
if (!url.startsWith("data:")) {
Log.w("DL", "url ignored because it does not start with data:")
return
}
val sdf = SimpleDateFormat("yyyy-M-dd-hh-mm")
val currentDate = sdf.format(Date())
// Extract filename from contentDisposition if available
if (url.startsWith("data:application/json;base64,")) {
Log.d("DL", "saving application/json ")
val base64Data = url.substringAfterLast(',')
val decodedBytes =
android.util.Base64.decode(base64Data, android.util.Base64.DEFAULT)
val jsonData = String(decodedBytes);
val dir =
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
val fileName = "breakout-71-save-$currentDate.b71"
val file = File(dir, fileName)
file.writeText(jsonData)
Toast.makeText(this, "Saved in $dir", Toast.LENGTH_LONG).show()
Log.d("DL", "finished saving application/json ")
} else if (url.startsWith("data:video/webm;base64,")) {
Log.d("DL", "saving video/webm ")
// TODO
Log.d("DL", "finished savign video/webm ")
} else {
Log.w("DL", "unexpected type " + url)
}
}catch (e:Exception){
Log.e("DL", "Error ${e.message}")
Toast.makeText(this, "Error ${e.message}", Toast.LENGTH_LONG).show()
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
requestWindowFeature(Window.FEATURE_NO_TITLE);
window.setFlags(
2025-03-15 10:35:50 +01:00
WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN
);
val webView = WebView(this)
webView.settings.javaScriptEnabled = true
webView.settings.domStorageEnabled = true
2025-03-17 11:50:13 +01:00
webView.settings.setSupportZoom(false)
2025-03-18 14:16:12 +01:00
webView.loadUrl("file:///android_asset/index.html?isInWebView=true")
val activity=this;
2025-03-17 11:50:13 +01:00
webView.webChromeClient = object : WebChromeClient() {
override fun onConsoleMessage(consoleMessage: ConsoleMessage): Boolean {
Log.d(
"WebView", "${consoleMessage.message()} -- From line " +
"${consoleMessage.lineNumber()} of ${consoleMessage.sourceId()}"
)
return true
}
2025-03-17 11:50:13 +01:00
2025-03-18 14:16:12 +01:00
override fun onShowFileChooser(
webView: WebView?,
filePathCallback: ValueCallback<Array<Uri>>?,
fileChooserParams: FileChooserParams?
): Boolean {
try{
startActivityForResult(fileChooserParams?.createIntent(), CHOOSE_FILE_REQUEST_CODE)
this@MainActivity.filePathCallback = filePathCallback
return true
}catch (e:Exception){
Log.e("DL", "Error ${e.message}")
Toast.makeText(activity, "Error ${e.message}", Toast.LENGTH_LONG).show()
return false
}
2025-03-17 11:50:13 +01:00
}
2025-03-18 14:16:12 +01:00
}
2025-03-17 11:50:13 +01:00
2025-03-18 14:16:12 +01:00
webView.setDownloadListener(DownloadListener { url, userAgent, contentDisposition, mimetype, contentLength ->
2025-03-17 11:50:13 +01:00
2025-03-18 14:16:12 +01:00
fileToDownload = url
if (activity.checkSelfPermission( android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
activity.requestPermissions(arrayOf( android.Manifest.permission.WRITE_EXTERNAL_STORAGE), PERM_REQUEST_CODE)
2025-03-17 11:50:13 +01:00
}else{
2025-03-18 14:16:12 +01:00
downloadFile()
2025-03-17 11:50:13 +01:00
}
})
2025-02-20 11:34:11 +01:00
setContentView(webView)
}
2025-02-20 11:34:11 +01:00
}