From 4c1144bc103a74d8fbdef79b3eb6c69c3e723b83 Mon Sep 17 00:00:00 2001 From: pacien Date: Sun, 26 Aug 2018 04:16:20 +0200 Subject: Limit email report log snippet size --- .../tincapp/activities/common/RecentCrashHandler.kt | 16 +++++++++++++++- app/src/main/java/org/pacien/tincapp/context/App.kt | 7 +++---- 2 files changed, 18 insertions(+), 5 deletions(-) (limited to 'app/src/main/java/org') diff --git a/app/src/main/java/org/pacien/tincapp/activities/common/RecentCrashHandler.kt b/app/src/main/java/org/pacien/tincapp/activities/common/RecentCrashHandler.kt index 82ecc9d..2bdbcca 100644 --- a/app/src/main/java/org/pacien/tincapp/activities/common/RecentCrashHandler.kt +++ b/app/src/main/java/org/pacien/tincapp/activities/common/RecentCrashHandler.kt @@ -19,17 +19,20 @@ package org.pacien.tincapp.activities.common import android.support.v7.app.AlertDialog +import org.apache.commons.io.input.ReversedLinesFileReader import org.pacien.tincapp.R import org.pacien.tincapp.activities.BaseActivity import org.pacien.tincapp.context.App import org.pacien.tincapp.context.AppPaths import org.pacien.tincapp.context.CrashRecorder +import java.io.File /** * @author pacien */ class RecentCrashHandler(private val parentActivity: BaseActivity) { private val resources by lazy { parentActivity.resources!! } + private val maxLines = 1000 fun handleRecentCrash() { if (!CrashRecorder.hasPreviouslyCrashed()) return @@ -53,6 +56,17 @@ class RecentCrashHandler(private val parentActivity: BaseActivity) { App.sendMail( resources.getString(R.string.crash_modal_dev_email), listOf(R.string.app_name, R.string.crash_modal_title).joinToString(" / ", transform = resources::getString), - AppPaths.appLogFile().let { if (it.exists()) it.readText() else "" } + AppPaths.appLogFile().let { if (it.exists()) it.readLastLines(maxLines) else "" } ) + + private fun File.readLastLines(n: Int): String { + val reader = ReversedLinesFileReader(this, Charsets.UTF_8) + val lastLines = generateSequence(reader::readLine) + .takeWhile { line: String? -> line != null } + .take(n) + .toList() + .asReversed() + + return lastLines.joinToString("\n") + } } diff --git a/app/src/main/java/org/pacien/tincapp/context/App.kt b/app/src/main/java/org/pacien/tincapp/context/App.kt index 6f28bd0..cf12dda 100644 --- a/app/src/main/java/org/pacien/tincapp/context/App.kt +++ b/app/src/main/java/org/pacien/tincapp/context/App.kt @@ -28,7 +28,6 @@ import android.support.annotation.StringRes import org.pacien.tincapp.BuildConfig import org.pacien.tincapp.R import org.slf4j.LoggerFactory -import java.io.File /** * @author pacien @@ -71,12 +70,12 @@ class App : Application() { appContext?.startActivity(chooser.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)) } - fun sendMail(recipient: String, subject: String, body: String? = null, attachment: File? = null) { + // https://developer.android.com/guide/components/intents-common#Email + fun sendMail(recipient: String, subject: String, body: String) { val intent = Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:")) .putExtra(Intent.EXTRA_EMAIL, arrayOf(recipient)) .putExtra(Intent.EXTRA_SUBJECT, subject) - .apply { if (body != null) putExtra(Intent.EXTRA_TEXT, body) } - .apply { if (attachment != null) putExtra(Intent.EXTRA_STREAM, Uri.fromFile(attachment)) } + .putExtra(Intent.EXTRA_TEXT, body) val chooser = Intent.createChooser(intent, getResources().getString(R.string.crash_modal_action_send_email)) appContext?.startActivity(chooser.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)) -- cgit v1.2.3