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

Optional background tracking taking 5 random times, instead of 5 regular times per day #127

Merged
Merged
Show file tree
Hide file tree
Changes from 18 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
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,4 @@ android/app/src/debug/AndroidManifest.xml
android/app/src/debug/res/values/strings.xml
android/app/src/profile/AndroidManifest.xml

ios/Runner/GoogleService-Info.plist
ios/Runner/Info.plist
ios/Runner/GoogleService-Info.plist
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ Mosquito Alert is a mobile phone application that is part of a larger citizen sc
1. Clone the repository to your local machine
2. Go to android/local.properties and at the end of the file add the following lines: (If the file doesn't exist, just create it yourself)
```
# By leaving the values empty, the app will compile successfully but packages using those licenses will display an error (this is ok!)
transistorsoft.Key=
# By leaving the values empty, the app will compile successfully but packages using this license will display an error (this is ok!)
googlemaps.Key=
```
3. Run the following commands, one by one
Expand Down
2 changes: 0 additions & 2 deletions android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

Properties properties = new Properties()
properties.load(project.rootProject.file('local.properties').newDataInputStream())
def transistorsoftKey = properties.getProperty('transistorsoft.Key')
def googlemapsKey = properties.getProperty('googlemaps.Key')

android {
Expand All @@ -60,7 +59,6 @@ android {
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

manifestPlaceholders = [
transistorsoftKey: transistorsoftKey,
googlemapsKey: googlemapsKey
]
}
Expand Down
6 changes: 6 additions & 0 deletions ios/Runner.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -700,3 +700,9 @@
};
rootObject = 97C146E61CF9000F007C117D /* Project object */;
}

SystemCapabilities = {
com.apple.BackgroundModes = {
enabled = 1;
};
};
4 changes: 4 additions & 0 deletions ios/Runner/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import UIKit
import Flutter
import GoogleMaps
import Firebase
import workmanager

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
Expand All @@ -16,6 +17,9 @@ import Firebase
}
GMSServices.provideAPIKey("AIzaSyC5szIWBSfTg3SzJPkTPU7DPfZcdkvFd4A")
GeneratedPluginRegistrant.register(with: self)
UIApplication.shared.setMinimumBackgroundFetchInterval(TimeInterval(60*15))
WorkmanagerPlugin.registerBGProcessingTask(withIdentifier: "trackingTask")
WorkmanagerPlugin.registerBGProcessingTask(withIdentifier: "scheduleDailyTasks")
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
}
210 changes: 210 additions & 0 deletions ios/Runner/Info.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>BGTaskSchedulerPermittedIdentifiers</key>
<array>
<string>com.example.mosquito_alert_app.scheduleDailyTasks</string>
<string>com.example.mosquito_alert_app.trackingTask</string>
</array>
<key>CFBundleDevelopmentRegion</key>
<string>$(DEVELOPMENT_LANGUAGE)</string>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>cat.ibeji.tigatrapp</string>
<key>MinimumOSVersion</key>
<string>13.0</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>Mosquito Alert</string>
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>$(MARKETING_VERSION)</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>CFBundleURLName</key>
<string>com.example.mosquito_alert_app.ios.deep</string>
<key>CFBundleURLSchemes</key>
<array>
<string>fb1971217746245293</string>
</array>
</dict>
<dict>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>CFBundleURLSchemes</key>
<array>
<string>com.googleusercontent.apps.960892168613-7b7r7duu9j9setj1vaa2tocola7l6g44</string>
</array>
</dict>
<dict>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>CFBundleURLName</key>
<string>twitter</string>
<key>CFBundleURLSchemes</key>
<array>
<string>twitterkit-555555555</string>
</array>
</dict>
</array>
<key>CFBundleVersion</key>
<string>$(CURRENT_PROJECT_VERSION)</string>
<key>FacebookAppID</key>
<string>346752412924238</string>
<key>FacebookDisplayName</key>
<string>Mosquito Alert</string>
<key>LSApplicationQueriesSchemes</key>
<array>
<string>fbapi</string>
<string>fb-messenger-share-api</string>
<string>fbauth2</string>
<string>fbshareextension</string>
<string>twitter</string>
<string>twitterauth</string>
</array>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>NSAppTransportSecurity</key>
<dict>
<key>NSExceptionDomains</key>
<dict>
<key>creaf.cat</key>
<dict>
<key>NSExceptionAllowsInsecureHTTPLoads</key>
<true/>
<key>NSExceptionMinimumTLSVersion</key>
<string>TLSv1.2</string>
<key>NSExceptionRequiresForwardSecrecy</key>
<true/>
<key>NSIncludesSubdomains</key>
<true/>
<key>NSRequiresCertificateTransparency</key>
<false/>
<key>NSThirdPartyExceptionAllowsInsecureHTTPLoads</key>
<false/>
<key>NSThirdPartyExceptionMinimumTLSVersion</key>
<string>TLSv1.2</string>
<key>NSThirdPartyExceptionRequiresForwardSecrecy</key>
<true/>
</dict>
<key>madev.creaf.cat</key>
<dict>
<key>NSExceptionAllowsInsecureHTTPLoads</key>
<true/>
<key>NSExceptionMinimumTLSVersion</key>
<string>TLSv1.2</string>
<key>NSExceptionRequiresForwardSecrecy</key>
<true/>
<key>NSIncludesSubdomains</key>
<true/>
<key>NSRequiresCertificateTransparency</key>
<false/>
<key>NSThirdPartyExceptionAllowsInsecureHTTPLoads</key>
<false/>
<key>NSThirdPartyExceptionMinimumTLSVersion</key>
<string>TLSv1.2</string>
<key>NSThirdPartyExceptionRequiresForwardSecrecy</key>
<true/>
</dict>
<key>mosquitoalert.com</key>
<dict>
<key>NSExceptionAllowsInsecureHTTPLoads</key>
<true/>
<key>NSExceptionMinimumTLSVersion</key>
<string>TLSv1.2</string>
<key>NSExceptionRequiresForwardSecrecy</key>
<true/>
<key>NSIncludesSubdomains</key>
<true/>
<key>NSRequiresCertificateTransparency</key>
<false/>
<key>NSThirdPartyExceptionAllowsInsecureHTTPLoads</key>
<false/>
<key>NSThirdPartyExceptionMinimumTLSVersion</key>
<string>TLSv1.2</string>
<key>NSThirdPartyExceptionRequiresForwardSecrecy</key>
<true/>
</dict>
<key>webserver.mosquitoalert.com</key>
<dict>
<key>NSExceptionAllowsInsecureHTTPLoads</key>
<true/>
<key>NSExceptionMinimumTLSVersion</key>
<string>TLSv1.2</string>
<key>NSExceptionRequiresForwardSecrecy</key>
<true/>
<key>NSIncludesSubdomains</key>
<true/>
<key>NSRequiresCertificateTransparency</key>
<false/>
<key>NSThirdPartyExceptionAllowsInsecureHTTPLoads</key>
<false/>
<key>NSThirdPartyExceptionMinimumTLSVersion</key>
<string>TLSv1.2</string>
<key>NSThirdPartyExceptionRequiresForwardSecrecy</key>
<true/>
</dict>
</dict>
</dict>
<key>NSAppleMusicUsageDescription</key>
<string>We need access to this feature in order to show results suited for you.</string>
<key>NSCalendarsUsageDescription</key>
<string>We need access to this feature in order to show results suited for you.</string>
<key>NSCameraUsageDescription</key>
<string>We use your camera to take pictures of what you want to ask us.</string>
<key>NSContactsUsageDescription</key>
<string>We need access to this feature in order to show results suited for you.</string>
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>We use your location to search and show business around you.</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>We use your location to search and show business around you.</string>
<key>NSLocationUsageDescription</key>
<string>We use your location to search and show business around you.</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>We use your location to search and show business around you.</string>
<key>NSMotionUsageDescription</key>
<string>Motion usage description</string>
<key>NSPhotoLibraryAddUsageDescription</key>
<string>We use your image gallery to take pictures of what you want to ask us.</string>
<key>NSPhotoLibraryUsageDescription</key>
<string>We use your image gallery to take pictures of what you want to ask us.</string>
<key>NSSpeechRecognitionUsageDescription</key>
<string>We need access to this feature in order to show results suited for you.</string>
<key>NSUserTrackingUsageDescription</key>
<string>We use your device identifier to track you across sessions and to save your reports accordingly.</string>
<key>UIBackgroundModes</key>
<array>
<string>fetch</string>
<string>location</string>
<string>processing</string>
</array>
<key>UILaunchStoryboardName</key>
<string>LaunchScreen</string>
<key>UIMainStoryboardFile</key>
<string>Main</string>
<key>UISupportedInterfaceOrientations</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
</array>
<key>UISupportedInterfaceOrientations~ipad</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationPortraitUpsideDown</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
<key>UIViewControllerBasedStatusBarAppearance</key>
<false/>
<key>io.flutter.embedded_views_preview</key>
<true/>
</dict>
</plist>
36 changes: 19 additions & 17 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import 'dart:async';

import 'package:battery_plus/battery_plus.dart';
import 'package:geolocator/geolocator.dart';
import 'package:mosquito_alert_app/utils/UserManager.dart';
import 'package:mosquito_alert_app/utils/BackgroundTracking.dart';
import 'package:workmanager/workmanager.dart';

import 'package:connectivity/connectivity.dart';
Expand Down Expand Up @@ -33,7 +31,18 @@ void main({String env = 'prod'}) async {
isInDebugMode: false
);

await Workmanager().registerPeriodicTask('backgroundTracking', 'backgroundTracking', frequency: Duration(hours: 4, minutes: 48));
// Start background tracking at midnight to ensure 5 random samples per day
var now = DateTime.now().toLocal();
var nextMidnight = DateTime(now.year, now.month, now.day + 1);
var timeUntilMidnight = nextMidnight.difference(now);

await Workmanager().registerPeriodicTask(
'scheduleDailyTasks',
'scheduleDailyTasks',
tag: 'scheduleDailyTasks',
frequency: Duration(days: 1),
initialDelay: timeUntilMidnight,
);

runApp(MyApp());
}
Expand All @@ -43,19 +52,12 @@ void callbackDispatcher() {
await Firebase.initializeApp();

switch (task) {
case 'backgroundTracking':
var permission = await Geolocator.checkPermission();
var isBgTrackingEnabled = await UserManager.getTracking();

if (permission == LocationPermission.always && isBgTrackingEnabled){
var position = await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
var battery = Battery();
await ApiSingleton().sendFixes(position.latitude,
position.longitude,
DateTime.now().toUtc().toIso8601String(),
await battery.batteryLevel);
break;
}
case 'trackingTask':
await BackgroundTracking.trackingTask();
break;
case 'scheduleDailyTasks':
await BackgroundTracking.scheduleMultipleTrackingTask(5);
break;
}
return Future.value(true);
});
Expand Down
4 changes: 4 additions & 0 deletions lib/pages/settings_pages/settings_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import 'package:mosquito_alert_app/utils/UserManager.dart';
import 'package:mosquito_alert_app/utils/Utils.dart';
import 'package:mosquito_alert_app/utils/style.dart';
import 'package:package_info/package_info.dart';
import 'package:workmanager/workmanager.dart';

class SettingsPage extends StatefulWidget {
SettingsPage();
Expand Down Expand Up @@ -182,6 +183,9 @@ class _SettingsPageState extends State<SettingsPage> {
setState(() {
isBgTrackingEnabled = trackingStatus;
});
if (!isBgTrackingEnabled){
await Workmanager().cancelByTag('trackingTask');
}
},
),
),
Expand Down
Loading