Better handle url not supported in RouterActivity

Make sure the url not supported dialog is only shown when the url is really not supported, not on any ExtractionException
This commit is contained in:
Stypox 2021-03-07 18:59:17 +01:00
parent 8ce18647f1
commit 285ea4e3fd
No known key found for this signature in database
GPG Key ID: 4BDF1B40A49FDD23
1 changed files with 24 additions and 25 deletions

View File

@ -150,36 +150,39 @@ public class RouterActivity extends AppCompatActivity {
private void handleUrl(final String url) { private void handleUrl(final String url) {
disposables.add(Observable disposables.add(Observable
.fromCallable(() -> { .fromCallable(() -> {
if (currentServiceId == -1) { try {
currentService = NewPipe.getServiceByUrl(url); if (currentServiceId == -1) {
currentServiceId = currentService.getServiceId(); currentService = NewPipe.getServiceByUrl(url);
currentLinkType = currentService.getLinkTypeByUrl(url); currentServiceId = currentService.getServiceId();
currentUrl = url; currentLinkType = currentService.getLinkTypeByUrl(url);
} else { currentUrl = url;
currentService = NewPipe.getService(currentServiceId); } else {
} currentService = NewPipe.getService(currentServiceId);
}
return currentLinkType != LinkType.NONE; // return whether the url was found to be supported or not
return currentLinkType != LinkType.NONE;
} catch (final ExtractionException e) {
// this can be reached only when the url is completely unsupported
return false;
}
}) })
.subscribeOn(Schedulers.io()) .subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
.subscribe(result -> { .subscribe(isUrlSupported -> {
if (result) { if (isUrlSupported) {
onSuccess(); onSuccess();
} else { } else {
showUnsupportedUrlDialog(url); showUnsupportedUrlDialog(url);
} }
}, throwable -> handleError(this, }, throwable -> handleError(this, new ErrorInfo(throwable,
new ErrorInfo(throwable, UserAction.SHARE_TO_NEWPIPE, url)))); UserAction.SHARE_TO_NEWPIPE, "Getting service from url: " + url))));
} }
/** /**
* @param context the context. If instance of {@link RouterActivity} it will be finished at the * @param context the context. It will be {@code finish()}ed at the end of the handling if it is
* end, and if needed {@link #showUnsupportedUrlDialog(String)} will be called * an instance of {@link RouterActivity}.
* on it. * @param errorInfo the error information
* @param errorInfo The error information. The field {@link ErrorInfo#getRequest()} has to
* contain the url, if context is instance of {@link RouterActivity}, since it
* could be used to call {@link #showUnsupportedUrlDialog(String)}.
*/ */
private static void handleError(final Context context, final ErrorInfo errorInfo) { private static void handleError(final Context context, final ErrorInfo errorInfo) {
if (errorInfo.getThrowable() != null) { if (errorInfo.getThrowable() != null) {
@ -214,10 +217,6 @@ public class RouterActivity extends AppCompatActivity {
Toast.makeText(context, R.string.content_not_available, Toast.LENGTH_LONG).show(); Toast.makeText(context, R.string.content_not_available, Toast.LENGTH_LONG).show();
} else if (errorInfo.getThrowable() instanceof ContentNotSupportedException) { } else if (errorInfo.getThrowable() instanceof ContentNotSupportedException) {
Toast.makeText(context, R.string.content_not_supported, Toast.LENGTH_LONG).show(); Toast.makeText(context, R.string.content_not_supported, Toast.LENGTH_LONG).show();
} else if (errorInfo.getThrowable() instanceof ExtractionException
&& context instanceof RouterActivity) {
// unfortunately we cannot tell if the error is really caused by an unsupported url
((RouterActivity) context).showUnsupportedUrlDialog(errorInfo.getRequest());
} else { } else {
ErrorActivity.reportError(context, errorInfo); ErrorActivity.reportError(context, errorInfo);
} }
@ -548,8 +547,8 @@ public class RouterActivity extends AppCompatActivity {
.subscribe(intent -> { .subscribe(intent -> {
startActivity(intent); startActivity(intent);
finish(); finish();
}, throwable -> handleError(this, }, throwable -> handleError(this, new ErrorInfo(throwable,
new ErrorInfo(throwable, UserAction.SHARE_TO_NEWPIPE, currentUrl))) UserAction.SHARE_TO_NEWPIPE, "Starting info activity: " + currentUrl)))
); );
return; return;
} }