π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Add QR code detection overlay in image viewer
Amrithesh Kakkoth Ente9f69268d40a6
diff --git a/mobile/apps/auth/plugins/qr/android/build.gradle b/mobile/apps/auth/plugins/qr/android/build.gradleindex 39a44b6686..2e8cdb232a 100644--- a/mobile/apps/auth/plugins/qr/android/build.gradle+++ b/mobile/apps/auth/plugins/qr/android/build.gradle@@ -25,7 +25,8 @@ apply plugin: 'com.android.library'apply plugin: 'kotlin-android'android {- compileSdkVersion 33+ namespace 'io.ente.auth.ente_qr'+ compileSdk 36compileOptions {sourceCompatibility JavaVersion.VERSION_1_8diff --git a/mobile/apps/auth/plugins/qr/android/src/main/AndroidManifest.xml b/mobile/apps/auth/plugins/qr/android/src/main/AndroidManifest.xmlindex ad0e4b4e06..a2f47b6057 100644--- a/mobile/apps/auth/plugins/qr/android/src/main/AndroidManifest.xml+++ b/mobile/apps/auth/plugins/qr/android/src/main/AndroidManifest.xml@@ -1,3 +1,2 @@-<manifest xmlns:android="http://schemas.android.com/apk/res/android"- package="io.ente.auth.ente_qr">+<manifest xmlns:android="http://schemas.android.com/apk/res/android"></manifest>diff --git a/mobile/apps/photos/lib/l10n/intl_en.arb b/mobile/apps/photos/lib/l10n/intl_en.arbindex 020361e036..0b053cfe27 100644--- a/mobile/apps/photos/lib/l10n/intl_en.arb+++ b/mobile/apps/photos/lib/l10n/intl_en.arb@@ -287,6 +287,7 @@"createPublicLink": "Create public link","sendLink": "Send link","qrCode": "QR Code",+ "readQr": "Read QR","sendQrCode": "Send QR code","copyLink": "Copy link","copyEmbedHtml": "Copy embed HTML",diff --git a/mobile/apps/photos/lib/ui/viewer/file/detail_page.dart b/mobile/apps/photos/lib/ui/viewer/file/detail_page.dartindex b1d15526f6..59389a1e53 100644--- a/mobile/apps/photos/lib/ui/viewer/file/detail_page.dart+++ b/mobile/apps/photos/lib/ui/viewer/file/detail_page.dart@@ -27,6 +27,7 @@ import "package:photos/ui/viewer/file/file_app_bar.dart";import "package:photos/ui/viewer/file/file_bottom_bar.dart";import 'package:photos/ui/viewer/file/file_widget.dart';import "package:photos/ui/viewer/file/panorama_viewer_screen.dart";+import "package:photos/ui/viewer/file/qr_code_overlay_button.dart";import "package:photos/ui/viewer/file/text_detection_overlay_button.dart";import 'package:photos/ui/viewer/gallery/gallery.dart';import 'package:photos/utils/dialog_util.dart';@@ -255,6 +256,21 @@ class _BodyState extends State<_Body> {);},),+ if (flagService.qrFeatureEnabled)+ ValueListenableBuilder(+ valueListenable: _selectedIndexNotifier,+ builder: (BuildContext context, int selectedIndex, _) {+ return widget.config.mode == DetailPageMode.minimalistic+ ? const SizedBox.shrink()+ : QrCodeOverlayButton(+ file: _files![selectedIndex],+ enableFullScreenNotifier:+ InheritedDetailPageState.of(context)+ .enableFullScreenNotifier,+ isGuestView: isGuestView,+ );+ },+ ),ValueListenableBuilder(valueListenable: _selectedIndexNotifier,builder: (BuildContext context, int selectedIndex, _) {diff --git a/mobile/apps/photos/lib/ui/viewer/file/qr_code_content_sheet.dart b/mobile/apps/photos/lib/ui/viewer/file/qr_code_content_sheet.dartnew file mode 100644index 0000000000..d8185ca03c--- /dev/null+++ b/mobile/apps/photos/lib/ui/viewer/file/qr_code_content_sheet.dart@@ -0,0 +1,122 @@+import "package:flutter/material.dart";+import "package:flutter/services.dart";+import "package:photos/generated/l10n.dart";+import "package:photos/theme/ente_theme.dart";+import "package:photos/ui/components/base_bottom_sheet.dart";+import "package:photos/ui/notification/toast.dart";+import "package:photos/utils/share_util.dart";+import "package:url_launcher/url_launcher.dart";++Future<void> showQrCodeContentSheet(+ BuildContext context, {+ required String content,+}) {+ return showBaseBottomSheet(+ context,+ title: AppLocalizations.of(context).qrCode,+ child: _QrCodeContentBody(content: content),+ );+}++class _QrCodeContentBody extends StatelessWidget {+ final String content;++ const _QrCodeContentBody({required this.content});++ @override+ Widget build(BuildContext context) {+ final colorScheme = getEnteColorScheme(context);+ final textTheme = getEnteTextTheme(context);+ final l10n = AppLocalizations.of(context);+ final Uri? parsedUri = Uri.tryParse(content);+ final bool isUrl = parsedUri != null &&+ (parsedUri.scheme == "http" || parsedUri.scheme == "https");++ return Column(+ mainAxisSize: MainAxisSize.min,+ crossAxisAlignment: CrossAxisAlignment.stretch,+ children: [+ Container(+ padding: const EdgeInsets.all(16),+ decoration: BoxDecoration(+ color: colorScheme.fillDark,+ borderRadius: BorderRadius.circular(12),+ ),+ child: SelectableText(+ content,+ style: textTheme.body.copyWith(color: colorScheme.textBase),+ ),+ ),+ const SizedBox(height: 16),+ Row(+ children: [+ _ActionButton(+ icon: Icons.copy,+ label: l10n.copyLink,+ onTap: () async {+ await Clipboard.setData(ClipboardData(text: content));+ if (context.mounted) {+ showShortToast(context, l10n.copied);+ }+ },+ ),+ if (isUrl) ...[+ const SizedBox(width: 12),+ _ActionButton(+ icon: Icons.open_in_new,+ label: l10n.openFile,+ onTap: () => launchUrl(parsedUri),+ ),+ ],+ const SizedBox(width: 12),+ _ActionButton(+ icon: Icons.share,+ label: l10n.share,+ onTap: () => shareText(content, context: context),+ ),+ ],+ ),+ ],+ );+ }+}++class _ActionButton extends StatelessWidget {+ final IconData icon;+ final String label;+ final VoidCallback onTap;++ const _ActionButton({+ required this.icon,+ required this.label,+ required this.onTap,+ });++ @override+ Widget build(BuildContext context) {+ final colorScheme = getEnteColorScheme(context);+ final textTheme = getEnteTextTheme(context);++ return GestureDetector(+ onTap: onTap,+ child: Container(+ padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 8),+ decoration: BoxDecoration(+ color: colorScheme.fillDark,+ borderRadius: BorderRadius.circular(8),+ ),+ child: Row(+ mainAxisSize: MainAxisSize.min,+ children: [+ Icon(icon, size: 18, color: colorScheme.textMuted),+ const SizedBox(width: 6),+ Text(+ label,+ style: textTheme.mini.copyWith(color: colorScheme.textMuted),+ ),+ ],+ ),+ ),+ );+ }+}diff --git a/mobile/apps/photos/lib/ui/viewer/file/qr_code_overlay_button.dart b/mobile/apps/photos/lib/ui/viewer/file/qr_code_overlay_button.dartnew file mode 100644index 0000000000..2fc8c1b6d4--- /dev/null+++ b/mobile/apps/photos/lib/ui/viewer/file/qr_code_overlay_button.dart@@ -0,0 +1,286 @@+import "dart:io";++import "package:ente_qr/ente_qr.dart";+import "package:flutter/foundation.dart";+import "package:flutter/material.dart";+import "package:logging/logging.dart";+import "package:photos/generated/l10n.dart";+import "package:photos/models/file/file.dart";+import "package:photos/models/file/file_type.dart";+import "package:photos/models/file/trash_file.dart";+import "package:photos/theme/ente_theme.dart";+import "package:photos/ui/viewer/file/qr_code_content_sheet.dart";+import "package:photos/utils/dialog_util.dart";+import "package:photos/utils/file_util.dart";++class QrCodeOverlayButton extends StatefulWidget {+ final EnteFile file;+ final ValueListenable<bool> enableFullScreenNotifier;+ final bool isGuestView;++ const QrCodeOverlayButton({+ required this.file,+ required this.enableFullScreenNotifier,+ required this.isGuestView,+ super.key,+ });++ @override+ State<QrCodeOverlayButton> createState() => _QrCodeOverlayButtonState();+}++class _QrCodeOverlayButtonState extends State<QrCodeOverlayButton> {+ static final Map<String, _DetectionResult> _cache = {};+ final Logger _logger = Logger("QrCodeOverlayButton");+ final EnteQr _enteQr = EnteQr();++ bool _isEligible = false;+ bool _hasQr = false;+ bool _isChecking = false;+ String? _qrContent;+ int _requestId = 0;++ @override+ void initState() {+ super.initState();+ _evaluateFile();+ }++ @override+ void didUpdateWidget(covariant QrCodeOverlayButton oldWidget) {+ super.didUpdateWidget(oldWidget);+ if (_didFileChange(oldWidget.file, widget.file)) {+ _evaluateFile();+ }+ }++ bool _didFileChange(EnteFile oldFile, EnteFile newFile) {+ if (oldFile.generatedID != newFile.generatedID) {+ return true;+ }+ if (oldFile.uploadedFileID != newFile.uploadedFileID) {+ return true;+ }+ if (oldFile.localID != newFile.localID) {+ return true;+ }+ return false;+ }++ Future<void> _evaluateFile() async {+ final bool isEligible = _isFileEligible(widget.file);+ final int requestId = ++_requestId;++ if (!isEligible) {+ setState(() {+ _isEligible = false;+ _hasQr = false;+ _qrContent = null;+ _isChecking = false;+ });+ return;+ }++ final String cacheKey = _cacheKey(widget.file);+ final _DetectionResult? cachedResult = _cache[cacheKey];+ if (cachedResult != null) {+ if (!mounted || requestId != _requestId) {+ return;+ }+ setState(() {+ _isEligible = true;+ _hasQr = cachedResult.hasQr;+ _qrContent = cachedResult.content;+ _isChecking = false;+ });+ return;+ }++ setState(() {+ _isEligible = true;+ _hasQr = false;+ _qrContent = null;+ _isChecking = true;+ });++ try {+ final File? localFile = await getFile(widget.file);+ if (!mounted || requestId != _requestId) {+ return;+ }+ if (localFile == null || !localFile.existsSync()) {+ _cache[cacheKey] = const _DetectionResult(hasQr: false);+ setState(() {+ _hasQr = false;+ _qrContent = null;+ _isChecking = false;+ });+ return;+ }++ bool hasQr = false;+ String? qrContent;+ try {+ final result = await _enteQr.scanQrFromImage(localFile.path);+ if (result.success && result.content != null) {+ hasQr = true;+ qrContent = result.content;+ }+ } catch (error, stackTrace) {+ _logger.severe("Failed to scan QR code", error, stackTrace);+ }++ if (!mounted || requestId != _requestId) {+ return;+ }++ final _DetectionResult result = _DetectionResult(+ hasQr: hasQr,+ content: hasQr ? qrContent : null,+ );+ _cache[cacheKey] = result;+ setState(() {+ _hasQr = result.hasQr;+ _qrContent = result.content;+ _isChecking = false;+ });+ } catch (error, stackTrace) {+ _logger.severe("QR code detection failed", error, stackTrace);+ if (!mounted || requestId != _requestId) {+ return;+ }+ _cache[cacheKey] = const _DetectionResult(hasQr: false);+ setState(() {+ _hasQr = false;+ _qrContent = null;+ _isChecking = false;+ });+ }+ }++ bool _isFileEligible(EnteFile file) {+ return file.fileType == FileType.image ||+ file.fileType == FileType.livePhoto;+ }++ String _cacheKey(EnteFile file) {+ if (file.uploadedFileID != null) {+ return "qr_uploaded_${file.uploadedFileID}";+ }+ if (file.localID != null) {+ return "qr_local_${file.localID}";+ }+ return "qr_generated_${file.generatedID}";+ }++ @override+ Widget build(BuildContext context) {+ if (!_isEligible || _isChecking || !_hasQr) {+ return const SizedBox.shrink();+ }++ return ValueListenableBuilder<bool>(+ valueListenable: widget.enableFullScreenNotifier,+ builder: (context, isFullScreen, _) {+ final bool shouldHide =+ isFullScreen || widget.isGuestView || widget.file is TrashFile;+ if (shouldHide) {+ return const SizedBox.shrink();+ }++ double bottomOffset = MediaQuery.paddingOf(context).bottom + 72.0;++ final caption = widget.file.caption;+ if (caption != null && caption.trim().isNotEmpty) {+ final textPainter = TextPainter(+ text: TextSpan(+ text: caption.trim(),+ style: getEnteTextTheme(context).mini,+ ),+ textDirection: TextDirection.ltr,+ textScaler: MediaQuery.textScalerOf(context),+ maxLines: 3,+ );++ final double maxWidth = MediaQuery.sizeOf(context).width - 16.0;+ textPainter.layout(maxWidth: maxWidth);++ bottomOffset += textPainter.height + 24.0;+ }++ // Position above the text detection button+ bottomOffset += 44.0;++ return Positioned(+ bottom: bottomOffset,+ left: 0,+ right: 0,+ child: Center(+ child: GestureDetector(+ onTap: _onPressed,+ child: Container(+ padding: const EdgeInsets.symmetric(+ horizontal: 12,+ vertical: 8,+ ),+ decoration: BoxDecoration(+ color: Colors.black.withAlpha(160),+ borderRadius: BorderRadius.circular(60),+ border: Border.all(+ color: Colors.white.withAlpha(60),+ width: 0.5,+ ),+ ),+ child: Row(+ mainAxisSize: MainAxisSize.min,+ children: [+ Container(+ width: 8,+ height: 8,+ decoration: BoxDecoration(+ color: getEnteColorScheme(context).primary700,+ shape: BoxShape.circle,+ ),+ ),+ const SizedBox(width: 8),+ Text(+ AppLocalizations.of(context).readQr,+ style: getEnteTextTheme(context).mini.copyWith(+ color: Colors.white,+ ),+ ),+ ],+ ),+ ),+ ),+ ),+ );+ },+ );+ }++ Future<void> _onPressed() async {+ try {+ final String? content = _qrContent;+ if (content == null || content.isEmpty) {+ throw Exception("QR code content is empty");+ }+ if (!mounted) {+ return;+ }+ await showQrCodeContentSheet(context, content: content);+ } catch (error, stackTrace) {+ _logger.severe("Failed to show QR content", error, stackTrace);+ if (mounted) {+ await showGenericErrorDialog(context: context, error: error);+ }+ }+ }+}++class _DetectionResult {+ final bool hasQr;+ final String? content;++ const _DetectionResult({required this.hasQr, this.content});+}diff --git a/mobile/apps/photos/plugins/ente_feature_flag/lib/src/service.dart b/mobile/apps/photos/plugins/ente_feature_flag/lib/src/service.dartindex 2309a58c2e..8968557ba5 100644--- a/mobile/apps/photos/plugins/ente_feature_flag/lib/src/service.dart+++ b/mobile/apps/photos/plugins/ente_feature_flag/lib/src/service.dart@@ -106,6 +106,8 @@ class FlagService {bool get useRustForFaceThumbnails => internalUser;+ bool get qrFeatureEnabled => internalUser;+Future<void> tryRefreshFlags() async {try {await _fetch();diff --git a/mobile/apps/photos/pubspec.lock b/mobile/apps/photos/pubspec.lockindex 0a1cbdff1e..5a5e9677c5 100644--- a/mobile/apps/photos/pubspec.lock+++ b/mobile/apps/photos/pubspec.lock@@ -629,6 +629,13 @@ packages:relative: truesource: pathversion: "1.0.0"+ ente_qr:+ dependency: "direct main"+ description:+ path: "../auth/plugins/qr"+ relative: true+ source: path+ version: "0.0.1"ente_qr_ui:dependency: "direct main"description:diff --git a/mobile/apps/photos/pubspec.yaml b/mobile/apps/photos/pubspec.yamlindex 40bd7838f1..5089b12152 100644--- a/mobile/apps/photos/pubspec.yaml+++ b/mobile/apps/photos/pubspec.yaml@@ -64,6 +64,8 @@ dependencies:path: rust_builderente_pure_utils:path: ../../packages/ente_pure_utils+ ente_qr:+ path: ../auth/plugins/qrente_qr_ui:path: ../../packages/qrente_rust: