Fix background update issue (#896)

This commit is contained in:
Imran Remtulla
2023-09-16 04:31:03 -04:00
parent 4951f62d4a
commit e1f2baeeda
4 changed files with 34 additions and 23 deletions

View File

@@ -19,7 +19,7 @@ import 'package:easy_localization/src/easy_localization_controller.dart';
// ignore: implementation_imports // ignore: implementation_imports
import 'package:easy_localization/src/localization.dart'; import 'package:easy_localization/src/localization.dart';
const String currentVersion = '0.14.14'; const String currentVersion = '0.14.15';
const String currentReleaseTag = const String currentReleaseTag =
'v$currentVersion-beta'; // KEEP THIS IN SYNC WITH GITHUB RELEASES 'v$currentVersion-beta'; // KEEP THIS IN SYNC WITH GITHUB RELEASES

View File

@@ -430,8 +430,8 @@ class AppsProvider with ChangeNotifier {
zipFile: File(filePath), destinationDir: Directory(destinationPath)); zipFile: File(filePath), destinationDir: Directory(destinationPath));
} }
Future<void> installXApkDir( Future<void> installXApkDir(DownloadedXApkDir dir,
DownloadedXApkDir dir, BuildContext? context) async { {bool needsBGWorkaround = false}) async {
// We don't know which APKs in an XAPK are supported by the user's device // We don't know which APKs in an XAPK are supported by the user's device
// So we try installing all of them and assume success if at least one installed // So we try installing all of them and assume success if at least one installed
// If 0 APKs installed, throw the first install error encountered // If 0 APKs installed, throw the first install error encountered
@@ -444,7 +444,8 @@ class AppsProvider with ChangeNotifier {
if (file.path.toLowerCase().endsWith('.apk')) { if (file.path.toLowerCase().endsWith('.apk')) {
try { try {
somethingInstalled = somethingInstalled || somethingInstalled = somethingInstalled ||
await installApk(DownloadedApk(dir.appId, file), context); await installApk(DownloadedApk(dir.appId, file),
needsBGWorkaround: needsBGWorkaround);
} catch (e) { } catch (e) {
logs.add( logs.add(
'Could not install APK from XAPK \'${file.path}\': ${e.toString()}'); 'Could not install APK from XAPK \'${file.path}\': ${e.toString()}');
@@ -464,7 +465,8 @@ class AppsProvider with ChangeNotifier {
} }
} }
Future<bool> installApk(DownloadedApk file, BuildContext? context) async { Future<bool> installApk(DownloadedApk file,
{bool needsBGWorkaround = false}) async {
var newInfo = var newInfo =
await pm.getPackageArchiveInfo(archiveFilePath: file.file.path); await pm.getPackageArchiveInfo(archiveFilePath: file.file.path);
PackageInfo? appInfo = await getInstalledInfo(apps[file.appId]!.app.id); PackageInfo? appInfo = await getInstalledInfo(apps[file.appId]!.app.id);
@@ -473,16 +475,19 @@ class AppsProvider with ChangeNotifier {
!(await canDowngradeApps())) { !(await canDowngradeApps())) {
throw DowngradeError(); throw DowngradeError();
} }
int? code; if (needsBGWorkaround) {
if (context == null) { // The below 'await' will never return if we are in a background process
// In background installs, 'installApk' never returns so don't wait for it // To work around this, we should assume the install will be successful
// TODO: Find a fix to make this work synchronously without context // So we update the app's installed version first as we will never get to the later code
AndroidPackageInstaller.installApk(apkFilePath: file.file.path); // We can't conditionally get rid of the 'await' as this causes install fails (BG process times out) - see #896
code = 0; // Be optimistic (ver. det. will get most wrong ones anyways) // TODO: When fixed, update this function and the calls to it accordingly
} else { apps[file.appId]!.app.installedVersion =
code = apps[file.appId]!.app.latestVersion;
await AndroidPackageInstaller.installApk(apkFilePath: file.file.path); await saveApps([apps[file.appId]!.app],
attemptToCorrectInstallStatus: false);
} }
int? code =
await AndroidPackageInstaller.installApk(apkFilePath: file.file.path);
bool installed = false; bool installed = false;
if (code != null && code != 0 && code != 3) { if (code != null && code != 0 && code != 3) {
throw InstallError(code); throw InstallError(code);
@@ -649,11 +654,17 @@ class AppsProvider with ChangeNotifier {
notifyListeners(); notifyListeners();
try { try {
if (downloadedFile != null) { if (downloadedFile != null) {
// ignore: use_build_context_synchronously if (willBeSilent && context == null) {
await installApk(downloadedFile, context); installApk(downloadedFile, needsBGWorkaround: true);
} else { } else {
// ignore: use_build_context_synchronously await installApk(downloadedFile);
await installXApkDir(downloadedDir!, context); }
} else {
if (willBeSilent && context == null) {
installXApkDir(downloadedDir!, needsBGWorkaround: true);
} else {
await installXApkDir(downloadedDir!);
}
} }
if (willBeSilent && context == null) { if (willBeSilent && context == null) {
notificationsProvider?.notify(SilentUpdateAttemptNotification( notificationsProvider?.notify(SilentUpdateAttemptNotification(

View File

@@ -198,10 +198,10 @@ packages:
dependency: "direct main" dependency: "direct main"
description: description:
name: dynamic_color name: dynamic_color
sha256: de4798a7069121aee12d5895315680258415de9b00e717723a1bd73d58f0126d sha256: "96bff3df72e3d428bda2b874c7a521e8c86f592cae626ea594922fcc8d166e0c"
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "1.6.6" version: "1.6.7"
easy_localization: easy_localization:
dependency: "direct main" dependency: "direct main"
description: description:
@@ -887,10 +887,10 @@ packages:
dependency: transitive dependency: transitive
description: description:
name: webview_flutter_android name: webview_flutter_android
sha256: "9427774649fd3c8b7ff53523051395d13aed2ca355822b822e6493d79f5fc05a" sha256: ddc167c6676f57c8b367d19fcbee267d6dc6adf81bd6c3cb87981d30746e0a6d
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "3.10.0" version: "3.10.1"
webview_flutter_platform_interface: webview_flutter_platform_interface:
dependency: transitive dependency: transitive
description: description:

View File

@@ -17,7 +17,7 @@ publish_to: 'none' # Remove this line if you wish to publish to pub.dev
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html # https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
# In Windows, build-name is used as the major, minor, and patch parts # In Windows, build-name is used as the major, minor, and patch parts
# of the product and file versions while build-number is used as the build suffix. # of the product and file versions while build-number is used as the build suffix.
version: 0.14.14+206 # When changing this, update the tag in main() accordingly version: 0.14.15+207 # When changing this, update the tag in main() accordingly
environment: environment:
sdk: '>=3.0.0 <4.0.0' sdk: '>=3.0.0 <4.0.0'