Skip to content

Commit

Permalink
correct 'view file' processing on android
Browse files Browse the repository at this point in the history
  • Loading branch information
Denis Zhdanov committed Jan 2, 2025
1 parent 90a13b0 commit 1488906
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions lib/file/widget/view_selected_file_widget.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import 'package:chrono_sheet/file/model/google_file.dart';
import 'package:chrono_sheet/file/state/files_state.dart';
import 'package:chrono_sheet/generated/app_localizations.dart';
import 'package:chrono_sheet/logging/logging.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:url_launcher/url_launcher.dart';

final _logger = getNamedLogger();

class ViewSelectedFileWidget extends ConsumerWidget {
const ViewSelectedFileWidget({super.key});

Expand All @@ -13,12 +16,23 @@ class ViewSelectedFileWidget extends ConsumerWidget {
final browserUri = Uri.parse("https://docs.google.com/spreadsheets/d/${file.id}/edit");
final messenger = ScaffoldMessenger.of(context);
final l10n = AppLocalizations.of(context);
if (await canLaunchUrl(appUri)) {
try {
// we experienced that canLaunchUrl() returns 'false' for all urls under android. That is due to the fact
// that by default applications are not allowed to query information about other applications.
// It's possible to overcome that by configuring 'android.intent.action.VIEW' intent action query
// and/or requesting 'android.permission.QUERY_ALL_PACKAGES' permission. However, it looks easier to
// just try to launch and handle an exception (if any).
// More details can be found at https://developer.android.com/training/package-visibility
await launchUrl(appUri);
} else if (await canLaunchUrl(browserUri)) {
await launchUrl(browserUri);
} else {
messenger.showSnackBar(SnackBar(content: Text(l10n.errorCanNotOpenFile)));
return;
} catch (_) {
_logger.info("failed to open the sheet document '${file.name}' in the google sheets application");
try {
await launchUrl(browserUri);
} catch (_) {
_logger.info("failed to open the sheet document '${file.name}' in the browser");
messenger.showSnackBar(SnackBar(content: Text(l10n.errorCanNotOpenFile)));
}
}
}

Expand Down

0 comments on commit 1488906

Please sign in to comment.