NewPipe-app-android/app/src/main/java/org/schabi/newpipe/error/ErrorActivity.java

350 lines
14 KiB
Java
Raw Normal View History

package org.schabi.newpipe.error;
2016-02-25 23:19:43 +01:00
import static org.schabi.newpipe.util.Localization.assureCorrectAppLanguage;
2016-02-25 22:02:42 +01:00
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
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;
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 androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
2020-04-20 11:02:45 +02:00
import com.grack.nanojson.JsonWriter;
2016-09-13 23:39:32 +02:00
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;
2017-02-27 12:55:15 +01:00
import org.schabi.newpipe.util.ThemeHelper;
import org.schabi.newpipe.util.external_communication.ShareUtils;
2016-02-25 22:02:42 +01:00
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
2020-04-20 11:02:45 +02:00
import java.util.Arrays;
2022-07-29 05:35:50 +02:00
import java.util.stream.Collectors;
2016-02-25 22:02:42 +01:00
/*
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/>.
*/
/**
* This activity is used to show error details and allow reporting them in various ways. Use {@link
* ErrorUtil#openActivity(Context, ErrorInfo)} to correctly open this activity.
*/
2021-12-01 09:43:24 +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";
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 ";
public static final String ERROR_GITHUB_ISSUE_URL =
"https://github.com/TeamNewPipe/NewPipe/issues";
public static final DateTimeFormatter CURRENT_TIMESTAMP_FORMATTER =
DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
2016-02-25 22:02:42 +01:00
private ErrorInfo errorInfo;
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
////////////////////////////////////////////////////////////////////////
// Activity lifecycle
////////////////////////////////////////////////////////////////////////
2016-09-13 23:24:49 +02:00
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);
2021-03-29 21:44:03 +02:00
ThemeHelper.setDayNightMode(this);
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
errorInfo = intent.getParcelableExtra(ERROR_INFO);
2016-02-25 22:02:42 +01:00
// important add guru meditation
addGuruMeditation();
currentTimeStamp = CURRENT_TIMESTAMP_FORMATTER.format(LocalDateTime.now());
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"));
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);
activityErrorBinding.errorMessageView.setText(errorInfo.getMessageStringId());
activityErrorBinding.errorView.setText(formErrorText(errorInfo.getStackTraces()));
2017-02-12 16:45:01 +01:00
// print stack trace once again for debugging:
for (final String e : errorInfo.getStackTraces()) {
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) {
2021-04-19 07:22:52 +02:00
switch (item.getItemId()) {
case android.R.id.home:
2021-04-19 07:22:52 +02:00
onBackPressed();
return true;
case R.id.menu_item_share_error:
ShareUtils.shareText(getApplicationContext(),
getString(R.string.error_report_title), buildJson());
2021-04-19 07:22:52 +02:00
return true;
default:
return false;
2016-02-25 22:02:42 +01:00
}
}
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
+ getString(R.string.app_name) + " "
+ BuildConfig.VERSION_NAME)
.putExtra(Intent.EXTRA_TEXT, buildJson());
ShareUtils.openIntentInApp(context, i, true);
} 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) {
2022-07-29 05:35:50 +02:00
final String separator = "-------------------------------------";
return Arrays.stream(el)
.collect(Collectors.joining(separator + "\n", separator + "\n", separator));
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;
}
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(errorInfo.getStackTraces()))
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 (errorInfo.getStackTraces().length > 1) {
htmlErrorReport
.append("<details><summary><b>Exceptions (")
.append(errorInfo.getStackTraces().length)
.append(")</b></summary><p>\n");
}
// add the logs
for (int i = 0; i < errorInfo.getStackTraces().length; i++) {
htmlErrorReport.append("<details><summary><b>Crash log ");
if (errorInfo.getStackTraces().length > 1) {
htmlErrorReport.append(i + 1);
}
htmlErrorReport.append("</b>")
.append("</summary><p>\n")
.append("\n```\n").append(errorInfo.getStackTraces()[i]).append("\n```\n")
.append("</details>\n");
}
// make sure to close everything
if (errorInfo.getStackTraces().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
}
}