Skip to content

Commit

Permalink
Completely remove return activity, now outdated
Browse files Browse the repository at this point in the history
  • Loading branch information
Stypox committed Mar 12, 2021
1 parent 0263125 commit 463dd8e
Show file tree
Hide file tree
Showing 27 changed files with 90 additions and 124 deletions.
2 changes: 1 addition & 1 deletion app/src/main/java/org/schabi/newpipe/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ protected void initACRA() {
ACRA.init(this, acraConfig);
} catch (final ACRAConfigurationException exception) {
exception.printStackTrace();
ErrorActivity.reportError(this, null, null, new ErrorInfo(exception,
ErrorActivity.reportError(this, new ErrorInfo(exception,
UserAction.SOMETHING_ELSE, "Could not initialize ACRA crash report"));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ private static String getCertificateSHA1Fingerprint(@NonNull final Application a
packageInfo = application.getPackageManager().getPackageInfo(
application.getPackageName(), PackageManager.GET_SIGNATURES);
} catch (final PackageManager.NameNotFoundException e) {
ErrorActivity.reportError(application, null, null, new ErrorInfo(e,
ErrorActivity.reportError(application, new ErrorInfo(e,
UserAction.CHECK_FOR_NEW_APP_VERSION, "Could not find package info"));
return "";
}
Expand All @@ -76,7 +76,7 @@ private static String getCertificateSHA1Fingerprint(@NonNull final Application a
final CertificateFactory cf = CertificateFactory.getInstance("X509");
c = (X509Certificate) cf.generateCertificate(input);
} catch (final CertificateException e) {
ErrorActivity.reportError(application, null, null, new ErrorInfo(e,
ErrorActivity.reportError(application, new ErrorInfo(e,
UserAction.CHECK_FOR_NEW_APP_VERSION, "Certificate error"));
return "";
}
Expand All @@ -86,7 +86,7 @@ private static String getCertificateSHA1Fingerprint(@NonNull final Application a
final byte[] publicKey = md.digest(c.getEncoded());
return byte2HexFormatted(publicKey);
} catch (NoSuchAlgorithmException | CertificateEncodingException e) {
ErrorActivity.reportError(application, null, null, new ErrorInfo(e,
ErrorActivity.reportError(application, new ErrorInfo(e,
UserAction.CHECK_FOR_NEW_APP_VERSION, "Could not retrieve SHA1 key"));
return "";
}
Expand Down
10 changes: 5 additions & 5 deletions app/src/main/java/org/schabi/newpipe/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ protected void onCreate(final Bundle savedInstanceState) {
try {
setupDrawer();
} catch (final Exception e) {
ErrorActivity.reportUiError(this, null, "Setting up drawer", e);
ErrorActivity.reportUiErrorInSnackbar(this, "Setting up drawer", e);
}

if (DeviceUtils.isTv(this)) {
Expand Down Expand Up @@ -238,7 +238,7 @@ private boolean drawerItemSelected(final MenuItem item) {
try {
tabSelected(item);
} catch (final Exception e) {
ErrorActivity.reportUiError(this, null, "Selecting main page tab", e);
ErrorActivity.reportUiErrorInSnackbar(this, "Selecting main page tab", e);
}
break;
case R.id.menu_options_about_group:
Expand Down Expand Up @@ -340,7 +340,7 @@ private void toggleServices() {
try {
showTabs();
} catch (final Exception e) {
ErrorActivity.reportUiError(this, null, "Showing main page tabs", e);
ErrorActivity.reportUiErrorInSnackbar(this, "Showing main page tabs", e);
}
}
}
Expand Down Expand Up @@ -487,7 +487,7 @@ protected void onResume() {
drawerHeaderBinding.drawerHeaderActionButton.setContentDescription(
getString(R.string.drawer_header_description) + selectedServiceName);
} catch (final Exception e) {
ErrorActivity.reportUiError(this, null, "Setting up service toggle", e);
ErrorActivity.reportUiErrorInSnackbar(this, "Setting up service toggle", e);
}

final SharedPreferences sharedPreferences
Expand Down Expand Up @@ -796,7 +796,7 @@ private void handleIntent(final Intent intent) {
NavigationHelper.gotoMainFragment(getSupportFragmentManager());
}
} catch (final Exception e) {
ErrorActivity.reportUiError(this, null, "Handling intent", e);
ErrorActivity.reportUiErrorInSnackbar(this, "Handling intent", e);
}
}

Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/org/schabi/newpipe/RouterActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ private static void handleError(final Context context, final ErrorInfo errorInfo
// unfortunately we cannot tell if the error is really caused by an unsupported url
((RouterActivity) context).showUnsupportedUrlDialog(errorInfo.getRequest());
} else {
ErrorActivity.reportError(context, MainActivity.class, null, errorInfo);
ErrorActivity.reportError(context, errorInfo);
}

if (context instanceof RouterActivity) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -693,7 +693,7 @@ private void checkSelectedDownload(final StoredDirectoryHelper mainStorage,
mainStorage.getTag());
}
} catch (final Exception e) {
ErrorActivity.reportError(context, null, null,
ErrorActivity.reportErrorInSnackbar(this,
new ErrorInfo(e, UserAction.DOWNLOAD_FAILED, "Getting storage"));
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class AcraReportSender implements ReportSender {

@Override
public void send(@NonNull final Context context, @NonNull final CrashReportData report) {
ErrorActivity.reportError(context, null, null, new ErrorInfo(
ErrorActivity.reportError(context, new ErrorInfo(
new String[]{report.getString(ReportField.STACK_TRACE)},
UserAction.UI_ERROR,
ErrorInfo.SERVICE_NONE,
Expand Down
104 changes: 44 additions & 60 deletions app/src/main/java/org/schabi/newpipe/error/ErrorActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
Expand All @@ -18,7 +17,7 @@
import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NavUtils;
import androidx.fragment.app.Fragment;

import com.google.android.material.snackbar.Snackbar;
import com.grack.nanojson.JsonWriter;
Expand Down Expand Up @@ -74,64 +73,67 @@ public class ErrorActivity extends AppCompatActivity {
= DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");


/**
* Singleton:
* Used to send data between certain Activity/Services within the same process.
* This can be considered as an ugly hack inside the Android universe.
**/
@Nullable private static Class savedReturnActivity = null;

private ErrorInfo errorInfo;
private String currentTimeStamp;

private ActivityErrorBinding activityErrorBinding;

public static void reportUiError(final Context context,
@Nullable final View rootView,
final String request,
final Throwable throwable) {
reportError(context, (context instanceof Activity ? context.getClass() : null), rootView,
new ErrorInfo(throwable, UserAction.UI_ERROR, request));
public static void reportError(final Context context, final ErrorInfo errorInfo) {
final Intent intent = new Intent(context, ErrorActivity.class);
intent.putExtra(ERROR_INFO, errorInfo);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}

public static void reportErrorInSnackbar(final Context context, final ErrorInfo errorInfo) {
final View rootView = context instanceof Activity
? ((Activity) context).findViewById(android.R.id.content) : null;
reportErrorInSnackbar(context, rootView, errorInfo);
}

public static void reportError(final Context context,
final Class returnActivity,
@Nullable final View rootView,
final ErrorInfo errorInfo) {
public static void reportErrorInSnackbar(final Fragment fragment, final ErrorInfo errorInfo) {
View rootView = fragment.getView();
if (rootView == null && fragment.getActivity() != null) {
rootView = fragment.getActivity().findViewById(android.R.id.content);
}
reportErrorInSnackbar(fragment.requireContext(), rootView, errorInfo);
}

public static void reportUiErrorInSnackbar(final Context context,
final String request,
final Throwable throwable) {
reportErrorInSnackbar(context, new ErrorInfo(throwable, UserAction.UI_ERROR, request));
}

public static void reportUiErrorInSnackbar(final Fragment fragment,
final String request,
final Throwable throwable) {
reportErrorInSnackbar(fragment, new ErrorInfo(throwable, UserAction.UI_ERROR, request));
}


////////////////////////////////////////////////////////////////////////
// Utils
////////////////////////////////////////////////////////////////////////

private static void reportErrorInSnackbar(final Context context,
@Nullable final View rootView,
final ErrorInfo errorInfo) {
if (rootView != null) {
Snackbar.make(rootView, R.string.error_snackbar_message, Snackbar.LENGTH_LONG)
.setActionTextColor(Color.YELLOW)
.setAction(context.getString(R.string.error_snackbar_action).toUpperCase(), v ->
startErrorActivity(returnActivity, context, errorInfo)).show();
reportError(context, errorInfo)).show();
} else {
startErrorActivity(returnActivity, context, errorInfo);
reportError(context, errorInfo);
}
}

// async call
public static void reportError(final Handler handler,
final Context context,
final Class returnActivity,
final View rootView,
final ErrorInfo errorInfo) {
handler.post(() -> reportError(context, returnActivity, rootView, errorInfo));
}


////////////////////////////////////////////////////////////////////////
// UTILS
// Activity lifecycle
////////////////////////////////////////////////////////////////////////

private static void startErrorActivity(@Nullable final Class returnActivity,
final Context context,
final ErrorInfo errorInfo) {
savedReturnActivity = returnActivity;
final Intent intent = new Intent(context, ErrorActivity.class);
intent.putExtra(ERROR_INFO, errorInfo);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}

@Override
protected void onCreate(final Bundle savedInstanceState) {
assureCorrectAppLanguage(this);
Expand Down Expand Up @@ -189,7 +191,7 @@ public boolean onCreateOptionsMenu(final Menu menu) {
public boolean onOptionsItemSelected(final MenuItem item) {
final int id = item.getItemId();
if (id == android.R.id.home) {
goToReturnActivity();
onBackPressed();
} else if (id == R.id.menu_item_share_error) {
ShareUtils.shareText(this, getString(R.string.error_report_title), buildJson());
} else {
Expand Down Expand Up @@ -258,18 +260,6 @@ static Class<? extends Activity> getReturnActivity(final Class<?> returnActivity
return checkedReturnActivity;
}

private void goToReturnActivity() {
final Class<? extends Activity> checkedReturnActivity =
getReturnActivity(savedReturnActivity);
if (checkedReturnActivity == null) {
super.onBackPressed();
} else {
final Intent intent = new Intent(this, checkedReturnActivity);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
NavUtils.navigateUpTo(this, intent);
}
}

private void buildInfo(final ErrorInfo info) {
String text = "";

Expand Down Expand Up @@ -409,10 +399,4 @@ private void addGuruMeditation() {
text += "\n" + getString(R.string.guru_meditation);
activityErrorBinding.errorSorryView.setText(text);
}

@Override
public void onBackPressed() {
//super.onBackPressed();
goToReturnActivity();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,7 @@ class ErrorPanelHelper(
} else {
errorButtonAction.setText(R.string.error_snackbar_action)
errorButtonAction.setOnClickListener {
ErrorActivity.reportError(
context,
MainActivity::class.java,
null,
errorInfo
)
ErrorActivity.reportError(context, errorInfo)
}

// hide retry button by default, then show only if not unavailable/unsupported content
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.schabi.newpipe.fragments;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
Expand All @@ -10,7 +9,6 @@
import androidx.annotation.Nullable;

import org.schabi.newpipe.BaseFragment;
import org.schabi.newpipe.MainActivity;
import org.schabi.newpipe.R;
import org.schabi.newpipe.error.ErrorActivity;
import org.schabi.newpipe.error.ErrorInfo;
Expand Down Expand Up @@ -157,7 +155,7 @@ public final void showError(final ErrorInfo errorInfo) {
errorPanelHelper.showError(errorInfo);
}

public final void showTextError(final @NonNull String errorString) {
public final void showTextError(@NonNull final String errorString) {
handleError();

if (isDetached() || isRemoving()) {
Expand All @@ -180,7 +178,7 @@ public final boolean isErrorPanelVisible() {

/**
* Show a SnackBar and only call
* {@link ErrorActivity#reportError(Context, Class, View, ErrorInfo)}
* {@link ErrorActivity.reportErrorInSnackbar(androidx.fragment.app.Fragment, ErrorInfo)}
* IF we a find a valid view (otherwise the error screen appears).
*
* @param errorInfo The error information
Expand All @@ -189,14 +187,6 @@ public void showSnackBarError(final ErrorInfo errorInfo) {
if (DEBUG) {
Log.d(TAG, "showSnackBarError() called with: errorInfo = [" + errorInfo + "]");
}
View rootView = activity != null ? activity.findViewById(android.R.id.content) : null;
if (rootView == null) {
rootView = getView();
}
if (rootView == null) {
return;
}

ErrorActivity.reportError(requireContext(), MainActivity.class, rootView, errorInfo);
ErrorActivity.reportErrorInSnackbar(this, errorInfo);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public boolean onOptionsItemSelected(final MenuItem item) {
NavigationHelper.openSearchFragment(getFM(),
ServiceHelper.getSelectedServiceId(activity), "");
} catch (final Exception e) {
ErrorActivity.reportUiError(getActivity(), null, "Opening search fragment", e);
ErrorActivity.reportUiErrorInSnackbar(this, "Opening search fragment", e);
}
return true;
}
Expand Down Expand Up @@ -238,7 +238,7 @@ public Fragment getItem(final int position) {
}

if (throwable != null) {
ErrorActivity.reportUiError(context, null, "Getting fragment item", throwable);
ErrorActivity.reportUiErrorInSnackbar(context, "Getting fragment item", throwable);
return new BlankFragment();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,7 @@ private void openChannel(final String subChannelUrl, final String subChannelName
NavigationHelper.openChannelFragment(getFM(), currentInfo.getServiceId(),
subChannelUrl, subChannelName);
} catch (final Exception e) {
ErrorActivity.reportUiError(getActivity(), null, "Opening channel fragment", e);
ErrorActivity.reportUiErrorInSnackbar(this, "Opening channel fragment", e);
}
}

Expand Down Expand Up @@ -1596,8 +1596,7 @@ public void openDownloadDialog() {

downloadDialog.show(activity.getSupportFragmentManager(), "downloadDialog");
} catch (final Exception e) {
ErrorActivity.reportError(activity, activity.getClass(),
activity.findViewById(android.R.id.content),
ErrorActivity.reportErrorInSnackbar(activity,
new ErrorInfo(e, UserAction.DOWNLOAD_OPEN_DIALOG, "Showing download dialog",
currentInfo));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,8 @@ public void selected(final ChannelInfoItem selectedItem) {
selectedItem.getUrl(),
selectedItem.getName());
} catch (final Exception e) {
ErrorActivity.reportUiError(getActivity(), null, "Opening channel fragment", e);
ErrorActivity.reportUiErrorInSnackbar(
BaseListFragment.this, "Opening channel fragment", e);
}
}
});
Expand All @@ -306,7 +307,7 @@ public void selected(final PlaylistInfoItem selectedItem) {
selectedItem.getUrl(),
selectedItem.getName());
} catch (final Exception e) {
ErrorActivity.reportUiError(getActivity(), null,
ErrorActivity.reportUiErrorInSnackbar(BaseListFragment.this,
"Opening playlist fragment", e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -403,8 +403,7 @@ public void onClick(final View v) {
currentInfo.getParentChannelUrl(),
currentInfo.getParentChannelName());
} catch (final Exception e) {
ErrorActivity.reportUiError(getActivity(), null,
"Opening channel fragment", e);
ErrorActivity.reportUiErrorInSnackbar(this, "Opening channel fragment", e);
}
} else if (DEBUG) {
Log.i(TAG, "Can't open parent channel because we got no channel URL");
Expand Down
Loading

0 comments on commit 463dd8e

Please sign in to comment.