Bugfixes + BG task seems to work

This commit is contained in:
Imran Remtulla
2022-08-19 14:59:00 -04:00
parent 15d5ac1eef
commit 6b43d4ed60
4 changed files with 70 additions and 15 deletions

View File

@@ -1,11 +1,42 @@
import 'package:flutter/material.dart';
import 'package:obtainium/pages/apps.dart';
import 'package:obtainium/services/apps_provider.dart';
import 'package:obtainium/services/source_service.dart';
import 'package:provider/provider.dart';
import 'package:toast/toast.dart';
import 'package:workmanager/workmanager.dart';
void backgroundUpdateCheck() {
Workmanager().executeTask((task, inputData) async {
var appsProvider = AppsProvider(bg: true);
await appsProvider.loadApps();
List<App> updates = await appsProvider.getUpdates();
if (updates.isNotEmpty) {
String message = updates.length == 1
? '${updates[0].name} has an update.'
: '${(updates.length == 2 ? '${updates[0].name} and ${updates[1].name}' : '${updates[0].name} and ${updates.length - 1} more apps')} have updates.';
appsProvider.downloaderNotifications.cancel(2);
appsProvider.notify(
2,
'Updates Available',
message,
'UPDATES_AVAILABLE',
'Updates Available',
'Notifies the user that updates are available for one or more Apps tracked by Obtainium');
}
return Future.value(true);
});
}
void main() async {
WidgetsFlutterBinding.ensureInitialized();
Workmanager().initialize(
backgroundUpdateCheck,
);
await Workmanager().cancelByUniqueName('update-apps-task');
await Workmanager().registerPeriodicTask(
'update-apps-task', 'backgroundUpdateCheck',
frequency: const Duration(minutes: 15),
constraints: Constraints(networkType: NetworkType.connected));
runApp(MultiProvider(
providers: [ChangeNotifierProvider(create: (context) => AppsProvider())],
child: const MyApp(),

View File

@@ -19,11 +19,14 @@ class AppsProvider with ChangeNotifier {
bool loadingApps = false;
bool gettingUpdates = false;
AppsProvider() {
initializeDownloader();
AppsProvider({bool bg = false}) {
initializeNotifs();
loadApps().then((_) {
clearDownloadStates();
});
if (!bg) {
initializeDownloader();
}
}
// Notifications plugin for downloads
@@ -53,15 +56,19 @@ class AppsProvider with ChangeNotifier {
int progress = data[2];
downloadCallbackForeground(id, status, progress);
});
// Initialize the notifications service
await downloaderNotifications.initialize(const InitializationSettings(
android: AndroidInitializationSettings('ic_launcher')));
// Subscribe to changes in the app foreground status
foregroundSubscription = FGBGEvents.stream.listen((event) async {
isForeground = event == FGBGType.foreground;
if (isForeground) await loadApps();
});
}
Future<void> initializeNotifs() async {
// Initialize the notifications service
await downloaderNotifications.initialize(const InitializationSettings(
android: AndroidInitializationSettings('ic_launcher')));
}
// Callback that receives FlutterDownloader status and forwards to a foreground function
@pragma('vm:entry-point')
static void downloadCallbackBackground(
@@ -71,24 +78,33 @@ class AppsProvider with ChangeNotifier {
send!.send([id, status, progress]);
}
Future<void> notify(int id, String title, String message, String channelCode,
String channelName, String channelDescription) {
return downloaderNotifications.show(
id,
title,
message,
NotificationDetails(
android: AndroidNotificationDetails(channelCode, channelName,
channelDescription: channelDescription,
importance: Importance.max,
priority: Priority.max,
groupKey: 'dev.imranr.obtainium.$channelCode')));
}
// Foreground function to act on FlutterDownloader status updates (install downloaded APK)
void downloadCallbackForeground(
String id, DownloadTaskStatus status, int progress) async {
if (status == DownloadTaskStatus.complete) {
// Wait for app to come to the foreground if not already, and notify the user
while (!isForeground) {
await downloaderNotifications.show(
await notify(
1,
'Complete App Installation',
'Obtainium must be open to install Apps',
const NotificationDetails(
android: AndroidNotificationDetails(
'COMPLETE_INSTALL', 'Complete App Installation',
channelDescription:
'Ask the user to return to Obtanium to finish installing an App',
importance: Importance.max,
priority: Priority.max,
groupKey: 'dev.imranr.obtainium.COMPLETE_INSTALL')));
'COMPLETE_INSTALL',
'Complete App Installation',
'Asks the user to return to Obtanium to finish installing an App');
if (await FGBGEvents.stream.first == FGBGType.foreground) {
break;
}