-
Notifications
You must be signed in to change notification settings - Fork 190
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1986 from wolfsolver/error_handle
Error handle
- Loading branch information
Showing
8 changed files
with
286 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
app/src/main/java/com/money/manager/ex/crashreport/CrashReportActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
105
app/src/main/java/com/money/manager/ex/crashreport/CrashReporter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters