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 45cfb4177..63b7e9ace 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; @@ -98,7 +99,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; @@ -561,6 +561,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)); @@ -590,30 +608,13 @@ 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.playWithKore(activity, Uri.parse(info.url.replace("https", "http"))); + if(activity instanceof HistoryListener) { + ((HistoryListener) activity).onVideoPlayed(info, null); + } } 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/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 f287c3a96..a68494706 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; @@ -294,4 +297,55 @@ 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 + * + * For a list of supported urls see the + * + * Kore source code + * . + * + * @param context the context to use + * @param videoURL the url to the video + */ + 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); + context.startActivity(intent); + } } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index a0068b07e..ad69915b9 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