Search in sources :

Example 1 with FlingLayout

use of com.github.chuross.flinglayout.FlingLayout in project twicalico by moko256.

the class ImagePagerChildFragment method onCreateView.

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    FlingLayout view = (FlingLayout) inflater.inflate(R.layout.fragment_image_pager_child, null);
    view.setDismissListener(() -> {
        getActivity().finish();
        return Unit.INSTANCE;
    });
    view.setPositionChangeListener((Integer top, Integer left, Float dragRangeRate) -> {
        view.setBackgroundColor(Color.argb(Math.round(255 * (1.0F - dragRangeRate)), 0, 0, 0));
        return Unit.INSTANCE;
    });
    MediaEntity mediaEntity;
    if (getArguments() == null)
        return view;
    mediaEntity = (MediaEntity) getArguments().getSerializable(FRAG_MEDIA_ENTITY);
    if (mediaEntity == null)
        return view;
    switch(mediaEntity.getType()) {
        case "video":
            String videoPath = null;
            boolean isHls = false;
            for (MediaEntity.Variant variant : mediaEntity.getVideoVariants()) {
                if (variant.getContentType().equals("application/x-mpegURL")) {
                    videoPath = variant.getUrl();
                    isHls = true;
                }
            }
            if (videoPath == null) {
                videoPath = mediaEntity.getVideoVariants()[0].getUrl();
            }
            videoPlayView = view.findViewById(R.id.fragment_image_pager_video);
            videoPlayView.setVisibility(View.VISIBLE);
            videoPlayView.setControllerVisibilityListener(visibility -> {
                if (visibility != View.VISIBLE) {
                    hideSystemUI();
                } else {
                    showSystemUI();
                }
            });
            getActivity().getWindow().getDecorView().setOnSystemUiVisibilityChangeListener(visibility -> {
                if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {
                    ((AppCompatActivity) getActivity()).getSupportActionBar().show();
                    videoPlayView.showController();
                }
            });
            player = ExoPlayerFactory.newSimpleInstance(new DefaultRenderersFactory(getContext()), new DefaultTrackSelector(new AdaptiveTrackSelection.Factory(new DefaultBandwidthMeter(), Integer.MAX_VALUE, AdaptiveTrackSelection.DEFAULT_MIN_DURATION_FOR_QUALITY_INCREASE_MS, AdaptiveTrackSelection.DEFAULT_MAX_DURATION_FOR_QUALITY_DECREASE_MS, AdaptiveTrackSelection.DEFAULT_MIN_DURATION_TO_RETAIN_AFTER_DISCARD_MS, AdaptiveTrackSelection.DEFAULT_BANDWIDTH_FRACTION)), new DefaultLoadControl());
            if (savedInstanceState != null) {
                player.seekTo(savedInstanceState.getLong("video_time", 0));
            }
            videoPlayView.setPlayer(player);
            player.prepare((isHls) ? new HlsMediaSource.Factory(new OkHttpDataSourceFactory(GlobalApplication.getOkHttpClient(), getResources().getText(R.string.app_name).toString(), null)).createMediaSource(Uri.parse(videoPath)) : new ExtractorMediaSource.Factory(new OkHttpDataSourceFactory(GlobalApplication.getOkHttpClient(), getResources().getText(R.string.app_name).toString(), null)).createMediaSource(Uri.parse(mediaEntity.getVideoVariants()[0].getUrl()), new Handler(), null));
            break;
        case "animated_gif":
            videoPlayView = view.findViewById(R.id.fragment_image_pager_video);
            videoPlayView.setVisibility(View.VISIBLE);
            videoPlayView.setControllerVisibilityListener(visibility -> {
                if (visibility != View.VISIBLE) {
                    hideSystemUI();
                } else {
                    showSystemUI();
                }
            });
            getActivity().getWindow().getDecorView().setOnSystemUiVisibilityChangeListener(visibility -> {
                if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {
                    ((AppCompatActivity) getActivity()).getSupportActionBar().show();
                    videoPlayView.showController();
                }
            });
            player = ExoPlayerFactory.newSimpleInstance(new DefaultRenderersFactory(getContext()), new DefaultTrackSelector(new AdaptiveTrackSelection.Factory(new DefaultBandwidthMeter(), Integer.MAX_VALUE, AdaptiveTrackSelection.DEFAULT_MIN_DURATION_FOR_QUALITY_INCREASE_MS, AdaptiveTrackSelection.DEFAULT_MAX_DURATION_FOR_QUALITY_DECREASE_MS, AdaptiveTrackSelection.DEFAULT_MIN_DURATION_TO_RETAIN_AFTER_DISCARD_MS, AdaptiveTrackSelection.DEFAULT_BANDWIDTH_FRACTION)), new DefaultLoadControl());
            if (savedInstanceState != null) {
                player.seekTo(savedInstanceState.getLong("video_time", 0));
            }
            videoPlayView.setPlayer(player);
            player.prepare(new LoopingMediaSource(new ExtractorMediaSource.Factory(new OkHttpDataSourceFactory(GlobalApplication.getOkHttpClient(), getResources().getText(R.string.app_name).toString(), null)).createMediaSource(Uri.parse(mediaEntity.getVideoVariants()[0].getUrl()), new Handler(), null)));
            player.setPlayWhenReady(true);
            break;
        case "photo":
        default:
            getActivity().getWindow().getDecorView().setOnSystemUiVisibilityChangeListener(visibility -> {
                if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {
                    ((AppCompatActivity) getActivity()).getSupportActionBar().show();
                }
            });
            imageView = view.findViewById(R.id.fragment_image_pager_image);
            imageView.setVisibility(View.VISIBLE);
            imageView.setOnClickListener(v -> {
                if ((getActivity().getWindow().getDecorView().getSystemUiVisibility() & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {
                    hideSystemUI();
                } else {
                    showSystemUI();
                }
            });
            imageView.setOnScaleChangeListener((float scaleFactor, float focusX, float focusY) -> view.setDragEnabled(scaleFactor <= 1F));
            GlideApp.with(this).load(TwitterStringUtils.convertLargeImageUrl(mediaEntity.getMediaURLHttps())).fitCenter().thumbnail(GlideApp.with(this).load(TwitterStringUtils.convertSmallImageUrl(mediaEntity.getMediaURLHttps())).fitCenter()).into(imageView);
            break;
    }
    return view;
}
Also used : DefaultBandwidthMeter(com.google.android.exoplayer2.upstream.DefaultBandwidthMeter) Handler(android.os.Handler) ExtractorMediaSource(com.google.android.exoplayer2.source.ExtractorMediaSource) DefaultLoadControl(com.google.android.exoplayer2.DefaultLoadControl) HlsMediaSource(com.google.android.exoplayer2.source.hls.HlsMediaSource) AdaptiveTrackSelection(com.google.android.exoplayer2.trackselection.AdaptiveTrackSelection) DefaultTrackSelector(com.google.android.exoplayer2.trackselection.DefaultTrackSelector) OkHttpDataSourceFactory(com.google.android.exoplayer2.ext.okhttp.OkHttpDataSourceFactory) DefaultRenderersFactory(com.google.android.exoplayer2.DefaultRenderersFactory) MediaEntity(twitter4j.MediaEntity) FlingLayout(com.github.chuross.flinglayout.FlingLayout) LoopingMediaSource(com.google.android.exoplayer2.source.LoopingMediaSource) Nullable(android.support.annotation.Nullable)

Aggregations

Handler (android.os.Handler)1 Nullable (android.support.annotation.Nullable)1 FlingLayout (com.github.chuross.flinglayout.FlingLayout)1 DefaultLoadControl (com.google.android.exoplayer2.DefaultLoadControl)1 DefaultRenderersFactory (com.google.android.exoplayer2.DefaultRenderersFactory)1 OkHttpDataSourceFactory (com.google.android.exoplayer2.ext.okhttp.OkHttpDataSourceFactory)1 ExtractorMediaSource (com.google.android.exoplayer2.source.ExtractorMediaSource)1 LoopingMediaSource (com.google.android.exoplayer2.source.LoopingMediaSource)1 HlsMediaSource (com.google.android.exoplayer2.source.hls.HlsMediaSource)1 AdaptiveTrackSelection (com.google.android.exoplayer2.trackselection.AdaptiveTrackSelection)1 DefaultTrackSelector (com.google.android.exoplayer2.trackselection.DefaultTrackSelector)1 DefaultBandwidthMeter (com.google.android.exoplayer2.upstream.DefaultBandwidthMeter)1 MediaEntity (twitter4j.MediaEntity)1