diff --git a/app/build.gradle b/app/build.gradle index 164990d8..1a857a75 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -39,6 +39,8 @@ android { } dependencies { + def acraVersion = '5.1.3' + implementation fileTree(include: ['*.jar'], dir: 'libs') implementation project(':plugin') implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" @@ -57,6 +59,8 @@ dependencies { implementation 'de.blox:graphview:0.6.0' implementation 'com.zeugmasolutions.localehelper:locale-helper-android:1.0.2' implementation 'com.github.daniel-stoneuk:material-about-library:2.4.2' + implementation "ch.acra:acra-core:$acraVersion" + implementation "ch.acra:acra-toast:$acraVersion" testImplementation 'junit:junit:4.12' testImplementation 'org.json:json:20160810' androidTestImplementation 'androidx.test:runner:1.1.1' diff --git a/app/src/main/java/ryey/easer/EaserApplication.java b/app/src/main/java/ryey/easer/EaserApplication.java index bfefe2e1..6e2f0df5 100644 --- a/app/src/main/java/ryey/easer/EaserApplication.java +++ b/app/src/main/java/ryey/easer/EaserApplication.java @@ -25,6 +25,7 @@ import android.content.Intent; import android.content.pm.PackageManager; import android.content.res.Configuration; +import android.os.Environment; import android.preference.PreferenceManager; import androidx.core.content.ContextCompat; @@ -34,10 +35,21 @@ import com.orhanobut.logger.Logger; import com.zeugmasolutions.localehelper.LocaleHelperApplicationDelegate; +import org.acra.ACRA; +import org.acra.annotation.AcraCore; +import org.acra.annotation.AcraToast; + +import java.io.File; + import ryey.easer.core.log.ActivityLogService; +@AcraCore(buildConfigClass = BuildConfig.class, + reportSenderFactoryClasses = ErrorSenderFactory.class) +@AcraToast(resText=R.string.prompt_error_logged) public class EaserApplication extends Application { + static final String LOG_DIR = new File(Environment.getExternalStorageDirectory(), "/logger/error").getAbsolutePath(); + private final LocaleHelperApplicationDelegate localeAppDelegate = new LocaleHelperApplicationDelegate(); @Override @@ -71,5 +83,6 @@ public void onConfigurationChanged(Configuration newConfig) { @Override protected void attachBaseContext(Context base) { super.attachBaseContext(localeAppDelegate.attachBaseContext(base)); + ACRA.init(this); } } diff --git a/app/src/main/java/ryey/easer/ErrorSender.kt b/app/src/main/java/ryey/easer/ErrorSender.kt new file mode 100644 index 00000000..8271c2e3 --- /dev/null +++ b/app/src/main/java/ryey/easer/ErrorSender.kt @@ -0,0 +1,63 @@ +/* + * Copyright (c) 2016 - 2019 Rui Zhao + * + * This file is part of Easer. + * + * Easer 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. + * + * Easer 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 Easer. If not, see . + */ + +package ryey.easer + +import android.content.Context +import org.acra.ReportField +import org.acra.data.CrashReportData +import org.acra.sender.ReportSender +import java.io.File +import java.io.FileWriter +import java.text.SimpleDateFormat +import java.util.* + +class ErrorSender : ReportSender { + override fun send(context: Context, errorContent: CrashReportData) { + if (SettingsHelper.logging(context)) { + val dir = File(EaserApplication.LOG_DIR) + if (!dir.exists()) { + dir.mkdirs() + } + val dateFormat = SimpleDateFormat("yyyy_MM_dd_HH_mm_ss") + val date = Date() + val filename = dateFormat.format(date) + ".log" + val reportFile = File(dir, filename) + FileWriter(reportFile).use { + for (elem in FIELDS) { + it.append("%s: %s\n".format(elem, errorContent.getString(elem))) + } + } + + } + } + + companion object { + val FIELDS = arrayOf( + ReportField.BUILD_CONFIG, + ReportField.APP_VERSION_CODE, + ReportField.USER_CRASH_DATE, + ReportField.ANDROID_VERSION, + ReportField.BRAND, + ReportField.PHONE_MODEL, + ReportField.PRODUCT, + ReportField.STACK_TRACE + ) + } +} \ No newline at end of file diff --git a/app/src/main/java/ryey/easer/ErrorSenderFactory.kt b/app/src/main/java/ryey/easer/ErrorSenderFactory.kt new file mode 100644 index 00000000..e189ccba --- /dev/null +++ b/app/src/main/java/ryey/easer/ErrorSenderFactory.kt @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2016 - 2019 Rui Zhao + * + * This file is part of Easer. + * + * Easer 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. + * + * Easer 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 Easer. If not, see . + */ + +package ryey.easer + +import android.content.Context +import org.acra.config.CoreConfiguration +import org.acra.sender.ReportSender +import org.acra.sender.ReportSenderFactory + +class ErrorSenderFactory : ReportSenderFactory { + override fun create(context: Context, config: CoreConfiguration): ReportSender { + return ErrorSender() + } +} \ No newline at end of file diff --git a/app/src/main/res/values-zh/strings.xml b/app/src/main/res/values-zh/strings.xml index 1eb478a5..397b4fcb 100644 --- a/app/src/main/res/values-zh/strings.xml +++ b/app/src/main/res/values-zh/strings.xml @@ -162,5 +162,6 @@ 捐助 個人主頁 源代碼 + Easer記錄了一次錯誤。請查看日誌目錄下的error子目錄。 diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index d4a7b350..09ae1ff3 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -96,6 +96,7 @@ Deletion failed. Check if this item is being used. Prevented saving data with invalid field(s). Please recheck your choices / inputs. Grant Easer <%s> permission for this to work.. + Easer just recorded an error. See the "error" subdirectory of log directory. Ease your life by automating routine actions on your device(s). \nChain different events and let their corresponding actions performed. No longer need to keep them in mind!