mirror of
https://github.com/twonlyapp/twonly-app.git
synced 2026-09-23 12:14:07 +00:00
112 lines
4.4 KiB
Kotlin
112 lines
4.4 KiB
Kotlin
package eu.twonly
|
|
|
|
import io.flutter.embedding.android.FlutterFragmentActivity
|
|
import android.view.KeyEvent
|
|
import dev.darttools.flutter_android_volume_keydown.FlutterAndroidVolumeKeydownPlugin.eventSink
|
|
import android.view.KeyEvent.KEYCODE_VOLUME_DOWN
|
|
import android.view.KeyEvent.KEYCODE_VOLUME_UP
|
|
import io.flutter.embedding.engine.FlutterEngine
|
|
import android.content.Context
|
|
import android.content.Intent
|
|
import io.crates.keyring.Keyring
|
|
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
|
|
import android.os.Bundle
|
|
import android.net.Uri
|
|
import java.io.InputStream
|
|
import androidx.activity.result.contract.ActivityResultContracts
|
|
import androidx.activity.result.ActivityResultLauncher
|
|
import androidx.activity.result.PickVisualMediaRequest
|
|
import io.flutter.plugin.common.MethodChannel
|
|
import eu.twonly.notifications.NotificationTapChannel
|
|
import eu.twonly.widget.WidgetRuntimeChannel
|
|
|
|
class MainActivity : FlutterFragmentActivity() {
|
|
private val CHANNEL = "eu.twonly/photo_picker"
|
|
private var pendingResult: MethodChannel.Result? = null
|
|
|
|
private lateinit var pickMultipleMedia: ActivityResultLauncher<PickVisualMediaRequest>
|
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
installSplashScreen()
|
|
|
|
// Buffer a notification tap before the Flutter engine exists so the
|
|
// cold-start route is not lost.
|
|
NotificationTapChannel.handleIntent(intent)
|
|
|
|
pickMultipleMedia = registerForActivityResult(ActivityResultContracts.PickMultipleVisualMedia()) { uris ->
|
|
if (uris.isNotEmpty()) {
|
|
val uriStrings = uris.map { it.toString() }
|
|
pendingResult?.success(uriStrings)
|
|
} else {
|
|
pendingResult?.success(emptyList<String>())
|
|
}
|
|
pendingResult = null
|
|
}
|
|
|
|
super.onCreate(savedInstanceState)
|
|
}
|
|
|
|
override fun onNewIntent(intent: Intent) {
|
|
super.onNewIntent(intent)
|
|
setIntent(intent)
|
|
NotificationTapChannel.handleIntent(intent)
|
|
}
|
|
|
|
override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
|
|
if (keyCode == KEYCODE_VOLUME_DOWN && eventSink != null) {
|
|
eventSink!!.success(true)
|
|
return true
|
|
}
|
|
if (keyCode == KEYCODE_VOLUME_UP && eventSink != null) {
|
|
eventSink!!.success(false)
|
|
return true
|
|
}
|
|
return super.onKeyDown(keyCode, event)
|
|
}
|
|
|
|
override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
|
|
super.configureFlutterEngine(flutterEngine)
|
|
|
|
Keyring.initializeNdkContext(applicationContext)
|
|
|
|
|
|
NotificationTapChannel.configure(flutterEngine, applicationContext)
|
|
WidgetRuntimeChannel.configure(flutterEngine, applicationContext)
|
|
|
|
MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL).setMethodCallHandler { call, result ->
|
|
when (call.method) {
|
|
"pickImages" -> {
|
|
pendingResult = result
|
|
pickMultipleMedia.launch(PickVisualMediaRequest(ActivityResultContracts.PickVisualMedia.ImageOnly))
|
|
}
|
|
"getUriBytes" -> {
|
|
val uriString = call.argument<String>("uri")
|
|
if (uriString != null) {
|
|
try {
|
|
val uri = Uri.parse(uriString)
|
|
val inputStream: InputStream? = contentResolver.openInputStream(uri)
|
|
if (inputStream != null) {
|
|
val bytes = inputStream.readBytes()
|
|
inputStream.close()
|
|
result.success(bytes)
|
|
} else {
|
|
result.error("UNAVAILABLE", "Could not open InputStream", null)
|
|
}
|
|
} catch (e: Exception) {
|
|
result.error("ERROR", e.message, null)
|
|
}
|
|
} else {
|
|
result.error("INVALID_ARGUMENT", "URI string is null", null)
|
|
}
|
|
}
|
|
else -> result.notImplemented()
|
|
}
|
|
}
|
|
}
|
|
|
|
override fun cleanUpFlutterEngine(flutterEngine: FlutterEngine) {
|
|
NotificationTapChannel.detach()
|
|
WidgetRuntimeChannel.detach()
|
|
super.cleanUpFlutterEngine(flutterEngine)
|
|
}
|
|
}
|