Files
LLeMbas-Android/app/src/main/java/eu/llembas/android/share/ShareActivity.kt
T
HomerandClaude Opus 5.5 41dd3c893c
CI / check (push) Canceled after 0s
Chats as a daily driver: agent chats, photos, copy, rename, offline and recovery
- New agent chat (Agents tab FAB, route new-agent): GET /devices for the devices linked now; a
  device chip, an optional folder (empty = the device's first root, the server's rule) and the
  mode (manual/edit/auto/plan) go into POST /chats as kind/device/directory/mode. No device
  linked: a sentence saying how. An open agent chat's mode is a chip too.
- Attach: Take a photo (TakePicture into a FileProvider cache file, deleted once uploaded; no
  CAMERA permission), Choose photos (PickMultipleVisualMedia), Choose files.
- Copy under every reply (its text steps) and user message; Rename chat in the menu.
- The new chat's effort is held until the first message and sent with it (EffortChip called
  PATCH, which needs a chat, so the choice was dropped).
- Follow the reply while it streams, decided when the person's own scroll comes to rest; the
  first scroll goes to the real last item (the "older" row shifted the index by one).
- ui/Errors.kt explain(): the server's sentence for a refusal, one "cannot be reached" sentence
  for UnknownHost/Connect/SocketTimeout/NoRouteToHost, used everywhere an error was shown raw.
- A failed send hands its text and attachments back to the composer. A replaced follow's
  CancellationException is no longer shown as an error. A refused stream re-reads the chat.
- The chat list reloads on resume (quietly if rows are shown) and offers Try again.
- Strings: 15 new; Rename chat, Copy message, Copied., Save, Try again, Device and Auto from the
  server's catalogue, the rest translated in strings_own.json. "Plan" was the price plan in
  de/fr/hu (Tarif/Forfait/Csomag) and "Manual" untranslated: own entries fix both.
- ClientTest: an agent chat's body, a plain chat's, and what counts as unreachable.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-09 08:09:53 +00:00

111 lines
4.6 KiB
Kotlin

package eu.llembas.android.share
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.provider.OpenableColumns
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.mutableStateOf
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.core.content.IntentCompat
import androidx.lifecycle.lifecycleScope
import eu.llembas.android.MainActivity
import eu.llembas.android.R
import eu.llembas.android.lembas
import eu.llembas.android.push.Notifications
import eu.llembas.android.ui.explain
import eu.llembas.android.ui.theme.LembasTheme
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
/**
* "Share to LLeMbas": text, a link, a picture or a file from any app starts a new chat with it.
* Files are uploaded first and bound to the first message, as the composer's are.
*/
class ShareActivity : ComponentActivity() {
private val failure = mutableStateOf<String?>(null)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
LembasTheme {
Surface(Modifier.fillMaxSize()) {
Column(
Modifier.fillMaxSize().padding(24.dp),
verticalArrangement = Arrangement.spacedBy(16.dp, Alignment.CenterVertically),
horizontalAlignment = Alignment.CenterHorizontally,
) {
val error = failure.value
if (error == null) {
CircularProgressIndicator()
Text(stringResource(R.string.share_sending))
} else {
Text(error, color = MaterialTheme.colorScheme.error)
TextButton(onClick = { finish() }) { Text(stringResource(R.string.close)) }
}
}
}
}
}
lifecycleScope.launch { share(intent) }
}
private suspend fun share(intent: Intent) {
try {
val api = lembas.instances.client() ?: run {
startActivity(Intent(this, MainActivity::class.java))
finish()
return
}
val text = listOfNotNull(
intent.getStringExtra(Intent.EXTRA_SUBJECT),
intent.getStringExtra(Intent.EXTRA_TEXT),
).joinToString("\n\n").trim()
val uris = when (intent.action) {
Intent.ACTION_SEND -> listOfNotNull(IntentCompat.getParcelableExtra(intent, Intent.EXTRA_STREAM, Uri::class.java))
Intent.ACTION_SEND_MULTIPLE ->
IntentCompat.getParcelableArrayListExtra(intent, Intent.EXTRA_STREAM, Uri::class.java).orEmpty()
else -> emptyList()
}
val files = uris.map { uri ->
val (name, bytes) = withContext(Dispatchers.IO) {
val name = contentResolver.query(uri, arrayOf(OpenableColumns.DISPLAY_NAME), null, null, null)?.use { c ->
if (c.moveToFirst()) c.getString(0) else null
} ?: "shared"
name to (contentResolver.openInputStream(uri)?.use { it.readBytes() } ?: ByteArray(0))
}
api.upload(name, contentResolver.getType(uri) ?: "application/octet-stream", bytes).id
}
if (text.isBlank() && files.isEmpty()) {
finish()
return
}
val made = api.createChat(text.ifBlank { getString(R.string.share_default_prompt) }, files)
startActivity(
Intent(this, MainActivity::class.java)
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP)
.putExtra(Notifications.EXTRA_CHAT, made.chat.id),
)
finish()
} catch (e: Exception) {
failure.value = explain(e)
}
}
}