From 23e0196fcc987a0188149ce0123f5b82699a2477 Mon Sep 17 00:00:00 2001 From: Adam Howard Date: Thu, 19 Nov 2015 00:08:51 +0000 Subject: [PATCH] * fixed counterintuitive back button behaviour in PlayVideoActivity; see https://github.com/theScrabi/NewPipe/issues/99 * fixed a rarely-caused NullPointerException caused by a related video's view-count field being missing --- .../org/schabi/newpipe/PlayVideoActivity.java | 26 ++++++++++++++++++- .../services/youtube/YoutubeExtractor.java | 21 ++++++++++----- 2 files changed, 40 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/PlayVideoActivity.java b/app/src/main/java/org/schabi/newpipe/PlayVideoActivity.java index 6b90161e9..cf1593903 100644 --- a/app/src/main/java/org/schabi/newpipe/PlayVideoActivity.java +++ b/app/src/main/java/org/schabi/newpipe/PlayVideoActivity.java @@ -15,6 +15,7 @@ import android.support.v7.app.AppCompatActivity; import android.util.DisplayMetrics; import android.util.Log; import android.view.Display; +import android.view.KeyEvent; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; @@ -86,7 +87,30 @@ public class PlayVideoActivity extends AppCompatActivity { actionBar.setDisplayHomeAsUpEnabled(true); Intent intent = getIntent(); if(mediaController == null) { - mediaController = new MediaController(this); + //prevents back button hiding media controller controls (after showing them) + //instead of exiting video + //see http://stackoverflow.com/questions/6051825 + //also solves https://github.com/theScrabi/NewPipe/issues/99 + mediaController = new MediaController(this) { + @Override + public boolean dispatchKeyEvent(KeyEvent event) { + int keyCode = event.getKeyCode(); + final boolean uniqueDown = event.getRepeatCount() == 0 + && event.getAction() == KeyEvent.ACTION_DOWN; + if (keyCode == KeyEvent.KEYCODE_BACK) { + if (uniqueDown) + { + if (isShowing()) { + finish(); + } else { + hide(); + } + } + return true; + } + return super.dispatchKeyEvent(event); + } + }; } position = intent.getIntExtra(START_POSITION, 0)*1000;//convert from seconds to milliseconds diff --git a/app/src/main/java/org/schabi/newpipe/services/youtube/YoutubeExtractor.java b/app/src/main/java/org/schabi/newpipe/services/youtube/YoutubeExtractor.java index 9c47b76cc..c3ebe3a40 100644 --- a/app/src/main/java/org/schabi/newpipe/services/youtube/YoutubeExtractor.java +++ b/app/src/main/java/org/schabi/newpipe/services/youtube/YoutubeExtractor.java @@ -494,7 +494,7 @@ public class YoutubeExtractor extends Extractor { * which is a subset of the fields in a full VideoInfo.*/ private VideoPreviewInfo extractVideoPreviewInfo(Element li) { VideoPreviewInfo info = new VideoPreviewInfo(); - info.webpage_url = li.select("a[class*=\"content-link\"]").first() + info.webpage_url = li.select("a.content-link").first() .attr("abs:href"); try { info.id = matchGroup1("v=([0-9a-zA-Z-]*)", info.webpage_url); @@ -503,12 +503,21 @@ public class YoutubeExtractor extends Extractor { } //todo: check NullPointerException causing - info.title = li.select("span[class=\"title\"]").first().text(); - info.view_count = Long.parseLong(li.select("span[class*=\"view-count\"]") - .first().text().replaceAll("[^\\d]", "")); - info.uploader = li.select("span[class=\"g-hovercard\"]").first().text(); + info.title = li.select("span.title").first().text(); + //this page causes the NullPointerException, after finding it by searching for "tjvg": + //https://www.youtube.com/watch?v=Uqg0aEhLFAg + String views = li.select("span.view-count").first().text(); + Log.i(TAG, "title:"+info.title); + Log.i(TAG, "view count:"+views); + try { + info.view_count = Long.parseLong(li.select("span.view-count") + .first().text().replaceAll("[^\\d]", "")); + } catch (NullPointerException e) {//related videos sometimes have no view count + info.view_count = 0; + } + info.uploader = li.select("span.g-hovercard").first().text(); - info.duration = li.select("span[class=\"video-time\"]").first().text(); + info.duration = li.select("span.video-time").first().text(); Element img = li.select("img").first(); info.thumbnail_url = img.attr("abs:src");