1
0
mirror of https://github.com/dzeiocom/OpenHealth.git synced 2025-04-23 03:12:14 +00:00

feat: Add Error handling

This commit is contained in:
Florian Bouillon 2022-07-19 12:01:41 +02:00
parent 375bad46a7
commit a31d85b1fb
6 changed files with 246 additions and 39 deletions

View File

@ -44,6 +44,10 @@
</intent-filter>
</activity>
<!-- Activity for error handling -->
<activity android:name=".ui.ErrorActivity"
android:exported="false" />
<service
android:name=".services.OpenHealthService"
android:permission="android.permission.ACTIVITY_RECOGNITION" />

View File

@ -1,39 +1,57 @@
package com.dzeio.openhealth
import android.app.Application
import android.content.SharedPreferences
import androidx.preference.PreferenceManager
import com.google.android.material.color.DynamicColors
import dagger.hilt.android.HiltAndroidApp
import java.util.Locale
@HiltAndroidApp
class Application : Application() {
companion object {
const val TAG = "OpenHealth"
}
override fun onCreate() {
super.onCreate()
// Android Dynamics Colors
DynamicColors.applyToActivitiesIfAvailable(this)
// Change application Language based on setting
val preferences: SharedPreferences = PreferenceManager.getDefaultSharedPreferences(this)
val lang = preferences.getString("global_language", Locale.getDefault().language)
val locale = Locale(lang)
Locale.setDefault(locale)
// val overrideConfiguration = baseContext.resources.configuration
// overrideConfiguration.locale = locale
// val context: Context = createConfigurationContext(overrideConfiguration)
// val resources: Resources = context.resources
}
/**
* SharedPreferences Key
*/
object Settings {
}
}
package com.dzeio.openhealth
import android.app.Application
import android.content.Intent
import android.content.SharedPreferences
import android.util.Log
import androidx.preference.PreferenceManager
import com.dzeio.openhealth.ui.ErrorActivity
import com.google.android.material.color.DynamicColors
import dagger.hilt.android.HiltAndroidApp
import java.util.Locale
@HiltAndroidApp
class Application : Application() {
companion object {
const val TAG = "OpenHealth"
}
override fun onCreate() {
super.onCreate()
// Application Error Handling
Thread.setDefaultUncaughtExceptionHandler { paramThread, paramThrowable ->
//Log error to logcat if it wasn't done before
Log.e(TAG, "Error, ${paramThread.id}", paramThrowable)
// send use to the Error Activity
val intent = Intent(applicationContext, ErrorActivity::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
intent.putExtra("error", paramThrowable.stackTraceToString())
startActivity(intent)
return@setDefaultUncaughtExceptionHandler
}
// Android Dynamics Colors
DynamicColors.applyToActivitiesIfAvailable(this)
// Change application Language based on setting
val preferences: SharedPreferences = PreferenceManager.getDefaultSharedPreferences(this)
val lang = preferences.getString("global_language", Locale.getDefault().language)
val locale = Locale(lang)
Locale.setDefault(locale)
// val overrideConfiguration = baseContext.resources.configuration
// overrideConfiguration.locale = locale
// val context: Context = createConfigurationContext(overrideConfiguration)
// val resources: Resources = context.resources
}
/**
* SharedPreferences Key
*/
object Settings {
}
}

View File

@ -0,0 +1,90 @@
package com.dzeio.openhealth.ui
import android.content.ActivityNotFoundException
import android.content.Intent
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.view.LayoutInflater
import android.widget.Toast
import com.dzeio.openhealth.Application
import com.dzeio.openhealth.BuildConfig
import com.dzeio.openhealth.core.BaseActivity
import com.dzeio.openhealth.databinding.ActivityErrorBinding
import dagger.hilt.android.AndroidEntryPoint
@AndroidEntryPoint
class ErrorActivity : BaseActivity<ActivityErrorBinding>() {
companion object {
const val TAG = "${Application.TAG}/ErrorActivity"
}
override val bindingInflater: (LayoutInflater) -> ActivityErrorBinding =
ActivityErrorBinding::inflate
override fun onCreated(savedInstanceState: Bundle?) {
super.onCreated(savedInstanceState)
val params = intent
val data = params.getStringExtra("error")
// Get Application datas
val deviceToReport = if (Build.DEVICE.contains(Build.MANUFACTURER)) Build.DEVICE else "${Build.MANUFACTURER} ${Build.DEVICE}"
val infos = """
Application ID: ${BuildConfig.APPLICATION_ID}
Version Code: ${BuildConfig.VERSION_CODE}
Version Name: ${BuildConfig.VERSION_NAME}
Android ${Build.VERSION.RELEASE} SDK ${Build.VERSION.SDK_INT}
Model: $deviceToReport ${Build.MODEL}
""".trimIndent()
// merge our texts
val reportText = infos + "\n\n" + data
// put it in the textView
binding.errorText.text = reportText
// Handle the Quit button
binding.errorQuit.setOnClickListener {
finish()
}
// Handle the Email Button
binding.errorSubmitEmail.setOnClickListener {
// Create Intent
val intent = Intent(Intent.ACTION_SEND)
intent.data = Uri.parse("mailto:")
intent.type = "text/plain"
intent.putExtra(Intent.EXTRA_EMAIL, arrayOf("report.openhealth@dzeio.com"))
intent.putExtra(Intent.EXTRA_SUBJECT, "Error report for application crash")
intent.putExtra(Intent.EXTRA_TEXT, "Send Report Email\n$reportText")
try {
startActivity(Intent.createChooser(intent, "Send Report Email..."))
} catch (e: ActivityNotFoundException) {
Toast.makeText(this, "Not Email client found :(", Toast.LENGTH_LONG).show()
}
}
// Handle the GitHub Button
binding.errorSubmitGithub.setOnClickListener {
// Build URL
val url = "https://github.com/dzeiocom/OpenHealth/issues/new?title=Application Error&body=$reportText"
try {
startActivity(
Intent(Intent.ACTION_VIEW, Uri.parse(url))
)
} catch (e: ActivityNotFoundException) {
Toast.makeText(this, "No Web Browser found :(", Toast.LENGTH_LONG).show()
}
}
}
}

View File

@ -0,0 +1,80 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView3"
style="?textAppearanceHeadline5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/error_app_crash"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="@string/error_app_report"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView3" />
<ScrollView
android:id="@+id/scrollView2"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="16dp"
android:layout_marginBottom="16dp"
app:layout_constraintBottom_toTopOf="@+id/error_submit_email"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView4">
<TextView
android:id="@+id/error_text"
android:layout_width="match_parent"
style="?textAppearanceCaption"
android:layout_height="wrap_content" />
</ScrollView>
<Button
android:id="@+id/error_submit_github"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/report_github"
android:layout_marginStart="16dp"
app:layout_constraintBaseline_toBaselineOf="@+id/error_submit_email"
app:layout_constraintStart_toStartOf="parent" />
<Button
android:id="@+id/error_submit_email"
android:layout_marginBottom="16dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:text="@string/report_email"
app:layout_constraintBottom_toTopOf="@+id/error_quit"
app:layout_constraintEnd_toEndOf="parent" />
<Button
android:id="@+id/error_quit"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:text="@string/quit"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

View File

@ -39,5 +39,12 @@
<string name="edit_goal">Modifier l\'objectif</string>
<string name="edit_daily_goal">Modifier le but journalier</string>
<string name="permission_declined">Vous avez décliné une permission, vous ne pouvez pas utiliser cette extension suaf si vous réactivez la permission manuellement</string>
<string name="menu_steps">Pas</string>
<!-- Error Activity Translations -->
<string name="error_app_crash">Une Erreur est survenu lors de l\'utilisation de l\'application</string>
<string name="error_app_report">Merci d\'envoyer le rapport d\'erreur afin que nous puissions améliorer votre experience</string>
<string name="report_email">Envoyer par Email</string>
<string name="report_github">Envoyer depuis Github</string>
<string name="quit">Quitter</string>
</resources>

View File

@ -51,4 +51,12 @@
<string name="permission_declined">You declined a permission, you can\'t use this extension unless you enable it manually</string>
<string name="edit_daily_goal">Modifiy daily goal</string>
<string name="menu_steps">Steps</string>
<!-- Error Activity Translations -->
<string name="error_app_crash">An Error Occurred in the application forcing it to close down</string>
<string name="error_app_report">Please report it so we can enhance you\'re Android OpenHealth experience</string>
<string name="report_github">Report on Github</string>
<string name="report_email">Report via Email</string>
<string name="quit">Quit</string>
</resources>