feat: save/share in the picture viewer, and a tap hides its chrome

- the viewer's top bar gains Save to Photos and Share on Android: saving
  writes through MediaStore into Pictures/Chamber Go (no permission on
  Android 10+, a runtime request on older versions), sharing hands a cache
  file to the system chooser through a FileProvider; both are a small
  native channel, not another plugin
- dsh-Go keeps its title bar always visible; here a single tap hides the
  title, keys and page indicator and the next tap brings them back, so they
  do not cover the picture. Double-tap still zooms, swiping still pages at
  1x, dragging pans while zoomed
- version 0.6.6+606; the viewer test now covers the platform calls, chrome
  toggle, zoom, pan and paging
This commit is contained in:
blumia committed 2026-12-01 11:53:54 +08:00
1 parent c2dabbe187
commit fedc4dd79c
10 files changed
+471 -45

No files matched your search

+14
View File
@@ -2,6 +2,10 @@
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.CAMERA"/>
<!-- Saving to the gallery; Android 10+ uses MediaStore without this. -->
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="28"/>
<!-- The camera plugin declares audio for video; the scanner only reads
frames, so the app must not end up asking for the microphone. -->
<uses-permission android:name="android.permission.RECORD_AUDIO" tools:node="remove"/>
@@ -33,6 +37,16 @@
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<!-- Pictures the image viewer shares out of the app's cache. -->
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths"/>
</provider>
<!-- Don't delete the meta-data below.
This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
<meta-data
@@ -1,10 +1,32 @@
package net.blumia.openchamber.flutter
import android.Manifest
import android.content.ClipData
import android.content.ContentValues
import android.content.Intent
import android.content.pm.PackageManager
import android.graphics.BitmapFactory
import android.media.MediaScannerConnection
import android.net.Uri
import android.os.Build
import android.os.Environment
import android.provider.MediaStore
import androidx.core.content.FileProvider
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 data class PendingSave(
val bytes: ByteArray,
val name: String,
val mime: String,
val result: MethodChannel.Result,
)
private var pendingSave: PendingSave? = null
override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
super.configureFlutterEngine(flutterEngine)
// The app's private files directory, for imported pet packs. A small
@@ -26,5 +48,160 @@ class MainActivity : FlutterActivity() {
else -> result.notImplemented()
}
}
// Saving a conversation picture to the gallery and sharing it: both
// need Android APIs (MediaStore, FileProvider) that a Dart package
// would pull in another native dependency for.
MethodChannel(flutterEngine.dartExecutor.binaryMessenger, "openchamber/media")
.setMethodCallHandler { call, result ->
when (call.method) {
"saveImage" -> {
val bytes = call.argument<ByteArray>("bytes")
val name = call.argument<String>("name")
val mime = call.argument<String>("mime") ?: "image/png"
if (bytes == null || bytes.isEmpty() || name.isNullOrBlank()) {
result.error("bad_args", "Missing image data", null)
return@setMethodCallHandler
}
saveImage(bytes, name, mime, result)
}
"shareImage" -> {
val bytes = call.argument<ByteArray>("bytes")
val name = call.argument<String>("name")
val mime = call.argument<String>("mime") ?: "image/png"
if (bytes == null || bytes.isEmpty() || name.isNullOrBlank()) {
result.error("bad_args", "Missing image data", null)
return@setMethodCallHandler
}
shareImage(bytes, name, mime, result)
}
else -> result.notImplemented()
}
}
}
private fun saveImage(
bytes: ByteArray,
name: String,
mime: String,
result: MethodChannel.Result,
) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q &&
checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) !=
PackageManager.PERMISSION_GRANTED
) {
if (pendingSave != null) {
result.error("busy", "Another picture is being saved", null)
return
}
pendingSave = PendingSave(bytes, name, mime, result)
requestPermissions(
arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE),
REQUEST_SAVE,
)
return
}
writeImage(bytes, name, mime, result)
}
private fun writeImage(
bytes: ByteArray,
name: String,
mime: String,
result: MethodChannel.Result,
) {
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
val values = ContentValues().apply {
put(MediaStore.Images.Media.DISPLAY_NAME, name)
put(MediaStore.Images.Media.MIME_TYPE, mime)
put(
MediaStore.Images.Media.RELATIVE_PATH,
"${Environment.DIRECTORY_PICTURES}/Chamber Go",
)
put(MediaStore.Images.Media.IS_PENDING, 1)
}
val collection = MediaStore.Images.Media.getContentUri(
MediaStore.VOLUME_EXTERNAL_PRIMARY,
)
val uri = contentResolver.insert(collection, values)
?: throw IllegalStateException("Could not create the picture")
contentResolver.openOutputStream(uri)?.use { it.write(bytes) }
?: throw IllegalStateException("Could not open the picture")
values.clear()
values.put(MediaStore.Images.Media.IS_PENDING, 0)
contentResolver.update(uri, values, null, null)
} else {
@Suppress("DEPRECATION")
val bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.size)
?: throw IllegalStateException("Could not decode the picture")
@Suppress("DEPRECATION")
val uri = MediaStore.Images.Media.insertImage(
contentResolver, bitmap, name, null,
) ?: throw IllegalStateException("Could not save the picture")
bitmap.recycle()
@Suppress("DEPRECATION")
MediaScannerConnection.scanFile(
this,
arrayOf(Uri.parse(uri).path ?: ""),
arrayOf(mime),
null,
)
}
result.success(true)
} catch (e: Exception) {
result.error("save_failed", e.message ?: "Could not save the picture", null)
}
}
private fun shareImage(
bytes: ByteArray,
name: String,
mime: String,
result: MethodChannel.Result,
) {
try {
val directory = File(cacheDir, "shared_images").apply { mkdirs() }
val file = File(directory, name)
file.writeBytes(bytes)
val uri = FileProvider.getUriForFile(
this, "$packageName.fileprovider", file,
)
val intent = Intent(Intent.ACTION_SEND).apply {
type = mime
putExtra(Intent.EXTRA_STREAM, uri)
clipData = ClipData.newRawUri("image", uri)
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
}
startActivity(Intent.createChooser(intent, "Share image"))
result.success(true)
} catch (e: Exception) {
result.error("share_failed", e.message ?: "Could not share the picture", null)
}
}
override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<out String>,
grantResults: IntArray,
) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
if (requestCode != REQUEST_SAVE) return
val pending = pendingSave ?: return
pendingSave = null
if (grantResults.isNotEmpty() &&
grantResults[0] == PackageManager.PERMISSION_GRANTED
) {
writeImage(pending.bytes, pending.name, pending.mime, pending.result)
} else {
pending.result.error(
"permission",
"Storage permission is required to save the picture",
null,
)
}
}
private companion object {
const val REQUEST_SAVE = 701
}
}
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<paths>
<!-- Pictures the viewer writes before sharing them. -->
<cache-path name="shared_images" path="shared_images/" />
</paths>