Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error handle #1986

Merged
merged 3 commits into from
Dec 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ dependencies {
}
// Parceler
implementation 'org.parceler:parceler-api:1.1.13'
implementation 'androidx.coordinatorlayout:coordinatorlayout:1.2.0'
annotationProcessor 'org.parceler:parceler:1.1.13'
// Dagger 2
implementation 'com.google.dagger:dagger:2.53.1'
Expand Down
9 changes: 9 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,15 @@
android:resource="@dimen/app_minimumsize_h" />

<activity android:name=".investment.PriceEditActivity" />

<!-- Error Handling -->
<activity
android:process=":crashreport"
android:name=".crashreport.CrashReportActivity"
android:label="@string/title_activity_auth"
android:exported="false"
android:theme="@style/AppTheme.NoActionBar" />

</application>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import com.money.manager.ex.R;
import com.money.manager.ex.core.Core;
import com.money.manager.ex.core.UIHelper;
import com.money.manager.ex.crashreport.CrashReporter;
import com.money.manager.ex.log.ErrorRaisedEvent;
import com.money.manager.ex.settings.AppSettings;

Expand All @@ -55,6 +56,8 @@ public abstract class MmxBaseFragmentActivity

@Override
protected void onCreate(Bundle savedInstanceState) {
new CrashReporter(this);

setTheme();

AppSettings settings = new AppSettings(this);
Expand All @@ -71,6 +74,7 @@ protected void onCreate(Bundle savedInstanceState) {
this.compositeSubscription = new CompositeSubscription();

super.onCreate(savedInstanceState);

// Initialize the ActivityResultLauncher
openDocumentLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(),
result -> {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.money.manager.ex.crashreport;


import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import com.money.manager.ex.R;

public class CrashReportActivity extends AppCompatActivity {
private Intent intent = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_auth);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
handleIntent();
}


void handleIntent() {
if (intent != null) return;

intent = getIntent();
if (intent == null) return;

String source = intent.getStringExtra("ERROR");
String report = intent.getStringExtra(Intent.EXTRA_TEXT);

TextView reportUI = findViewById(R.id.editTextReport);
reportUI.setText(report);

Button openIssue = findViewById(R.id.buttonOpenIssue);
openIssue.setOnClickListener(v -> {
String body = "[Put here your description]\n" + report;
String uri = Uri.parse("https://github.com/moneymanagerex/android-money-manager-ex/issues/new")
.buildUpon()
.appendQueryParameter("label", "bug")
// .appendQueryParameter("title", "Your title here")
.appendQueryParameter("body", body)
.build().toString();
Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
this.startActivity(myIntent);

closeActivity();

});

Button cancel = findViewById(R.id.buttonQuit);
cancel.setOnClickListener(view -> closeActivity());
}

private void closeActivity() {
this.finish();
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(0);
}
}
105 changes: 105 additions & 0 deletions app/src/main/java/com/money/manager/ex/crashreport/CrashReporter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package com.money.manager.ex.crashreport;


import android.content.Context;
import android.content.Intent;
import android.os.Build;

/**
* Activity based Exception handler ...
*/
public class CrashReporter implements Thread.UncaughtExceptionHandler {

private final Context mContext;
private final Thread.UncaughtExceptionHandler rootHandler;

public CrashReporter(Context context) {
mContext = context;
// we should store the current exception handler -- to invoke it for all not handled exceptions ...
rootHandler = Thread.getDefaultUncaughtExceptionHandler();
// we replace the exception handler now with us -- we will properly dispatch the exceptions ...
Thread.setDefaultUncaughtExceptionHandler(this);
}


@Override
public void uncaughtException(final Thread thread, final Throwable ex) {
// note we can't just open in Android an dialog etc. we have to use Intents here
// http://stackoverflow.com/questions/13416879/show-a-dialog-in-thread-setdefaultuncaughtexceptionhandler

Intent registerActivity = new Intent(mContext, CrashReportActivity.class);
registerActivity.setAction("HANDLE_ERROR");
registerActivity.putExtra("ERROR", CrashReporter.class.getName());
registerActivity.putExtra(Intent.EXTRA_TEXT, generateReport(thread,ex));
registerActivity.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

mContext.startActivity(registerActivity);

rootHandler.uncaughtException(thread, ex);
// // make sure we die, otherwise the app will hang ...
// android.os.Process.killProcess(android.os.Process.myPid());
// // sometimes on older android version killProcess wasn't enough -- strategy pattern should be considered here
// System.exit(0);

}

public static String generateReport(final Thread t, final Throwable e) {
StackTraceElement[] arr = e.getStackTrace();
final StringBuffer report = new StringBuffer(e.toString());
final String lineSeperator = "-------------------------------\n\n";
report.append("\n\n");
report.append("--------- Stack trace ---------\n\n");
for (int i = 0; i < arr.length; i++) {
report.append( " ");
report.append(arr[i].toString());
report.append("\n");
}
report.append(lineSeperator);
// If the exception was thrown in a background thread inside
// AsyncTask, then the actual exception can be found with getCause
report.append("--------- Cause ---------\n\n");
Throwable cause = e.getCause();
if (cause != null) {
report.append(cause.toString());
report.append("\n\n");
arr = cause.getStackTrace();
for (int i = 0; i < arr.length; i++) {
report.append(" ");
report.append(arr[i].toString());
report.append("\n");
}
}
// Getting the Device brand,model and sdk verion details.
report.append(lineSeperator);
report.append("--------- Device ---------\n\n");
report.append("Brand: ");
report.append(Build.BRAND);
report.append("\n");
report.append("Device: ");
report.append(Build.DEVICE);
report.append("\n");
report.append("Model: ");
report.append(Build.MODEL);
report.append("\n");
report.append("Id: ");
report.append(Build.ID);
report.append("\n");
report.append("Product: ");
report.append(Build.PRODUCT);
report.append("\n");
report.append(lineSeperator);
report.append("--------- Firmware ---------\n\n");
report.append("SDK: ");
report.append(Build.VERSION.SDK_INT);
report.append("\n");
report.append("Release: ");
report.append(Build.VERSION.RELEASE);
report.append("\n");
report.append("Incremental: ");
report.append(Build.VERSION.INCREMENTAL);
report.append("\n");
report.append(lineSeperator);
return report.toString();
}

}
90 changes: 90 additions & 0 deletions app/src/main/res/layout/activity_auth.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.money.manager.ex.crashreport.CrashReportActivity">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">

<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />

</com.google.android.material.appbar.AppBarLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
>

<Button
android:id="@+id/buttonMail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="20"
android:text="Send Mail" />

<Button
android:id="@+id/buttonOpenIssue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="20"
android:text="Open Issue" />

<Button
android:id="@+id/buttonQuit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="20"
android:text="Cancel" />
</LinearLayout>

<!-- include layout="@layout/content_auth" / -->
<TextView
android:id="@+id/textViewClass"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/bad_error_occurs" />

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.money.manager.ex.crashreport.CrashReportActivity"
tools:showIn="@layout/activity_auth">

<EditText
android:id="@+id/editTextReport"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:editable="false"
android:ems="10"
android:gravity="start|top"
android:inputType="textMultiLine" />
</RelativeLayout>

</LinearLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -830,4 +830,7 @@
<string name="issue_open">"Open issue on GitHub "</string>


<!-- Error Handling -->
<string name="title_activity_auth">Opps! Something went wrong.</string>
<string name="bad_error_occurs">We are so sad to notice an unexpected error. please report to developer</string>
</resources>
9 changes: 9 additions & 0 deletions app/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,13 @@
<item name="background">@android:drawable/dialog_holo_light_frame</item>
<item name="android:padding">0dp</item>
</style>


<!-- for Error Handling -->
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
<style name="AppTheme.NoActionBar" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
</resources>
Loading