From 9548dfabd6f058a0952b3699f2e9ee56dd78dec2 Mon Sep 17 00:00:00 2001 From: Christian Schabesberger Date: Fri, 4 Mar 2016 14:10:41 +0100 Subject: [PATCH] made service list do propper error handling --- .../java/org/schabi/newpipe/ErrorActivity.java | 4 ++++ .../schabi/newpipe/VideoInfoItemViewCreator.java | 1 - .../schabi/newpipe/VideoItemListActivity.java | 13 ++++++++++--- .../schabi/newpipe/extractor/ServiceList.java | 16 ++++++++++------ .../services/youtube/YoutubeSearchEngine.java | 1 - .../services/youtube/YoutubeStreamExtractor.java | 12 +++++------- 6 files changed, 29 insertions(+), 18 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/ErrorActivity.java b/app/src/main/java/org/schabi/newpipe/ErrorActivity.java index 0de0cdabb..c5ba0e65c 100644 --- a/app/src/main/java/org/schabi/newpipe/ErrorActivity.java +++ b/app/src/main/java/org/schabi/newpipe/ErrorActivity.java @@ -77,9 +77,11 @@ public class ErrorActivity extends AppCompatActivity { public static final int SEARCHED = 0; public static final int REQUESTED_STREAM = 1; public static final int GET_SUGGESTIONS = 2; + public static final int SOMETHING_ELSE = 3; public static final String SEARCHED_STRING = "searched"; public static final String REQUESTED_STREAM_STRING = "requested stream"; public static final String GET_SUGGESTIONS_STRING = "get suggestions"; + public static final String SOMETHING_ELSE_STRING = "something"; public static final String ERROR_EMAIL_ADDRESS = "crashreport@newpipe.schabi.org"; public static final String ERROR_EMAIL_SUBJECT = "Exception in NewPipe " + BuildConfig.VERSION_NAME; @@ -308,6 +310,8 @@ public class ErrorActivity extends AppCompatActivity { return SEARCHED_STRING; case GET_SUGGESTIONS: return GET_SUGGESTIONS_STRING; + case SOMETHING_ELSE: + return SOMETHING_ELSE_STRING; default: return "Your description is in another castle."; } diff --git a/app/src/main/java/org/schabi/newpipe/VideoInfoItemViewCreator.java b/app/src/main/java/org/schabi/newpipe/VideoInfoItemViewCreator.java index 53950361f..9f0eefcb9 100644 --- a/app/src/main/java/org/schabi/newpipe/VideoInfoItemViewCreator.java +++ b/app/src/main/java/org/schabi/newpipe/VideoInfoItemViewCreator.java @@ -117,7 +117,6 @@ class VideoInfoItemViewCreator { } public static String getDurationString(int duration) { - Log.d("asfd", Integer.toString(duration) + " ----------------------"); String output = ""; int days = duration / (24 * 60 * 60); /* greater than a day */ duration %= (24 * 60 * 60); diff --git a/app/src/main/java/org/schabi/newpipe/VideoItemListActivity.java b/app/src/main/java/org/schabi/newpipe/VideoItemListActivity.java index 265663aed..d3062fdd3 100644 --- a/app/src/main/java/org/schabi/newpipe/VideoItemListActivity.java +++ b/app/src/main/java/org/schabi/newpipe/VideoItemListActivity.java @@ -197,9 +197,16 @@ public class VideoItemListActivity extends AppCompatActivity super.onCreate(savedInstanceState); setContentView(R.layout.activity_videoitem_list); - //------ todo: remove this line when multiservice support is implemented ------ - currentStreamingServiceId = ServiceList.getIdOfService("Youtube"); - streamingService=ServiceList.getService(currentStreamingServiceId); + try { + //------ todo: remove this line when multiservice support is implemented ------ + currentStreamingServiceId = ServiceList.getIdOfService("Youtube"); + streamingService = ServiceList.getService(currentStreamingServiceId); + } catch (Exception e) { + e.printStackTrace(); + ErrorActivity.reportError(VideoItemListActivity.this, e, null, findViewById(R.id.videoitem_list), + ErrorActivity.ErrorInfo.make(ErrorActivity.SEARCHED, + /* todo: this shoudl not be assigned static */ "Youtube", "", R.string.general_error)); + } //----------------------------------------------------------------------------- //to solve issue 38 listFragment = (VideoItemListFragment) getSupportFragmentManager() diff --git a/app/src/main/java/org/schabi/newpipe/extractor/ServiceList.java b/app/src/main/java/org/schabi/newpipe/extractor/ServiceList.java index 7306faabb..9d57225a7 100644 --- a/app/src/main/java/org/schabi/newpipe/extractor/ServiceList.java +++ b/app/src/main/java/org/schabi/newpipe/extractor/ServiceList.java @@ -36,19 +36,23 @@ public class ServiceList { public static StreamingService[] getServices() { return services; } - public static StreamingService getService(int serviceId) { - return services[serviceId]; + public static StreamingService getService(int serviceId) throws ExtractionException { + for(StreamingService s : services) { + if(s.getServiceId() == serviceId) { + return s; + } + } + throw new ExtractionException("Service not known: " + Integer.toString(serviceId)); } - public static StreamingService getService(String serviceName) { + public static StreamingService getService(String serviceName) throws ExtractionException { return services[getIdOfService(serviceName)]; } - public static int getIdOfService(String serviceName) { + public static int getIdOfService(String serviceName) throws ExtractionException { for(int i = 0; i < services.length; i++) { if(services[i].getServiceInfo().name.equals(serviceName)) { return i; } } - Log.e(TAG, "Error: Service " + serviceName + " not known."); - return -1; + throw new ExtractionException("Error: Service " + serviceName + " not known."); } } diff --git a/app/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeSearchEngine.java b/app/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeSearchEngine.java index 110f3d513..beb295f2a 100644 --- a/app/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeSearchEngine.java +++ b/app/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeSearchEngine.java @@ -11,7 +11,6 @@ import org.schabi.newpipe.extractor.ExtractionException; import org.schabi.newpipe.extractor.Parser; import org.schabi.newpipe.extractor.ParsingException; import org.schabi.newpipe.extractor.SearchEngine; -import org.schabi.newpipe.extractor.StreamExtractor; import org.schabi.newpipe.extractor.StreamPreviewInfoCollector; import org.schabi.newpipe.extractor.StreamPreviewInfoExtractor; import org.schabi.newpipe.extractor.StreamUrlIdHandler; diff --git a/app/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeStreamExtractor.java b/app/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeStreamExtractor.java index 9f33e8cc5..8ec716d2d 100644 --- a/app/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeStreamExtractor.java +++ b/app/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeStreamExtractor.java @@ -1,7 +1,5 @@ package org.schabi.newpipe.extractor.services.youtube; -import android.util.Log; - import org.json.JSONException; import org.json.JSONObject; import org.jsoup.Jsoup; @@ -15,7 +13,6 @@ import org.schabi.newpipe.extractor.ExtractionException; import org.schabi.newpipe.extractor.Downloader; import org.schabi.newpipe.extractor.Parser; import org.schabi.newpipe.extractor.ParsingException; -import org.schabi.newpipe.extractor.StreamInfo; import org.schabi.newpipe.extractor.StreamPreviewInfo; import org.schabi.newpipe.extractor.StreamUrlIdHandler; import org.schabi.newpipe.extractor.StreamExtractor; @@ -311,7 +308,7 @@ public class YoutubeStreamExtractor extends StreamExtractor { return playerArgs.getString("title"); } catch(JSONException je) {//html method je.printStackTrace(); - Log.w(TAG, "failed to load title from JSON args; trying to extract it from HTML"); + System.err.println("failed to load title from JSON args; trying to extract it from HTML"); try { // fall through to fall-back return doc.select("meta[name=title]").attr("content"); } catch (Exception e) { @@ -339,7 +336,7 @@ public class YoutubeStreamExtractor extends StreamExtractor { return playerArgs.getString("author"); } catch(JSONException je) { je.printStackTrace(); - Log.w(TAG, + System.err.println( "failed to load uploader name from JSON args; trying to extract it from HTML"); } try {//fall through to fallback HTML method return doc.select("div.yt-user-info").first().text(); @@ -387,7 +384,7 @@ public class YoutubeStreamExtractor extends StreamExtractor { try { return doc.select("link[itemprop=\"thumbnailUrl\"]").first().attr("abs:href"); } catch(Exception e) { - Log.w(TAG, "Could not find high res Thumbnail. Using low res instead"); + System.err.println("Could not find high res Thumbnail. Using low res instead"); } try { //fall through to fallback return playerArgs.getString("thumbnail_url"); @@ -512,7 +509,8 @@ public class YoutubeStreamExtractor extends StreamExtractor { } } } catch (Exception e) { - Log.w(TAG, "Could not get Video stream."); + //todo: dont log throw an error + System.err.println( "Could not get Video stream."); e.printStackTrace(); } }