huge refactoring
Signed-off-by: androidacy-user <opensource@androidacy.com>pull/277/head
parent
24ec7f6cc6
commit
8898d0674c
@ -1,14 +1,132 @@
|
||||
package com.fox2code.mmm;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.content.ClipboardManager;
|
||||
import android.content.SharedPreferences;
|
||||
import android.os.Bundle;
|
||||
import android.view.View;
|
||||
import android.widget.EditText;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.fox2code.foxcompat.app.FoxActivity;
|
||||
import com.google.android.material.textview.MaterialTextView;
|
||||
|
||||
import java.io.StringWriter;
|
||||
|
||||
import io.sentry.Sentry;
|
||||
import io.sentry.UserFeedback;
|
||||
import io.sentry.protocol.SentryId;
|
||||
|
||||
public class CrashHandler extends FoxActivity {
|
||||
|
||||
@SuppressLint("RestrictedApi")
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_crash_handler);
|
||||
// set crash_details MaterialTextView to the exception passed in the intent or unknown if null
|
||||
// convert stacktrace from array to string, and pretty print it (first line is the exception, the rest is the stacktrace, with each line indented by 4 spaces)
|
||||
// first line is the exception, the rest is the stacktrace, with each line indented by 4 spaces. empty out the material text view first
|
||||
MaterialTextView crashDetails = findViewById(R.id.crash_details);
|
||||
crashDetails.setText("");
|
||||
// get the exception from the intent
|
||||
Throwable exception = (Throwable) getIntent().getSerializableExtra("exception");
|
||||
// if the exception is null, set the crash details to "Unknown"
|
||||
if (exception == null) {
|
||||
crashDetails.setText(R.string.crash_details);
|
||||
} else {
|
||||
// if the exception is not null, set the crash details to the exception and stacktrace
|
||||
// stacktrace is an StacktraceElement, so convert it to a string and replace the commas with newlines
|
||||
StringWriter stringWriter = new StringWriter();
|
||||
exception.printStackTrace(new java.io.PrintWriter(stringWriter));
|
||||
String stacktrace = stringWriter.toString();
|
||||
stacktrace = stacktrace.replace(",", "\n ");
|
||||
crashDetails.setText(getString(R.string.crash_full_stacktrace, stacktrace));
|
||||
}
|
||||
// disable feedback if sentry is disabled
|
||||
if (MainApplication.isCrashReportingEnabled()) {
|
||||
SharedPreferences preferences = getSharedPreferences("sentry", MODE_PRIVATE);
|
||||
// get lastEventId from intent
|
||||
SentryId lastEventId = Sentry.captureException((Throwable) getIntent().getSerializableExtra("exception"));
|
||||
// get name, email, and message fields
|
||||
EditText name = findViewById(R.id.feedback_name);
|
||||
EditText email = findViewById(R.id.feedback_email);
|
||||
EditText description = findViewById(R.id.feedback_message);
|
||||
// get submit button
|
||||
findViewById(R.id.feedback_submit).setOnClickListener(v -> {
|
||||
// require the feedback_message, rest is optional
|
||||
if (description.getText().toString().equals("")) {
|
||||
Toast.makeText(this, R.string.sentry_dialogue_empty_message, Toast.LENGTH_LONG).show();
|
||||
return;
|
||||
}
|
||||
// if email or name is empty, use "Anonymous"
|
||||
String nameString = name.getText().toString().equals("") ? "Anonymous" : name.getText().toString();
|
||||
String emailString = email.getText().toString().equals("") ? "Anonymous" : email.getText().toString();
|
||||
// Prevent strict mode violation
|
||||
new Thread(() -> {
|
||||
// create sentry userFeedback request
|
||||
UserFeedback userFeedback = new UserFeedback(lastEventId);
|
||||
userFeedback.setName(nameString);
|
||||
userFeedback.setEmail(emailString);
|
||||
userFeedback.setComments(description.getText().toString());
|
||||
// send the request
|
||||
Sentry.captureUserFeedback(userFeedback);
|
||||
}).start();
|
||||
// Close the activity
|
||||
finish();
|
||||
// start the main activity
|
||||
startActivity(getPackageManager().getLaunchIntentForPackage(getPackageName()));
|
||||
});
|
||||
// get restart button
|
||||
findViewById(R.id.restart).setOnClickListener(v -> {
|
||||
// Save the user's name and email
|
||||
preferences.edit().putString("name", name.getText().toString()).putString("email", email.getText().toString()).apply();
|
||||
// Restart the app
|
||||
finish();
|
||||
startActivity(getPackageManager().getLaunchIntentForPackage(getPackageName()));
|
||||
});
|
||||
} else {
|
||||
// disable feedback if sentry is disabled
|
||||
findViewById(R.id.feedback_name).setEnabled(false);
|
||||
findViewById(R.id.feedback_email).setEnabled(false);
|
||||
findViewById(R.id.feedback_message).setEnabled(false);
|
||||
// fade out all the fields
|
||||
findViewById(R.id.feedback_name).setAlpha(0.5f);
|
||||
findViewById(R.id.feedback_email).setAlpha(0.5f);
|
||||
findViewById(R.id.feedback_message).setAlpha(0.5f);
|
||||
// fade out the submit button
|
||||
findViewById(R.id.feedback_submit).setAlpha(0.5f);
|
||||
// set feedback_text to "Crash reporting is disabled"
|
||||
((MaterialTextView) findViewById(R.id.feedback_text)).setText(R.string.sentry_enable_nag);
|
||||
findViewById(R.id.feedback_submit).setOnClickListener(v -> Toast.makeText(this, R.string.sentry_dialogue_disabled, Toast.LENGTH_LONG).show());
|
||||
// handle restart button
|
||||
// we have to explicitly enable it because it's disabled by default
|
||||
findViewById(R.id.restart).setEnabled(true);
|
||||
findViewById(R.id.restart).setOnClickListener(v -> {
|
||||
// Restart the app
|
||||
finish();
|
||||
startActivity(getPackageManager().getLaunchIntentForPackage(getPackageName()));
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public void copyCrashDetails(View view) {
|
||||
// change view to a checkmark
|
||||
view.setBackgroundResource(R.drawable.baseline_check_24);
|
||||
// copy crash_details to clipboard
|
||||
ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
|
||||
String crashDetails = ((MaterialTextView) findViewById(R.id.crash_details)).getText().toString();
|
||||
clipboard.setPrimaryClip(android.content.ClipData.newPlainText("crash_details", crashDetails));
|
||||
// show a toast
|
||||
Toast.makeText(this, R.string.crash_details_copied, Toast.LENGTH_LONG).show();
|
||||
// after 1 second, change the view back to a copy button
|
||||
new Thread(() -> {
|
||||
try {
|
||||
Thread.sleep(1000);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
runOnUiThread(() -> view.setBackgroundResource(R.drawable.baseline_copy_all_24));
|
||||
}).start();
|
||||
}
|
||||
}
|
@ -0,0 +1,5 @@
|
||||
<vector xmlns:android="http://schemas.android.com/apk/res/android" android:width="24dp"
|
||||
android:height="24dp" android:autoMirrored="true"
|
||||
android:tint="?attr/colorControlNormal" android:viewportWidth="24" android:viewportHeight="24">
|
||||
<path android:fillColor="@android:color/white" android:pathData="M9,16.2L4.8,12l-1.4,1.4L9,19 21,7l-1.4,-1.4z"/>
|
||||
</vector>
|
@ -0,0 +1,5 @@
|
||||
<vector xmlns:android="http://schemas.android.com/apk/res/android" android:width="24dp"
|
||||
android:height="24dp" android:autoMirrored="true"
|
||||
android:tint="?attr/colorControlNormal" android:viewportWidth="24" android:viewportHeight="24">
|
||||
<path android:fillColor="@android:color/white" android:pathData="M18,2H9C7.9,2 7,2.9 7,4v12c0,1.1 0.9,2 2,2h9c1.1,0 2,-0.9 2,-2V4C20,2.9 19.1,2 18,2zM18,16H9V4h9V16zM3,15v-2h2v2H3zM3,9.5h2v2H3V9.5zM10,20h2v2h-2V20zM3,18.5v-2h2v2H3zM5,22c-1.1,0 -2,-0.9 -2,-2h2V22zM8.5,22h-2v-2h2V22zM13.5,22L13.5,22l0,-2h2v0C15.5,21.1 14.6,22 13.5,22zM5,6L5,6l0,2H3v0C3,6.9 3.9,6 5,6z"/>
|
||||
</vector>
|
Loading…
Reference in New Issue