ente/lib/utils/email_util.dart

269 lines
7.4 KiB
Dart
Raw Normal View History

2021-06-12 10:33:24 +00:00
import 'dart:io';
import 'package:archive/archive_io.dart';
2021-05-29 22:48:23 +00:00
import 'package:email_validator/email_validator.dart';
2021-08-09 14:12:42 +00:00
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
2021-06-12 10:33:24 +00:00
import 'package:flutter_email_sender/flutter_email_sender.dart';
2022-03-05 21:22:45 +00:00
import 'package:logging/logging.dart';
2022-07-11 09:35:38 +00:00
import 'package:open_mail_app/open_mail_app.dart';
import 'package:package_info_plus/package_info_plus.dart';
2021-06-12 10:33:24 +00:00
import 'package:path_provider/path_provider.dart';
2022-06-23 04:12:58 +00:00
import 'package:photos/core/configuration.dart';
import 'package:photos/core/error-reporting/super_logging.dart';
import 'package:photos/ente_theme_data.dart';
import 'package:photos/ui/common/dialogs.dart';
2022-07-01 14:39:02 +00:00
import 'package:photos/ui/tools/debug/log_file_viewer.dart';
2021-06-12 10:33:24 +00:00
import 'package:photos/utils/dialog_util.dart';
2022-07-11 09:35:38 +00:00
import 'package:photos/utils/toast_util.dart';
2021-10-05 04:56:48 +00:00
import 'package:share_plus/share_plus.dart';
2022-07-11 09:35:38 +00:00
import 'package:url_launcher/url_launcher.dart';
2021-05-29 22:48:23 +00:00
2022-03-05 21:22:45 +00:00
final Logger _logger = Logger('email_util');
2020-10-09 21:45:07 +00:00
bool isValidEmail(String email) {
2021-05-29 22:48:23 +00:00
return EmailValidator.validate(email);
2020-10-09 21:45:07 +00:00
}
2021-06-12 10:33:24 +00:00
Future<void> sendLogs(
BuildContext context,
2021-08-09 14:12:42 +00:00
String title,
String toEmail, {
Function postShare,
2021-06-12 10:33:24 +00:00
String subject,
String body,
2021-08-09 14:12:42 +00:00
}) async {
2021-08-09 14:35:17 +00:00
final List<Widget> actions = [
TextButton(
child: Row(
mainAxisAlignment: MainAxisAlignment.start,
children: [
Icon(
Icons.feed_outlined,
color: Theme.of(context).iconTheme.color.withOpacity(0.85),
2021-08-09 14:35:17 +00:00
),
2022-07-04 06:02:17 +00:00
const Padding(padding: EdgeInsets.all(4)),
2021-08-09 14:12:42 +00:00
Text(
2022-06-06 13:42:27 +00:00
"View logs",
2021-08-09 14:12:42 +00:00
style: TextStyle(
2022-06-11 08:23:52 +00:00
color: Theme.of(context)
.colorScheme
.defaultTextColor
.withOpacity(0.85),
),
2021-08-09 14:12:42 +00:00
),
],
),
2021-08-09 14:35:17 +00:00
onPressed: () async {
showDialog(
context: context,
builder: (BuildContext context) {
return LogFileViewer(SuperLogging.logFile);
},
barrierColor: Colors.black87,
barrierDismissible: false,
);
},
2021-08-09 14:12:42 +00:00
),
2021-08-09 14:35:17 +00:00
TextButton(
child: Text(
title,
style: TextStyle(
color: Theme.of(context).buttonColor,
),
),
onPressed: () async {
Navigator.of(context, rootNavigator: true).pop('dialog');
await _sendLogs(context, toEmail, subject, body);
if (postShare != null) {
postShare();
}
},
),
];
final List<Widget> content = [];
content.addAll(
[
2022-07-04 06:02:17 +00:00
const Text(
2022-06-23 07:25:45 +00:00
"This will send across logs to help us debug your issue. Please note that file names will be included to help track issues with specific files.",
2021-08-09 14:35:17 +00:00
style: TextStyle(
height: 1.5,
fontSize: 16,
2021-08-09 14:12:42 +00:00
),
2021-08-09 14:35:17 +00:00
),
2022-07-04 06:02:17 +00:00
const Padding(padding: EdgeInsets.all(12)),
2021-08-09 14:35:17 +00:00
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: actions,
2021-08-09 14:12:42 +00:00
),
],
);
2021-08-09 14:35:17 +00:00
final confirmation = AlertDialog(
title: Text(
title,
2022-07-04 06:02:17 +00:00
style: const TextStyle(
2021-08-09 14:35:17 +00:00
fontSize: 18,
),
),
content: SingleChildScrollView(
child: ListBody(
children: content,
),
),
);
2021-08-09 14:12:42 +00:00
showDialog(
context: context,
builder: (_) {
return confirmation;
},
);
}
Future<void> _sendLogs(
2022-06-11 08:23:52 +00:00
BuildContext context,
String toEmail,
String subject,
String body,
) async {
String zipFilePath = await getZippedLogsFile(context);
2021-06-12 10:33:24 +00:00
final Email email = Email(
recipients: [toEmail],
subject: subject,
body: body,
attachmentPaths: [zipFilePath],
isHTML: false,
);
try {
await FlutterEmailSender.send(email);
2022-03-05 21:22:45 +00:00
} catch (e, s) {
_logger.severe('email sender failed', e, s);
await shareLogs(context, toEmail, zipFilePath);
}
}
Future<String> getZippedLogsFile(BuildContext context) async {
2022-06-06 13:42:27 +00:00
final dialog = createProgressDialog(context, "Preparing logs...");
await dialog.show();
2022-06-23 04:12:58 +00:00
final logsPath = (await getApplicationSupportDirectory()).path;
final logsDirectory = Directory(logsPath + "/logs");
final tempPath = (await getTemporaryDirectory()).path;
final zipFilePath =
tempPath + "/logs-${Configuration.instance.getUserID() ?? 0}.zip";
var encoder = ZipFileEncoder();
encoder.create(zipFilePath);
encoder.addDirectory(logsDirectory);
encoder.close();
await dialog.hide();
return zipFilePath;
}
Future<void> shareLogs(
2022-06-11 08:23:52 +00:00
BuildContext context,
String toEmail,
String zipFilePath,
) async {
final result = await showChoiceDialog(
2022-06-11 08:23:52 +00:00
context,
"Email logs",
"Please send the logs to $toEmail",
firstAction: "Copy email",
2022-07-11 10:19:48 +00:00
secondAction: "Export logs",
2022-06-11 08:23:52 +00:00
);
if (result != null && result == DialogUserChoice.firstChoice) {
await Clipboard.setData(ClipboardData(text: toEmail));
2021-06-12 10:33:24 +00:00
}
final Size size = MediaQuery.of(context).size;
2022-06-11 08:23:52 +00:00
await Share.shareFiles(
[zipFilePath],
sharePositionOrigin: Rect.fromLTWH(0, 0, size.width, size.height / 2),
);
2021-06-12 10:33:24 +00:00
}
2022-07-11 09:35:38 +00:00
Future<void> sendEmail(
BuildContext context, {
@required String to,
String subject,
String body,
}) async {
try {
String clientDebugInfo = await clientInfo();
EmailContent email = EmailContent(
to: [
to,
],
subject: subject ?? '[Support]',
body: (body ?? '') + clientDebugInfo,
);
if (Platform.isAndroid) {
// Special handling due to issue in proton mail android client
// https://github.com/ente-io/frame/pull/253
final Uri params = Uri(
scheme: 'mailto',
path: to,
query: 'subject=${email.subject}&body=${email.body}',
);
if (await canLaunchUrl(params)) {
await launchUrl(params);
} else {
// this will trigger _showNoMailAppsDialog
throw Exception('Could not launch ${params.toString()}');
}
} else {
OpenMailAppResult result = await OpenMailApp.composeNewEmailInMailApp(
nativePickerTitle: 'Select email app',
emailContent: email,
);
if (!result.didOpen && !result.canOpen) {
_showNoMailAppsDialog(context, to);
} else if (!result.didOpen && result.canOpen) {
showDialog(
context: context,
builder: (_) => MailAppPickerDialog(
mailApps: result.options,
emailContent: email,
),
);
}
}
} catch (e) {
_logger.severe("Failed to send email to $to", e);
_showNoMailAppsDialog(context, to);
}
}
Future<String> clientInfo() async {
final packageInfo = await PackageInfo.fromPlatform();
String debugInfo = '\n\n\n\n ------------------- \nFollowing information can '
'help us in debugging if you are facing any issue '
'\nRegistered email: ${Configuration.instance.getEmail()}'
'\nClient: ${packageInfo.packageName}'
'\nVersion : ${packageInfo.version}';
return debugInfo;
}
void _showNoMailAppsDialog(BuildContext context, String toEmail) {
showDialog(
context: context,
builder: (context) {
return AlertDialog(
title: Text('Please email us at $toEmail'),
actions: <Widget>[
TextButton(
child: const Text("Copy email"),
onPressed: () async {
await Clipboard.setData(ClipboardData(text: toEmail));
showShortToast(context, 'Copied');
},
),
TextButton(
child: const Text("OK"),
onPressed: () {
Navigator.pop(context);
},
)
],
);
},
);
}