1
0
mirror of https://github.com/TeamNewPipe/NewPipe synced 2024-12-25 01:20:34 +00:00

Fail-fast for service id == -1

This commit is contained in:
Coffeemakr 2017-10-08 21:04:37 +02:00
parent 69302fcbd0
commit 89b11ff71c
6 changed files with 26 additions and 4 deletions

View File

@ -7,6 +7,7 @@ import android.arch.persistence.room.Index;
import android.arch.persistence.room.PrimaryKey; import android.arch.persistence.room.PrimaryKey;
import org.schabi.newpipe.extractor.channel.ChannelInfoItem; import org.schabi.newpipe.extractor.channel.ChannelInfoItem;
import org.schabi.newpipe.util.Constants;
import static org.schabi.newpipe.database.subscription.SubscriptionEntity.SUBSCRIPTION_SERVICE_ID; import static org.schabi.newpipe.database.subscription.SubscriptionEntity.SUBSCRIPTION_SERVICE_ID;
import static org.schabi.newpipe.database.subscription.SubscriptionEntity.SUBSCRIPTION_TABLE; import static org.schabi.newpipe.database.subscription.SubscriptionEntity.SUBSCRIPTION_TABLE;
@ -28,7 +29,7 @@ public class SubscriptionEntity {
private long uid = 0; private long uid = 0;
@ColumnInfo(name = SUBSCRIPTION_SERVICE_ID) @ColumnInfo(name = SUBSCRIPTION_SERVICE_ID)
private int serviceId = -1; private int serviceId = Constants.NO_SERVICE_ID;
@ColumnInfo(name = SUBSCRIPTION_URL) @ColumnInfo(name = SUBSCRIPTION_URL)
private String url; private String url;

View File

@ -65,6 +65,7 @@ import org.schabi.newpipe.player.PopupVideoPlayer;
import org.schabi.newpipe.player.old.PlayVideoActivity; import org.schabi.newpipe.player.old.PlayVideoActivity;
import org.schabi.newpipe.report.ErrorActivity; import org.schabi.newpipe.report.ErrorActivity;
import org.schabi.newpipe.report.UserAction; import org.schabi.newpipe.report.UserAction;
import org.schabi.newpipe.util.Constants;
import org.schabi.newpipe.util.ExtractorHelper; import org.schabi.newpipe.util.ExtractorHelper;
import org.schabi.newpipe.util.InfoCache; import org.schabi.newpipe.util.InfoCache;
import org.schabi.newpipe.util.ListHelper; import org.schabi.newpipe.util.ListHelper;
@ -110,7 +111,7 @@ public class VideoDetailFragment extends BaseStateFragment<StreamInfo> implement
private boolean wasRelatedStreamsExpanded = false; private boolean wasRelatedStreamsExpanded = false;
@State @State
protected int serviceId = -1; protected int serviceId = Constants.NO_SERVICE_ID;
@State @State
protected String name; protected String name;
@State @State

View File

@ -8,6 +8,7 @@ import android.view.View;
import org.schabi.newpipe.extractor.ListExtractor; import org.schabi.newpipe.extractor.ListExtractor;
import org.schabi.newpipe.extractor.ListInfo; import org.schabi.newpipe.extractor.ListInfo;
import org.schabi.newpipe.util.Constants;
import java.util.Queue; import java.util.Queue;
@ -21,7 +22,7 @@ import io.reactivex.schedulers.Schedulers;
public abstract class BaseListInfoFragment<I extends ListInfo> extends BaseListFragment<I, ListExtractor.NextItemsResult> { public abstract class BaseListInfoFragment<I extends ListInfo> extends BaseListFragment<I, ListExtractor.NextItemsResult> {
@State @State
protected int serviceId = -1; protected int serviceId = Constants.NO_SERVICE_ID;
@State @State
protected String name; protected String name;
@State @State

View File

@ -41,6 +41,7 @@ import org.schabi.newpipe.extractor.search.SearchResult;
import org.schabi.newpipe.fragments.list.BaseListFragment; import org.schabi.newpipe.fragments.list.BaseListFragment;
import org.schabi.newpipe.history.HistoryListener; import org.schabi.newpipe.history.HistoryListener;
import org.schabi.newpipe.report.UserAction; import org.schabi.newpipe.report.UserAction;
import org.schabi.newpipe.util.Constants;
import org.schabi.newpipe.util.ExtractorHelper; import org.schabi.newpipe.util.ExtractorHelper;
import org.schabi.newpipe.util.NavigationHelper; import org.schabi.newpipe.util.NavigationHelper;
import org.schabi.newpipe.util.StateSaver; import org.schabi.newpipe.util.StateSaver;
@ -86,7 +87,7 @@ public class SearchFragment extends BaseListFragment<SearchResult, ListExtractor
private SearchEngine.Filter filter = SearchEngine.Filter.ANY; private SearchEngine.Filter filter = SearchEngine.Filter.ANY;
@State @State
protected int serviceId = -1; protected int serviceId = Constants.NO_SERVICE_ID;
@State @State
protected String searchQuery = ""; protected String searchQuery = "";
@State @State

View File

@ -9,4 +9,6 @@ public class Constants {
public static final String KEY_QUERY = "key_query"; public static final String KEY_QUERY = "key_query";
public static final String KEY_THEME_CHANGE = "key_theme_change"; public static final String KEY_THEME_CHANGE = "key_theme_change";
public static final int NO_SERVICE_ID = -1;
} }

View File

@ -50,7 +50,14 @@ public final class ExtractorHelper {
//no instance //no instance
} }
private static void checkServiceId(int serviceId) {
if(serviceId == Constants.NO_SERVICE_ID) {
throw new IllegalArgumentException("serviceId is NO_SERVICE_ID");
}
}
public static Single<SearchResult> searchFor(final int serviceId, final String query, final int pageNumber, final String searchLanguage, final SearchEngine.Filter filter) { public static Single<SearchResult> searchFor(final int serviceId, final String query, final int pageNumber, final String searchLanguage, final SearchEngine.Filter filter) {
checkServiceId(serviceId);
return Single.fromCallable(new Callable<SearchResult>() { return Single.fromCallable(new Callable<SearchResult>() {
@Override @Override
public SearchResult call() throws Exception { public SearchResult call() throws Exception {
@ -61,6 +68,7 @@ public final class ExtractorHelper {
} }
public static Single<NextItemsResult> getMoreSearchItems(final int serviceId, final String query, final int nextPageNumber, final String searchLanguage, final SearchEngine.Filter filter) { public static Single<NextItemsResult> getMoreSearchItems(final int serviceId, final String query, final int nextPageNumber, final String searchLanguage, final SearchEngine.Filter filter) {
checkServiceId(serviceId);
return searchFor(serviceId, query, nextPageNumber, searchLanguage, filter) return searchFor(serviceId, query, nextPageNumber, searchLanguage, filter)
.map(new Function<SearchResult, NextItemsResult>() { .map(new Function<SearchResult, NextItemsResult>() {
@Override @Override
@ -71,6 +79,7 @@ public final class ExtractorHelper {
} }
public static Single<List<String>> suggestionsFor(final int serviceId, final String query, final String searchLanguage) { public static Single<List<String>> suggestionsFor(final int serviceId, final String query, final String searchLanguage) {
checkServiceId(serviceId);
return Single.fromCallable(new Callable<List<String>>() { return Single.fromCallable(new Callable<List<String>>() {
@Override @Override
public List<String> call() throws Exception { public List<String> call() throws Exception {
@ -80,6 +89,7 @@ public final class ExtractorHelper {
} }
public static Single<StreamInfo> getStreamInfo(final int serviceId, final String url, boolean forceLoad) { public static Single<StreamInfo> getStreamInfo(final int serviceId, final String url, boolean forceLoad) {
checkServiceId(serviceId);
return checkCache(forceLoad, serviceId, url, Single.fromCallable(new Callable<StreamInfo>() { return checkCache(forceLoad, serviceId, url, Single.fromCallable(new Callable<StreamInfo>() {
@Override @Override
public StreamInfo call() throws Exception { public StreamInfo call() throws Exception {
@ -89,6 +99,7 @@ public final class ExtractorHelper {
} }
public static Single<ChannelInfo> getChannelInfo(final int serviceId, final String url, boolean forceLoad) { public static Single<ChannelInfo> getChannelInfo(final int serviceId, final String url, boolean forceLoad) {
checkServiceId(serviceId);
return checkCache(forceLoad, serviceId, url, Single.fromCallable(new Callable<ChannelInfo>() { return checkCache(forceLoad, serviceId, url, Single.fromCallable(new Callable<ChannelInfo>() {
@Override @Override
public ChannelInfo call() throws Exception { public ChannelInfo call() throws Exception {
@ -98,6 +109,7 @@ public final class ExtractorHelper {
} }
public static Single<NextItemsResult> getMoreChannelItems(final int serviceId, final String url, final String nextStreamsUrl) { public static Single<NextItemsResult> getMoreChannelItems(final int serviceId, final String url, final String nextStreamsUrl) {
checkServiceId(serviceId);
return Single.fromCallable(new Callable<NextItemsResult>() { return Single.fromCallable(new Callable<NextItemsResult>() {
@Override @Override
public NextItemsResult call() throws Exception { public NextItemsResult call() throws Exception {
@ -107,6 +119,7 @@ public final class ExtractorHelper {
} }
public static Single<PlaylistInfo> getPlaylistInfo(final int serviceId, final String url, boolean forceLoad) { public static Single<PlaylistInfo> getPlaylistInfo(final int serviceId, final String url, boolean forceLoad) {
checkServiceId(serviceId);
return checkCache(forceLoad, serviceId, url, Single.fromCallable(new Callable<PlaylistInfo>() { return checkCache(forceLoad, serviceId, url, Single.fromCallable(new Callable<PlaylistInfo>() {
@Override @Override
public PlaylistInfo call() throws Exception { public PlaylistInfo call() throws Exception {
@ -116,6 +129,7 @@ public final class ExtractorHelper {
} }
public static Single<NextItemsResult> getMorePlaylistItems(final int serviceId, final String url, final String nextStreamsUrl) { public static Single<NextItemsResult> getMorePlaylistItems(final int serviceId, final String url, final String nextStreamsUrl) {
checkServiceId(serviceId);
return Single.fromCallable(new Callable<NextItemsResult>() { return Single.fromCallable(new Callable<NextItemsResult>() {
@Override @Override
public NextItemsResult call() throws Exception { public NextItemsResult call() throws Exception {
@ -133,6 +147,7 @@ public final class ExtractorHelper {
* and put the results in the cache. * and put the results in the cache.
*/ */
private static <I extends Info> Single<I> checkCache(boolean forceLoad, int serviceId, String url, Single<I> loadFromNetwork) { private static <I extends Info> Single<I> checkCache(boolean forceLoad, int serviceId, String url, Single<I> loadFromNetwork) {
checkServiceId(serviceId);
loadFromNetwork = loadFromNetwork.doOnSuccess(new Consumer<I>() { loadFromNetwork = loadFromNetwork.doOnSuccess(new Consumer<I>() {
@Override @Override
public void accept(@NonNull I i) throws Exception { public void accept(@NonNull I i) throws Exception {
@ -157,6 +172,7 @@ public final class ExtractorHelper {
* Default implementation uses the {@link InfoCache} to get cached results * Default implementation uses the {@link InfoCache} to get cached results
*/ */
public static <I extends Info> Maybe<I> loadFromCache(final int serviceId, final String url) { public static <I extends Info> Maybe<I> loadFromCache(final int serviceId, final String url) {
checkServiceId(serviceId);
return Maybe.defer(new Callable<MaybeSource<? extends I>>() { return Maybe.defer(new Callable<MaybeSource<? extends I>>() {
@Override @Override
public MaybeSource<? extends I> call() throws Exception { public MaybeSource<? extends I> call() throws Exception {