Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

locker: add independent update check flow and update dialog

AmanRajSinghMourya722a97ea1342
diff --git a/mobile/apps/locker/lib/l10n/app_en.arb b/mobile/apps/locker/lib/l10n/app_en.arb
index ccc3047cb2..abb8c23ee7 100644
--- a/mobile/apps/locker/lib/l10n/app_en.arb
+++ b/mobile/apps/locker/lib/l10n/app_en.arb
@@ -360,6 +360,14 @@
"privacy": "Privacy",
"terms": "Terms",
"termsOfServicesTitle": "Terms",
+ "checkForUpdates": "Check for updates",
+ "checkingForUpdates": "Checking for updates...",
+ "unableToCheckForUpdatesRightNow": "Unable to check for updates right now",
+ "youAreOnTheLatestVersion": "You are on the latest version",
+ "aNewVersionOfEnteLockerIsAvailable": "A new version of Ente Locker is available.",
+ "criticalUpdateAvailable": "Critical update available",
+ "updateAvailable": "Update available",
+ "downloadUpdate": "Download update",
"support": "Support",
"helpAndSupport": "Help and Support",
"contactSupport": "Contact support",
diff --git a/mobile/apps/locker/lib/l10n/app_localizations.dart b/mobile/apps/locker/lib/l10n/app_localizations.dart
index b0feee96b7..e973a4564b 100644
--- a/mobile/apps/locker/lib/l10n/app_localizations.dart
+++ b/mobile/apps/locker/lib/l10n/app_localizations.dart
@@ -1074,6 +1074,54 @@ abstract class AppLocalizations {
/// **'Terms'**
String get termsOfServicesTitle;
+ /// No description provided for @checkForUpdates.
+ ///
+ /// In en, this message translates to:
+ /// **'Check for updates'**
+ String get checkForUpdates;
+
+ /// No description provided for @checkingForUpdates.
+ ///
+ /// In en, this message translates to:
+ /// **'Checking for updates...'**
+ String get checkingForUpdates;
+
+ /// No description provided for @unableToCheckForUpdatesRightNow.
+ ///
+ /// In en, this message translates to:
+ /// **'Unable to check for updates right now'**
+ String get unableToCheckForUpdatesRightNow;
+
+ /// No description provided for @youAreOnTheLatestVersion.
+ ///
+ /// In en, this message translates to:
+ /// **'You are on the latest version'**
+ String get youAreOnTheLatestVersion;
+
+ /// No description provided for @aNewVersionOfEnteLockerIsAvailable.
+ ///
+ /// In en, this message translates to:
+ /// **'A new version of Ente Locker is available.'**
+ String get aNewVersionOfEnteLockerIsAvailable;
+
+ /// No description provided for @criticalUpdateAvailable.
+ ///
+ /// In en, this message translates to:
+ /// **'Critical update available'**
+ String get criticalUpdateAvailable;
+
+ /// No description provided for @updateAvailable.
+ ///
+ /// In en, this message translates to:
+ /// **'Update available'**
+ String get updateAvailable;
+
+ /// No description provided for @downloadUpdate.
+ ///
+ /// In en, this message translates to:
+ /// **'Download update'**
+ String get downloadUpdate;
+
/// No description provided for @support.
///
/// In en, this message translates to:
diff --git a/mobile/apps/locker/lib/l10n/app_localizations_en.dart b/mobile/apps/locker/lib/l10n/app_localizations_en.dart
index 43e9e8a154..d26193796e 100644
--- a/mobile/apps/locker/lib/l10n/app_localizations_en.dart
+++ b/mobile/apps/locker/lib/l10n/app_localizations_en.dart
@@ -541,6 +541,32 @@ class AppLocalizationsEn extends AppLocalizations {
@override
String get termsOfServicesTitle => 'Terms';
+ @override
+ String get checkForUpdates => 'Check for updates';
+
+ @override
+ String get checkingForUpdates => 'Checking for updates...';
+
+ @override
+ String get unableToCheckForUpdatesRightNow =>
+ 'Unable to check for updates right now';
+
+ @override
+ String get youAreOnTheLatestVersion => 'You are on the latest version';
+
+ @override
+ String get aNewVersionOfEnteLockerIsAvailable =>
+ 'A new version of Ente Locker is available.';
+
+ @override
+ String get criticalUpdateAvailable => 'Critical update available';
+
+ @override
+ String get updateAvailable => 'Update available';
+
+ @override
+ String get downloadUpdate => 'Download update';
+
@override
String get support => 'Support';
diff --git a/mobile/apps/locker/lib/main.dart b/mobile/apps/locker/lib/main.dart
index a7cde5b333..dadfc03abc 100644
--- a/mobile/apps/locker/lib/main.dart
+++ b/mobile/apps/locker/lib/main.dart
@@ -31,6 +31,7 @@ import 'package:locker/services/files/download/service_locator.dart';
import "package:locker/services/files/links/links_client.dart";
import "package:locker/services/files/links/links_service.dart";
import 'package:locker/services/trash/trash_service.dart';
+import "package:locker/services/update_service.dart";
import 'package:locker/ui/pages/home_page.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:path_provider/path_provider.dart';
@@ -186,6 +187,7 @@ Future<void> _init(bool bool, {String? via}) async {
Network.instance.getDio(),
packageInfo,
);
+ await UpdateService.instance.init(preferences, packageInfo);
await TrashService.instance.init(preferences);
await EmergencyContactService.instance.init(
UserService.instance,
diff --git a/mobile/apps/locker/lib/services/update_service.dart b/mobile/apps/locker/lib/services/update_service.dart
new file mode 100644
index 0000000000..de278f5e30
--- /dev/null
+++ b/mobile/apps/locker/lib/services/update_service.dart
@@ -0,0 +1,195 @@
+import "dart:io";
+
+import "package:ente_network/network.dart";
+import "package:flutter/foundation.dart";
+import "package:locker/core/constants.dart";
+import "package:logging/logging.dart";
+import "package:package_info_plus/package_info_plus.dart";
+import "package:shared_preferences/shared_preferences.dart";
+
+class UpdateService {
+ UpdateService._privateConstructor();
+
+ static final UpdateService instance = UpdateService._privateConstructor();
+ static const String kUpdateAvailableShownTimeKey =
+ "update_available_shown_time_key";
+ static const String kChangeLogShownVersionKey = "update_change_log_key";
+ static const int currentChangeLogVersion = 1;
+ static const String _lockerIndependentPackageName =
+ "io.ente.locker.independent";
+ static const String _lockerIndependentPackagePrefix =
+ "io.ente.locker.independent";
+ static const String _lockerFDroidPackagePrefix = "io.ente.locker.fdroid";
+ static const String _releaseInfoUrl = String.fromEnvironment(
+ "locker.release_info_url",
+ defaultValue: "https://ente.io/release-info/locker-independent.json",
+ );
+
+ final _logger = Logger("UpdateService");
+
+ SharedPreferences? _prefs;
+ PackageInfo? _packageInfo;
+ LatestVersionInfo? _latestVersion;
+
+ Future<void> init(SharedPreferences prefs, PackageInfo packageInfo) async {
+ _prefs = prefs;
+ _packageInfo = packageInfo;
+ }
+
+ Future<bool> shouldUpdate() async {
+ if (!_isInitialized || !isIndependent()) {
+ return false;
+ }
+ _latestVersion = null;
+ try {
+ _latestVersion = await _getLatestVersionInfo();
+ final currentVersionCode = int.tryParse(_packageInfo!.buildNumber) ?? 0;
+ return currentVersionCode < _latestVersion!.code;
+ } catch (e, s) {
+ _logger.severe("Failed to check for app updates", e, s);
+ return false;
+ }
+ }
+
+ bool shouldForceUpdate(LatestVersionInfo? info) {
+ if (!_isInitialized || !isIndependent() || info == null) {
+ return false;
+ }
+ try {
+ final currentVersionCode = int.tryParse(_packageInfo!.buildNumber) ?? 0;
+ return currentVersionCode < info.lastSupportedVersionCode;
+ } catch (e, s) {
+ _logger.severe("Failed to determine force-update status", e, s);
+ return false;
+ }
+ }
+
+ LatestVersionInfo? getLatestVersionInfo() {
+ return _latestVersion;
+ }
+
+ Future<bool> shouldShowUpdateNotification() async {
+ if (!_isInitialized || !isIndependent()) {
+ return false;
+ }
+
+ final shouldUpdate = await this.shouldUpdate();
+ if (!shouldUpdate || _latestVersion == null) {
+ return false;
+ }
+
+ final lastNotificationShownTime =
+ _prefs!.getInt(kUpdateAvailableShownTimeKey) ?? 0;
+ final now = DateTime.now().microsecondsSinceEpoch;
+ final thresholdInDays = _latestVersion!.shouldNotify ? 1 : 3;
+ final hasExceededThreshold = (now - lastNotificationShownTime) >
+ (thresholdInDays * microSecondsInDay);
+
+ return hasExceededThreshold;
+ }
+
+ Future<void> markUpdateNotificationShown() async {
+ if (!_isInitialized) return;
+ await _prefs!.setInt(
+ kUpdateAvailableShownTimeKey,
+ DateTime.now().microsecondsSinceEpoch,
+ );
+ }
+
+ Future<bool> shouldShowChangeLog() async {
+ if (!_isInitialized) {
+ return false;
+ }
+ final lastShownVersion = _prefs!.getInt(kChangeLogShownVersionKey) ?? 0;
+ return lastShownVersion < currentChangeLogVersion;
+ }
+
+ Future<void> markChangeLogShown() async {
+ if (!_isInitialized) return;
+ await _prefs!.setInt(kChangeLogShownVersionKey, currentChangeLogVersion);
+ }
+
+ Future<void> resetChangeLogShown() async {
+ if (!_isInitialized) return;
+ await _prefs!.remove(kChangeLogShownVersionKey);
+ }
+
+ bool isIndependent() {
+ if (!_isInitialized) {
+ return false;
+ }
+ if (Platform.isIOS) {
+ return false;
+ }
+ if (kDebugMode) {
+ return true;
+ }
+ return _packageInfo!.packageName == _lockerIndependentPackageName;
+ }
+
+ bool isIndependentFlavor() {
+ if (!_isInitialized || Platform.isIOS) {
+ return false;
+ }
+ return _packageInfo!.packageName
+ .startsWith(_lockerIndependentPackagePrefix);
+ }
+
+ bool isFDroidFlavor() {
+ if (!_isInitialized || Platform.isIOS) {
+ return false;
+ }
+ return _packageInfo!.packageName.startsWith(_lockerFDroidPackagePrefix);
+ }
+
+ bool isPlayStoreFlavor() {
+ if (!_isInitialized || Platform.isIOS) {
+ return false;
+ }
+ return !isIndependentFlavor() && !isFDroidFlavor();
+ }
+
+ Future<LatestVersionInfo> _getLatestVersionInfo() async {
+ final response = await Network.instance.getDio().get(_releaseInfoUrl);
+ final latestVersion =
+ Map<String, dynamic>.from(response.data["latestVersion"]);
+ return LatestVersionInfo.fromMap(latestVersion);
+ }
+
+ bool get _isInitialized => _prefs != null && _packageInfo != null;
+}
+
+class LatestVersionInfo {
+ final String name;
+ final int code;
+ final List<String> changelog;
+ final bool shouldForceUpdate;
+ final int lastSupportedVersionCode;
+ final String url;
+ final int size;
+ final bool shouldNotify;
+
+ LatestVersionInfo(
+ this.name,
+ this.code,
+ this.changelog,
+ this.shouldForceUpdate,
+ this.lastSupportedVersionCode,
+ this.url,
+ this.size,
+ this.shouldNotify,
+ );
+
+ factory LatestVersionInfo.fromMap(Map<String, dynamic> map) {
+ return LatestVersionInfo(
+ map['name'],
+ map['code'],
+ List<String>.from(map['changelog']),
+ map['shouldForceUpdate'],
+ map['lastSupportedVersionCode'] ?? 1,
+ map['url'],
+ map['size'],
+ map['shouldNotify'],
+ );
+ }
+}
diff --git a/mobile/apps/locker/lib/ui/settings/pages/about_page.dart b/mobile/apps/locker/lib/ui/settings/pages/about_page.dart
index ac20695b55..7eb6d6b82b 100644
--- a/mobile/apps/locker/lib/ui/settings/pages/about_page.dart
+++ b/mobile/apps/locker/lib/ui/settings/pages/about_page.dart
@@ -1,8 +1,12 @@
import "package:ente_ui/components/title_bar_title_widget.dart";
import "package:ente_ui/theme/ente_theme.dart";
+import "package:ente_ui/utils/dialog_util.dart";
+import "package:ente_ui/utils/toast_util.dart";
import "package:ente_utils/platform_util.dart";
import "package:flutter/material.dart";
import "package:locker/l10n/l10n.dart";
+import "package:locker/services/update_service.dart";
+import "package:locker/ui/settings/widgets/app_update_dialog.dart";
import "package:locker/ui/settings/widgets/settings_widget.dart";
import "package:url_launcher/url_launcher.dart";
@@ -48,6 +52,13 @@ class AboutPage extends StatelessWidget {
title: l10n.termsOfServicesTitle,
onTap: () => _onTermsTapped(context),
),
+ if (UpdateService.instance.isIndependent()) ...[
+ const SizedBox(height: 8),
+ SettingsItem(
+ title: l10n.checkForUpdates,
+ onTap: () => _onCheckForUpdatesTapped(context),
+ ),
+ ],
],
),
),
@@ -77,4 +88,28 @@ class AboutPage extends StatelessWidget {
"https://ente.io/terms",
);
}
+
+ Future<void> _onCheckForUpdatesTapped(BuildContext context) async {
+ final l10n = context.l10n;
+ final dialog = createProgressDialog(context, l10n.checkingForUpdates);
+ await dialog.show();
+ final shouldUpdate = await UpdateService.instance.shouldUpdate();
+ final latestVersion = UpdateService.instance.getLatestVersionInfo();
+ await dialog.hide();
+ if (!context.mounted) {
+ return;
+ }
+ if (latestVersion == null) {
+ showShortToast(context, l10n.unableToCheckForUpdatesRightNow);
+ return;
+ }
+ if (!shouldUpdate) {
+ showShortToast(context, l10n.youAreOnTheLatestVersion);
+ return;
+ }
+ await showAppUpdateBottomSheet(
+ context,
+ latestVersionInfo: latestVersion,
+ );
+ }
}
diff --git a/mobile/apps/locker/lib/ui/settings/pages/settings_search_page.dart b/mobile/apps/locker/lib/ui/settings/pages/settings_search_page.dart
index bace16202e..3d8aba60ba 100644
--- a/mobile/apps/locker/lib/ui/settings/pages/settings_search_page.dart
+++ b/mobile/apps/locker/lib/ui/settings/pages/settings_search_page.dart
@@ -12,6 +12,7 @@ import "package:flutter/material.dart";
import "package:hugeicons/hugeicons.dart";
import "package:locker/l10n/l10n.dart";
import "package:locker/services/configuration.dart";
+import "package:locker/services/update_service.dart";
import "package:locker/ui/settings/pages/about_page.dart";
import "package:locker/ui/settings/pages/account_settings_page.dart";
import "package:locker/ui/settings/pages/general_settings_page.dart";
@@ -175,6 +176,12 @@ class _SettingsSearchPageState extends State<SettingsSearchPage> {
category: l10n.aboutUs,
page: const AboutPage(),
),
+ if (UpdateService.instance.isIndependent())
+ _SearchableSetting(
+ title: l10n.checkForUpdates,
+ category: l10n.aboutUs,
+ page: const AboutPage(),
+ ),
if (hasLoggedIn)
_SearchableSetting(
title: l10n.logout,
diff --git a/mobile/apps/locker/lib/ui/settings/widgets/app_update_dialog.dart b/mobile/apps/locker/lib/ui/settings/widgets/app_update_dialog.dart
new file mode 100644
index 0000000000..6d7748bec0
--- /dev/null
+++ b/mobile/apps/locker/lib/ui/settings/widgets/app_update_dialog.dart
@@ -0,0 +1,46 @@
+import "dart:async";
+
+import "package:ente_ui/components/alert_bottom_sheet.dart";
+import "package:flutter/material.dart";
+import "package:locker/l10n/l10n.dart";
+import "package:locker/services/update_service.dart";
+import "package:locker/ui/components/gradient_button.dart";
+import "package:url_launcher/url_launcher_string.dart";
+
+Future<void> showAppUpdateBottomSheet(
+ BuildContext context, {
+ required LatestVersionInfo latestVersionInfo,
+}) async {
+ final navigator = Navigator.of(context);
+ final l10n = context.l10n;
+ final shouldForceUpdate =
+ UpdateService.instance.shouldForceUpdate(latestVersionInfo);
+ final updateMessage = l10n.aNewVersionOfEnteLockerIsAvailable;
+ final title =
+ shouldForceUpdate ? l10n.criticalUpdateAvailable : l10n.updateAvailable;
+
+ await showAlertBottomSheet<void>(
+ context,
+ title: title,
+ message: updateMessage,
+ assetPath: "assets/warning-blue.png",
+ isDismissible: !shouldForceUpdate,
+ showCloseButton: !shouldForceUpdate,
+ buttons: [
+ GradientButton(
+ text: l10n.downloadUpdate,
+ onTap: () {
+ unawaited(
+ launchUrlString(
+ latestVersionInfo.url,
+ mode: LaunchMode.externalApplication,
+ ),
+ );
+ if (!shouldForceUpdate) {
+ navigator.pop();
+ }
+ },
+ ),
+ ],
+ );
+}