Skip to content

Commit

Permalink
RMET-3191 H&F Plugin - Use sensors for background jobs (Activity Reco…
Browse files Browse the repository at this point in the history
…gnition 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
  • Loading branch information
alexgerardojacinto authored Mar 20, 2024
1 parent 858ef9e commit 0785fbc
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 9 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).

Expand Down
1 change: 1 addition & 0 deletions hooks/androidCopyPreferencesPermissions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
6 changes: 2 additions & 4 deletions src/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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()
}
Expand Down

0 comments on commit 0785fbc

Please sign in to comment.