π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Preserve motion photo probing before OCR
Neeraj Guptab7bc263b27ec
diff --git a/mobile/apps/photos/lib/ui/viewer/file/detail_page.dart b/mobile/apps/photos/lib/ui/viewer/file/detail_page.dartindex 5e223404da..9616a52587 100644--- a/mobile/apps/photos/lib/ui/viewer/file/detail_page.dart+++ b/mobile/apps/photos/lib/ui/viewer/file/detail_page.dart@@ -427,7 +427,7 @@ class _BodyState extends State<_Body> {widget.config.mode != DetailPageMode.minimalistic &&!file.isLiveOrMotionPhoto? (details) => _inlineTextDetectionController- .startTextSelectionAt(details.globalPosition)+ .startTextSelectionAt(file, details.globalPosition): null,);return GestureDetector(diff --git a/mobile/apps/photos/lib/ui/viewer/file/inline_text_detection.dart b/mobile/apps/photos/lib/ui/viewer/file/inline_text_detection.dartindex b692a365cc..3fe46f385f 100644--- a/mobile/apps/photos/lib/ui/viewer/file/inline_text_detection.dart+++ b/mobile/apps/photos/lib/ui/viewer/file/inline_text_detection.dart@@ -41,8 +41,10 @@ class InlineTextDetectionController {}}- void startTextSelectionAt(Offset globalPosition) {- _state?._handleLongPressAt(globalPosition);+ void startTextSelectionAt(EnteFile file, Offset globalPosition) {+ final state = _state;+ if (state == null || state._didFileChange(file, state.widget.file)) return;+ state._handleLongPressAt(globalPosition);}}diff --git a/mobile/apps/photos/lib/ui/viewer/file/zoomable_live_image_new.dart b/mobile/apps/photos/lib/ui/viewer/file/zoomable_live_image_new.dartindex 29aa15459e..6267010439 100644--- a/mobile/apps/photos/lib/ui/viewer/file/zoomable_live_image_new.dart+++ b/mobile/apps/photos/lib/ui/viewer/file/zoomable_live_image_new.dart@@ -50,9 +50,10 @@ class _ZoomableLiveImageNewState extends State<ZoomableLiveImageNew>with SingleTickerProviderStateMixin {final Logger _logger = Logger("ZoomableLiveImageNew");late EnteFile _enteFile;+ late final LiveImageLongPressRouter _longPressRouter;bool _showVideo = false;- bool _isLoadingVideoPlayer = false;bool _isVideoFrameReady = true;+ Future<MotionPhotoAvailability>? _videoLoad;late final _player = Player();VideoController? _videoController;@@ -65,6 +66,13 @@ class _ZoomableLiveImageNewState extends State<ZoomableLiveImageNew>super.initState();_enteFile = widget.enteFile;+ _longPressRouter = LiveImageLongPressRouter(+ initialAvailability: switch (_enteFile.pubMagicMetadata?.mvi) {+ final index? when index > 0 => MotionPhotoAvailability.present,+ _ => MotionPhotoAvailability.unknown,+ },+ probeMotionPhoto: _loadLiveVideo,+ );_logger.info('initState for ${_enteFile.generatedID} with tag ${_enteFile.tag} and name ${_enteFile.displayName}',);@@ -118,7 +126,7 @@ class _ZoomableLiveImageNewState extends State<ZoomableLiveImageNew>return false;}- void _onLongPressEvent(bool isPressed, [Offset? localPosition]) {+ bool _isLongPressInVisibleQrRegion(Offset localPosition) {// If pressing within a QR code region, let the QR overlay handle it,// but only when the overlay is actually visible (not in fullscreen mode).final isQrOverlayVisible =@@ -126,23 +134,12 @@ class _ZoomableLiveImageNewState extends State<ZoomableLiveImageNew>context,)?.enableFullScreenNotifier.value ??true);- if (isPressed &&- isQrOverlayVisible &&- localPosition != null &&- _isPositionInQrRegion(localPosition)) {- return;- }+ return isQrOverlayVisible && _isPositionInQrRegion(localPosition);+ }- if (isPressed) {- if (_videoController == null) {- unawaited(_loadLiveVideo());- } else {- _videoController!.player.seek(Duration.zero).ignore();- _videoController!.player.play().ignore();- }- } else if (_videoController != null) {- // stop playing video- _videoController!.player.pause().ignore();+ void _setPlaybackPressed(bool isPressed) {+ if (!isPressed) {+ _videoController?.player.pause().ignore();}if (mounted) {setState(() {@@ -151,6 +148,49 @@ class _ZoomableLiveImageNewState extends State<ZoomableLiveImageNew>}}+ void _playOrLoadVideo() {+ if (!_showVideo) return;+ if (_videoController == null) {+ unawaited(_loadLiveVideo());+ return;+ }+ _videoController!.player.seek(Duration.zero).ignore();+ _videoController!.player.play().ignore();+ }++ void _onLongPressStart(LongPressStartDetails details) {+ if (_isLongPressInVisibleQrRegion(details.localPosition)) return;++ final onTextSelectionStart = widget.onLongPressStart;+ if (onTextSelectionStart == null || _enteFile.isLivePhoto) {+ _setPlaybackPressed(true);+ _playOrLoadVideo();+ return;+ }++ _setPlaybackPressed(true);+ unawaited(_routeLongPress(details, onTextSelectionStart));+ }++ Future<void> _routeLongPress(+ LongPressStartDetails details,+ GestureLongPressStartCallback onTextSelectionStart,+ ) async {+ var action = LiveImageLongPressAction.textSelection;+ try {+ action = await _longPressRouter.resolve();+ } catch (error, stackTrace) {+ _logger.warning("Motion photo probe failed", error, stackTrace);+ }+ if (!mounted) return;+ if (action == LiveImageLongPressAction.playback) {+ _playOrLoadVideo();+ } else {+ _setPlaybackPressed(false);+ onTextSelectionStart(details);+ }+ }+@overrideWidget build(BuildContext context) {final image = ZoomableImage(@@ -184,11 +224,9 @@ class _ZoomableLiveImageNewState extends State<ZoomableLiveImageNew>);if (!widget.isFromMemories) {- return buildLiveImageLongPressGesture(- onTextSelectionStart: widget.onLongPressStart,- onPlaybackStart: (details) =>- _onLongPressEvent(true, details.localPosition),- onPlaybackEnd: (_) => _onLongPressEvent(false),+ return GestureDetector(+ onLongPressStart: _onLongPressStart,+ onLongPressEnd: (_) => _setPlaybackPressed(false),child: content,);}@@ -212,29 +250,45 @@ class _ZoomableLiveImageNewState extends State<ZoomableLiveImageNew>);}- Future<void> _loadLiveVideo() async {- // do nothing is already loading or loaded- if (_isLoadingVideoPlayer || _videoController != null) {- return;+ Future<MotionPhotoAvailability> _loadLiveVideo() {+ if (_videoController != null) {+ return Future.value(MotionPhotoAvailability.present);}- _isLoadingVideoPlayer = true;- try {- // For non-live photo, with fileType as Image, we still call _getMotionPhoto- // to check if it is a motion photo. This is needed to handle earlier- // uploads and upload from desktop- final File? videoFile = _enteFile.isLivePhoto- ? await _getLivePhotoVideo()- : await _getMotionPhotoVideo();-- if (!mounted) return;- if (videoFile != null && videoFile.existsSync()) {- await _setVideoController(videoFile.path);- } else if (_enteFile.isLivePhoto) {- showShortToast(context, AppLocalizations.of(context).downloadFailed);+ final activeLoad = _videoLoad;+ if (activeLoad != null) return activeLoad;++ late final Future<MotionPhotoAvailability> load;+ load = _loadLiveVideoOnce().whenComplete(() {+ if (identical(_videoLoad, load)) {+ _videoLoad = null;}- } finally {- _isLoadingVideoPlayer = false;+ });+ _videoLoad = load;+ return load;+ }++ Future<MotionPhotoAvailability> _loadLiveVideoOnce() async {+ // For non-live photo, with fileType as Image, we still call _getMotionPhoto+ // to check if it is a motion photo. This is needed to handle earlier+ // uploads and upload from desktop.+ final _MotionPhotoVideoResult result;+ if (_enteFile.isLivePhoto) {+ result = _MotionPhotoVideoResult(+ MotionPhotoAvailability.present,+ await _getLivePhotoVideo(),+ );+ } else {+ result = await _getMotionPhotoVideo();+ }++ if (!mounted) return result.availability;+ final videoFile = result.videoFile;+ if (videoFile != null && videoFile.existsSync()) {+ await _setVideoController(videoFile.path);+ } else if (_enteFile.isLivePhoto) {+ showShortToast(context, AppLocalizations.of(context).downloadFailed);}+ return result.availability;}Future<File?> _getLivePhotoVideo() async {@@ -266,9 +320,14 @@ class _ZoomableLiveImageNewState extends State<ZoomableLiveImageNew>return videoFile;}- Future<File?> _getMotionPhotoVideo() async {+ Future<_MotionPhotoVideoResult> _getMotionPhotoVideo() async {if (_enteFile.isRemoteOnlyFile && !(await isFileCached(_enteFile))) {- if (!mounted) return null;+ if (!mounted) {+ return const _MotionPhotoVideoResult(+ MotionPhotoAvailability.unknown,+ null,+ );+ }showShortToast(context, AppLocalizations.of(context).downloading);}@@ -298,16 +357,27 @@ class _ZoomableLiveImageNewState extends State<ZoomableLiveImageNew>index: index,);if (outputPath != null) {- return File(outputPath);+ return _MotionPhotoVideoResult(+ MotionPhotoAvailability.present,+ File(outputPath),+ );}+ return const _MotionPhotoVideoResult(+ MotionPhotoAvailability.present,+ null,+ );} else if (_enteFile.isMotionPhoto && _enteFile.canEditMetaInfo) {_logger.info('Incorrectly tagged as MP, reset tag ${_enteFile.tag}');FileMagicService.instance.updatePublicMagicMetadata([_enteFile], {motionVideoIndexKey: 0}).ignore();}+ return const _MotionPhotoVideoResult(+ MotionPhotoAvailability.absent,+ null,+ );}- return null;+ return const _MotionPhotoVideoResult(MotionPhotoAvailability.unknown, null);}Future<void> _setVideoController(String url) async {@@ -361,15 +431,46 @@ class _ZoomableLiveImageNewState extends State<ZoomableLiveImageNew>}}-GestureDetector buildLiveImageLongPressGesture({- required Widget child,- required GestureLongPressStartCallback onPlaybackStart,- required GestureLongPressEndCallback onPlaybackEnd,- GestureLongPressStartCallback? onTextSelectionStart,-}) {- return GestureDetector(- onLongPressStart: onTextSelectionStart ?? onPlaybackStart,- onLongPressEnd: onTextSelectionStart == null ? onPlaybackEnd : null,- child: child,- );+enum MotionPhotoAvailability { unknown, absent, present }++enum LiveImageLongPressAction { playback, textSelection }++class LiveImageLongPressRouter {+ LiveImageLongPressRouter({+ required MotionPhotoAvailability initialAvailability,+ required Future<MotionPhotoAvailability> Function() probeMotionPhoto,+ }) : _availability = initialAvailability,+ _probeMotionPhoto = probeMotionPhoto;++ MotionPhotoAvailability _availability;+ final Future<MotionPhotoAvailability> Function() _probeMotionPhoto;+ Future<MotionPhotoAvailability>? _activeProbe;++ Future<LiveImageLongPressAction> resolve() async {+ var availability = _availability;+ if (availability == MotionPhotoAvailability.unknown) {+ final probe = _activeProbe ?? _probeMotionPhoto();+ _activeProbe = probe;+ try {+ availability = await probe;+ if (availability != MotionPhotoAvailability.unknown) {+ _availability = availability;+ }+ } finally {+ if (identical(_activeProbe, probe)) {+ _activeProbe = null;+ }+ }+ }+ return availability == MotionPhotoAvailability.present+ ? LiveImageLongPressAction.playback+ : LiveImageLongPressAction.textSelection;+ }+}++class _MotionPhotoVideoResult {+ final MotionPhotoAvailability availability;+ final File? videoFile;++ const _MotionPhotoVideoResult(this.availability, this.videoFile);}diff --git a/mobile/apps/photos/test/ui/viewer/file/zoomable_live_image_new_test.dart b/mobile/apps/photos/test/ui/viewer/file/zoomable_live_image_new_test.dartindex 829886b256..660ee96074 100644--- a/mobile/apps/photos/test/ui/viewer/file/zoomable_live_image_new_test.dart+++ b/mobile/apps/photos/test/ui/viewer/file/zoomable_live_image_new_test.dart@@ -26,51 +26,34 @@ void main() {);});- testWidgets("text selection owns the long press when provided", (- tester,- ) async {- var textSelectionStarts = 0;- var playbackStarts = 0;- var playbackEnds = 0;-- await tester.pumpWidget(- MaterialApp(- home: buildLiveImageLongPressGesture(- onTextSelectionStart: (_) => textSelectionStarts++,- onPlaybackStart: (_) => playbackStarts++,- onPlaybackEnd: (_) => playbackEnds++,- child: const ColoredBox(key: Key("image"), color: Colors.black),- ),- ),+ test("untagged motion photos retain playback after probing", () async {+ var probes = 0;+ final router = LiveImageLongPressRouter(+ initialAvailability: MotionPhotoAvailability.unknown,+ probeMotionPhoto: () async {+ probes++;+ return MotionPhotoAvailability.present;+ },);- await tester.longPress(find.byKey(const Key("image")));-- expect(textSelectionStarts, 1);- expect(playbackStarts, 0);- expect(playbackEnds, 0);+ expect(await router.resolve(), LiveImageLongPressAction.playback);+ expect(await router.resolve(), LiveImageLongPressAction.playback);+ expect(probes, 1);});- testWidgets("live-photo playback keeps its long-press lifecycle", (- tester,- ) async {- var playbackStarts = 0;- var playbackEnds = 0;-- await tester.pumpWidget(- MaterialApp(- home: buildLiveImageLongPressGesture(- onPlaybackStart: (_) => playbackStarts++,- onPlaybackEnd: (_) => playbackEnds++,- child: const ColoredBox(key: Key("image"), color: Colors.black),- ),- ),+ test("untagged still photos fall back to text selection", () async {+ var probes = 0;+ final router = LiveImageLongPressRouter(+ initialAvailability: MotionPhotoAvailability.unknown,+ probeMotionPhoto: () async {+ probes++;+ return MotionPhotoAvailability.absent;+ },);- await tester.longPress(find.byKey(const Key("image")));-- expect(playbackStarts, 1);- expect(playbackEnds, 1);+ expect(await router.resolve(), LiveImageLongPressAction.textSelection);+ expect(await router.resolve(), LiveImageLongPressAction.textSelection);+ expect(probes, 1);});}