NewPipe/app/src/main/java/org/schabi/newpipe/report/ErrorActivity.java

477 lines
18 KiB
Java
Raw Normal View History

2016-09-13 23:39:32 +02:00
package org.schabi.newpipe.report;
2016-02-25 23:19:43 +01:00
2016-02-25 22:02:42 +01:00
import android.app.Activity;
import android.app.AlertDialog;
2016-02-25 22:02:42 +01:00
import android.content.Context;
import android.content.Intent;
2016-02-29 21:21:15 +01:00
import android.graphics.Color;
2016-02-25 23:19:43 +01:00
import android.net.Uri;
2016-02-25 22:02:42 +01:00
import android.os.Build;
2017-02-27 12:55:15 +01:00
import android.os.Bundle;
import android.os.Handler;
2016-02-25 23:19:43 +01:00
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
2016-02-25 22:02:42 +01:00
import android.view.MenuItem;
import android.view.View;
import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NavUtils;
import com.google.android.material.snackbar.Snackbar;
2020-04-20 11:02:45 +02:00
import com.grack.nanojson.JsonWriter;
2016-09-13 23:24:49 +02:00
import org.acra.ReportField;
2020-05-01 18:26:12 +02:00
import org.acra.data.CrashReportData;
2016-09-13 23:39:32 +02:00
import org.schabi.newpipe.ActivityCommunicator;
import org.schabi.newpipe.BuildConfig;
import org.schabi.newpipe.MainActivity;
import org.schabi.newpipe.R;
2020-10-31 11:14:11 +01:00
import org.schabi.newpipe.databinding.ActivityErrorBinding;
import org.schabi.newpipe.util.Localization;
import org.schabi.newpipe.util.ShareUtils;
2017-02-27 12:55:15 +01:00
import org.schabi.newpipe.util.ThemeHelper;
2016-02-25 22:02:42 +01:00
import java.io.PrintWriter;
import java.io.StringWriter;
2016-02-27 15:02:28 +01:00
import java.text.SimpleDateFormat;
2020-04-20 11:02:45 +02:00
import java.util.Arrays;
2016-02-27 15:02:28 +01:00
import java.util.Date;
2016-02-25 22:02:42 +01:00
import java.util.List;
2016-02-27 15:02:28 +01:00
import java.util.TimeZone;
2016-02-25 22:02:42 +01:00
import java.util.Vector;
import static org.schabi.newpipe.util.Localization.assureCorrectAppLanguage;
/*
2016-02-25 23:19:43 +01:00
* Created by Christian Schabesberger on 24.10.15.
2016-09-12 00:33:11 +02:00
*
2016-02-25 23:19:43 +01:00
* Copyright (C) Christian Schabesberger 2016 <chris.schabesberger@mailbox.org>
* ErrorActivity.java is part of NewPipe.
2016-09-12 00:33:11 +02:00
*
2016-02-25 23:19:43 +01:00
* NewPipe 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.
2016-09-12 00:33:11 +02:00
* <
2016-02-25 23:19:43 +01:00
* NewPipe 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.
2016-09-12 00:33:11 +02:00
* <
2016-02-25 23:19:43 +01:00
* You should have received a copy of the GNU General Public License
* along with NewPipe. If not, see <http://www.gnu.org/licenses/>.
*/
2017-02-27 12:55:15 +01:00
public class ErrorActivity extends AppCompatActivity {
// LOG TAGS
2016-02-25 22:02:42 +01:00
public static final String TAG = ErrorActivity.class.toString();
// BUNDLE TAGS
public static final String ERROR_INFO = "error_info";
public static final String ERROR_LIST = "error_list";
2016-02-27 15:57:37 +01:00
public static final String ERROR_EMAIL_ADDRESS = "crashreport@newpipe.schabi.org";
public static final String ERROR_EMAIL_SUBJECT
= "Exception in NewPipe " + BuildConfig.VERSION_NAME;
public static final String ERROR_GITHUB_ISSUE_URL
= "https://github.com/TeamNewPipe/NewPipe/issues";
private String[] errorList;
2016-02-25 22:02:42 +01:00
private ErrorInfo errorInfo;
private Class returnActivity;
2016-02-27 15:02:28 +01:00
private String currentTimeStamp;
2020-10-31 11:14:11 +01:00
private ActivityErrorBinding activityErrorBinding;
2016-02-25 22:02:42 +01:00
2017-02-18 21:59:48 +01:00
public static void reportUiError(final AppCompatActivity activity, final Throwable el) {
reportError(activity, el, activity.getClass(), null, ErrorInfo.make(UserAction.UI_ERROR,
"none", "", R.string.app_ui_crash));
2017-02-18 21:59:48 +01:00
}
2016-08-01 01:56:19 +02:00
public static void reportError(final Context context, final List<Throwable> el,
final Class returnActivity, final View rootView,
final ErrorInfo errorInfo) {
2016-02-27 15:02:28 +01:00
if (rootView != null) {
Snackbar.make(rootView, R.string.error_snackbar_message, 3 * 1000)
2016-02-29 21:21:15 +01:00
.setActionTextColor(Color.YELLOW)
.setAction(context.getString(R.string.error_snackbar_action).toUpperCase(), v ->
2018-04-01 21:54:00 +02:00
startErrorActivity(returnActivity, context, errorInfo, el)).show();
2016-02-25 22:02:42 +01:00
} else {
startErrorActivity(returnActivity, context, errorInfo, el);
2016-02-25 22:02:42 +01:00
}
}
private static void startErrorActivity(final Class returnActivity, final Context context,
final ErrorInfo errorInfo, final List<Throwable> el) {
2020-08-16 10:24:58 +02:00
final ActivityCommunicator ac = ActivityCommunicator.getCommunicator();
ac.setReturnActivity(returnActivity);
2020-08-16 10:24:58 +02:00
final Intent intent = new Intent(context, ErrorActivity.class);
intent.putExtra(ERROR_INFO, errorInfo);
intent.putExtra(ERROR_LIST, elToSl(el));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
2016-08-01 01:56:19 +02:00
public static void reportError(final Context context, final Throwable e,
final Class returnActivity, final View rootView,
final ErrorInfo errorInfo) {
2016-08-01 01:56:19 +02:00
List<Throwable> el = null;
if (e != null) {
2016-03-11 14:56:41 +01:00
el = new Vector<>();
el.add(e);
}
reportError(context, el, returnActivity, rootView, errorInfo);
2016-02-25 22:02:42 +01:00
}
// async call
public static void reportError(final Handler handler, final Context context,
final Throwable e, final Class returnActivity,
final View rootView, final ErrorInfo errorInfo) {
2016-03-11 14:56:41 +01:00
2016-08-01 01:56:19 +02:00
List<Throwable> el = null;
if (e != null) {
2016-03-11 14:56:41 +01:00
el = new Vector<>();
el.add(e);
}
reportError(handler, context, el, returnActivity, rootView, errorInfo);
2016-02-25 22:02:42 +01:00
}
// async call
public static void reportError(final Handler handler, final Context context,
final List<Throwable> el, final Class returnActivity,
final View rootView, final ErrorInfo errorInfo) {
2018-02-04 17:50:22 +01:00
handler.post(() -> reportError(context, el, returnActivity, rootView, errorInfo));
2016-02-25 22:02:42 +01:00
}
public static void reportError(final Context context, final CrashReportData report,
final ErrorInfo errorInfo) {
final String[] el = {report.getString(ReportField.STACK_TRACE)};
2016-09-13 23:24:49 +02:00
2020-08-16 10:24:58 +02:00
final Intent intent = new Intent(context, ErrorActivity.class);
2016-09-13 23:24:49 +02:00
intent.putExtra(ERROR_INFO, errorInfo);
intent.putExtra(ERROR_LIST, el);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2016-09-13 23:24:49 +02:00
context.startActivity(intent);
}
2017-02-27 12:55:15 +01:00
private static String getStackTrace(final Throwable throwable) {
final StringWriter sw = new StringWriter();
final PrintWriter pw = new PrintWriter(sw, true);
throwable.printStackTrace(pw);
return sw.getBuffer().toString();
}
// errorList to StringList
private static String[] elToSl(final List<Throwable> stackTraces) {
2020-08-16 10:24:58 +02:00
final String[] out = new String[stackTraces.size()];
2017-02-27 12:55:15 +01:00
for (int i = 0; i < stackTraces.size(); i++) {
out[i] = getStackTrace(stackTraces.get(i));
}
return out;
}
2016-02-25 22:02:42 +01:00
@Override
protected void onCreate(final Bundle savedInstanceState) {
assureCorrectAppLanguage(this);
2016-02-25 22:02:42 +01:00
super.onCreate(savedInstanceState);
2017-04-26 21:32:20 +02:00
ThemeHelper.setTheme(this);
2020-10-31 11:14:11 +01:00
activityErrorBinding = ActivityErrorBinding.inflate(getLayoutInflater());
setContentView(activityErrorBinding.getRoot());
2020-08-16 10:24:58 +02:00
final Intent intent = getIntent();
2020-10-31 11:14:11 +01:00
setSupportActionBar(activityErrorBinding.toolbarLayout.toolbar);
2017-04-26 21:32:20 +02:00
2020-08-16 10:24:58 +02:00
final ActionBar actionBar = getSupportActionBar();
2017-04-26 21:32:20 +02:00
if (actionBar != null) {
2016-05-25 23:51:22 +02:00
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setTitle(R.string.error_report_title);
actionBar.setDisplayShowTitleEnabled(true);
}
2016-02-25 22:02:42 +01:00
2020-08-16 10:24:58 +02:00
final ActivityCommunicator ac = ActivityCommunicator.getCommunicator();
returnActivity = ac.getReturnActivity();
errorInfo = intent.getParcelableExtra(ERROR_INFO);
errorList = intent.getStringArrayExtra(ERROR_LIST);
2016-02-25 22:02:42 +01:00
// important add guru meditation
addGuruMeditation();
2016-02-27 15:02:28 +01:00
currentTimeStamp = getCurrentTimeStamp();
2016-02-25 23:19:43 +01:00
2020-10-31 11:14:11 +01:00
activityErrorBinding.errorReportEmailButton.setOnClickListener(v ->
2020-08-27 22:56:41 +02:00
openPrivacyPolicyDialog(this, "EMAIL"));
2020-10-31 11:14:11 +01:00
activityErrorBinding.errorReportCopyButton.setOnClickListener(v -> {
ShareUtils.copyToClipboard(this, buildMarkdown());
});
2016-02-25 23:19:43 +01:00
2020-10-31 11:14:11 +01:00
activityErrorBinding.errorReportGitHubButton.setOnClickListener(v ->
2020-08-27 22:56:41 +02:00
openPrivacyPolicyDialog(this, "GITHUB"));
2016-02-27 15:02:28 +01:00
// normal bugreport
buildInfo(errorInfo);
if (errorInfo.getMessage() != 0) {
2020-10-31 11:14:11 +01:00
activityErrorBinding.errorMessageView.setText(errorInfo.getMessage());
2016-02-29 21:13:02 +01:00
} else {
2020-10-31 11:14:11 +01:00
activityErrorBinding.errorMessageView.setVisibility(View.GONE);
activityErrorBinding.messageWhatHappenedView.setVisibility(View.GONE);
2016-02-29 21:13:02 +01:00
}
2020-10-31 11:14:11 +01:00
activityErrorBinding.errorView.setText(formErrorText(errorList));
2017-02-12 16:45:01 +01:00
// print stack trace once again for debugging:
2020-08-16 10:24:58 +02:00
for (final String e : errorList) {
2017-02-12 16:45:01 +01:00
Log.e(TAG, e);
}
2016-02-25 23:19:43 +01:00
}
@Override
public boolean onCreateOptionsMenu(final Menu menu) {
2020-08-16 10:24:58 +02:00
final MenuInflater inflater = getMenuInflater();
2016-02-25 23:19:43 +01:00
inflater.inflate(R.menu.error_menu, menu);
return true;
2016-02-25 22:02:42 +01:00
}
@Override
public boolean onOptionsItemSelected(final MenuItem item) {
2020-08-16 10:24:58 +02:00
final int id = item.getItemId();
2016-02-27 15:02:28 +01:00
switch (id) {
2016-02-25 23:19:43 +01:00
case android.R.id.home:
goToReturnActivity();
break;
case R.id.menu_item_share_error:
ShareUtils.shareUrl(this, getString(R.string.error_report_title), buildJson());
break;
2016-02-25 22:02:42 +01:00
}
return false;
}
private void openPrivacyPolicyDialog(final Context context, final String action) {
new AlertDialog.Builder(context)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle(R.string.privacy_policy_title)
.setMessage(R.string.start_accept_privacy_policy)
.setCancelable(false)
2020-08-27 22:56:41 +02:00
.setNeutralButton(R.string.read_privacy_policy, (dialog, which) ->
ShareUtils.openUrlInBrowser(context,
context.getString(R.string.privacy_policy_url)))
.setPositiveButton(R.string.accept, (dialog, which) -> {
if (action.equals("EMAIL")) { // send on email
final Intent i = new Intent(Intent.ACTION_SENDTO)
.setData(Uri.parse("mailto:")) // only email apps should handle this
.putExtra(Intent.EXTRA_EMAIL, new String[]{ERROR_EMAIL_ADDRESS})
.putExtra(Intent.EXTRA_SUBJECT, ERROR_EMAIL_SUBJECT)
.putExtra(Intent.EXTRA_TEXT, buildJson());
if (i.resolveActivity(getPackageManager()) != null) {
ShareUtils.openContentInApp(context, i);
}
} else if (action.equals("GITHUB")) { // open the NewPipe issue page on GitHub
ShareUtils.openUrlInBrowser(this, ERROR_GITHUB_ISSUE_URL, false);
}
})
.setNegativeButton(R.string.decline, (dialog, which) -> {
// do nothing
})
.show();
}
private String formErrorText(final String[] el) {
2020-08-16 10:24:58 +02:00
final StringBuilder text = new StringBuilder();
if (el != null) {
2020-08-16 10:24:58 +02:00
for (final String e : el) {
text.append("-------------------------------------\n").append(e);
2016-03-11 14:56:41 +01:00
}
2016-02-25 22:02:42 +01:00
}
text.append("-------------------------------------");
return text.toString();
2016-02-25 22:02:42 +01:00
}
2020-04-02 13:51:10 +02:00
/**
* Get the checked activity.
*
* @param returnActivity the activity to return to
* @return the casted return activity or null
*/
@Nullable
static Class<? extends Activity> getReturnActivity(final Class<?> returnActivity) {
Class<? extends Activity> checkedReturnActivity = null;
if (returnActivity != null) {
if (Activity.class.isAssignableFrom(returnActivity)) {
checkedReturnActivity = returnActivity.asSubclass(Activity.class);
} else {
checkedReturnActivity = MainActivity.class;
}
}
return checkedReturnActivity;
}
2016-02-25 22:02:42 +01:00
private void goToReturnActivity() {
2020-08-16 10:24:58 +02:00
final Class<? extends Activity> checkedReturnActivity = getReturnActivity(returnActivity);
if (checkedReturnActivity == null) {
2016-02-25 22:02:42 +01:00
super.onBackPressed();
} else {
2020-08-16 10:24:58 +02:00
final Intent intent = new Intent(this, checkedReturnActivity);
2016-02-25 22:02:42 +01:00
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
NavUtils.navigateUpTo(this, intent);
}
}
private void buildInfo(final ErrorInfo info) {
2016-02-25 22:02:42 +01:00
String text = "";
2020-10-31 11:14:11 +01:00
activityErrorBinding.errorInfoLabelsView.setText(getString(R.string.info_labels)
.replace("\\n", "\n"));
2016-02-25 22:02:42 +01:00
text += getUserActionString(info.getUserAction()) + "\n"
+ info.getRequest() + "\n"
+ getContentLanguageString() + "\n"
+ getContentCountryString() + "\n"
+ getAppLanguage() + "\n"
+ info.getServiceName() + "\n"
+ currentTimeStamp + "\n"
+ getPackageName() + "\n"
+ BuildConfig.VERSION_NAME + "\n"
+ getOsString();
2016-02-25 22:02:42 +01:00
2020-10-31 11:14:11 +01:00
activityErrorBinding.errorInfosView.setText(text);
2016-02-25 22:02:42 +01:00
}
2016-02-25 23:19:43 +01:00
private String buildJson() {
try {
2020-04-20 11:02:45 +02:00
return JsonWriter.string()
.object()
.value("user_action", getUserActionString(errorInfo.getUserAction()))
.value("request", errorInfo.getRequest())
.value("content_language", getContentLanguageString())
.value("content_country", getContentCountryString())
.value("app_language", getAppLanguage())
.value("service", errorInfo.getServiceName())
2020-04-20 11:02:45 +02:00
.value("package", getPackageName())
.value("version", BuildConfig.VERSION_NAME)
.value("os", getOsString())
.value("time", currentTimeStamp)
.array("exceptions", Arrays.asList(errorList))
2020-10-31 11:14:11 +01:00
.value("user_comment", activityErrorBinding.errorCommentBox.getText()
.toString())
2020-04-20 11:02:45 +02:00
.end()
.done();
2020-08-16 10:24:58 +02:00
} catch (final Throwable e) {
2016-02-25 23:19:43 +01:00
Log.e(TAG, "Error while erroring: Could not build json");
e.printStackTrace();
}
return "";
}
private String buildMarkdown() {
try {
final StringBuilder htmlErrorReport = new StringBuilder();
2020-10-31 11:14:11 +01:00
final String userComment = activityErrorBinding.errorCommentBox.getText().toString();
if (!userComment.isEmpty()) {
htmlErrorReport.append(userComment).append("\n");
}
// basic error info
htmlErrorReport
.append("## Exception")
.append("\n* __User Action:__ ")
.append(getUserActionString(errorInfo.getUserAction()))
.append("\n* __Request:__ ").append(errorInfo.getRequest())
.append("\n* __Content Country:__ ").append(getContentCountryString())
.append("\n* __Content Language:__ ").append(getContentLanguageString())
.append("\n* __App Language:__ ").append(getAppLanguage())
.append("\n* __Service:__ ").append(errorInfo.getServiceName())
.append("\n* __Version:__ ").append(BuildConfig.VERSION_NAME)
.append("\n* __OS:__ ").append(getOsString()).append("\n");
// Collapse all logs to a single paragraph when there are more than one
// to keep the GitHub issue clean.
if (errorList.length > 1) {
htmlErrorReport
.append("<details><summary><b>Exceptions (")
.append(errorList.length)
.append(")</b></summary><p>\n");
}
// add the logs
for (int i = 0; i < errorList.length; i++) {
htmlErrorReport.append("<details><summary><b>Crash log ");
if (errorList.length > 1) {
htmlErrorReport.append(i + 1);
}
htmlErrorReport.append("</b>")
.append("</summary><p>\n")
.append("\n```\n").append(errorList[i]).append("\n```\n")
.append("</details>\n");
}
// make sure to close everything
if (errorList.length > 1) {
htmlErrorReport.append("</p></details>\n");
}
htmlErrorReport.append("<hr>\n");
return htmlErrorReport.toString();
2020-08-16 10:24:58 +02:00
} catch (final Throwable e) {
Log.e(TAG, "Error while erroring: Could not build markdown");
e.printStackTrace();
return "";
}
}
private String getUserActionString(final UserAction userAction) {
if (userAction == null) {
return "Your description is in another castle.";
} else {
return userAction.getMessage();
2016-02-25 23:19:43 +01:00
}
}
private String getContentCountryString() {
return Localization.getPreferredContentCountry(this).getCountryCode();
}
private String getContentLanguageString() {
return Localization.getPreferredLocalization(this).getLocalizationCode();
}
private String getAppLanguage() {
return Localization.getAppLocale(getApplicationContext()).toString();
2016-02-25 23:19:43 +01:00
}
private String getOsString() {
2020-08-27 22:59:29 +02:00
final String osBase = Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
? Build.VERSION.BASE_OS : "Android";
2016-02-27 15:02:28 +01:00
return System.getProperty("os.name")
2016-02-25 23:19:43 +01:00
+ " " + (osBase.isEmpty() ? "Android" : osBase)
+ " " + Build.VERSION.RELEASE
+ " - " + Build.VERSION.SDK_INT;
2016-02-25 23:19:43 +01:00
}
private void addGuruMeditation() {
2016-02-25 22:02:42 +01:00
//just an easter egg
2020-10-31 11:14:11 +01:00
String text = activityErrorBinding.errorSorryView.getText().toString();
2016-02-25 22:02:42 +01:00
text += "\n" + getString(R.string.guru_meditation);
2020-10-31 11:14:11 +01:00
activityErrorBinding.errorSorryView.setText(text);
2016-02-25 22:02:42 +01:00
}
@Override
public void onBackPressed() {
//super.onBackPressed();
goToReturnActivity();
}
2016-02-27 15:02:28 +01:00
public String getCurrentTimeStamp() {
2020-08-16 10:24:58 +02:00
final SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
2016-02-27 15:02:28 +01:00
df.setTimeZone(TimeZone.getTimeZone("GMT"));
return df.format(new Date());
}
2016-02-25 22:02:42 +01:00
}