π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Optimize home widget image quality with V2 implementation
Prateek Sunal2cab6d89c233
diff --git a/mobile/apps/photos/lib/services/album_home_widget_service.dart b/mobile/apps/photos/lib/services/album_home_widget_service.dartindex 7cf07d9e13..ce8ae2a20a 100644--- a/mobile/apps/photos/lib/services/album_home_widget_service.dart+++ b/mobile/apps/photos/lib/services/album_home_widget_service.dart@@ -357,8 +357,9 @@ class AlbumHomeWidgetService {}final bool isWidgetPresent = await countHomeWidgets() > 0;-- final limit = isWidgetPresent ? MAX_ALBUMS_LIMIT : 5;+ final limit = isWidgetPresent+ ? HomeWidgetService.instance.getWidgetImageLimit()+ : HomeWidgetService.WIDGET_IMAGE_LIMIT_MINIMAL;final maxAttempts = limit * 10;int renderedCount = 0;diff --git a/mobile/apps/photos/lib/services/home_widget_service.dart b/mobile/apps/photos/lib/services/home_widget_service.dartindex 279df255b0..bcfbde9c00 100644--- a/mobile/apps/photos/lib/services/home_widget_service.dart+++ b/mobile/apps/photos/lib/services/home_widget_service.dart@@ -1,7 +1,11 @@import 'dart:convert';import 'dart:io';+import 'dart:typed_data';+import 'dart:ui' as ui;+import 'package:flutter/foundation.dart';import 'package:flutter/material.dart';+import 'package:flutter/services.dart';import 'package:home_widget/home_widget.dart' as hw;import 'package:home_widget/home_widget.dart';import 'package:logging/logging.dart';@@ -9,12 +13,15 @@ import 'package:path_provider/path_provider.dart';import 'package:path_provider_foundation/path_provider_foundation.dart';import 'package:photos/core/constants.dart';import 'package:photos/models/file/file.dart';+import 'package:photos/service_locator.dart';import 'package:photos/services/album_home_widget_service.dart';import 'package:photos/services/memory_home_widget_service.dart';import 'package:photos/services/people_home_widget_service.dart';import 'package:photos/services/smart_memories_service.dart';import 'package:photos/utils/thumbnail_util.dart';+import 'package:photos/utils/file_util.dart';import "package:synchronized/synchronized.dart";+import 'package:flutter/painting.dart' as paint;enum WidgetStatus {// notSynced means the widget is not initialized or has no data@@ -29,12 +36,21 @@ enum WidgetStatus {syncedAll,}+// Top-level function for isolate to decode image+Future<ui.Image> _decodeImageInIsolate(Uint8List imageBytes) async {+ return await paint.decodeImageFromList(imageBytes);+}+/// Service to manage home screen widgets across the application/// Handles widget initialization, updates, and interaction with platform-specific widget APIsclass HomeWidgetService {// Constantsstatic const double THUMBNAIL_SIZE = 512.0;+ static const double THUMBNAIL_SIZE_V2 = 1024.0;static const String WIDGET_DIRECTORY = 'home_widget';+ static const int WIDGET_IMAGE_LIMIT_V1 = 50;+ static const int WIDGET_IMAGE_LIMIT_V2 = 25;+ static const int WIDGET_IMAGE_LIMIT_MINIMAL = 5;// URI schemes for different widget typesstatic const String MEMORY_WIDGET_SCHEME = 'memorywidget';@@ -68,6 +84,12 @@ class HomeWidgetService {_isAppGroupSet = true;}+ int getWidgetImageLimit() {+ return flagService.useWidgetV2+ ? WIDGET_IMAGE_LIMIT_V2+ : WIDGET_IMAGE_LIMIT_V1;+ }+Future<void> initHomeWidget([bool isBg = false]) async {await setAppGroup();await AlbumHomeWidgetService.instance.initAlbumHomeWidget(isBg);@@ -101,6 +123,21 @@ class HomeWidgetService {String title,String? mainKey,) async {+ // Use V2 (1024x1024 rendering) only in debug mode for now+ final bool useV2 = flagService.useWidgetV2;++ if (useV2) {+ // Try V2 first, fallback to V1 if it fails (e.g., for videos, live photos)+ final result = await _captureFileV2(file, key, title, mainKey);+ if (result) {+ return const Size(THUMBNAIL_SIZE_V2, THUMBNAIL_SIZE_V2);+ }+ _logger.info(+ "V2 capture failed for ${file.displayName}, falling back to V1",+ );+ }++ // Use V1 (either useV2 is false, or V2 failed)final result = await _captureFile(file, key, title, mainKey);if (!result) {_logger.warning("Failed to capture file ${file.displayName}");@@ -182,6 +219,97 @@ class HomeWidgetService {}}+ Future<bool> _captureFileV2(+ EnteFile file,+ String key,+ String title,+ String? mainKey,+ ) async {+ try {+ // Only process image files with V2+ if (file.fileType != FileType.image) {+ _logger.info(+ "Skipping V2 for non-image file type: ${file.fileType}",+ );+ return false;+ }++ // Get full image file or high quality version+ final File? imageFile = file.isRemoteFile+ ? await getFileFromServer(file)+ : await getFile(file);++ if (imageFile == null) {+ _logger.warning("Failed to get file for V2 widget ${file.displayName}");+ return false;+ }++ // Read image bytes and decode to get dimensions+ final Uint8List imageBytes = await imageFile.readAsBytes();++ // Decode image in isolate to avoid UI jank+ final ui.Image decodedImage = await compute(+ _decodeImageInIsolate,+ imageBytes,+ );++ final width = decodedImage.width.toDouble();+ final height = decodedImage.height.toDouble();+ final minDimension = width < height ? width : height;+ final size = minDimension < THUMBNAIL_SIZE_V2 ? minDimension : THUMBNAIL_SIZE_V2;++ // Clean up decoded image+ decodedImage.dispose();++ // Create image provider from file+ final imageProvider = FileImage(imageFile);++ // Create widget with image+ final widget = ClipRRect(+ borderRadius: BorderRadius.circular(32),+ child: Container(+ width: size,+ height: size,+ decoration: BoxDecoration(+ color: Colors.black,+ image: DecorationImage(+ image: imageProvider,+ fit: BoxFit.cover,+ ),+ ),+ ),+ );++ // Render widget using home_widget package+ await hw.HomeWidget.renderFlutterWidget(+ widget,+ logicalSize: Size(size, size),+ key: key,+ );++ // Format date for display+ final subText = await SmartMemoriesService.getDateFormattedLocale(+ creationTime: file.creationTime!,+ );++ // Create metadata+ final Map<String, dynamic> metadata = {+ "title": title,+ "subText": subText,+ "generatedId": file.generatedID!,+ if (mainKey != null) "mainKey": mainKey,+ };++ // Save metadata in platform-specific format+ await _saveWidgetMetadata(key, metadata);++ return true;+ } catch (error, stackTrace) {+ _logger.severe("Failed to save the image V2", error, stackTrace);+ return false;+ }+ }+Future<void> _saveWidgetMetadata(String key,Map<String, dynamic> metadata,diff --git a/mobile/apps/photos/lib/services/memory_home_widget_service.dart b/mobile/apps/photos/lib/services/memory_home_widget_service.dartindex a78a650a9d..c9e1929708 100644--- a/mobile/apps/photos/lib/services/memory_home_widget_service.dart+++ b/mobile/apps/photos/lib/services/memory_home_widget_service.dart@@ -275,7 +275,9 @@ class MemoryHomeWidgetService {}final bool isWidgetPresent = await countHomeWidgets() > 0;- final limit = isWidgetPresent ? MAX_MEMORIES_LIMIT : 5;+ final limit = isWidgetPresent+ ? HomeWidgetService.instance.getWidgetImageLimit()+ : HomeWidgetService.WIDGET_IMAGE_LIMIT_MINIMAL;final maxAttempts = limit * 10;int renderedCount = 0;diff --git a/mobile/apps/photos/lib/services/people_home_widget_service.dart b/mobile/apps/photos/lib/services/people_home_widget_service.dartindex 323bac5351..fac9a2dd77 100644--- a/mobile/apps/photos/lib/services/people_home_widget_service.dart+++ b/mobile/apps/photos/lib/services/people_home_widget_service.dart@@ -324,8 +324,11 @@ class PeopleHomeWidgetService {return;}- const limit = MAX_PEOPLE_LIMIT;- const maxAttempts = limit * 10;+ final bool isWidgetPresent = await countHomeWidgets() > 0;+ final limit = isWidgetPresent+ ? HomeWidgetService.instance.getWidgetImageLimit()+ : HomeWidgetService.WIDGET_IMAGE_LIMIT_MINIMAL;+ final maxAttempts = limit * 10;int renderedCount = 0;int attemptsCount = 0;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 272791ab17..1933aaf41b 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@@ -72,6 +72,8 @@ class FlagService {bool get useNativeVideoEditor => internalUser;+ bool get useWidgetV2 => kDebugMode;+bool hasSyncedAccountFlags() {return _prefs.containsKey("remote_flags");}