diff options
Diffstat (limited to 'android/app/src/main/kotlin/org/buetow')
| -rw-r--r-- | android/app/src/main/kotlin/org/buetow/quicklog/MainActivity.kt | 56 | ||||
| -rw-r--r-- | android/app/src/main/kotlin/org/buetow/quicklog/ShareActivity.kt | 54 |
2 files changed, 110 insertions, 0 deletions
diff --git a/android/app/src/main/kotlin/org/buetow/quicklog/MainActivity.kt b/android/app/src/main/kotlin/org/buetow/quicklog/MainActivity.kt new file mode 100644 index 0000000..426589f --- /dev/null +++ b/android/app/src/main/kotlin/org/buetow/quicklog/MainActivity.kt @@ -0,0 +1,56 @@ +package org.buetow.quicklog + +import android.content.Intent +import android.os.Bundle +import io.flutter.embedding.android.FlutterActivity +import io.flutter.embedding.engine.FlutterEngine +import io.flutter.plugin.common.MethodChannel +import java.io.File + +class MainActivity : FlutterActivity() { + private val channelName = "org.buetow.quicklog/share" + private val cacheFilename = "quicklog-shared.txt" + + override fun configureFlutterEngine(flutterEngine: FlutterEngine) { + super.configureFlutterEngine(flutterEngine) + MethodChannel(flutterEngine.dartExecutor.binaryMessenger, channelName) + .setMethodCallHandler { call, result -> + when (call.method) { + "readSharedTextFromCache" -> result.success(readCache()) + "clearSharedTextCache" -> { + clearCache() + result.success(null) + } + else -> result.notImplemented() + } + } + } + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + captureSendIntent(intent) + } + + override fun onNewIntent(intent: Intent) { + super.onNewIntent(intent) + captureSendIntent(intent) + } + + private fun captureSendIntent(intent: Intent?) { + if (intent?.action == Intent.ACTION_SEND && intent.type?.startsWith("text/") == true) { + intent.getStringExtra(Intent.EXTRA_TEXT)?.let { text -> + File(cacheDir, cacheFilename).writeText(text) + } + } + } + + private fun readCache(): String? { + val f = File(cacheDir, cacheFilename) + return if (f.exists()) f.readText() else null + } + + private fun clearCache() { + val f = File(cacheDir, cacheFilename) + if (f.exists()) f.delete() + } +} diff --git a/android/app/src/main/kotlin/org/buetow/quicklog/ShareActivity.kt b/android/app/src/main/kotlin/org/buetow/quicklog/ShareActivity.kt new file mode 100644 index 0000000..15261e6 --- /dev/null +++ b/android/app/src/main/kotlin/org/buetow/quicklog/ShareActivity.kt @@ -0,0 +1,54 @@ +package org.buetow.quicklog + +import android.app.Activity +import android.content.Intent +import android.os.Bundle +import android.util.Log +import java.io.File + +class ShareActivity : Activity() { + companion object { + private const val TAG = "QuicklogShare" + private const val CACHE_FILENAME = "quicklog-shared.txt" + } + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + handleShare(intent) + launchMain() + finish() + } + + private fun handleShare(intent: Intent?) { + if (intent == null) return + val type = intent.type + if (intent.action == Intent.ACTION_SEND && type != null && type.startsWith("text/")) { + val sharedText = intent.getStringExtra(Intent.EXTRA_TEXT) + if (sharedText != null) { + try { + val f = File(cacheDir, CACHE_FILENAME) + f.writeText(sharedText) + Log.i(TAG, "Wrote shared text to ${f.absolutePath}") + } catch (e: Exception) { + Log.e(TAG, "Error writing shared text", e) + } + } + } + } + + private fun launchMain() { + try { + val launch = Intent(this, MainActivity::class.java).apply { + action = Intent.ACTION_MAIN + addCategory(Intent.CATEGORY_LAUNCHER) + flags = Intent.FLAG_ACTIVITY_NEW_TASK or + Intent.FLAG_ACTIVITY_SINGLE_TOP or + Intent.FLAG_ACTIVITY_CLEAR_TOP or + Intent.FLAG_ACTIVITY_REORDER_TO_FRONT + } + startActivity(launch) + } catch (t: Throwable) { + Log.e(TAG, "Failed to launch main activity", t) + } + } +} |
