Skip to content

Commit

Permalink
RMET-3046 H&F Cordova Plugin - Migrate RequestPermissions to HealthCo…
Browse files Browse the repository at this point in the history
…nnect (#96)

* feat: migrate requestPermissions to Health Connect

References: https://outsystemsrd.atlassian.net/browse/RMET-3046

* feat: pass all necessary parameters for requestPermissions

References: https://outsystemsrd.atlassian.net/browse/RMET-3046

* refactor: call setAsActivityResultCallback before anything else

References: https://outsystemsrd.atlassian.net/browse/RMET-3046

* feat: update bridge according to new lib version

References: https://outsystemsrd.atlassian.net/browse/RMET-3046

* chore: update dependency version

References: https://outsystemsrd.atlassian.net/browse/RMET-3046

* refactor: inline parameters for permissions

References: https://outsystemsrd.atlassian.net/browse/RMET-3046

* refactor: avoid using unnecessary variable

References: https://outsystemsrd.atlassian.net/browse/RMET-3046

* chore: update changelog

References: https://outsystemsrd.atlassian.net/browse/RMET-3046

* chore: fix date on changelog entry

References: https://outsystemsrd.atlassian.net/browse/RMET-3046

* refactor: remove empty lines

References: https://outsystemsrd.atlassian.net/browse/RMET-3046
  • Loading branch information
alexgerardojacinto committed Apr 9, 2024
1 parent d5a2476 commit a9decc1
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 46 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

The changes documented here do not include those from the original repository.

## [Unreleased]

## 2024-02-01
- Re-implemented RequestPermissions feature (https://outsystemsrd.atlassian.net/browse/RMET-3046).

## [Version 1.4.0]

## 2023-08-25
Expand Down
2 changes: 1 addition & 1 deletion src/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ dependencies{

implementation("com.github.outsystems:oscore-android:1.2.0@aar")
implementation("com.github.outsystems:oscordova-android:1.2.0@aar")
implementation("com.github.outsystems:oshealthfitness-android:1.2.0@aar")
implementation("com.github.outsystems:oshealthfitness-android:1.2.0.4@aar")
implementation("com.github.outsystems:osnotificationpermissions-android:0.0.4@aar")

def roomVersion = "2.4.2"
Expand Down
129 changes: 84 additions & 45 deletions src/android/com/outsystems/plugins/healthfitness/OSHealthFitness.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,40 @@ import com.outsystems.plugins.healthfitness.store.*
import com.outsystems.plugins.oscordova.CordovaImplementation
import org.apache.cordova.*
import org.json.JSONArray
import org.json.JSONException

class OSHealthFitness : CordovaImplementation() {
override var callbackContext: CallbackContext? = null

var healthStore: HealthStoreInterface? = null
val gson by lazy { Gson() }
var notificationPermissions = OSNotificationPermissions()
lateinit var healthConnectViewModel: HealthConnectViewModel
lateinit var healthConnectRepository: HealthConnectRepository
lateinit var healthConnectDataManager: HealthConnectDataManager
lateinit var healthConnectHelper: HealthConnectHelper

override fun initialize(cordova: CordovaInterface, webView: CordovaWebView) {
super.initialize(cordova, webView)
val manager = HealthFitnessManager(cordova.context, cordova.activity)
val database = DatabaseManager(cordova.context)
healthStore = HealthStore(cordova.context.applicationContext.packageName, manager, database)

healthConnectDataManager = HealthConnectDataManager()
healthConnectRepository = HealthConnectRepository(healthConnectDataManager)
healthConnectHelper = HealthConnectHelper()
healthConnectViewModel = HealthConnectViewModel(healthConnectRepository, healthConnectHelper)
}

override fun execute(
action: String,
args: JSONArray,
callbackContext: CallbackContext
): Boolean {

this.callbackContext = callbackContext

if(!areGooglePlayServicesAvailable()) {
if (!areGooglePlayServicesAvailable()) {
return false;
}

Expand Down Expand Up @@ -75,58 +86,75 @@ class OSHealthFitness : CordovaImplementation() {
return true
}

//create array of permission oauth
private fun initAndRequestPermissions(args : JSONArray) {
val customPermissions = args.getString(0)
val allVariables = args.getString(1)
val fitnessVariables = args.getString(2)
val healthVariables = args.getString(3)
val profileVariables = args.getString(4)
val summaryVariables = args.getString(5)

private fun initAndRequestPermissions(args: JSONArray) {
try {
healthStore?.initAndRequestPermissions(
customPermissions,
allVariables,
fitnessVariables,
healthVariables,
profileVariables,
summaryVariables)
checkAndGrantPermissions()
}
catch (hse : HealthStoreException) {
healthConnectViewModel.initAndRequestPermissions(
getActivity(),
gson.fromJson(args.getString(0), Array<HealthFitnessPermission>::class.java),
gson.fromJson(args.getString(1), HealthFitnessGroupPermission::class.java),
gson.fromJson(args.getString(2), HealthFitnessGroupPermission::class.java),
gson.fromJson(args.getString(3), HealthFitnessGroupPermission::class.java),
gson.fromJson(args.getString(4), HealthFitnessGroupPermission::class.java),
{
setAsActivityResultCallback()
},
{
sendPluginResult(null, Pair(it.code.toString(), it.message))
}
)
} catch (hse: HealthStoreException) {
sendPluginResult(null, Pair(hse.error.code.toString(), hse.error.message))
} catch (e: JSONException) {
sendPluginResult(
null,
Pair(
HealthFitnessError.PARSING_PARAMETERS_ERROR.code.toString(),
HealthFitnessError.PARSING_PARAMETERS_ERROR.message
)
)
} catch (e: Exception) {
sendPluginResult(
null,
Pair(
HealthFitnessError.REQUEST_PERMISSIONS_GENERAL_ERROR.code.toString(),
HealthFitnessError.REQUEST_PERMISSIONS_GENERAL_ERROR.message
)
)
}
}


private fun areAndroidPermissionsGranted(permissions: List<String>): Boolean {
permissions.forEach {
if (ContextCompat.checkSelfPermission(cordova.activity, it) != PackageManager.PERMISSION_GRANTED) {
if (ContextCompat.checkSelfPermission(
getActivity(),
it
) != PackageManager.PERMISSION_GRANTED
) {
return false
}
}
return true
}

private fun checkAndGrantPermissions(){
private fun checkAndGrantPermissions() {
val permissions = mutableListOf(
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.BODY_SENSORS
)

if(SDK_INT >= Build.VERSION_CODES.Q) {
if (SDK_INT >= Build.VERSION_CODES.Q) {
permissions.add(Manifest.permission.ACTIVITY_RECOGNITION)
}

if (areAndroidPermissionsGranted(permissions)) {
if(!healthStore!!.areGoogleFitPermissionsGranted()){
if (!healthStore!!.areGoogleFitPermissionsGranted()) {
setAsActivityResultCallback()
}
if(healthStore?.requestGoogleFitPermissions() == true) {
if (healthStore?.requestGoogleFitPermissions() == true) {
sendPluginResult("success")
}
}
else {
} else {
PermissionHelper.requestPermissions(
this,
ACTIVITY_LOCATION_PERMISSIONS_REQUEST_CODE,
Expand All @@ -135,7 +163,7 @@ class OSHealthFitness : CordovaImplementation() {
}
}

private fun advancedQuery(args : JSONArray) {
private fun advancedQuery(args: JSONArray) {
val parameters = gson.fromJson(args.getString(0), AdvancedQueryParameters::class.java)
healthStore?.advancedQueryAsync(
parameters,
Expand Down Expand Up @@ -183,8 +211,11 @@ class OSHealthFitness : CordovaImplementation() {
}

private fun setBackgroundJob(args: JSONArray) {
notificationPermissions.requestNotificationPermission(this, ACTIVITY_NOTIFICATION_PERMISSIONS_REQUEST_CODE)

notificationPermissions.requestNotificationPermission(
this,
ACTIVITY_NOTIFICATION_PERMISSIONS_REQUEST_CODE
)

//process parameters
val parameters = gson.fromJson(args.getString(0), BackgroundJobParameters::class.java)
healthStore?.setBackgroundJob(
Expand All @@ -210,7 +241,7 @@ class OSHealthFitness : CordovaImplementation() {
}
)
}

private fun listBackgroundJobs() {
healthStore?.listBackgroundJobs(
{ response ->
Expand All @@ -235,7 +266,7 @@ class OSHealthFitness : CordovaImplementation() {
}
)
}

private fun disconnectFromGoogleFit() {
healthStore?.disconnectFromGoogleFit(
{
Expand All @@ -248,27 +279,34 @@ class OSHealthFitness : CordovaImplementation() {
}

override fun onActivityResult(requestCode: Int, resultCode: Int, intent: Intent) {
//super.onActivityResult(requestCode, resultCode, intent)
try {
healthStore?.handleActivityResult(requestCode, resultCode, intent)
}
catch(hse : HealthStoreException) {
val error = hse.error
sendPluginResult(null, Pair(error.code.toString(), error.message))
}
super.onActivityResult(requestCode, resultCode, intent)
healthConnectViewModel.handleActivityResult(requestCode, resultCode, intent,
{
sendPluginResult("success", null)
},
{
sendPluginResult(null, Pair(it.code.toString(), it.message))
}
)
}

override fun areGooglePlayServicesAvailable(): Boolean {
val googleApiAvailability = GoogleApiAvailability.getInstance()
val status = googleApiAvailability.isGooglePlayServicesAvailable(cordova.activity)
val status = googleApiAvailability.isGooglePlayServicesAvailable(getActivity())

if (status != ConnectionResult.SUCCESS) {
var result: Pair<String, String>? = null
result = if (googleApiAvailability.isUserResolvableError(status)) {
googleApiAvailability.getErrorDialog(cordova.activity, status, 1)?.show()
Pair(HealthFitnessError.GOOGLE_SERVICES_RESOLVABLE_ERROR.code.toString(), HealthFitnessError.GOOGLE_SERVICES_RESOLVABLE_ERROR.message)
googleApiAvailability.getErrorDialog(getActivity(), status, 1)?.show()
Pair(
HealthFitnessError.GOOGLE_SERVICES_RESOLVABLE_ERROR.code.toString(),
HealthFitnessError.GOOGLE_SERVICES_RESOLVABLE_ERROR.message
)
} else {
Pair(HealthFitnessError.GOOGLE_SERVICES_ERROR.code.toString(), HealthFitnessError.GOOGLE_SERVICES_ERROR.message)
Pair(
HealthFitnessError.GOOGLE_SERVICES_ERROR.code.toString(),
HealthFitnessError.GOOGLE_SERVICES_ERROR.message
)
}
sendPluginResult(null, result)
return false
Expand All @@ -279,7 +317,8 @@ class OSHealthFitness : CordovaImplementation() {
override fun onRequestPermissionResult(
requestCode: Int,
permissions: Array<String>,
grantResults: IntArray) {
grantResults: IntArray
) {
when (requestCode) {
ACTIVITY_LOCATION_PERMISSIONS_REQUEST_CODE -> {
// If request is cancelled, the result arrays are empty.
Expand Down

0 comments on commit a9decc1

Please sign in to comment.