Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding health connect warning message. #320

Merged
merged 13 commits into from
Aug 30, 2024
3 changes: 3 additions & 0 deletions assets/lang/da.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,12 @@
"expired": "udløbet",
"completed": "færdig",
"cancel": "Anuller",
"install": "Installér",
"pages.about.message.read_more": "Tryk for at læse mere",
"pages.about.study.website": "Hjemmeside",
"pages.about.study.privacy": "Fortrolighedspolitik",
"pages.about.install_health_connect.title": "Health Connect påkrævet",
"pages.about.install_health_connect.description": "Denne app kræver, at Google Connect er installeret. Vil du installere det?",
"dialog.location.permission": "Lokaliseringstjeneste",
"dialog.location.allow": "Tillad",
"pages.ic.need_accept": "Du skal acceptere denne informerede samtykke for at kunne deltage i dette studie.",
Expand Down
3 changes: 3 additions & 0 deletions assets/lang/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,12 @@
"expired": "expired",
"completed": "completed",
"cancel": "Cancel",
"install": "Install",
"pages.about.message.read_more": "Tap to read more",
"pages.about.study.website": "Website",
"pages.about.study.privacy": "Privacy policy",
"pages.about.install_health_connect.title": "Health Connect Required",
"pages.about.install_health_connect.description": "This app requires Google Health Connect to be installed. Would you like to install it?",
"dialog.location.permission": "Permission Required",
"dialog.location.allow": "Allow",
"pages.ic.need_accept": "You need to accept the informed consent to participate in this study.",
Expand Down
20 changes: 20 additions & 0 deletions lib/blocs/app_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class StudyAppBLoC extends ChangeNotifier {
List<Message> _messages = [];
final StreamController<int> _messageStreamController =
StreamController.broadcast();
final String _healthConnectPackageName = 'com.google.android.apps.healthdata';

/// The state of this BloC.
StudyAppState get state => _state;
Expand Down Expand Up @@ -84,6 +85,8 @@ class StudyAppBLoC extends ChangeNotifier {
GlobalKey<ScaffoldMessengerState> get scaffoldKey => _scaffoldKey;
State? get scaffoldMessengerState => scaffoldKey.currentState;

String get healthConnectPackageName => _healthConnectPackageName;

/// Create the BLoC for the app.
StudyAppBLoC() : super() {
const dep =
Expand Down Expand Up @@ -151,6 +154,10 @@ class StudyAppBLoC extends ChangeNotifier {
/// The overall data model for this app
CarpStudyAppViewModel get appViewModel => _appViewModel;

final appCheck = AppCheck();

List<AppInfo>? installedApps;

/// Initialize this BLOC. Called before being used for anything.
Future<void> initialize() async {
if (isInitialized) return;
Expand Down Expand Up @@ -178,6 +185,19 @@ class StudyAppBLoC extends ChangeNotifier {
element == ConnectivityResult.wifi);
}

Future<bool> _isHealthConnectInstalled() async {
try {
final apps = await appCheck.getInstalledApps();
if (apps != null) {
return apps.any((app) => app.packageName == _healthConnectPackageName);
}
} catch (e) {
debug("Error checking Health Connect installation: $e");
return false;
}
return false;
}

/// Set the active study in the app based on an [invitation].
///
/// If a [context] is provided, the translation for this study is re-loaded
Expand Down
2 changes: 2 additions & 0 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import 'package:permission_handler/permission_handler.dart';
import 'package:connectivity_plus/connectivity_plus.dart';
import 'package:open_settings_plus/open_settings_plus.dart';
import 'package:open_settings_plus/core/open_settings_plus.dart';
import 'package:appcheck/appcheck.dart';

// the CARP packages
import 'package:carp_serializable/carp_serializable.dart';
Expand Down Expand Up @@ -77,6 +78,7 @@ part 'view_models/user_tasks.dart';
part 'carp_study_app.dart';
part 'ui/pages/informed_consent_page.dart';
part 'ui/pages/home_page.dart';
part 'ui/pages/home_page.install_health_connect_dialog.dart';
part 'ui/carp_study_style.dart';
part 'ui/colors.dart';
part 'ui/pages/data_visualization_page.dart';
Expand Down
10 changes: 5 additions & 5 deletions lib/main.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions lib/ui/pages/home_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,22 @@ class HomePageState extends State<HomePage> {
// - starting sensing
askForLocationPermissions(context)
.then((_) => bloc.configureStudy().then((_) => bloc.start()));

if (Platform.isAndroid) {
// Check if HealthConnect is installed
_checkHealthConnectInstallation();
}
}

Future<void> _checkHealthConnectInstallation() async {
bool isInstalled = await bloc._isHealthConnectInstalled();
if (!isInstalled) {
showDialog<void>(
context: context,
barrierDismissible: true,
builder: (context) => InstallHealthConnectDialog(context),
);
}
}

@override
Expand Down
46 changes: 46 additions & 0 deletions lib/ui/pages/home_page.install_health_connect_dialog.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
part of carp_study_app;

class InstallHealthConnectDialog extends StatelessWidget {
const InstallHealthConnectDialog(BuildContext context, {super.key});

@override
Widget build(BuildContext context) {
RPLocalizations locale = RPLocalizations.of(context)!;
return AlertDialog(
titlePadding: const EdgeInsets.symmetric(vertical: 4),
insetPadding: const EdgeInsets.symmetric(vertical: 24, horizontal: 40),
title: const DialogTitle(
title: "pages.about.install_health_connect.title",
),
content: Text(
locale.translate('pages.about.install_health_connect.description'),
style: aboutCardContentStyle,
textAlign: TextAlign.justify,
),
actions: [
TextButton(
onPressed: () => Navigator.of(context).pop(),
child: Text(locale.translate('cancel')),
),
TextButton(
child: Text(locale.translate('install')),
onPressed: () async {
_redirectToHealthConnectPlayStore();
Navigator.of(context).pop();
},
),
],
);
}

void _redirectToHealthConnectPlayStore() async {
final Uri url = Uri.parse(
'https://play.google.com/store/apps/details?id=${bloc.healthConnectPackageName}');
var canLaunch = await canLaunchUrl(url);
if (canLaunch) {
await launchUrl(url);
} else {
throw 'Could not launch $url';
}
}
}
Loading
Loading