Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[mob] add workmanager for android #3963

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions mobile/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,14 @@ dependencies {
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'


constraints {
implementation("androidx.work:work-runtime:2.8.1") {
because("Align work-runtime versions")
}
implementation("androidx.work:work-runtime-ktx:2.8.1") {
because("Align work-runtime-ktx versions")
}
}
}
6 changes: 6 additions & 0 deletions mobile/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,8 @@ PODS:
- Flutter
- wakelock_plus (0.0.1):
- Flutter
- workmanager (0.0.1):
- Flutter

DEPENDENCIES:
- background_fetch (from `.symlinks/plugins/background_fetch/ios`)
Expand Down Expand Up @@ -300,6 +302,7 @@ DEPENDENCIES:
- video_thumbnail (from `.symlinks/plugins/video_thumbnail/ios`)
- volume_controller (from `.symlinks/plugins/volume_controller/ios`)
- wakelock_plus (from `.symlinks/plugins/wakelock_plus/ios`)
- workmanager (from `.symlinks/plugins/workmanager/ios`)

SPEC REPOS:
trunk:
Expand Down Expand Up @@ -437,6 +440,8 @@ EXTERNAL SOURCES:
:path: ".symlinks/plugins/volume_controller/ios"
wakelock_plus:
:path: ".symlinks/plugins/wakelock_plus/ios"
workmanager:
:path: ".symlinks/plugins/workmanager/ios"

SPEC CHECKSUMS:
background_fetch: 39f11371c0dce04b001c4bfd5e782bcccb0a85e2
Expand Down Expand Up @@ -515,6 +520,7 @@ SPEC CHECKSUMS:
video_thumbnail: c4e2a3c539e247d4de13cd545344fd2d26ffafd1
volume_controller: 531ddf792994285c9b17f9d8a7e4dcdd29b3eae9
wakelock_plus: 8b09852c8876491e4b6d179e17dfe2a0b5f60d47
workmanager: 0afdcf5628bbde6924c21af7836fed07b42e30e6

PODFILE CHECKSUM: 20e086e6008977d43a3d40260f3f9bffcac748dd

Expand Down
7 changes: 4 additions & 3 deletions mobile/ios/Runner.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
97C146FC1CF9000F007C117D /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 97C146FA1CF9000F007C117D /* Main.storyboard */; };
97C146FE1CF9000F007C117D /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 97C146FD1CF9000F007C117D /* Assets.xcassets */; };
97C147011CF9000F007C117D /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 97C146FF1CF9000F007C117D /* LaunchScreen.storyboard */; };
DA6BE5E826B3BC8600656280 /* BuildFile in Resources */ = {isa = PBXBuildFile; };
DA6BE5E826B3BC8600656280 /* (null) in Resources */ = {isa = PBXBuildFile; };
/* End PBXBuildFile section */

/* Begin PBXCopyFilesBuildPhase section */
Expand Down Expand Up @@ -197,7 +197,6 @@
TargetAttributes = {
97C146ED1CF9000F007C117D = {
CreatedOnToolsVersion = 7.3.1;
DevelopmentTeam = 6Z68YJY9Q2;
LastSwiftMigration = 1100;
ProvisioningStyle = Automatic;
};
Expand Down Expand Up @@ -230,7 +229,7 @@
3B3967161E833CAA004F5970 /* AppFrameworkInfo.plist in Resources */,
97C146FE1CF9000F007C117D /* Assets.xcassets in Resources */,
97C146FC1CF9000F007C117D /* Main.storyboard in Resources */,
DA6BE5E826B3BC8600656280 /* BuildFile in Resources */,
DA6BE5E826B3BC8600656280 /* (null) in Resources */,
277218A0270F596900FFE3CC /* GoogleService-Info.plist in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down Expand Up @@ -342,6 +341,7 @@
"${BUILT_PRODUCTS_DIR}/video_thumbnail/video_thumbnail.framework",
"${BUILT_PRODUCTS_DIR}/volume_controller/volume_controller.framework",
"${BUILT_PRODUCTS_DIR}/wakelock_plus/wakelock_plus.framework",
"${BUILT_PRODUCTS_DIR}/workmanager/workmanager.framework",
"${PODS_XCFRAMEWORKS_BUILD_DIR}/ffmpeg-kit-ios-min/ffmpegkit.framework/ffmpegkit",
"${PODS_XCFRAMEWORKS_BUILD_DIR}/ffmpeg-kit-ios-min/libavcodec.framework/libavcodec",
"${PODS_XCFRAMEWORKS_BUILD_DIR}/ffmpeg-kit-ios-min/libavdevice.framework/libavdevice",
Expand Down Expand Up @@ -437,6 +437,7 @@
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/video_thumbnail.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/volume_controller.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/wakelock_plus.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/workmanager.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/ffmpegkit.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/libavcodec.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/libavdevice.framework",
Expand Down
104 changes: 72 additions & 32 deletions mobile/lib/app.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import "dart:async";
import 'dart:io';

import 'package:adaptive_theme/adaptive_theme.dart';
import 'package:background_fetch/background_fetch.dart';
import "package:background_fetch/background_fetch.dart";
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
Expand All @@ -13,23 +12,21 @@ import 'package:media_extension/media_extension_action_types.dart';
import 'package:photos/ente_theme_data.dart';
import "package:photos/generated/l10n.dart";
import "package:photos/l10n/l10n.dart";
import "package:photos/main.dart";
import "package:photos/service_locator.dart";
import 'package:photos/services/app_lifecycle_service.dart';
import "package:photos/services/home_widget_service.dart";
import 'package:photos/services/sync_service.dart';
import 'package:photos/ui/tabs/home_widget.dart';
import "package:photos/ui/viewer/actions/file_viewer.dart";
import "package:photos/utils/intent_util.dart";
import "package:workmanager/workmanager.dart" as workmanager;

class EnteApp extends StatefulWidget {
final Future<void> Function(String) runBackgroundTask;
final Future<void> Function(String) killBackgroundTask;
final AdaptiveThemeMode? savedThemeMode;
final Locale? locale;

const EnteApp(
this.runBackgroundTask,
this.killBackgroundTask,
this.locale,
this.savedThemeMode, {
super.key,
Expand All @@ -45,8 +42,8 @@ class EnteApp extends StatefulWidget {
}

class _EnteAppState extends State<EnteApp> with WidgetsBindingObserver {
final _logger = Logger("EnteAppState");
late Locale? locale;
final _logger = Logger("EnteAppState");

@override
void initState() {
Expand Down Expand Up @@ -87,7 +84,11 @@ class _EnteAppState extends State<EnteApp> with WidgetsBindingObserver {
: MediaExtentionAction(action: IntentAction.main);
AppLifecycleService.instance.setMediaExtensionAction(mediaExtentionAction);
if (mediaExtentionAction.action == IntentAction.main) {
_configureBackgroundFetch();
if (!enableWorkManager) {
_configureBackgroundFetch();
} else {
_configureWorkManager();
}
}
}

Expand Down Expand Up @@ -165,29 +166,68 @@ class _EnteAppState extends State<EnteApp> with WidgetsBindingObserver {
AppLifecycleService.instance.onAppInBackground(stateChangeReason);
}
}
}

void _configureBackgroundFetch() {
BackgroundFetch.configure(
BackgroundFetchConfig(
minimumFetchInterval: 15,
forceAlarmManager: false,
stopOnTerminate: false,
startOnBoot: true,
enableHeadless: true,
requiresBatteryNotLow: true,
requiresCharging: false,
requiresStorageNotLow: false,
requiresDeviceIdle: false,
requiredNetworkType: NetworkType.ANY,
), (String taskId) async {
await widget.runBackgroundTask(taskId);
}, (taskId) {
_logger.info("BG task timeout taskID: $taskId");
widget.killBackgroundTask(taskId);
}).then((int status) {
_logger.info('[BackgroundFetch] configure success: $status');
}).catchError((e) {
_logger.info('[BackgroundFetch] configure ERROR: $e');
});
}
final _logger = Logger("BackgroundInitializer");

void _configureWorkManager() {
workmanager.Workmanager().initialize(
callbackDispatcher,
isInDebugMode: kDebugMode,
);
workmanager.Workmanager().registerPeriodicTask(
'backgroundFetchTask',
'syncThings',
frequency: const Duration(minutes: 15),
initialDelay: const Duration(minutes: 1),
constraints: workmanager.Constraints(
networkType: workmanager.NetworkType.connected,
requiresBatteryNotLow: true,
requiresCharging: false,
requiresStorageNotLow: false,
requiresDeviceIdle: false,
),
existingWorkPolicy: workmanager.ExistingWorkPolicy.keep,
backoffPolicy: workmanager.BackoffPolicy.linear,
backoffPolicyDelay: const Duration(minutes: 15),
);
}

void _configureBackgroundFetch() {
BackgroundFetch.configure(
BackgroundFetchConfig(
minimumFetchInterval: 15,
forceAlarmManager: false,
stopOnTerminate: false,
startOnBoot: true,
enableHeadless: true,
requiresBatteryNotLow: true,
requiresCharging: false,
requiresStorageNotLow: false,
requiresDeviceIdle: false,
requiredNetworkType: NetworkType.ANY,
), (String taskId) async {
await runBackgroundTask(taskId);
}, (taskId) {
_logger.info("BG task timeout taskID: $taskId");
killBGTask(taskId);
}).then((int status) {
_logger.info('[BackgroundFetch] configure success: $status');
}).catchError((e) {
_logger.info('[BackgroundFetch] configure ERROR: $e');
});
}

@pragma('vm:entry-point')
void callbackDispatcher() {
workmanager.Workmanager().executeTask((taskName, inputData) async {
try {
await runBackgroundTask(taskName);
return true;
} catch (e) {
_logger.info('[WorkManager] task error: $e');
await killBGTask(taskName);
return false;
}
});
}
Loading