Android Open Source - Amphitheatre Video Details Fragment






From Project

Back to project page Amphitheatre.

License

The source code is released under:

Apache License

If you think the Android project Amphitheatre listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Copyright (C) 2014 Jerrell Mardis//w w w.j  a v a  2s. co  m
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.jerrellmardis.amphitheatre.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v17.leanback.app.BackgroundManager;
import android.support.v17.leanback.app.DetailsFragment;
import android.support.v17.leanback.widget.ArrayObjectAdapter;
import android.support.v17.leanback.widget.OnItemClickedListener;
import android.support.v17.leanback.widget.Row;
import android.text.TextUtils;
import android.util.DisplayMetrics;

import com.jerrellmardis.amphitheatre.R;
import com.jerrellmardis.amphitheatre.activity.DetailsActivity;
import com.jerrellmardis.amphitheatre.listeners.RowBuilderTaskListener;
import com.jerrellmardis.amphitheatre.model.Video;
import com.jerrellmardis.amphitheatre.model.VideoGroup;
import com.jerrellmardis.amphitheatre.task.DetailRowBuilderTask;
import com.jerrellmardis.amphitheatre.util.BlurTransform;
import com.jerrellmardis.amphitheatre.util.Constants;
import com.jerrellmardis.amphitheatre.util.PicassoBackgroundManagerTarget;
import com.orm.query.Condition;
import com.orm.query.Select;
import com.squareup.picasso.Picasso;
import com.squareup.picasso.Target;
import com.squareup.picasso.Transformation;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

public class VideoDetailsFragment extends DetailsFragment implements RowBuilderTaskListener {

    private Transformation mBlurTransformation;
    private Target mBackgroundTarget;
    private DisplayMetrics mMetrics;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mBlurTransformation = new BlurTransform(getActivity());

        BackgroundManager backgroundManager = BackgroundManager.getInstance(getActivity());
        backgroundManager.attach(getActivity().getWindow());
        mBackgroundTarget = new PicassoBackgroundManagerTarget(backgroundManager);

        mMetrics = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(mMetrics);

        boolean isVideo = getActivity().getIntent().getBooleanExtra(Constants.IS_VIDEO, true);
        Video video;

        if (isVideo) {
            video = (Video) getActivity().getIntent().getSerializableExtra(Constants.VIDEO);

            if (video.getTvShow() != null && video.getTvShow().getEpisode() != null) {
                updateBackground(video.getTvShow().getEpisode().getStillPath());
            } else {
                updateBackground(video.getBackgroundImageUrl());
            }

            Map<String, List<Video>> relatedVideos = Collections.emptyMap();

            if (video.isMovie()) {
                relatedVideos = getRelatedMovies(video);
            } else if (video.getTvShow() != null && video.getTvShow().getEpisode() != null) {
                relatedVideos = getRelatedTvShows(video);
            }

            new DetailRowBuilderTask(getActivity(), relatedVideos, true, this).execute(video);
        } else {
            VideoGroup videoGroup = (VideoGroup) getActivity().getIntent().getSerializableExtra(Constants.VIDEO_GROUP);
            video = videoGroup.getVideo();
            updateBackground(video.getBackgroundImageUrl());
            new DetailRowBuilderTask(getActivity(), getRelatedTvShows(video), false, this).execute(video);
        }

        setOnItemClickedListener(getDefaultItemClickedListener());
    }

    protected OnItemClickedListener getDefaultItemClickedListener() {
        return new OnItemClickedListener() {
            @Override
            public void onItemClicked(Object item, Row row) {
                if (item instanceof Video) {
                    Intent intent = new Intent(getActivity(), DetailsActivity.class);
                    intent.putExtra(Constants.IS_VIDEO, true);
                    intent.putExtra(Constants.VIDEO, (Video) item);
                    startActivity(intent);
                }
            }
        };
    }

    private Map<String, List<Video>> getRelatedMovies(Video video) {
        List<Video> videos = Select
                .from(Video.class)
                .where(Condition.prop("is_matched").eq(1),
                        Condition.prop("is_movie").eq(1))
                .list();

        Map<String, List<Video>> relatedVideos = new HashMap<String, List<Video>>();
        String key = getString(R.string.related_videos);
        relatedVideos.put(key, new ArrayList<Video>());

        if (video.getMovie() != null && !TextUtils.isEmpty(video.getMovie().getFlattenedGenres())) {
            String[] genresArray = video.getMovie().getFlattenedGenres().split(",");
            Set<String> genres = new HashSet<String>(Arrays.asList(genresArray));

            for (Video vid : videos) {
                if (vid.getMovie() != null &&
                        !TextUtils.isEmpty(vid.getMovie().getFlattenedGenres())) {

                    Set<String> intersection = new HashSet<String>(Arrays.asList(
                            vid.getMovie().getFlattenedGenres().split(",")));
                    intersection.retainAll(genres);

                    if (intersection.size() == genresArray.length &&
                            !video.getMovie().getTitle().equals(vid.getMovie().getTitle())) {

                        relatedVideos.get(key).add(vid);
                    }
                }
            }
        }

        return relatedVideos;
    }

    private Map<String, List<Video>> getRelatedTvShows(Video video) {
        List<Video> videos = Select
                .from(Video.class)
                .where(Condition.prop("name").eq(video.getName()),
                        Condition.prop("is_movie").eq(0))
                .list();

        Map<String, List<Video>> relatedVideos = new TreeMap<String, List<Video>>(Collections.reverseOrder());

        for (Video vid : videos) {
            // if an Episode item exists then categorize it
            // otherwise, add it to the uncategorized list
            if (vid.getTvShow() != null && vid.getTvShow().getEpisode() != null) {
                int seasonNumber = vid.getTvShow().getEpisode().getSeasonNumber();
                String key = String.format(getString(R.string.season_number), seasonNumber);

                if (relatedVideos.containsKey(key)) {
                    List<Video> subVideos = relatedVideos.get(key);
                    subVideos.add(vid);
                } else {
                    List<Video> list = new ArrayList<Video>();
                    list.add(vid);
                    relatedVideos.put(key, list);
                }
            } else {
                String key = getString(R.string.uncategorized);

                if (relatedVideos.containsKey(key)) {
                    relatedVideos.get(key).add(vid);
                } else {
                    List<Video> list = new ArrayList<Video>();
                    list.add(vid);
                    relatedVideos.put(key, list);
                }
            }
        }

        return relatedVideos;
    }

    private void updateBackground(String url) {
        Picasso.with(getActivity())
                .load(url)
                .transform(mBlurTransformation)
                .placeholder(R.drawable.placeholder)
                .resize(mMetrics.widthPixels, mMetrics.heightPixels)
                .centerCrop()
                .skipMemoryCache()
                .into(mBackgroundTarget);
    }

    @Override
    public void taskCompleted(ArrayObjectAdapter adapter) {
        setAdapter(adapter);
    }
}




Java Source Code List

com.jerrellmardis.amphitheatre.activity.BootupActivity.java
com.jerrellmardis.amphitheatre.activity.BrowseActivity.java
com.jerrellmardis.amphitheatre.activity.DetailsActivity.java
com.jerrellmardis.amphitheatre.activity.GridViewActivity.java
com.jerrellmardis.amphitheatre.activity.SearchActivity.java
com.jerrellmardis.amphitheatre.api.ApiClient.java
com.jerrellmardis.amphitheatre.api.GuessItClient.java
com.jerrellmardis.amphitheatre.api.MediaClientFactory.java
com.jerrellmardis.amphitheatre.api.MediaClient.java
com.jerrellmardis.amphitheatre.api.TMDbClient.java
com.jerrellmardis.amphitheatre.api.TVDBClientTest.java
com.jerrellmardis.amphitheatre.api.TVDBClient.java
com.jerrellmardis.amphitheatre.api.TVDBService.java
com.jerrellmardis.amphitheatre.fragment.AddSourceDialogFragment.java
com.jerrellmardis.amphitheatre.fragment.BrowseFragment.java
com.jerrellmardis.amphitheatre.fragment.CustomizeDialogFragment.java
com.jerrellmardis.amphitheatre.fragment.SearchFragment.java
com.jerrellmardis.amphitheatre.fragment.VerticalGridFragment.java
com.jerrellmardis.amphitheatre.fragment.VideoDetailsFragment.java
com.jerrellmardis.amphitheatre.listeners.RowBuilderTaskListener.java
com.jerrellmardis.amphitheatre.listeners.TaskListener.java
com.jerrellmardis.amphitheatre.model.GridGenre.java
com.jerrellmardis.amphitheatre.model.Source.java
com.jerrellmardis.amphitheatre.model.VideoGroup.java
com.jerrellmardis.amphitheatre.model.Video.java
com.jerrellmardis.amphitheatre.model.guessit.Guess.java
com.jerrellmardis.amphitheatre.model.tmdb.Config.java
com.jerrellmardis.amphitheatre.model.tmdb.Episode.java
com.jerrellmardis.amphitheatre.model.tmdb.Genre.java
com.jerrellmardis.amphitheatre.model.tmdb.Movie.java
com.jerrellmardis.amphitheatre.model.tmdb.ProductionCompany.java
com.jerrellmardis.amphitheatre.model.tmdb.SearchResult.java
com.jerrellmardis.amphitheatre.model.tmdb.TvShow.java
com.jerrellmardis.amphitheatre.model.tmdb.Videos.java
com.jerrellmardis.amphitheatre.model.tvdb.BaseResponse.java
com.jerrellmardis.amphitheatre.model.tvdb.EpisodeResponse.java
com.jerrellmardis.amphitheatre.model.tvdb.Episode.java
com.jerrellmardis.amphitheatre.model.tvdb.Language.java
com.jerrellmardis.amphitheatre.model.tvdb.SeriesResult.java
com.jerrellmardis.amphitheatre.model.tvdb.Series.java
com.jerrellmardis.amphitheatre.server.StreamServer.java
com.jerrellmardis.amphitheatre.server.StreamSource.java
com.jerrellmardis.amphitheatre.server.Streamer.java
com.jerrellmardis.amphitheatre.service.LibraryUpdateService.java
com.jerrellmardis.amphitheatre.service.RecommendationsService.java
com.jerrellmardis.amphitheatre.task.DetailRowBuilderTask.java
com.jerrellmardis.amphitheatre.task.DownloadMovieTask.java
com.jerrellmardis.amphitheatre.task.DownloadTaskHelper.java
com.jerrellmardis.amphitheatre.task.DownloadTvShowTask.java
com.jerrellmardis.amphitheatre.task.DownloadVideoTask.java
com.jerrellmardis.amphitheatre.task.GetFilesTask.java
com.jerrellmardis.amphitheatre.task.NetworkSearchTask.java
com.jerrellmardis.amphitheatre.util.ApiConstants.java
com.jerrellmardis.amphitheatre.util.Base64.java
com.jerrellmardis.amphitheatre.util.BlurTransform.java
com.jerrellmardis.amphitheatre.util.Constants.java
com.jerrellmardis.amphitheatre.util.Enums.java
com.jerrellmardis.amphitheatre.util.PicassoBackgroundManagerTarget.java
com.jerrellmardis.amphitheatre.util.RecommendationBuilder.java
com.jerrellmardis.amphitheatre.util.SecurePreferences.java
com.jerrellmardis.amphitheatre.util.Utils.java
com.jerrellmardis.amphitheatre.util.VideoUtils.java
com.jerrellmardis.amphitheatre.widget.CardPresenter.java
com.jerrellmardis.amphitheatre.widget.DetailsDescriptionPresenter.java
com.jerrellmardis.amphitheatre.widget.GridItemPresenter.java
com.jerrellmardis.amphitheatre.widget.SeasonCardPresenter.java
com.jerrellmardis.amphitheatre.widget.SortedObjectAdapter.java
com.jerrellmardis.amphitheatre.widget.TvShowsCardPresenter.java