From f78b21a97fd58830068f70cac3ee612596cf7a56 Mon Sep 17 00:00:00 2001 From: pacien Date: Sun, 26 Aug 2018 02:55:30 +0200 Subject: Isolate recent crash handler --- .../org/pacien/tincapp/activities/BaseActivity.kt | 22 -------- .../activities/common/RecentCrashHandler.kt | 58 ++++++++++++++++++++++ .../tincapp/activities/start/StartActivity.kt | 4 +- .../tincapp/activities/status/StatusActivity.kt | 4 +- 4 files changed, 64 insertions(+), 24 deletions(-) create mode 100644 app/src/main/java/org/pacien/tincapp/activities/common/RecentCrashHandler.kt (limited to 'app') diff --git a/app/src/main/java/org/pacien/tincapp/activities/BaseActivity.kt b/app/src/main/java/org/pacien/tincapp/activities/BaseActivity.kt index 975d4cf..196ccd3 100644 --- a/app/src/main/java/org/pacien/tincapp/activities/BaseActivity.kt +++ b/app/src/main/java/org/pacien/tincapp/activities/BaseActivity.kt @@ -30,8 +30,6 @@ import kotlinx.android.synthetic.main.base_activity.* import org.pacien.tincapp.R import org.pacien.tincapp.context.App import org.pacien.tincapp.context.AppInfo -import org.pacien.tincapp.context.AppPaths -import org.pacien.tincapp.context.CrashRecorder /** * @author pacien @@ -98,26 +96,6 @@ abstract class BaseActivity : AppCompatActivity() { if (active) super.runOnUiThread(action) } - fun handleRecentCrash() { - if (!CrashRecorder.hasPreviouslyCrashed()) return - CrashRecorder.dismissPreviousCrash() - - AlertDialog.Builder(this) - .setTitle(R.string.crash_modal_title) - .setMessage(listOf( - resources.getString(R.string.crash_modal_message), - resources.getString(R.string.crash_modal_crash_logged, AppPaths.appLogFile().absolutePath) - ).joinToString("\n\n")) - .setNeutralButton(R.string.crash_modal_action_send_report) { _, _ -> - 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 "" }) - } - .setPositiveButton(R.string.generic_action_close) { _, _ -> Unit } - .show() - } - fun inflate(@LayoutRes layout: Int) = layoutInflater.inflate(layout, rootView, false)!! fun inflate(inflateFunc: (LayoutInflater, ViewGroup?, Boolean) -> View) = inflateFunc(layoutInflater, rootView, false) 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 new file mode 100644 index 0000000..82ecc9d --- /dev/null +++ b/app/src/main/java/org/pacien/tincapp/activities/common/RecentCrashHandler.kt @@ -0,0 +1,58 @@ +/* + * Tinc App, an Android binding and user interface for the tinc mesh VPN daemon + * Copyright (C) 2017-2018 Pacien TRAN-GIRARD + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package org.pacien.tincapp.activities.common + +import android.support.v7.app.AlertDialog +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 + +/** + * @author pacien + */ +class RecentCrashHandler(private val parentActivity: BaseActivity) { + private val resources by lazy { parentActivity.resources!! } + + fun handleRecentCrash() { + if (!CrashRecorder.hasPreviouslyCrashed()) return + CrashRecorder.dismissPreviousCrash() + + AlertDialog.Builder(parentActivity) + .setTitle(R.string.crash_modal_title) + .setMessage(makeMessage()) + .setNeutralButton(R.string.crash_modal_action_send_report) { _, _ -> sendReportMail() } + .setPositiveButton(R.string.generic_action_close) { _, _ -> Unit } + .show() + } + + private fun makeMessage() = + listOf( + resources.getString(R.string.crash_modal_message), + resources.getString(R.string.crash_modal_crash_logged, AppPaths.appLogFile().absolutePath) + ).joinToString("\n\n") + + private fun sendReportMail() = + 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 "" } + ) +} diff --git a/app/src/main/java/org/pacien/tincapp/activities/start/StartActivity.kt b/app/src/main/java/org/pacien/tincapp/activities/start/StartActivity.kt index 802115f..0e50060 100644 --- a/app/src/main/java/org/pacien/tincapp/activities/start/StartActivity.kt +++ b/app/src/main/java/org/pacien/tincapp/activities/start/StartActivity.kt @@ -28,6 +28,7 @@ import kotlinx.android.synthetic.main.start_activity.* import org.pacien.tincapp.R import org.pacien.tincapp.activities.BaseActivity import org.pacien.tincapp.activities.common.ProgressModal +import org.pacien.tincapp.activities.common.RecentCrashHandler import org.pacien.tincapp.activities.configure.ConfigureActivity import org.pacien.tincapp.activities.status.StatusActivity import org.pacien.tincapp.intent.Actions @@ -40,6 +41,7 @@ import org.pacien.tincapp.service.TincVpnService class StartActivity : BaseActivity() { val permissionRequestCode = 0 private val connectionStarter by lazy { ConnectionStarter(this) } + private val recentCrashHandler by lazy { RecentCrashHandler(this) } private val broadcastMapper = BroadcastMapper(mapOf( Actions.EVENT_CONNECTED to this::onVpnStart, Actions.EVENT_ABORTED to this::onVpnStartError @@ -75,7 +77,7 @@ class StartActivity : BaseActivity() { super.onResume() if (TincVpnService.isConnected()) openStatusActivity(false) broadcastMapper.register() - handleRecentCrash() + recentCrashHandler.handleRecentCrash() } override fun onPause() { diff --git a/app/src/main/java/org/pacien/tincapp/activities/status/StatusActivity.kt b/app/src/main/java/org/pacien/tincapp/activities/status/StatusActivity.kt index 152ed83..cebbc16 100644 --- a/app/src/main/java/org/pacien/tincapp/activities/status/StatusActivity.kt +++ b/app/src/main/java/org/pacien/tincapp/activities/status/StatusActivity.kt @@ -28,6 +28,7 @@ import org.pacien.tincapp.R import org.pacien.tincapp.activities.BaseActivity import org.pacien.tincapp.activities.common.FragmentListPagerAdapter import org.pacien.tincapp.activities.common.ProgressModal +import org.pacien.tincapp.activities.common.RecentCrashHandler import org.pacien.tincapp.activities.start.StartActivity import org.pacien.tincapp.activities.status.networkinfo.NetworkInfoFragment import org.pacien.tincapp.activities.status.nodes.NodeListFragment @@ -41,6 +42,7 @@ import org.pacien.tincapp.service.TincVpnService * @author pacien */ class StatusActivity : BaseActivity() { + private val recentCrashHandler by lazy { RecentCrashHandler(this) } private val vpnService by lazy { TincVpnService } private val netName by lazy { vpnService.getCurrentNetName() } private val pagerAdapter by lazy { FragmentListPagerAdapter(pages, supportFragmentManager) } @@ -72,7 +74,7 @@ class StatusActivity : BaseActivity() { if (!TincVpnService.isConnected()) openStartActivity() broadcastMapper.register() - handleRecentCrash() + recentCrashHandler.handleRecentCrash() } override fun onPause() { -- cgit v1.2.3