-
Notifications
You must be signed in to change notification settings - Fork 0
feat(android): widget, tile, and share-sheet capture entry points #31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
ac937a6
9282038
c1cad08
7ac3b72
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,303 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||
| // Copyright (c) 2026 Tyler Stapler | ||||||||||||||||||||||||||||||||||||||||||||||||||
| // SPDX-License-Identifier: Elastic-2.0 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| // https://www.elastic.co/licensing/elastic-license | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| package dev.stapler.stelekit | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| import android.content.ComponentName | ||||||||||||||||||||||||||||||||||||||||||||||||||
| import android.content.Intent | ||||||||||||||||||||||||||||||||||||||||||||||||||
| import android.graphics.drawable.Icon | ||||||||||||||||||||||||||||||||||||||||||||||||||
| import android.os.Build | ||||||||||||||||||||||||||||||||||||||||||||||||||
| import android.os.Bundle | ||||||||||||||||||||||||||||||||||||||||||||||||||
| import androidx.activity.ComponentActivity | ||||||||||||||||||||||||||||||||||||||||||||||||||
| import androidx.activity.compose.BackHandler | ||||||||||||||||||||||||||||||||||||||||||||||||||
| import androidx.activity.compose.setContent | ||||||||||||||||||||||||||||||||||||||||||||||||||
| import androidx.activity.enableEdgeToEdge | ||||||||||||||||||||||||||||||||||||||||||||||||||
| import androidx.activity.viewModels | ||||||||||||||||||||||||||||||||||||||||||||||||||
| import androidx.annotation.RequiresApi | ||||||||||||||||||||||||||||||||||||||||||||||||||
| import androidx.compose.foundation.background | ||||||||||||||||||||||||||||||||||||||||||||||||||
| import androidx.compose.foundation.clickable | ||||||||||||||||||||||||||||||||||||||||||||||||||
| import androidx.compose.foundation.interaction.MutableInteractionSource | ||||||||||||||||||||||||||||||||||||||||||||||||||
| import androidx.compose.foundation.layout.Arrangement | ||||||||||||||||||||||||||||||||||||||||||||||||||
| import androidx.compose.foundation.layout.Box | ||||||||||||||||||||||||||||||||||||||||||||||||||
| import androidx.compose.foundation.layout.Column | ||||||||||||||||||||||||||||||||||||||||||||||||||
| import androidx.compose.foundation.layout.Row | ||||||||||||||||||||||||||||||||||||||||||||||||||
| import androidx.compose.foundation.layout.Spacer | ||||||||||||||||||||||||||||||||||||||||||||||||||
| import androidx.compose.foundation.layout.fillMaxSize | ||||||||||||||||||||||||||||||||||||||||||||||||||
| import androidx.compose.foundation.layout.fillMaxWidth | ||||||||||||||||||||||||||||||||||||||||||||||||||
| import androidx.compose.foundation.layout.height | ||||||||||||||||||||||||||||||||||||||||||||||||||
| import androidx.compose.foundation.layout.imePadding | ||||||||||||||||||||||||||||||||||||||||||||||||||
| import androidx.compose.foundation.layout.navigationBarsPadding | ||||||||||||||||||||||||||||||||||||||||||||||||||
| import androidx.compose.foundation.layout.padding | ||||||||||||||||||||||||||||||||||||||||||||||||||
| import androidx.compose.foundation.layout.size | ||||||||||||||||||||||||||||||||||||||||||||||||||
| import androidx.compose.foundation.layout.width | ||||||||||||||||||||||||||||||||||||||||||||||||||
| import androidx.compose.foundation.layout.wrapContentHeight | ||||||||||||||||||||||||||||||||||||||||||||||||||
| import androidx.compose.foundation.shape.RoundedCornerShape | ||||||||||||||||||||||||||||||||||||||||||||||||||
| import androidx.compose.material3.Button | ||||||||||||||||||||||||||||||||||||||||||||||||||
| import androidx.compose.material3.CircularProgressIndicator | ||||||||||||||||||||||||||||||||||||||||||||||||||
| import androidx.compose.material3.MaterialTheme | ||||||||||||||||||||||||||||||||||||||||||||||||||
| import androidx.compose.material3.OutlinedTextField | ||||||||||||||||||||||||||||||||||||||||||||||||||
| import androidx.compose.material3.SnackbarHost | ||||||||||||||||||||||||||||||||||||||||||||||||||
| import androidx.compose.material3.SnackbarHostState | ||||||||||||||||||||||||||||||||||||||||||||||||||
| import androidx.compose.material3.Surface | ||||||||||||||||||||||||||||||||||||||||||||||||||
| import androidx.compose.material3.Text | ||||||||||||||||||||||||||||||||||||||||||||||||||
| import androidx.compose.material3.TextButton | ||||||||||||||||||||||||||||||||||||||||||||||||||
| import androidx.compose.runtime.Composable | ||||||||||||||||||||||||||||||||||||||||||||||||||
| import androidx.compose.runtime.LaunchedEffect | ||||||||||||||||||||||||||||||||||||||||||||||||||
| import androidx.compose.runtime.collectAsState | ||||||||||||||||||||||||||||||||||||||||||||||||||
| import androidx.compose.runtime.getValue | ||||||||||||||||||||||||||||||||||||||||||||||||||
| import androidx.compose.runtime.remember | ||||||||||||||||||||||||||||||||||||||||||||||||||
| import androidx.compose.ui.Alignment | ||||||||||||||||||||||||||||||||||||||||||||||||||
| import androidx.compose.ui.Modifier | ||||||||||||||||||||||||||||||||||||||||||||||||||
| import androidx.compose.ui.focus.FocusRequester | ||||||||||||||||||||||||||||||||||||||||||||||||||
| import androidx.compose.ui.focus.focusRequester | ||||||||||||||||||||||||||||||||||||||||||||||||||
| import androidx.compose.ui.graphics.Color | ||||||||||||||||||||||||||||||||||||||||||||||||||
| import androidx.compose.ui.unit.dp | ||||||||||||||||||||||||||||||||||||||||||||||||||
| import dev.stapler.stelekit.app.R | ||||||||||||||||||||||||||||||||||||||||||||||||||
| import dev.stapler.stelekit.tile.CaptureTileService | ||||||||||||||||||||||||||||||||||||||||||||||||||
| import dev.stapler.stelekit.ui.NoGraphPlaceholderContent | ||||||||||||||||||||||||||||||||||||||||||||||||||
| import dev.stapler.stelekit.ui.theme.StelekitTheme | ||||||||||||||||||||||||||||||||||||||||||||||||||
| import dev.stapler.stelekit.ui.theme.StelekitThemeMode | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||
| * Lightweight translucent overlay for quick note capture. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| * Launched from the home screen widget, Quick Settings Tile, and Android share sheet. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| * Writes to today's journal page via DatabaseWriteActor + GraphWriter. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||
| class CaptureActivity : ComponentActivity() { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| private val viewModel: CaptureViewModel by viewModels() | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| override fun onCreate(savedInstanceState: Bundle?) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| super.onCreate(savedInstanceState) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| enableEdgeToEdge() | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| val app = application as SteleKitApplication | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| // Task 1.3: parse share intent before setContent (EXTRA_STREAM copy is synchronous) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| if (savedInstanceState == null) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| val shareContent = parseShareIntent(intent) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| if (shareContent.imageLocalPath != null) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| viewModel.initializeText("[image: ${shareContent.imageLocalPath}]\n${shareContent.text}".trim()) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| viewModel.initializeText(shareContent.text) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| setContent { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| StelekitTheme(themeMode = StelekitThemeMode.SYSTEM) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| if (app.graphManager?.getActiveRepositorySet() == null) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| Box(modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.Center) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| NoGraphPlaceholderContent() | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| CaptureScreen( | ||||||||||||||||||||||||||||||||||||||||||||||||||
| viewModel = viewModel, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| onSaved = { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| // Task 2.2: prompt tile add on first save | ||||||||||||||||||||||||||||||||||||||||||||||||||
| if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| promptAddTileOnce() | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| finish() | ||||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| onDismiss = { finish() }, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+87
to
+105
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| // Task 1.3: re-parse share extras when singleTop brings this Activity to front | ||||||||||||||||||||||||||||||||||||||||||||||||||
| override fun onNewIntent(intent: Intent) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| super.onNewIntent(intent) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| setIntent(intent) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| val shareContent = parseShareIntent(intent) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| if (shareContent.imageLocalPath != null) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| viewModel.initializeText("[image: ${shareContent.imageLocalPath}]\n${shareContent.text}".trim()) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| viewModel.initializeText(shareContent.text) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| // Task 1.3: Bug 3 mitigation — read in mandated null-safe order: clipData → EXTRA_TEXT → EXTRA_SUBJECT | ||||||||||||||||||||||||||||||||||||||||||||||||||
| private fun parseShareIntent(intent: Intent): ShareContent { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| if (intent.action != Intent.ACTION_SEND && intent.action != Intent.ACTION_SEND_MULTIPLE) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| return ShareContent("", null) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| val text = intent.clipData?.getItemAt(0)?.coerceToText(this)?.toString() | ||||||||||||||||||||||||||||||||||||||||||||||||||
| ?: intent.getStringExtra(Intent.EXTRA_TEXT) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| ?: intent.getStringExtra(Intent.EXTRA_SUBJECT) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| ?: "" | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| // Bug 2 mitigation: copy EXTRA_STREAM synchronously before any coroutine launch | ||||||||||||||||||||||||||||||||||||||||||||||||||
| val imagePath = if (intent.type?.startsWith("image/") == true) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| @Suppress("DEPRECATION") | ||||||||||||||||||||||||||||||||||||||||||||||||||
| val streamUri = intent.getParcelableExtra<android.net.Uri>(Intent.EXTRA_STREAM) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| streamUri?.let { copyStreamToPrivateStorage(it) } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } else null | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| return ShareContent(text, imagePath) | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+123
to
+139
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| private fun copyStreamToPrivateStorage(uri: android.net.Uri): String? = try { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| val outFile = java.io.File(cacheDir, "share_${System.currentTimeMillis()}.jpg") | ||||||||||||||||||||||||||||||||||||||||||||||||||
| val copied = contentResolver.openInputStream(uri)?.use { input -> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| outFile.outputStream().use { output -> input.copyTo(output) } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| if (copied != null) outFile.absolutePath else null | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } catch (_: SecurityException) { null } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| catch (_: Exception) { null } | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+142
to
+150
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| private fun copyStreamToPrivateStorage(uri: android.net.Uri): String? = try { | |
| val outFile = java.io.File(cacheDir, "share_${System.currentTimeMillis()}.jpg") | |
| contentResolver.openInputStream(uri)?.use { input -> | |
| outFile.outputStream().use { output -> input.copyTo(output) } | |
| } | |
| outFile.absolutePath | |
| } catch (_: SecurityException) { null } | |
| catch (_: Exception) { null } | |
| private fun copyStreamToPrivateStorage(uri: android.net.Uri): String? { | |
| return try { | |
| val outFile = java.io.File(cacheDir, "share_${System.currentTimeMillis()}.jpg") | |
| val bytesCopied = contentResolver.openInputStream(uri)?.use { input -> | |
| outFile.outputStream().use { output -> input.copyTo(output) } | |
| } ?: return null | |
| if (bytesCopied <= 0L) { | |
| outFile.delete() | |
| return null | |
| } | |
| outFile.absolutePath | |
| } catch (_: SecurityException) { null } | |
| catch (_: Exception) { null } | |
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
android:name=".SteleKitApplication"resolves relative to the manifest package/namespace(heredev.stapler.stelekit.app), butSteleKitApplicationis declared in packagedev.stapler.stelekit. This will fail to instantiate the Application (and laterCaptureActivitywill crash when castingapplication as SteleKitApplication). Use the fully-qualified name (dev.stapler.stelekit.SteleKitApplication) or move the class into the manifest namespace.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not a bug — Android resolves
android:namerelative names using theapplicationId, not thenamespace. HereapplicationId = "dev.stapler.stelekit"(set inandroidApp/build.gradle.kts), so.SteleKitApplicationresolves correctly todev.stapler.stelekit.SteleKitApplication.