diff --git a/lib/custom_errors.dart b/lib/custom_errors.dart index 04e6104..657a82a 100644 --- a/lib/custom_errors.dart +++ b/lib/custom_errors.dart @@ -1,3 +1,5 @@ +import 'dart:io'; + import 'package:android_package_installer/android_package_installer.dart'; import 'package:easy_localization/easy_localization.dart'; import 'package:flutter/material.dart'; @@ -72,6 +74,9 @@ class MultiAppMultiError extends ObtainiumError { MultiAppMultiError() : super(tr('placeholder'), unexpected: true); add(String appId, dynamic error, {String? appName}) { + if (error is SocketException) { + error = error.message; + } rawErrors[appId] = error; var string = error.toString(); var tempIds = idsByErrorString.remove(string); @@ -83,12 +88,17 @@ class MultiAppMultiError extends ObtainiumError { } } - String errorString(String appId) => - '${appIdNames.containsKey(appId) ? '${appIdNames[appId]} ($appId)' : appId}: ${rawErrors[appId].toString()}'; + String errorString(String appId, {bool includeIdsWithNames = false}) => + '${appIdNames.containsKey(appId) ? '${appIdNames[appId]}${includeIdsWithNames ? ' ($appId)' : ''}' : appId}: ${rawErrors[appId].toString()}'; + + String errorsAppsString(String errString, List appIds, + {bool includeIdsWithNames = false}) => + '$errString [${list2FriendlyString(appIds.map((id) => appIdNames.containsKey(id) == true ? '${appIdNames[id]}${includeIdsWithNames ? ' ($id)' : ''}' : id).toList())}]'; @override - String toString() => - idsByErrorString.keys.map((e) => errorString(e)).join('\n\n'); + String toString() => idsByErrorString.entries + .map((e) => errorsAppsString(e.key, e.value)) + .join('\n\n'); } showError(dynamic e, BuildContext context) { diff --git a/lib/providers/apps_provider.dart b/lib/providers/apps_provider.dart index 9acd085..330a605 100644 --- a/lib/providers/apps_provider.dart +++ b/lib/providers/apps_provider.dart @@ -1375,6 +1375,30 @@ Future bgUpdateCheck(int taskId, Map? params) async { logs.add( 'BG ${installMode ? 'install' : 'update'} task $taskId: Started (${installMode ? toInstall.length : toCheck.length}).'); + var netResult = await (Connectivity().checkConnectivity()); + + if (netResult == ConnectivityResult.none) { + var networkBasedRetryInterval = 15; + var nextRegularCheck = appsProvider.settingsProvider.lastBGCheckTime + .add(Duration(minutes: appsProvider.settingsProvider.updateInterval)); + var potentialNetworkRetryCheck = + DateTime.now().add(Duration(minutes: networkBasedRetryInterval)); + var shouldRetry = potentialNetworkRetryCheck.isBefore(nextRegularCheck); + logs.add( + 'BG update task $taskId: No network. Will ${shouldRetry ? 'retry in $networkBasedRetryInterval minutes' : 'not retry'}.'); + AndroidAlarmManager.oneShot( + const Duration(minutes: 15), taskId + 1, bgUpdateCheck, + params: { + 'toCheck': toCheck + .map((entry) => {'key': entry.key, 'value': entry.value}) + .toList(), + 'toInstall': toInstall + .map((entry) => {'key': entry.key, 'value': entry.value}) + .toList(), + }); + return; + } + if (!installMode) { // If in update mode, we check for updates. // We divide the results into 4 groups: @@ -1391,7 +1415,7 @@ Future bgUpdateCheck(int taskId, Map? params) async { List toNotify = []; List> toRetry = []; var retryAfterXSeconds = 0; - List toThrow = []; + MultiAppMultiError toThrow = MultiAppMultiError(); var networkRestricted = false; if (appsProvider.settingsProvider.bgUpdatesOnWiFiOnly) { var netResult = await (Connectivity().checkConnectivity()); @@ -1427,7 +1451,7 @@ Future bgUpdateCheck(int taskId, Map? params) async { retryAfterXSeconds = minRetryIntervalForThisApp; } } else { - toThrow.add(key); + toThrow.add(key, err, appName: errors?.appIdNames[key]); } }); } else { @@ -1455,10 +1479,10 @@ Future bgUpdateCheck(int taskId, Map? params) async { } // Send the error notifications - if (toThrow.isNotEmpty) { - for (var appId in toThrow) { + if (toThrow.rawErrors.isNotEmpty) { + for (var element in toThrow.idsByErrorString.entries) { notificationsProvider.notify(ErrorCheckingUpdatesNotification( - errors!.errorString(appId), + errors!.errorsAppsString(element.key, element.value), id: Random().nextInt(10000))); } }