2017-04-09 17:34:00 +00:00
|
|
|
package org.schabi.newpipe.util;
|
|
|
|
|
2017-07-04 07:31:53 +00:00
|
|
|
import android.app.Activity;
|
2017-04-09 17:34:00 +00:00
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.preference.PreferenceManager;
|
2017-06-05 19:33:01 +00:00
|
|
|
import android.support.v4.app.Fragment;
|
|
|
|
import android.support.v4.app.FragmentManager;
|
|
|
|
|
|
|
|
import com.nostra13.universalimageloader.core.ImageLoader;
|
2017-04-09 17:34:00 +00:00
|
|
|
|
|
|
|
import org.schabi.newpipe.MainActivity;
|
|
|
|
import org.schabi.newpipe.R;
|
2017-09-03 06:04:18 +00:00
|
|
|
import org.schabi.newpipe.about.AboutActivity;
|
2017-07-04 07:31:53 +00:00
|
|
|
import org.schabi.newpipe.download.DownloadActivity;
|
2017-04-09 17:34:00 +00:00
|
|
|
import org.schabi.newpipe.extractor.NewPipe;
|
2017-09-15 17:27:39 +00:00
|
|
|
import org.schabi.newpipe.extractor.ServiceList;
|
2017-04-09 17:34:00 +00:00
|
|
|
import org.schabi.newpipe.extractor.StreamingService;
|
2017-09-15 17:27:39 +00:00
|
|
|
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
|
2017-09-03 06:04:18 +00:00
|
|
|
import org.schabi.newpipe.extractor.stream.AudioStream;
|
|
|
|
import org.schabi.newpipe.extractor.stream.StreamInfo;
|
2017-06-05 19:33:01 +00:00
|
|
|
import org.schabi.newpipe.fragments.MainFragment;
|
2017-04-09 17:34:00 +00:00
|
|
|
import org.schabi.newpipe.fragments.detail.VideoDetailFragment;
|
2017-09-03 06:04:18 +00:00
|
|
|
import org.schabi.newpipe.fragments.list.channel.ChannelFragment;
|
|
|
|
import org.schabi.newpipe.fragments.list.feed.FeedFragment;
|
|
|
|
import org.schabi.newpipe.fragments.list.playlist.PlaylistFragment;
|
|
|
|
import org.schabi.newpipe.fragments.list.search.SearchFragment;
|
|
|
|
import org.schabi.newpipe.history.HistoryActivity;
|
2017-04-17 04:21:48 +00:00
|
|
|
import org.schabi.newpipe.player.BackgroundPlayer;
|
|
|
|
import org.schabi.newpipe.player.BasePlayer;
|
|
|
|
import org.schabi.newpipe.player.VideoPlayer;
|
2017-07-04 07:31:53 +00:00
|
|
|
import org.schabi.newpipe.settings.SettingsActivity;
|
2017-04-09 17:34:00 +00:00
|
|
|
|
2017-09-03 06:04:18 +00:00
|
|
|
import java.util.ArrayList;
|
|
|
|
|
2017-04-09 17:34:00 +00:00
|
|
|
@SuppressWarnings({"unused", "WeakerAccess"})
|
|
|
|
public class NavigationHelper {
|
2017-07-10 03:09:57 +00:00
|
|
|
public static final String MAIN_FRAGMENT_TAG = "main_fragment_tag";
|
2017-04-09 17:34:00 +00:00
|
|
|
|
2017-04-26 19:32:20 +00:00
|
|
|
/*//////////////////////////////////////////////////////////////////////////
|
|
|
|
// Players
|
|
|
|
//////////////////////////////////////////////////////////////////////////*/
|
|
|
|
|
2017-04-17 04:21:48 +00:00
|
|
|
public static Intent getOpenVideoPlayerIntent(Context context, Class targetClazz, StreamInfo info, int selectedStreamIndex) {
|
|
|
|
Intent mIntent = new Intent(context, targetClazz)
|
2017-09-03 06:04:18 +00:00
|
|
|
.putExtra(BasePlayer.VIDEO_TITLE, info.name)
|
|
|
|
.putExtra(BasePlayer.VIDEO_URL, info.url)
|
2017-04-17 04:21:48 +00:00
|
|
|
.putExtra(BasePlayer.VIDEO_THUMBNAIL_URL, info.thumbnail_url)
|
2017-09-03 06:04:18 +00:00
|
|
|
.putExtra(BasePlayer.CHANNEL_NAME, info.uploader_name)
|
2017-04-17 04:21:48 +00:00
|
|
|
.putExtra(VideoPlayer.INDEX_SEL_VIDEO_STREAM, selectedStreamIndex)
|
2017-09-03 06:04:18 +00:00
|
|
|
.putExtra(VideoPlayer.VIDEO_STREAMS_LIST, new ArrayList<>(ListHelper.getSortedStreamVideosList(context, info.video_streams, info.video_only_streams, false)))
|
|
|
|
.putExtra(VideoPlayer.VIDEO_ONLY_AUDIO_STREAM, ListHelper.getHighestQualityAudio(info.audio_streams));
|
|
|
|
if (info.start_position > 0) mIntent.putExtra(BasePlayer.START_POSITION, info.start_position * 1000L);
|
2017-04-17 04:21:48 +00:00
|
|
|
return mIntent;
|
2017-04-12 06:07:15 +00:00
|
|
|
}
|
|
|
|
|
2017-04-17 04:21:48 +00:00
|
|
|
public static Intent getOpenVideoPlayerIntent(Context context, Class targetClazz, VideoPlayer instance) {
|
2017-04-12 06:07:15 +00:00
|
|
|
return new Intent(context, targetClazz)
|
2017-04-17 04:21:48 +00:00
|
|
|
.putExtra(BasePlayer.VIDEO_TITLE, instance.getVideoTitle())
|
|
|
|
.putExtra(BasePlayer.VIDEO_URL, instance.getVideoUrl())
|
|
|
|
.putExtra(BasePlayer.VIDEO_THUMBNAIL_URL, instance.getVideoThumbnailUrl())
|
2017-09-03 06:04:18 +00:00
|
|
|
.putExtra(BasePlayer.CHANNEL_NAME, instance.getUploaderName())
|
2017-04-17 04:21:48 +00:00
|
|
|
.putExtra(VideoPlayer.INDEX_SEL_VIDEO_STREAM, instance.getSelectedStreamIndex())
|
|
|
|
.putExtra(VideoPlayer.VIDEO_STREAMS_LIST, instance.getVideoStreamsList())
|
|
|
|
.putExtra(VideoPlayer.VIDEO_ONLY_AUDIO_STREAM, instance.getAudioStream())
|
2017-09-03 06:04:18 +00:00
|
|
|
.putExtra(BasePlayer.START_POSITION, instance.getPlayer().getCurrentPosition())
|
2017-06-26 02:41:52 +00:00
|
|
|
.putExtra(BasePlayer.PLAYBACK_SPEED, instance.getPlaybackSpeed());
|
2017-04-17 04:21:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static Intent getOpenBackgroundPlayerIntent(Context context, StreamInfo info) {
|
2017-09-03 06:04:18 +00:00
|
|
|
return getOpenBackgroundPlayerIntent(context, info, info.audio_streams.get(ListHelper.getDefaultAudioFormat(context, info.audio_streams)));
|
2017-04-17 04:21:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static Intent getOpenBackgroundPlayerIntent(Context context, StreamInfo info, AudioStream audioStream) {
|
|
|
|
Intent mIntent = new Intent(context, BackgroundPlayer.class)
|
2017-09-03 06:04:18 +00:00
|
|
|
.putExtra(BasePlayer.VIDEO_TITLE, info.name)
|
|
|
|
.putExtra(BasePlayer.VIDEO_URL, info.url)
|
2017-04-17 04:21:48 +00:00
|
|
|
.putExtra(BasePlayer.VIDEO_THUMBNAIL_URL, info.thumbnail_url)
|
2017-09-03 06:04:18 +00:00
|
|
|
.putExtra(BasePlayer.CHANNEL_NAME, info.uploader_name)
|
2017-04-17 04:21:48 +00:00
|
|
|
.putExtra(BackgroundPlayer.AUDIO_STREAM, audioStream);
|
2017-09-03 06:04:18 +00:00
|
|
|
if (info.start_position > 0) mIntent.putExtra(BasePlayer.START_POSITION, info.start_position * 1000L);
|
2017-04-17 04:21:48 +00:00
|
|
|
return mIntent;
|
2017-04-12 06:07:15 +00:00
|
|
|
}
|
|
|
|
|
2017-04-09 17:34:00 +00:00
|
|
|
/*//////////////////////////////////////////////////////////////////////////
|
2017-06-05 19:33:01 +00:00
|
|
|
// Through FragmentManager
|
2017-04-09 17:34:00 +00:00
|
|
|
//////////////////////////////////////////////////////////////////////////*/
|
|
|
|
|
2017-07-10 03:09:57 +00:00
|
|
|
public static void gotoMainFragment(FragmentManager fragmentManager) {
|
2017-06-05 19:33:01 +00:00
|
|
|
ImageLoader.getInstance().clearMemoryCache();
|
2017-07-10 03:09:57 +00:00
|
|
|
|
|
|
|
boolean popped = fragmentManager.popBackStackImmediate(MAIN_FRAGMENT_TAG, 0);
|
|
|
|
if (!popped) openMainFragment(fragmentManager);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void openMainFragment(FragmentManager fragmentManager) {
|
2017-09-03 06:04:18 +00:00
|
|
|
InfoCache.getInstance().trimCache();
|
|
|
|
|
2017-07-10 03:09:57 +00:00
|
|
|
fragmentManager.popBackStackImmediate(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
|
2017-06-05 19:33:01 +00:00
|
|
|
fragmentManager.beginTransaction()
|
2017-09-03 06:04:18 +00:00
|
|
|
.setCustomAnimations(R.animator.custom_fade_in, R.animator.custom_fade_out, R.animator.custom_fade_in, R.animator.custom_fade_out)
|
2017-06-05 19:33:01 +00:00
|
|
|
.replace(R.id.fragment_holder, new MainFragment())
|
2017-07-10 03:09:57 +00:00
|
|
|
.addToBackStack(MAIN_FRAGMENT_TAG)
|
2017-06-05 19:33:01 +00:00
|
|
|
.commit();
|
2017-04-09 17:34:00 +00:00
|
|
|
}
|
|
|
|
|
2017-06-05 19:33:01 +00:00
|
|
|
public static void openSearchFragment(FragmentManager fragmentManager, int serviceId, String query) {
|
|
|
|
fragmentManager.beginTransaction()
|
2017-09-03 06:04:18 +00:00
|
|
|
.setCustomAnimations(R.animator.custom_fade_in, R.animator.custom_fade_out, R.animator.custom_fade_in, R.animator.custom_fade_out)
|
2017-06-05 19:33:01 +00:00
|
|
|
.replace(R.id.fragment_holder, SearchFragment.getInstance(serviceId, query))
|
|
|
|
.addToBackStack(null)
|
|
|
|
.commit();
|
2017-04-09 17:34:00 +00:00
|
|
|
}
|
|
|
|
|
2017-06-05 19:33:01 +00:00
|
|
|
public static void openVideoDetailFragment(FragmentManager fragmentManager, int serviceId, String url, String title) {
|
|
|
|
openVideoDetailFragment(fragmentManager, serviceId, url, title, false);
|
2017-04-09 17:34:00 +00:00
|
|
|
}
|
|
|
|
|
2017-06-05 19:33:01 +00:00
|
|
|
public static void openVideoDetailFragment(FragmentManager fragmentManager, int serviceId, String url, String title, boolean autoPlay) {
|
|
|
|
Fragment fragment = fragmentManager.findFragmentById(R.id.fragment_holder);
|
|
|
|
if (title == null) title = "";
|
|
|
|
|
|
|
|
if (fragment instanceof VideoDetailFragment && fragment.isVisible()) {
|
|
|
|
VideoDetailFragment detailFragment = (VideoDetailFragment) fragment;
|
|
|
|
detailFragment.setAutoplay(autoPlay);
|
|
|
|
detailFragment.selectAndLoadVideo(serviceId, url, title);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
VideoDetailFragment instance = VideoDetailFragment.getInstance(serviceId, url, title);
|
|
|
|
instance.setAutoplay(autoPlay);
|
|
|
|
|
|
|
|
fragmentManager.beginTransaction()
|
2017-09-03 06:04:18 +00:00
|
|
|
.setCustomAnimations(R.animator.custom_fade_in, R.animator.custom_fade_out, R.animator.custom_fade_in, R.animator.custom_fade_out)
|
2017-06-05 19:33:01 +00:00
|
|
|
.replace(R.id.fragment_holder, instance)
|
|
|
|
.addToBackStack(null)
|
|
|
|
.commit();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void openChannelFragment(FragmentManager fragmentManager, int serviceId, String url, String name) {
|
|
|
|
if (name == null) name = "";
|
|
|
|
fragmentManager.beginTransaction()
|
2017-09-03 06:04:18 +00:00
|
|
|
.setCustomAnimations(R.animator.custom_fade_in, R.animator.custom_fade_out, R.animator.custom_fade_in, R.animator.custom_fade_out)
|
2017-06-05 19:33:01 +00:00
|
|
|
.replace(R.id.fragment_holder, ChannelFragment.getInstance(serviceId, url, name))
|
|
|
|
.addToBackStack(null)
|
|
|
|
.commit();
|
2017-04-09 17:34:00 +00:00
|
|
|
}
|
|
|
|
|
2017-09-03 06:04:18 +00:00
|
|
|
public static void openPlaylistFragment(FragmentManager fragmentManager, int serviceId, String url, String name) {
|
|
|
|
if (name == null) name = "";
|
|
|
|
fragmentManager.beginTransaction()
|
|
|
|
.setCustomAnimations(R.animator.custom_fade_in, R.animator.custom_fade_out, R.animator.custom_fade_in, R.animator.custom_fade_out)
|
|
|
|
.replace(R.id.fragment_holder, PlaylistFragment.getInstance(serviceId, url, name))
|
|
|
|
.addToBackStack(null)
|
|
|
|
.commit();
|
|
|
|
}
|
|
|
|
|
2017-08-07 13:02:30 +00:00
|
|
|
public static void openWhatsNewFragment(FragmentManager fragmentManager) {
|
|
|
|
fragmentManager.beginTransaction()
|
2017-09-03 06:04:18 +00:00
|
|
|
.setCustomAnimations(R.animator.custom_fade_in, R.animator.custom_fade_out, R.animator.custom_fade_in, R.animator.custom_fade_out)
|
2017-08-07 13:02:30 +00:00
|
|
|
.replace(R.id.fragment_holder, new FeedFragment())
|
|
|
|
.addToBackStack(null)
|
|
|
|
.commit();
|
|
|
|
}
|
|
|
|
|
2017-04-09 17:34:00 +00:00
|
|
|
/*//////////////////////////////////////////////////////////////////////////
|
|
|
|
// Through Intents
|
|
|
|
//////////////////////////////////////////////////////////////////////////*/
|
|
|
|
|
2017-06-05 19:33:01 +00:00
|
|
|
public static void openSearch(Context context, int serviceId, String query) {
|
|
|
|
Intent mIntent = new Intent(context, MainActivity.class);
|
|
|
|
mIntent.putExtra(Constants.KEY_SERVICE_ID, serviceId);
|
|
|
|
mIntent.putExtra(Constants.KEY_QUERY, query);
|
|
|
|
mIntent.putExtra(Constants.KEY_OPEN_SEARCH, true);
|
|
|
|
context.startActivity(mIntent);
|
2017-04-09 17:34:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static void openChannel(Context context, int serviceId, String url) {
|
|
|
|
openChannel(context, serviceId, url, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void openChannel(Context context, int serviceId, String url, String name) {
|
|
|
|
Intent openIntent = getOpenIntent(context, url, serviceId, StreamingService.LinkType.CHANNEL);
|
|
|
|
if (name != null && !name.isEmpty()) openIntent.putExtra(Constants.KEY_TITLE, name);
|
|
|
|
context.startActivity(openIntent);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void openVideoDetail(Context context, int serviceId, String url) {
|
|
|
|
openVideoDetail(context, serviceId, url, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void openVideoDetail(Context context, int serviceId, String url, String title) {
|
|
|
|
Intent openIntent = getOpenIntent(context, url, serviceId, StreamingService.LinkType.STREAM);
|
|
|
|
if (title != null && !title.isEmpty()) openIntent.putExtra(Constants.KEY_TITLE, title);
|
|
|
|
context.startActivity(openIntent);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void openMainActivity(Context context) {
|
|
|
|
Intent mIntent = new Intent(context, MainActivity.class);
|
2017-09-03 06:04:18 +00:00
|
|
|
mIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
|
|
mIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
|
2017-04-09 17:34:00 +00:00
|
|
|
context.startActivity(mIntent);
|
|
|
|
}
|
|
|
|
|
2017-09-03 06:04:18 +00:00
|
|
|
public static void openAbout(Context context) {
|
|
|
|
Intent intent = new Intent(context, AboutActivity.class);
|
|
|
|
context.startActivity(intent);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void openHistory(Context context) {
|
|
|
|
Intent intent = new Intent(context, HistoryActivity.class);
|
|
|
|
context.startActivity(intent);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void openSettings(Context context) {
|
|
|
|
Intent intent = new Intent(context, SettingsActivity.class);
|
|
|
|
context.startActivity(intent);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean openDownloads(Activity activity) {
|
|
|
|
if (!PermissionHelper.checkStoragePermissions(activity)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
Intent intent = new Intent(activity, DownloadActivity.class);
|
|
|
|
activity.startActivity(intent);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*//////////////////////////////////////////////////////////////////////////
|
|
|
|
// Link handling
|
|
|
|
//////////////////////////////////////////////////////////////////////////*/
|
|
|
|
|
2017-09-15 17:27:39 +00:00
|
|
|
public static boolean openByLink(Context context, String url) {
|
|
|
|
Intent intentByLink;
|
|
|
|
try {
|
|
|
|
intentByLink = getIntentByLink(context, url);
|
|
|
|
} catch (ExtractionException e) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-06-05 19:33:01 +00:00
|
|
|
intentByLink.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
|
|
intentByLink.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
|
|
|
|
context.startActivity(intentByLink);
|
2017-09-15 17:27:39 +00:00
|
|
|
return true;
|
2017-04-26 19:32:20 +00:00
|
|
|
}
|
|
|
|
|
2017-04-09 17:34:00 +00:00
|
|
|
private static Intent getOpenIntent(Context context, String url, int serviceId, StreamingService.LinkType type) {
|
|
|
|
Intent mIntent = new Intent(context, MainActivity.class);
|
|
|
|
mIntent.putExtra(Constants.KEY_SERVICE_ID, serviceId);
|
|
|
|
mIntent.putExtra(Constants.KEY_URL, url);
|
|
|
|
mIntent.putExtra(Constants.KEY_LINK_TYPE, type);
|
|
|
|
return mIntent;
|
|
|
|
}
|
|
|
|
|
2017-09-15 17:27:39 +00:00
|
|
|
public static Intent getIntentByLink(Context context, String url) throws ExtractionException {
|
|
|
|
return getIntentByLink(context, NewPipe.getServiceByUrl(url), url);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Intent getIntentByLink(Context context, StreamingService service, String url) throws ExtractionException {
|
|
|
|
if (service != ServiceList.YouTube.getService()) {
|
|
|
|
throw new ExtractionException("Service not supported at the moment");
|
|
|
|
}
|
2017-09-03 06:04:18 +00:00
|
|
|
|
2017-04-09 17:34:00 +00:00
|
|
|
int serviceId = service.getServiceId();
|
2017-09-03 06:04:18 +00:00
|
|
|
StreamingService.LinkType linkType = service.getLinkTypeByUrl(url);
|
|
|
|
|
|
|
|
if (linkType == StreamingService.LinkType.NONE) {
|
2017-09-15 17:27:39 +00:00
|
|
|
throw new ExtractionException("Url not known to service. service=" + serviceId + " url=" + url);
|
2017-09-03 06:04:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
url = getCleanUrl(service, url, linkType);
|
|
|
|
Intent rIntent = getOpenIntent(context, url, serviceId, linkType);
|
|
|
|
|
|
|
|
switch (linkType) {
|
2017-04-09 17:34:00 +00:00
|
|
|
case STREAM:
|
2017-09-03 06:04:18 +00:00
|
|
|
rIntent.putExtra(VideoDetailFragment.AUTO_PLAY, PreferenceManager.getDefaultSharedPreferences(context)
|
2017-04-09 17:34:00 +00:00
|
|
|
.getBoolean(context.getString(R.string.autoplay_through_intent_key), false));
|
2017-09-03 06:04:18 +00:00
|
|
|
break;
|
2017-04-09 17:34:00 +00:00
|
|
|
}
|
2017-07-04 07:31:53 +00:00
|
|
|
|
2017-09-03 06:04:18 +00:00
|
|
|
return rIntent;
|
2017-07-04 07:31:53 +00:00
|
|
|
}
|
|
|
|
|
2017-09-15 17:27:39 +00:00
|
|
|
private static String getCleanUrl(StreamingService service, String dirtyUrl, StreamingService.LinkType linkType) throws ExtractionException {
|
2017-09-03 06:04:18 +00:00
|
|
|
switch (linkType) {
|
|
|
|
case STREAM:
|
|
|
|
return service.getStreamUrlIdHandler().cleanUrl(dirtyUrl);
|
|
|
|
case CHANNEL:
|
|
|
|
return service.getChannelUrlIdHandler().cleanUrl(dirtyUrl);
|
|
|
|
case PLAYLIST:
|
|
|
|
return service.getPlaylistUrlIdHandler().cleanUrl(dirtyUrl);
|
|
|
|
case NONE:
|
|
|
|
break;
|
2017-07-04 07:31:53 +00:00
|
|
|
}
|
2017-09-03 06:04:18 +00:00
|
|
|
return null;
|
2017-07-04 07:31:53 +00:00
|
|
|
}
|
2017-04-09 17:34:00 +00:00
|
|
|
}
|