Compress Android diary photo attachments

This commit is contained in:
waozi committed 2026-10-16 02:42:58 -03:00
1 parent e3da2c0102
commit 13175486f4
10 files changed
+521 -10

No files matched your search

+4
View File
@@ -3097,6 +3097,10 @@ test: app-preferences-sync-test
cells-diary-test: $(TARGET)
sh tests/cells_diary_test.sh $(abspath $(TARGET))
.PHONY: android-diary-photo-test
android-diary-photo-test:
sh tests/android_diary_photo_test.sh
.PHONY: package-version-test
package-version-test:
python3 scripts/check-package-versions.py
@@ -0,0 +1,200 @@
package xyz.waozi.inbe;
import android.content.ContentResolver;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.ImageDecoder;
import android.graphics.Matrix;
import android.media.ExifInterface;
import android.net.Uri;
import android.os.Build;
import android.provider.MediaStore;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
/** Android's image codecs produce a small, metadata-free private picker copy. */
final class DiaryPhotoImport {
static final int KIND = 4;
static final int MAX_EDGE = 1600;
static final int MAX_BYTES = 512 * 1024;
static final long MAX_SOURCE_BYTES = 64L * 1024 * 1024;
static Intent pickerIntent() {
Intent intent;
if (Build.VERSION.SDK_INT >= 33) {
intent = new Intent(MediaStore.ACTION_PICK_IMAGES);
} else {
intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
}
intent.setType("image/*");
return intent;
}
static File copy(ContentResolver resolver, Uri uri, File directory) throws IOException {
if (!directory.isDirectory() && !directory.mkdirs()) {
throw new IOException("Cannot create photo import directory");
}
File source = File.createTempFile("diary-source-", ".tmp", directory);
File result = null;
boolean saved = false;
try {
try (InputStream input = resolver.openInputStream(uri);
FileOutputStream output = new FileOutputStream(source)) {
if (input == null) {
throw new IOException("Cannot read selected photo");
}
copySource(input, output);
}
result = File.createTempFile("diary-", ".bin", directory);
compress(source, result);
saved = true;
return result;
} finally {
source.delete();
if (!saved && result != null) {
result.delete();
}
}
}
static void copySource(InputStream input, FileOutputStream output) throws IOException {
byte[] buffer = new byte[8192];
long total = 0;
int count;
while ((count = input.read(buffer)) != -1) {
total += count;
if (total > MAX_SOURCE_BYTES) {
throw new IOException("Selected photo exceeds import limit");
}
output.write(buffer, 0, count);
}
}
static void compress(File source, File result) throws IOException {
Bitmap bitmap = decode(source);
try {
Bitmap.CompressFormat format = hasTransparency(bitmap)
? Bitmap.CompressFormat.PNG : Bitmap.CompressFormat.JPEG;
while (true) {
// Use formats understood by the existing native image host.
// PNG preserves alpha; re-encoding drops EXIF/GPS metadata.
try (FileOutputStream output = new FileOutputStream(result)) {
if (!bitmap.compress(format, 78, output)) {
throw new IOException("Cannot compress selected photo");
}
}
if (result.length() > 0 && result.length() <= MAX_BYTES) {
return;
}
int edge = Math.max(bitmap.getWidth(), bitmap.getHeight());
if (edge <= 320) {
throw new IOException("Cannot fit selected photo in storage limit");
}
Bitmap smaller = scale(bitmap, Math.max(320, edge * 3 / 4));
bitmap.recycle();
bitmap = smaller;
}
} finally {
bitmap.recycle();
}
}
private static boolean hasTransparency(Bitmap bitmap) {
if (!bitmap.hasAlpha()) {
return false;
}
int[] row = new int[bitmap.getWidth()];
for (int y = 0; y < bitmap.getHeight(); y++) {
bitmap.getPixels(row, 0, row.length, 0, y, row.length, 1);
for (int pixel : row) {
if ((pixel >>> 24) != 255) {
return true;
}
}
}
return false;
}
private static Bitmap decode(File source) throws IOException {
if (Build.VERSION.SDK_INT >= 28) {
return ImageDecoder.decodeBitmap(ImageDecoder.createSource(source), (decoder, info, input) -> {
int width = info.getSize().getWidth();
int height = info.getSize().getHeight();
int edge = Math.max(width, height);
double ratio = Math.min(1.0, (double) MAX_EDGE / edge);
decoder.setTargetSize(Math.max(1, (int) (width * ratio)),
Math.max(1, (int) (height * ratio)));
decoder.setAllocator(ImageDecoder.ALLOCATOR_SOFTWARE);
});
}
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(source.getAbsolutePath(), options);
if (options.outWidth <= 0 || options.outHeight <= 0) {
throw new IOException("Invalid selected photo");
}
int edge = Math.max(options.outWidth, options.outHeight);
options.inSampleSize = 1;
while (edge / options.inSampleSize > MAX_EDGE) {
options.inSampleSize *= 2;
}
options.inJustDecodeBounds = false;
Bitmap bitmap = BitmapFactory.decodeFile(source.getAbsolutePath(), options);
if (bitmap == null) {
throw new IOException("Cannot decode selected photo");
}
try {
// On older Android releases the bitmap decoder ignores JPEG EXIF.
int orientation = new ExifInterface(source.getAbsolutePath()).getAttributeInt(
ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
Matrix matrix = new Matrix();
switch (orientation) {
case 2:
matrix.setScale(-1, 1);
break;
case 3:
matrix.setRotate(180);
break;
case 4:
matrix.setScale(1, -1);
break;
case 5:
matrix.setRotate(90);
matrix.postScale(-1, 1);
break;
case 6:
matrix.setRotate(90);
break;
case 7:
matrix.setRotate(270);
matrix.postScale(-1, 1);
break;
case 8:
matrix.setRotate(270);
break;
default:
return bitmap;
}
Bitmap oriented = Bitmap.createBitmap(bitmap, 0, 0,
bitmap.getWidth(), bitmap.getHeight(), matrix, true);
if (oriented != bitmap) {
bitmap.recycle();
}
return oriented;
} catch (IOException ignored) {
// EXIF is optional (for example PNG on older releases).
return bitmap;
}
}
private static Bitmap scale(Bitmap bitmap, int edge) {
double ratio = (double) edge / Math.max(bitmap.getWidth(), bitmap.getHeight());
return Bitmap.createScaledBitmap(bitmap,
Math.max(1, (int) (bitmap.getWidth() * ratio)),
Math.max(1, (int) (bitmap.getHeight() * ratio)), true);
}
}
@@ -640,8 +640,12 @@ public class MainActivity extends NativeActivity {
@Override
public void run() {
try {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
Intent intent = kind == DiaryPhotoImport.KIND
? DiaryPhotoImport.pickerIntent()
: new Intent(Intent.ACTION_OPEN_DOCUMENT);
if (Intent.ACTION_OPEN_DOCUMENT.equals(intent.getAction())) {
intent.addCategory(Intent.CATEGORY_OPENABLE);
}
String[] mimeTypes = parseMimeTypes(mimeTypesCsv);
intent.setType(primaryMimeType(mimeTypes));
if (mimeTypes.length > 0) {
@@ -649,7 +653,18 @@ public class MainActivity extends NativeActivity {
}
pendingImportKind = kind;
Log.d(TAG, "Opening import picker - kind=" + kind + ", mimeTypes=" + mimeTypesCsv + ", parsed=" + Arrays.toString(mimeTypes));
startActivityForResult(intent, REQUEST_IMPORT_ZIP);
try {
startActivityForResult(intent, REQUEST_IMPORT_ZIP);
} catch (android.content.ActivityNotFoundException unavailable) {
if (kind != DiaryPhotoImport.KIND
|| Intent.ACTION_OPEN_DOCUMENT.equals(intent.getAction())) {
throw unavailable;
}
Intent fallback = new Intent(Intent.ACTION_OPEN_DOCUMENT);
fallback.addCategory(Intent.CATEGORY_OPENABLE);
fallback.setType("image/*");
startActivityForResult(fallback, REQUEST_IMPORT_ZIP);
}
} catch (Exception e) {
Log.e(TAG, "Failed to open import picker", e);
nativeImportCancelled(kind);
@@ -868,6 +883,19 @@ public class MainActivity extends NativeActivity {
Log.d(TAG, "File selected for import - kind=" + pendingImportKind + ", uri=" + uri);
File importDir = new File(getCacheDir(), "imports");
if (pendingImportKind == DiaryPhotoImport.KIND) {
// Decoding and compression must not block input or lifecycle events.
new Thread(() -> {
try {
File photo = DiaryPhotoImport.copy(getContentResolver(), uri, importDir);
nativeImportSelectedFile(DiaryPhotoImport.KIND, photo.getAbsolutePath());
} catch (Exception | OutOfMemoryError error) {
Log.e(TAG, "Failed to prepare diary photo", error);
nativeImportSelectedFile(DiaryPhotoImport.KIND, "");
}
}, "diary-photo-import").start();
return;
}
String extension = extensionForUri(uri);
String displayName = sanitizeFileName(displayNameForUri(uri));
String importName;
@@ -907,9 +935,6 @@ public class MainActivity extends NativeActivity {
long totalBytes = 0;
while ((read = input.read(buffer)) != -1) {
totalBytes += read;
if (pendingImportKind == 4 && totalBytes > 16L * 1024 * 1024) {
throw new java.io.IOException("Diary photo exceeds 16 MiB");
}
output.write(buffer, 0, read);
}
Log.d(TAG, "File copied successfully - kind=" + pendingImportKind + ", path=" + importFile.getAbsolutePath() + ", size=" + totalBytes + " bytes");
+1
View File
@@ -446,6 +446,7 @@ DiaryStoreAttach :: (source: string) -> string {
return ""
}
if !DiaryRecordSave(name, data) {
unused RemoveFile(path)
PackageFree(name)
return ""
}
+14 -2
View File
@@ -13,6 +13,7 @@
#import, file "../cells/diary_history.zi";
#import, file "../cells/diary_spelling.zi";
#import "file_picker"
#import "file_linux"
#import, file "../platform/environment.zi";
#import, file "../platform/android/android_import.zi";
#import "android_picker"
@@ -40,6 +41,7 @@
#import, file "../cells/package_files.zi";
#import, file "../storage/sync_node_storage.zi";
#import, file "../storage/storage_core.zi";
#import, file "../storage/state.zi";
using AndroidImportKind;
using ImageFit;
@@ -61,6 +63,7 @@ diary_photo_waiting: bool;
diary_photo_year: s32;
diary_photo_month: s32;
diary_photo_day: s32;
diary_photo_owner: string;
diary_photo_error: bool;
// The host words a refused photo itself; the Diary app shows its photo error.
diary_photo_no_room: [320]u8;
@@ -158,9 +161,10 @@ app_diary_reset :: () {
DiaryAcceptPhoto :: (path: string) {
if path.count == 0 || diary_state.load_error || !diary_loaded ||
diary_photo_owner != AppTextFromCString(storage_user_id_buffer(), 64) ||
diary_state.year != diary_photo_year || diary_state.month != diary_photo_month ||
diary_state.day != diary_photo_day {
diary_photo_error = path.count > 0
diary_photo_error = true
return
}
filename := DiaryStoreAttach(path)
@@ -200,7 +204,13 @@ DiaryPhotoPicker :: (app: *InnerBreeze) {
diary_photo_waiting = false
app.file_dialog_active = 0
if result == .AndroidPickerSelected {
DiaryAcceptPhoto(AppTextFromCString(*path[0], cast(s32)path.count))
selected := AppTextFromCString(*path[0], cast(s32)path.count)
DiaryAcceptPhoto(selected)
// This is the Android picker's disposable compressed copy, never
// the original photo or the account's saved attachment.
if selected.count > 0 {
unused RemoveFile(selected)
}
}
}
if !diary_photo_requested { return }
@@ -210,6 +220,8 @@ DiaryPhotoPicker :: (app: *InnerBreeze) {
diary_photo_year = diary_state.year
diary_photo_month = diary_state.month
diary_photo_day = diary_state.day
PackageFree(diary_photo_owner)
diary_photo_owner = PackageText(AppTextFromCString(storage_user_id_buffer(), 64), "")
if !app_diary_save() { diary_photo_error = true; return }
app.file_dialog_active = 1
if AndroidPlatform() != 0 {
@@ -0,0 +1,156 @@
package xyz.waozi.inbe;
import static org.junit.Assert.*;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.media.ExifInterface;
import android.net.Uri;
import android.provider.MediaStore;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.util.Random;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
import org.robolectric.annotation.GraphicsMode;
@RunWith(RobolectricTestRunner.class)
@Config(sdk = 34, manifest = Config.NONE)
@GraphicsMode(GraphicsMode.Mode.NATIVE)
public class DiaryPhotoImportTest {
@Rule public TemporaryFolder temporary = new TemporaryFolder();
private File image(int width, int height, boolean noise, Bitmap.CompressFormat format) throws IOException {
File file = temporary.newFile();
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
bitmap.eraseColor(Color.argb(128, 20, 100, 180));
if (noise) {
Random random = new Random(42);
int[] row = new int[width];
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
row[x] = random.nextInt() | 0xff000000;
}
bitmap.setPixels(row, 0, width, 0, y, width, 1);
}
}
try (FileOutputStream output = new FileOutputStream(file)) {
assertTrue(bitmap.compress(format, 95, output));
} finally {
bitmap.recycle();
}
return file;
}
private File copy(File source, File directory) throws IOException {
return DiaryPhotoImport.copy(RuntimeEnvironment.getApplication().getContentResolver(),
Uri.fromFile(source), directory);
}
@Test public void android14UsesPermissionFreeSingleImagePicker() {
Intent intent = DiaryPhotoImport.pickerIntent();
assertEquals(MediaStore.ACTION_PICK_IMAGES, intent.getAction());
assertEquals("image/*", intent.getType());
assertFalse(intent.getBooleanExtra(Intent.EXTRA_ALLOW_MULTIPLE, false));
}
@Test @Config(sdk = 28) public void olderAndroidUsesOpenDocument() {
Intent intent = DiaryPhotoImport.pickerIntent();
assertEquals(Intent.ACTION_OPEN_DOCUMENT, intent.getAction());
assertTrue(intent.hasCategory(Intent.CATEGORY_OPENABLE));
}
@Test public void largeNoisyImageFitsBothLimitsAndOriginalStaysUntouched() throws Exception {
File source = image(3200, 2400, true, Bitmap.CompressFormat.PNG);
byte[] original = Files.readAllBytes(source.toPath());
File directory = temporary.newFolder();
File result = copy(source, directory);
assertTrue(result.length() > 0);
assertTrue(result.length() <= DiaryPhotoImport.MAX_BYTES);
assertTrue(result.length() < source.length() / 10);
byte[] encoded = Files.readAllBytes(result.toPath());
assertEquals(255, encoded[0] & 255);
assertEquals(216, encoded[1] & 255);
Bitmap bitmap = BitmapFactory.decodeFile(result.getAbsolutePath());
assertNotNull(bitmap);
assertTrue(bitmap.getWidth() <= DiaryPhotoImport.MAX_EDGE);
assertEquals(4.0 / 3.0, (double) bitmap.getWidth() / bitmap.getHeight(), 0.01);
bitmap.recycle();
assertArrayEquals(original, Files.readAllBytes(source.toPath()));
assertArrayEquals(new String[]{result.getName()}, directory.list());
}
@Test @Config(sdk = {27, 34})
public void smallTransparentImageKeepsSizeAndAlphaAndAllowsAnotherAttachment() throws Exception {
File source = image(120, 80, false, Bitmap.CompressFormat.PNG);
File directory = temporary.newFolder();
File first = copy(source, directory);
File second = copy(source, directory);
assertNotEquals(first, second);
byte[] encoded = Files.readAllBytes(first.toPath());
assertEquals(137, encoded[0] & 255);
assertEquals('P', encoded[1]);
Bitmap bitmap = BitmapFactory.decodeFile(first.getAbsolutePath());
assertEquals(120, bitmap.getWidth());
assertEquals(80, bitmap.getHeight());
assertEquals(128, Color.alpha(bitmap.getPixel(40, 40)), 1);
bitmap.recycle();
assertEquals(2, directory.list().length);
}
@Test @Config(sdk = {27, 34})
public void rotatedJpegIsUprightAndDropsLocationMetadata() throws Exception {
File source = image(240, 120, false, Bitmap.CompressFormat.JPEG);
ExifInterface exif = new ExifInterface(source.getAbsolutePath());
exif.setAttribute(ExifInterface.TAG_ORIENTATION, "6");
exif.setAttribute(ExifInterface.TAG_GPS_LATITUDE, "25/1,18/1,0/1");
exif.setAttribute(ExifInterface.TAG_GPS_LATITUDE_REF, "S");
exif.saveAttributes();
File result = copy(source, temporary.newFolder());
Bitmap bitmap = BitmapFactory.decodeFile(result.getAbsolutePath());
assertEquals(120, bitmap.getWidth());
assertEquals(240, bitmap.getHeight());
bitmap.recycle();
ExifInterface outputExif = new ExifInterface(result.getAbsolutePath());
assertNull(outputExif.getAttribute(ExifInterface.TAG_GPS_LATITUDE));
}
@Test public void invalidPhotoLeavesNoCacheFiles() throws Exception {
File source = temporary.newFile();
Files.write(source.toPath(), new byte[]{1, 2, 3, 4});
File directory = temporary.newFolder();
try {
copy(source, directory);
fail("Invalid image was accepted");
} catch (IOException expected) {
assertEquals(0, directory.list().length);
assertTrue(source.isFile());
}
}
@Test public void sourceCopyStopsAtLimit() throws Exception {
File destination = temporary.newFile();
InputStream oversized = new InputStream() {
@Override public int read() { return 0; }
@Override public int read(byte[] bytes, int offset, int length) { return length; }
};
try (FileOutputStream output = new FileOutputStream(destination)) {
try {
DiaryPhotoImport.copySource(oversized, output);
fail("Oversized input was accepted");
} catch (IOException expected) {
assertEquals(DiaryPhotoImport.MAX_SOURCE_BYTES, destination.length());
}
}
}
}
+49
View File
@@ -0,0 +1,49 @@
plugins {
id 'java'
}
repositories {
google()
mavenCentral()
}
layout.buildDirectory = file('../../build/android-diary-photo-test')
def sdk = System.getenv('ANDROID_SDK_ROOT') ?: '/home/wao/Android/Sdk'
sourceSets {
main.java {
srcDir '../../droid/app/src/main/java'
include '**/DiaryPhotoImport.java'
}
test.java.srcDir '.'
}
dependencies {
compileOnly files("${sdk}/platforms/android-36/android.jar")
testImplementation files("${sdk}/platforms/android-36/android.jar")
testImplementation 'junit:junit:4.13.2'
testImplementation 'org.robolectric:robolectric:4.16'
}
def aarDirectory = layout.buildDirectory.dir('aar-classes')
tasks.register('extractAndroidTestAars') {
inputs.files(configurations.testRuntimeClasspath)
outputs.dir(aarDirectory)
doLast {
configurations.testRuntimeClasspath.files.findAll { it.name.endsWith('.aar') }.each { aar ->
copy {
from zipTree(aar)
include 'classes.jar'
into aarDirectory
rename 'classes.jar', "${aar.name}.jar"
}
}
}
}
test {
dependsOn 'extractAndroidTestAars'
classpath += files(aarDirectory.map { fileTree(it) { include '*.jar' } })
systemProperty 'robolectric.dependency.repo.url', 'https://repo.maven.apache.org/maven2'
testLogging {
events 'passed', 'failed', 'skipped'
exceptionFormat 'full'
}
}
@@ -0,0 +1 @@
rootProject.name = 'inbe-diary-photo-test'
+5
View File
@@ -0,0 +1,5 @@
#!/bin/sh
set -eu
root=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd)
exec env -u DISPLAY -u WAYLAND_DISPLAY -u XAUTHORITY -u DBUS_SESSION_BUS_ADDRESS \
"$root/droid/gradlew" -p "$root/tests/android-diary-photo" --no-daemon test
+60 -2
View File
@@ -3,6 +3,7 @@ import contextlib
import json
import os
from pathlib import Path
import sqlite3
import subprocess
import sys
import tempfile
@@ -48,7 +49,7 @@ def wait_for_text(entry, matches, message):
@contextlib.contextmanager
def application(profile, label, import_source=None):
def application(profile, label, import_source=None, size=(900, 720)):
with (OUTPUT / f"{label}.log").open("w") as log:
environment = ENV | {"APP_DATA_ROOT": str(profile)}
if import_source is not None:
@@ -68,9 +69,11 @@ def application(profile, label, import_source=None):
time.sleep(0.1)
else:
raise AssertionError("Standalone Diary window did not appear")
command("xdotool", "windowsize", window, "900", "720")
command("xdotool", "windowsize", window, str(size[0]), str(size[1]))
command("xdotool", "windowfocus", window)
time.sleep(1.5)
# Fresh profiles can show the first-run Lumi guide over the editor.
tap(window, size[0] // 2 + 112, 114)
yield window
assert app.poll() is None, "Diary crashed"
finally:
@@ -150,5 +153,60 @@ with tempfile.TemporaryDirectory(prefix="inbe-diary-ui-") as temporary:
(source / entry.name).write_text(json.dumps(dict(original, text="Changed source")))
with application(migrated, "migration-restart", source):
assert json.loads(target.read_text()) == original, "import overwrote an existing Inbe entry"
# Render the same stored attachments at phone width and the reporter's
# scale in both sage modes, including an unavailable photo. The fixture
# contains three references; navigation and removal must preserve text.
blue_path = source / "photo-blue.bin"
Image.new("RGB", (100, 60), (24, 90, 220)).save(blue_path, format="JPEG")
(source / entry.name).write_text(json.dumps(dict(original, photos=[
*original["photos"], dict(file=blue_path.name, name="Blue photo"),
dict(file="photo-missing.bin", name="Missing photo")])))
for dark in (0, 1):
phone = profile / f"phone-{dark}"
phone_source = profile / f"phone-source-{dark}"
phone_source.mkdir()
for name in (entry.name, "photo-local.bin", blue_path.name):
(phone_source / name).write_bytes((source / name).read_bytes())
# Initialize an isolated profile before setting its appearance.
with application(phone, f"phone-init-{dark}", phone_source):
pass
with sqlite3.connect(phone / "inbe.db") as database:
owner = database.execute("select user_id from settings limit 1").fetchone()[0]
for key, value in {"theme": 7, "theme_source": 0, "theme_mode": 1 + dark,
"dark_mode": dark, "ui_scale": 10}.items():
database.execute("insert into settings(user_id,key,value,updated_at) values(?,?,?,?) "
"on conflict(user_id,key) do update set value=excluded.value",
(owner, key, str(value), int(time.time())))
with application(phone, f"sage-{dark}", size=(390, 844)) as window:
phone_entry = next((phone / "diary").glob("*/" + entry.name))
command("xdotool", "mousemove", "--window", window, "200", "800")
command("xdotool", "click", "--repeat", "16", "--delay", "70", "5")
time.sleep(0.5)
capture(window, f"sage-{dark}-first")
with Image.open(OUTPUT / f"sage-{dark}-first.png") as image:
red = sum(count for count, color in image.convert("RGB").getcolors(image.width * image.height)
if color[0] > 200 and color[1] < 100 and color[2] < 120)
assert red > 1000, "First photo not visible at phone scale"
# The page's bottom is stable: its last row has the three controls.
tap(window, 310, 763)
capture(window, f"sage-{dark}-second")
with Image.open(OUTPUT / f"sage-{dark}-second.png") as image:
blue = sum(count for count, color in image.convert("RGB").getcolors(image.width * image.height)
if color[2] > 180 and color[0] < 100 and color[1] < 140)
assert blue > 1000, "Next photo not visible at phone scale"
tap(window, 310, 763)
capture(window, f"sage-{dark}-missing")
assert json.loads(phone_entry.read_text())["text"] == original["text"]
tap(window, 195, 763)
deadline = time.monotonic() + 5
while len(json.loads(phone_entry.read_text())["photos"]) != 2:
assert time.monotonic() < deadline, "Photo removal was not saved"
time.sleep(0.1)
assert json.loads(phone_entry.read_text())["text"] == original["text"]
with application(phone, f"sage-{dark}-restart", size=(390, 844)):
assert len(json.loads(phone_entry.read_text())["photos"]) == 2
assert (phone_entry.parent / "photo-local.bin").read_bytes() == photo_bytes
print("Standalone Diary: native editing, delayed save, time insertion, undo, redo, spelling without an account, calendar, restart and day navigation passed")
print("Harmony import: text and photo bytes copied, originals preserved, symlinks skipped and existing entries retained")
print("Diary photos: multiple images, missing files, navigation, removal and restart at phone width in light/dark sage passed")