This commit is contained in:
LLan 2025-01-08 18:06:23 +01:00
parent 42784d94eb
commit c47d5a4113
3 changed files with 12 additions and 16 deletions

View File

@ -104,7 +104,6 @@
<uses-permission android:name="android.permission.health.READ_STEPS"/> <uses-permission android:name="android.permission.health.READ_STEPS"/>
<uses-permission android:name="android.permission.health.WRITE_STEPS"/> <uses-permission android:name="android.permission.health.WRITE_STEPS"/>
<uses-sdk tools:overrideLibrary="androidx.health.connect.client"/> <uses-sdk tools:overrideLibrary="androidx.health.connect.client"/>
<!-- <uses-sdk tools:overrideLibrary="androidx.health.connect.client" /> &lt;!&ndash; Override the library's minSdkVersion &ndash;&gt;-->
<uses-feature <uses-feature
android:name="android.hardware.bluetooth" android:name="android.hardware.bluetooth"

View File

@ -327,7 +327,7 @@ public class SettingsActivity extends AbstractSettingsActivityV2 {
pref = findPreference(GBPrefs.EXPORT_HEALTH_CONNECT_ENABLED); pref = findPreference(GBPrefs.EXPORT_HEALTH_CONNECT_ENABLED);
if (pref != null) { if (pref != null) {
// (I don't know what Kotlin Continuation are used for, but they are needed // (I don't know what Kotlin Continuation are used for, but they are needed
Continuation<Set<String>> continuationString = new Continuation<Set<String>>() { Continuation<Set<String>> continuationString = new Continuation<Set<String>>() {
@NotNull @NotNull
@Override @Override
public CoroutineContext getContext() { public CoroutineContext getContext() {
@ -346,7 +346,7 @@ public class SettingsActivity extends AbstractSettingsActivityV2 {
if ((boolean) exportHealthConnectEnabled) { if ((boolean) exportHealthConnectEnabled) {
if(!grantedPermissions.containsAll(healthConnectUtils.requiredHealthConnectPermissions)) { if(!grantedPermissions.containsAll(healthConnectUtils.requiredHealthConnectPermissions)) {
// If we weren't enabled at some point already, show Permission screen // If we weren't enabled at some point already, show Permission screen
healthConnectUtils.healthConnectLauncherInit().launch(healthConnectUtils.requiredHealthConnectPermissions); healthConnectUtils.activityResultLauncher.launch(healthConnectUtils.requiredHealthConnectPermissions);
} }
} }
return true; return true;

View File

@ -52,22 +52,26 @@ import nodomain.freeyourgadget.gadgetbridge.util.GBPrefs;
public class HealthConnectUtils { public class HealthConnectUtils {
private static final Logger LOG = LoggerFactory.getLogger(HealthConnectUtils.class); private static final Logger LOG = LoggerFactory.getLogger(HealthConnectUtils.class);
private final PreferenceFragmentCompat preferenceFragmentCompat; private final PreferenceFragmentCompat preferenceFragmentCompat;
Class<StepsRecord> stepsRecordJavaClass = StepsRecord.class; public final ActivityResultLauncher<Set<String>> activityResultLauncher;
Class<HeartRateRecord> heartrateRecordJavaClass = HeartRateRecord.class; private final Class<StepsRecord> stepsRecordJavaClass = StepsRecord.class;
KClass<StepsRecord> stepsRecordKClass = JvmClassMappingKt.getKotlinClass(stepsRecordJavaClass); private final Class<HeartRateRecord> heartrateRecordJavaClass = HeartRateRecord.class;
KClass<HeartRateRecord> heartrateRecordKClass = JvmClassMappingKt.getKotlinClass(heartrateRecordJavaClass); private final KClass<StepsRecord> stepsRecordKClass = JvmClassMappingKt.getKotlinClass(stepsRecordJavaClass);
public Set<String> requiredHealthConnectPermissions = Set.of( private final KClass<HeartRateRecord> heartrateRecordKClass = JvmClassMappingKt.getKotlinClass(heartrateRecordJavaClass);
public final Set<String> requiredHealthConnectPermissions = Set.of(
HealthPermission.getReadPermission(stepsRecordKClass), HealthPermission.getReadPermission(stepsRecordKClass),
HealthPermission.getWritePermission(stepsRecordKClass), HealthPermission.getWritePermission(stepsRecordKClass),
HealthPermission.getReadPermission(heartrateRecordKClass), HealthPermission.getReadPermission(heartrateRecordKClass),
HealthPermission.getWritePermission(heartrateRecordKClass) HealthPermission.getWritePermission(heartrateRecordKClass)
); );
public ActivityResultContract<Set<String>, Set<String>> requestPermissionResultContract = PermissionController.createRequestPermissionResultContract(); public ActivityResultContract<Set<String>, Set<String>> requestPermissionResultContract = PermissionController.createRequestPermissionResultContract();
public HealthConnectUtils(PreferenceFragmentCompat fragmentCompat) { public HealthConnectUtils(PreferenceFragmentCompat fragmentCompat) {
preferenceFragmentCompat = fragmentCompat; preferenceFragmentCompat = fragmentCompat;
activityResultLauncher = preferenceFragmentCompat.registerForActivityResult(
requestPermissionResultContract,
this::permissionCallback
);
} }
@SuppressLint("RestrictedApi") @SuppressLint("RestrictedApi")
@ -89,13 +93,6 @@ public class HealthConnectUtils {
} }
} }
public ActivityResultLauncher<Set<String>> healthConnectLauncherInit() {
return preferenceFragmentCompat.registerForActivityResult(
requestPermissionResultContract,
this::permissionCallback
);
}
public HealthConnectClient healthConnectInit(Context context) { public HealthConnectClient healthConnectInit(Context context) {
// First check if we can even use Health Connect // First check if we can even use Health Connect
int availabilityStatus = HealthConnectClient.getSdkStatus(context); int availabilityStatus = HealthConnectClient.getSdkStatus(context);