From 7340bc05b4010d0ee8f8b22dee150812dcf7adb3 Mon Sep 17 00:00:00 2001 From: Coffeemakr Date: Mon, 30 Oct 2017 21:15:06 +0100 Subject: [PATCH 1/2] Small refactoring for Kore and Kodi * Improve installation procedure --- .../fragments/detail/VideoDetailFragment.java | 46 +++++++++--------- .../schabi/newpipe/util/NavigationHelper.java | 48 +++++++++++++++++++ app/src/main/res/values/strings.xml | 2 +- 3 files changed, 71 insertions(+), 25 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/fragments/detail/VideoDetailFragment.java b/app/src/main/java/org/schabi/newpipe/fragments/detail/VideoDetailFragment.java index 4bb0c2cca..3e4165875 100644 --- a/app/src/main/java/org/schabi/newpipe/fragments/detail/VideoDetailFragment.java +++ b/app/src/main/java/org/schabi/newpipe/fragments/detail/VideoDetailFragment.java @@ -1,6 +1,7 @@ package org.schabi.newpipe.fragments.detail; import android.app.Activity; +import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.content.SharedPreferences; @@ -94,7 +95,6 @@ public class VideoDetailFragment extends BaseStateFragment implement // Amount of videos to show on start private static final int INITIAL_RELATED_VIDEOS = 8; - private static final String KORE_PACKET = "org.xbmc.kore"; private ActionBarHandler actionBarHandler; private ArrayList sortedStreamVideosList; @@ -513,6 +513,24 @@ public class VideoDetailFragment extends BaseStateFragment implement return (!isLoading.get() && actionBarHandler.onItemSelected(item)) || super.onOptionsItemSelected(item); } + private static void showInstallKoreDialog(final Context context) { + final AlertDialog.Builder builder = new AlertDialog.Builder(context); + builder.setMessage(R.string.kore_not_found) + .setPositiveButton(R.string.install, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + NavigationHelper.installKore(context); + } + }) + .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + + } + }); + builder.create().show(); + } + private void setupActionBarHandler(final StreamInfo info) { if (DEBUG) Log.d(TAG, "setupActionBarHandler() called with: info = [" + info + "]"); sortedStreamVideosList = new ArrayList<>(ListHelper.getSortedStreamVideosList(activity, info.video_streams, info.video_only_streams, false)); @@ -542,30 +560,10 @@ public class VideoDetailFragment extends BaseStateFragment implement @Override public void onActionSelected(int selectedStreamId) { try { - Intent intent = new Intent(Intent.ACTION_VIEW); - intent.setPackage(KORE_PACKET); - intent.setData(Uri.parse(info.url.replace("https", "http"))); - activity.startActivity(intent); + NavigationHelper.startKore(activity, Uri.parse(info.url.replace("https", "http"))); } catch (Exception e) { - e.printStackTrace(); - AlertDialog.Builder builder = new AlertDialog.Builder(activity); - builder.setMessage(R.string.kore_not_found) - .setPositiveButton(R.string.install, new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - Intent intent = new Intent(); - intent.setAction(Intent.ACTION_VIEW); - intent.setData(Uri.parse(activity.getString(R.string.fdroid_kore_url))); - activity.startActivity(intent); - } - }) - .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - - } - }); - builder.create().show(); + if(DEBUG) Log.i(TAG, "Failed to start kore", e); + showInstallKoreDialog(activity); } } }); diff --git a/app/src/main/java/org/schabi/newpipe/util/NavigationHelper.java b/app/src/main/java/org/schabi/newpipe/util/NavigationHelper.java index b08251436..417c3bbdf 100644 --- a/app/src/main/java/org/schabi/newpipe/util/NavigationHelper.java +++ b/app/src/main/java/org/schabi/newpipe/util/NavigationHelper.java @@ -1,11 +1,14 @@ package org.schabi.newpipe.util; import android.app.Activity; +import android.content.ActivityNotFoundException; import android.content.Context; import android.content.Intent; +import android.net.Uri; import android.preference.PreferenceManager; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; +import android.support.v7.app.AppCompatActivity; import com.nostra13.universalimageloader.core.ImageLoader; @@ -303,4 +306,49 @@ public class NavigationHelper { } return null; } + + + private static Uri openMarketUrl(String packageName) { + return Uri.parse("market://details") + .buildUpon() + .appendQueryParameter("id", packageName) + .build(); + } + + private static Uri getGooglePlayUrl(String packageName) { + return Uri.parse("https://play.google.com/store/apps/details") + .buildUpon() + .appendQueryParameter("id", packageName) + .build(); + } + + private static void installApp(Context context, String packageName) { + try { + // Try market:// scheme + context.startActivity(new Intent(Intent.ACTION_VIEW, openMarketUrl(packageName))); + } catch (ActivityNotFoundException e) { + // Fall back to google play URL (don't worry F-Droid can handle it :) + context.startActivity(new Intent(Intent.ACTION_VIEW, getGooglePlayUrl(packageName))); + } + } + + /** + * Start an activity to install Kore + * @param context the context + */ + public static void installKore(Context context) { + installApp(context, context.getString(R.string.kore_package)); + } + + /** + * Start Kore app to show a video on Kodi + * @param context the context to use + * @param videoURL the url to the video stream + */ + public static void startKore(Context context, Uri videoURL) { + Intent intent = new Intent(Intent.ACTION_VIEW); + intent.setPackage(context.getString(R.string.kore_package)); + intent.setData(videoURL); + context.startActivity(intent); + } } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 4f06dc4c4..400f1b560 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -53,7 +53,7 @@ Only some devices support playing 2K/4K videos Play with Kodi Kore app not found. Install it? - https://f-droid.org/repository/browse/?fdfilter=Kore&fdid=org.xbmc.kore + org.xbmc.kore Show \"Play with Kodi\" option Display an option to play a video via Kodi media center Audio From 6f18dd26a2cf5fd7d8a9fa4ae27e93906a541dde Mon Sep 17 00:00:00 2001 From: Coffeemakr Date: Mon, 30 Oct 2017 21:31:59 +0100 Subject: [PATCH 2/2] Call history listener for Kodi (closes #798) If Kore (the Kodi App) was sucessfully started the history listener is invoked. --- app/src/main/java/org/schabi/newpipe/MainActivity.java | 3 ++- .../newpipe/fragments/detail/VideoDetailFragment.java | 5 ++++- .../org/schabi/newpipe/history/HistoryListener.java | 7 +++++-- .../java/org/schabi/newpipe/util/NavigationHelper.java | 10 ++++++++-- 4 files changed, 19 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/MainActivity.java b/app/src/main/java/org/schabi/newpipe/MainActivity.java index 03b856d31..056db3500 100644 --- a/app/src/main/java/org/schabi/newpipe/MainActivity.java +++ b/app/src/main/java/org/schabi/newpipe/MainActivity.java @@ -27,6 +27,7 @@ import android.os.Handler; import android.os.Looper; import android.preference.PreferenceManager; import android.support.annotation.NonNull; +import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.support.v7.app.ActionBar; import android.support.v7.app.AppCompatActivity; @@ -309,7 +310,7 @@ public class MainActivity extends AppCompatActivity implements HistoryListener { } @Override - public void onVideoPlayed(StreamInfo streamInfo, VideoStream videoStream) { + public void onVideoPlayed(StreamInfo streamInfo, @Nullable VideoStream videoStream) { addWatchHistoryEntry(streamInfo); } diff --git a/app/src/main/java/org/schabi/newpipe/fragments/detail/VideoDetailFragment.java b/app/src/main/java/org/schabi/newpipe/fragments/detail/VideoDetailFragment.java index 3e4165875..c5b743193 100644 --- a/app/src/main/java/org/schabi/newpipe/fragments/detail/VideoDetailFragment.java +++ b/app/src/main/java/org/schabi/newpipe/fragments/detail/VideoDetailFragment.java @@ -560,7 +560,10 @@ public class VideoDetailFragment extends BaseStateFragment implement @Override public void onActionSelected(int selectedStreamId) { try { - NavigationHelper.startKore(activity, Uri.parse(info.url.replace("https", "http"))); + NavigationHelper.playWithKore(activity, Uri.parse(info.url.replace("https", "http"))); + if(activity instanceof HistoryListener) { + ((HistoryListener) activity).onVideoPlayed(info, null); + } } catch (Exception e) { if(DEBUG) Log.i(TAG, "Failed to start kore", e); showInstallKoreDialog(activity); diff --git a/app/src/main/java/org/schabi/newpipe/history/HistoryListener.java b/app/src/main/java/org/schabi/newpipe/history/HistoryListener.java index 8b6c91328..5c729b022 100644 --- a/app/src/main/java/org/schabi/newpipe/history/HistoryListener.java +++ b/app/src/main/java/org/schabi/newpipe/history/HistoryListener.java @@ -1,5 +1,7 @@ package org.schabi.newpipe.history; +import android.support.annotation.Nullable; + import org.schabi.newpipe.extractor.stream.AudioStream; import org.schabi.newpipe.extractor.stream.StreamInfo; import org.schabi.newpipe.extractor.stream.VideoStream; @@ -9,9 +11,10 @@ public interface HistoryListener { * Called when a video is played * * @param streamInfo the stream info - * @param videoStream the video stream that is played + * @param videoStream the video stream that is played. Can be null if it's not sure what + * quality was viewed (e.g. with Kodi). */ - void onVideoPlayed(StreamInfo streamInfo, VideoStream videoStream); + void onVideoPlayed(StreamInfo streamInfo, @Nullable VideoStream videoStream); /** * Called when the audio is played in the background diff --git a/app/src/main/java/org/schabi/newpipe/util/NavigationHelper.java b/app/src/main/java/org/schabi/newpipe/util/NavigationHelper.java index 417c3bbdf..e58d9996a 100644 --- a/app/src/main/java/org/schabi/newpipe/util/NavigationHelper.java +++ b/app/src/main/java/org/schabi/newpipe/util/NavigationHelper.java @@ -342,10 +342,16 @@ public class NavigationHelper { /** * Start Kore app to show a video on Kodi + * + * For a list of supported urls see the + * + * Kore source code + * . + * * @param context the context to use - * @param videoURL the url to the video stream + * @param videoURL the url to the video */ - public static void startKore(Context context, Uri videoURL) { + public static void playWithKore(Context context, Uri videoURL) { Intent intent = new Intent(Intent.ACTION_VIEW); intent.setPackage(context.getString(R.string.kore_package)); intent.setData(videoURL);