dariosalvi78 / cordova-plugin-health

Cordova plugin for the HealthKit and Google Fit frameworks
MIT License
176 stars 126 forks source link

Open healthConnect / HealthKit programmatically to see app authorizations #320

Closed BenLaKnet closed 2 months ago

BenLaKnet commented 3 months ago

Hi,

I do not know how is it possible to open health connect to see authorization of my app and change options of read/write.

Many thanks.

dariosalvi78 commented 3 months ago

It's either a separate app or as part of the settings

BenLaKnet commented 3 months ago

I found [com.lampa.startapp](https://github.com/lampaa/com.lampa.startapp)

dariosalvi78 commented 3 months ago

ahh I didn't get the question, you mean how do I programmatically open the HealthConnect settings?

I have no idea, as said, it's either buried in the settings or is installed as a separate app. You will need to send an Intent, but I don't know which one exactly. If you find out, let me know and I can try to add this functionality to the plugin.

dariosalvi78 commented 3 months ago

I suspect it has something to do with this: https://developer.android.com/reference/android/health/connect/HealthConnectManager#ACTION_MANAGE_HEALTH_PERMISSIONS

the issue is that this is supported from API 34 and on, so for earlier versions I would need to find out how this is done through the external app.

BenLaKnet commented 3 months ago

Hi,

I tried this plugin and it is running:

function openHealthSettings() {

    if (cordova.platformId == "android") {

        var pkgName = "com.google.android.apps.healthdata"

        var sApp = startApp.set({
            "package": pkgName,
            "application": pkgName
        });

        sApp.check(
            function (values) {

                //console.log(JSON.stringify(values));
                if (values.versionName) {
                    sApp.start();
                }
                else {
                    cordova.InAppBrowser.open('https://play.google.com/store/apps/details?id=' + pkgName, '_system', 'location=yes');
                }

            },
            function (error) {

                console.log(error);

            }       
        );  

    }
    else if (cordova.platformId == "ios") {

        var sApp = startApp.set("x-apple-health://").start();

    }

}
dariosalvi78 commented 3 months ago

so that starts an Android Intent with "com.google.android.apps.healthdata". Have you tried it on Android 14 ? I was wondering if this is the same on all versions of the OS

BenLaKnet commented 3 months ago

No I just have Android 10 and 13.

BenLaKnet commented 3 months ago

After updating on Android 13, it does not work. I will try to see what happens.

BenLaKnet commented 3 months ago

It triggers an error when Health Connect is not installed. It is running now.

What could you do to use Intent with your plugin and open settings or install health ?

dariosalvi78 commented 2 months ago

launching an Intent in Android is trivial, but you need to identify which intent to launch (is it com.google.android.apps.healthdata ?). If you have time, mabe you can try adding a function that does that?

rengadevigg commented 2 months ago

fun openHealthConnect() { if(healthConnectHandler.isInstalled()) { val intent = Intent(HealthConnectClient.ACTION_HEALTH_CONNECT_SETTINGS) bridge.activity.startActivity(intent) } else { val providerPackageName = "com.google.android.apps.healthdata"; val uriString = "market://details?id=$providerPackageName&url=healthconnect%3A%2F%2Fonboarding" bridge.activity.startActivity( Intent(Intent.ACTION_VIEW).apply { setPackage("com.android.vending") data = Uri.parse(uriString) } ) }

    it works in all versions. 
    that version compatibilit android itself takes care does this help?
dariosalvi78 commented 2 months ago

yes it does! Weirdly, I have it as HealthConnectClient.ACTION_HEALTH_CONNECT_SETTINGS_LEGACY, but it's using androidx. The string corresponds to: "androidx.health.ACTION_HEALTH_CONNECT_SETTINGS". I can add it to the plugin, but I'd need to check if it works on Android 14 too, because there HealthConnect becomes embedded into the OS and is treated slightly differently. In fact, HealthConnectManager.ACTION_MANAGE_HEALTH_PERMISSIONS (which is NOT within androidx, but available from Android 14) corresponds to the string "android.health.connect.action.MANAGE_HEALTH_PERMISSIONS" , which is different.

dariosalvi78 commented 2 months ago

the API has this function which fits 100% with what we need: https://developer.android.com/reference/androidx/health/connect/client/HealthConnectClient#getHealthConnectManageDataIntent(android.content.Context,kotlin.String)

Unfortunately it's not available in AndroidX yet, so I cannot call it at the moment. When we will able to use android 14 as minimum API level, then we will be able to use this API to its full extent.

BenLaKnet commented 2 months ago

When you will publish the new version?

BenLaKnet commented 2 months ago

Help me, please, I am waiting the new version to publish updates on my app. Many thanks.

dariosalvi78 commented 2 months ago

Hi, I am a bit overworked these days, can you maybe test the latest code and confirm that it works on both Android and iOS? Particularly, I would need to check that the openHealthSettings() function works on both. If you tell me it's working for you I can publish it.

BenLaKnet commented 2 months ago

OK many thanks for your answer.

I have to see that.

dariosalvi78 commented 2 months ago

I have tested it and released it, v 3.2.0

BenLaKnet commented 2 months ago

OK, it is running for me.

Thank you very much for your job @dariosalvi78