From 0785fbced05f305514b40c64dc4c20e9e85b9197 Mon Sep 17 00:00:00 2001 From: Alexandre Jacinto Date: Wed, 20 Mar 2024 12:17:33 +0000 Subject: [PATCH] RMET-3191 H&F Plugin - Use sensors for background jobs (Activity Recognition Transition API) (#111) * feat: include necessary permissions for API 28 and below Context: To use the Activity Recognition Transition API, we need to include this permission for API 28 (Android 9), and below. More info here: https://developer.android.com/codelabs/activity-recognition-transition#2 References: https://outsystemsrd.atlassian.net/browse/RMET-3191 * feat: declare SCHEDULE_EXACT_ALARM permission in manifest Context: We're using exact alarms to schedule background jobs to run, so we need to declare this permission in the AndroidManifest.xml file of the app. References: https://outsystemsrd.atlassian.net/browse/RMET-3190 * feat: request SCHEDULE_EXACT_ALARM permission when setting background job References: https://outsystemsrd.atlassian.net/browse/RMET-3190 * feat: handle SCHEDULE_EXACT_ALARM permission not being given when setting a background job Context: As for the time being the only way we have to set background jobs to run is using exact alarms, if the permission is not given, then we should return an error. References: https://outsystemsrd.atlassian.net/browse/RMET-3190 * feat: ask for SCHEDULE_EXACT_ALARM permission before other ones References: https://outsystemsrd.atlassian.net/browse/RMET-3190 * feat: get foreground notification info from strings.xml file References: https://outsystemsrd.atlassian.net/browse/RMET-3190 * feat: replace workManager with alarmManager Context: We're now using exact alarms for background jobs, so we need to use the AlarmManager. References: https://outsystemsrd.atlassian.net/browse/RMET-3190 * feat: pass context parameter to AdvancedQuery, as it is needed References: https://outsystemsrd.atlassian.net/browse/RMET-3190 * chore: update dependency to H&F Android library References: https://outsystemsrd.atlassian.net/browse/RMET-3190 * chore: update dependency to H&F Android lib References: https://outsystemsrd.atlassian.net/browse/RMET-3190 * chore: add explanatory comment References: https://outsystemsrd.atlassian.net/browse/RMET-3190 * chore: update changelog References: https://outsystemsrd.atlassian.net/browse/RMET-3190 * feat: only request SCHEDULE_EXACT_ALARM permission if variable requires it Context: We only need the SCHEDULE_EXACT_ALARM permission if we want to use an exact alarm to setup the background job, and we only want to do that for variables that are not activity related (e.g. STEPS), since for those we can use the Activity Recognition Transition API instead, avoiding requesting a somewhat odd permission. References: https://outsystemsrd.atlassian.net/browse/RMET-3191 * fix: properly check if SCHEDULE_EXACT_ALARM permission is necessary References: https://outsystemsrd.atlassian.net/browse/RMET-3191 * refactor: remove unnecessary semicolon References: https://outsystemsrd.atlassian.net/browse/RMET-3191 * feat: use ActivityTransitionHelper from Android library References: https://outsystemsrd.atlassian.net/browse/RMET-3191 * chore: update dependency to H&F Android library References: https://outsystemsrd.atlassian.net/browse/RMET-3191 * feat: include play-services-services-location dependency through Gralde References: https://outsystemsrd.atlassian.net/browse/RMET-3191 * chore: remove unnecessary gradle dependency References: https://outsystemsrd.atlassian.net/browse/RMET-3191 * chore: update changelog References: https://outsystemsrd.atlassian.net/browse/RMET-3191 * chore: update dependency to H&F Android lib References: https://outsystemsrd.atlassian.net/browse/RMET-3191 * chore: update dependency to H&F Android library References: https://outsystemsrd.atlassian.net/browse/RMET-3191 --- CHANGELOG.md | 3 +++ hooks/androidCopyPreferencesPermissions.js | 1 + src/android/build.gradle | 6 ++---- .../plugins/healthfitness/OSHealthFitness.kt | 12 +++++++----- 4 files changed, 13 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ebddeaa6..b56c9b17 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,9 @@ The changes documented here do not include those from the original repository. ## [Unreleased] +## 2024-03-18 +- Implemented the usage of the Activity Transition Recognition API for background jobs (https://outsystemsrd.atlassian.net/browse/RMET-3191). + ## 2024-03-14 - Implemented the usage of exact alarms for background jobs (https://outsystemsrd.atlassian.net/browse/RMET-3190). diff --git a/hooks/androidCopyPreferencesPermissions.js b/hooks/androidCopyPreferencesPermissions.js index 2fecef2b..c272ee9b 100644 --- a/hooks/androidCopyPreferencesPermissions.js +++ b/hooks/androidCopyPreferencesPermissions.js @@ -272,6 +272,7 @@ function addBackgroundJobPermissionsToManifest(configParser, projectRoot, parser // add permissions to XML document addEntryToManifest(manifestXmlDoc, 'android.permission.POST_NOTIFICATIONS') addEntryToManifest(manifestXmlDoc, 'android.permission.ACTIVITY_RECOGNITION') + addEntryToManifest(manifestXmlDoc, 'com.google.android.gms.permission.ACTIVITY_RECOGNITION') // necessary for API 28 and below addEntryToManifest(manifestXmlDoc, 'android.permission.FOREGROUND_SERVICE') addEntryToManifest(manifestXmlDoc, 'android.permission.FOREGROUND_SERVICE_HEALTH') addEntryToManifest(manifestXmlDoc, 'android.permission.HIGH_SAMPLING_RATE_SENSORS') diff --git a/src/android/build.gradle b/src/android/build.gradle index a8b5e498..4edc5a37 100644 --- a/src/android/build.gradle +++ b/src/android/build.gradle @@ -19,13 +19,14 @@ allprojects { dependencies{ implementation("com.google.android.gms:play-services-fitness:20.0.0") implementation("com.google.android.gms:play-services-auth:19.2.0") + implementation("com.google.android.gms:play-services-location:19.0.1") implementation("com.google.code.gson:gson:2.8.8") implementation 'com.google.code.findbugs:jsr305:1.3.9' implementation("com.github.outsystems:oscore-android:1.2.0@aar") implementation("com.github.outsystems:oscordova-android:2.0.1@aar") - implementation("com.github.outsystems:oshealthfitness-android:1.2.0.22@aar") + implementation("com.github.outsystems:oshealthfitness-android:1.2.0.25@aar") implementation("com.github.outsystems:osnotificationpermissions-android:0.0.4@aar") // activity @@ -37,9 +38,6 @@ dependencies{ // health connect sdk implementation "androidx.health.connect:connect-client:1.1.0-alpha07" - // work manager - implementation "androidx.work:work-runtime-ktx:2.9.0" - // compose implementation 'androidx.activity:activity-compose:1.8.2' implementation 'androidx.compose.material3:material3:1.2.0' diff --git a/src/android/com/outsystems/plugins/healthfitness/OSHealthFitness.kt b/src/android/com/outsystems/plugins/healthfitness/OSHealthFitness.kt index 149400c0..7ba0031f 100755 --- a/src/android/com/outsystems/plugins/healthfitness/OSHealthFitness.kt +++ b/src/android/com/outsystems/plugins/healthfitness/OSHealthFitness.kt @@ -33,6 +33,7 @@ class OSHealthFitness : CordovaImplementation() { private lateinit var healthConnectDataManager: HealthConnectDataManager private lateinit var healthConnectHelper: HealthConnectHelper private lateinit var alarmManagerHelper: AlarmManagerHelper + private lateinit var activityTransitionHelper: ActivityTransitionHelper private lateinit var backgroundParameters: BackgroundJobParameters private lateinit var alarmManager: AlarmManager @@ -57,8 +58,9 @@ class OSHealthFitness : CordovaImplementation() { healthConnectRepository = HealthConnectRepository(healthConnectDataManager) healthConnectHelper = HealthConnectHelper() alarmManagerHelper = AlarmManagerHelper() + activityTransitionHelper = ActivityTransitionHelper() healthConnectViewModel = - HealthConnectViewModel(healthConnectRepository, healthConnectHelper, alarmManagerHelper) + HealthConnectViewModel(healthConnectRepository, healthConnectHelper, alarmManagerHelper, activityTransitionHelper) alarmManager = getContext().getSystemService(Context.ALARM_SERVICE) as AlarmManager // get foreground notification title and description from resources (strings.xml) @@ -279,12 +281,12 @@ class OSHealthFitness : CordovaImplementation() { backgroundParameters = gson.fromJson(args.getString(0), BackgroundJobParameters::class.java) //request permission for exact alarms if necessary - if (SDK_INT >= 31 && !alarmManager.canScheduleExactAlarms()) { + if (!Constants.ACTIVITY_VARIABLES.contains(backgroundParameters.variable) && SDK_INT >= 31 && !alarmManager.canScheduleExactAlarms()) { requestingExactAlarmPermission = true // we only need to request this permission if exact alarms need to be used - // when there's another way to schedule background jobs to run, we can avoid this for some variables (e.g. steps) - // we intended to use the Activity Recognition API, but it currently has a bug already reported to Google - getContext().startActivity(Intent(ACTION_REQUEST_SCHEDULE_EXACT_ALARM)); + // when the variable is an activity variable (e.g. steps), + // we use the Activity Recognition Transition API instead of exact alarms. + getContext().startActivity(Intent(ACTION_REQUEST_SCHEDULE_EXACT_ALARM)) } else { // we can move on to other permissions if we don't need to request exact alarm permissions requestBackgroundJobPermissions() }