From 9051d1e7dc4010a116674ee3d66ee6597ef5f4de Mon Sep 17 00:00:00 2001 From: Vishesh Handa Date: Mon, 20 May 2019 14:15:45 +0200 Subject: [PATCH] Add a 'Report Bug' button This also attaches the entire 'adb logcat' in Android. This way, we can hopefully get useful info about why something is not working. This currently breaks the build as we need to migrate to Android X --- .../io/gitjournal/gitjournal/LogDumper.java | 29 +++++++++++++++++++ .../gitjournal/gitjournal/MainActivity.java | 11 +++++++ lib/utils.dart | 9 ++++++ lib/widgets/app_drawer.dart | 27 ++++++++++++++++- pubspec.lock | 7 +++++ pubspec.yaml | 1 + 6 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 android/app/src/main/java/io/gitjournal/gitjournal/LogDumper.java diff --git a/android/app/src/main/java/io/gitjournal/gitjournal/LogDumper.java b/android/app/src/main/java/io/gitjournal/gitjournal/LogDumper.java new file mode 100644 index 00000000..165367f3 --- /dev/null +++ b/android/app/src/main/java/io/gitjournal/gitjournal/LogDumper.java @@ -0,0 +1,29 @@ +package io.gitjournal.gitjournal; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileOutputStream; +import java.io.InputStreamReader; +import java.io.PrintWriter; + +public class LogDumper { + public static void dumpLogs(String filePath) throws Exception { + File file = new File(filePath); + + // Truncate the existing file + PrintWriter pw = new PrintWriter(file); + pw.close(); + + FileOutputStream stream = new FileOutputStream(file, true); + + Process logcat = Runtime.getRuntime().exec(new String[]{"logcat", "-d"}); + BufferedReader br = new BufferedReader(new InputStreamReader(logcat.getInputStream()), 4 * 1024); + String line; + String separator = System.getProperty("line.separator"); + while ((line = br.readLine()) != null) { + stream.write(line.getBytes()); + stream.write(separator.getBytes()); + } + stream.close(); + } +} diff --git a/android/app/src/main/java/io/gitjournal/gitjournal/MainActivity.java b/android/app/src/main/java/io/gitjournal/gitjournal/MainActivity.java index a06f6de5..93570427 100644 --- a/android/app/src/main/java/io/gitjournal/gitjournal/MainActivity.java +++ b/android/app/src/main/java/io/gitjournal/gitjournal/MainActivity.java @@ -227,6 +227,17 @@ public class MainActivity extends FlutterActivity implements MethodCallHandler { result.success(publicKey); return; + } else if (call.method.equals("dumpAppLogs")) { + String filePath = filesDir + "/app-logs.txt"; + + try { + LogDumper.dumpLogs(filePath); + } catch (Exception e) { + e.printStackTrace(); + result.error("FAILED", e.toString(), null); + } + + result.success(filePath); } result.notImplemented(); diff --git a/lib/utils.dart b/lib/utils.dart index 817e9853..0f07bd32 100644 --- a/lib/utils.dart +++ b/lib/utils.dart @@ -1,4 +1,5 @@ import 'package:flutter/material.dart'; +import 'package:flutter/services.dart'; import 'package:package_info/package_info.dart'; import 'app.dart'; @@ -19,6 +20,14 @@ Future getVersionString() async { return versionText; } +/// adb logcat +/// Returns the file path where the logs were dumped +Future dumpAppLogs() async { + const _platform = const MethodChannel('gitjournal.io/git'); + final String logsFilePath = await _platform.invokeMethod('dumpAppLogs'); + return logsFilePath; +} + SnackBar buildUndoDeleteSnackbar( BuildContext context, Note deletedNote, diff --git a/lib/widgets/app_drawer.dart b/lib/widgets/app_drawer.dart index 5ae7d934..0985b384 100644 --- a/lib/widgets/app_drawer.dart +++ b/lib/widgets/app_drawer.dart @@ -1,4 +1,5 @@ import 'package:flutter/material.dart'; +import 'package:flutter_email_sender/flutter_email_sender.dart'; import 'package:journal/analytics.dart'; import 'package:journal/state_container.dart'; import 'package:journal/utils.dart'; @@ -84,7 +85,7 @@ class AppDrawer extends StatelessWidget { onTap: () async { var versionText = await getVersionString(); - var emailAddress = 'gitjournal.io@gmail.com'; + var emailAddress = 'gitjournal.io+feedback@gmail.com'; var subject = 'GitJournal Feedback'; var body = "Hey!\n\nHere are some ways to improve GitJournal - \n \n\nVersion: $versionText"; @@ -98,6 +99,30 @@ class AppDrawer extends StatelessWidget { ); }, ), + ListTile( + leading: Icon(Icons.bug_report, color: textStyle.color), + title: Text('Bug Report', style: textStyle), + onTap: () async { + var versionText = await getVersionString(); + var appLogsFilePath = await dumpAppLogs(); + + final Email email = Email( + body: + "Hey!\n\nI found a bug in GitJournal - \n \n\nVersion: $versionText", + subject: 'GitJournal Bug', + recipients: ['gitjournal.io+bugs@gmail.com'], + attachmentPath: appLogsFilePath, + ); + + await FlutterEmailSender.send(email); + + Navigator.pop(context); + + getAnalytics().logEvent( + name: "drawer_bugreport", + ); + }, + ), ListTile( leading: Icon(Icons.settings, color: textStyle.color), title: Text('Settings', style: textStyle), diff --git a/pubspec.lock b/pubspec.lock index 687a6aad..5f09cb28 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -132,6 +132,13 @@ packages: description: flutter source: sdk version: "0.0.0" + flutter_email_sender: + dependency: "direct main" + description: + name: flutter_email_sender + url: "https://pub.dartlang.org" + source: hosted + version: "2.0.2" flutter_launcher_icons: dependency: "direct dev" description: diff --git a/pubspec.yaml b/pubspec.yaml index cd1e7798..8c8da3bd 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -21,6 +21,7 @@ dependencies: launch_review: ^1.0.1 device_info: ^0.4.0+1 flutter_markdown: ^0.2.0 + flutter_email_sender: ^2.0.2 dev_dependencies: flutter_launcher_icons: "^0.7.0"