diff --git a/base/src/main/java/io/github/xwz/base/activities/VideoPlayerActivity.java b/base/src/main/java/io/github/xwz/base/activities/VideoPlayerActivity.java index d06a48b..225a4b4 100644 --- a/base/src/main/java/io/github/xwz/base/activities/VideoPlayerActivity.java +++ b/base/src/main/java/io/github/xwz/base/activities/VideoPlayerActivity.java @@ -38,14 +38,14 @@ import java.util.List; import io.github.xwz.base.Utils; +import io.github.xwz.base.api.EpisodeBaseModel; +import io.github.xwz.base.content.ContentManagerBase; import io.github.xwz.base.player.DurationLogger; import io.github.xwz.base.player.EventLogger; import io.github.xwz.base.player.HlsRendererBuilder; import io.github.xwz.base.player.VideoPlayer; import io.github.xwz.base.views.VideoPlayerView; import io.github.xwz.base.R; -import io.github.xwz.base.content.IContentManager; -import io.github.xwz.base.api.IEpisodeModel; /** * An activity that plays media using {@link VideoPlayer}. @@ -83,7 +83,7 @@ public abstract class VideoPlayerActivity extends BaseActivity implements Surfac private boolean ready = false; - private IEpisodeModel mCurrentEpisode; + private EpisodeBaseModel mCurrentEpisode; private List mOtherEpisodeUrls; private long resumePosition; @@ -91,11 +91,11 @@ public abstract class VideoPlayerActivity extends BaseActivity implements Surfac @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); - Log.d(TAG, "Action: " + action + ", tag: " + intent.getStringExtra(IContentManager.CONTENT_TAG)); - if (IContentManager.CONTENT_AUTH_DONE.equals(action)) { + Log.d(TAG, "Action: " + action + ", tag: " + intent.getStringExtra(ContentManagerBase.CONTENT_TAG)); + if (ContentManagerBase.CONTENT_AUTH_DONE.equals(action)) { prepareStream(intent); } - if (IContentManager.CONTENT_AUTH_ERROR.equals(action)) { + if (ContentManagerBase.CONTENT_AUTH_ERROR.equals(action)) { authFailed(intent); } } @@ -104,8 +104,8 @@ public void onReceive(Context context, Intent intent) { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - IEpisodeModel episode = (IEpisodeModel) getIntent().getSerializableExtra(IContentManager.CONTENT_ID); - mOtherEpisodeUrls = Arrays.asList(getIntent().getStringArrayExtra(IContentManager.OTHER_EPISODES)); + EpisodeBaseModel episode = (EpisodeBaseModel) getIntent().getSerializableExtra(ContentManagerBase.CONTENT_ID); + mOtherEpisodeUrls = Arrays.asList(getIntent().getStringArrayExtra(ContentManagerBase.OTHER_EPISODES)); resumePosition = getIntent().getLongExtra(RESUME_POSITION, 0); setContentView(R.layout.video_player_activity); @@ -125,7 +125,7 @@ public void onCreate(Bundle savedInstanceState) { playEpisode(episode); } - private void playEpisode(IEpisodeModel episode) { + private void playEpisode(EpisodeBaseModel episode) { releasePlayer(); playerPosition = resumePosition; ready = false; @@ -134,7 +134,7 @@ private void playEpisode(IEpisodeModel episode) { getContentManger().fetchAuthToken(episode); } - protected abstract IContentManager getContentManger(); + protected abstract ContentManagerBase getContentManger(); private void prepareStream(Intent intent) { contentUri = getContentManger().getEpisodeStreamUrl(mCurrentEpisode); @@ -146,8 +146,8 @@ private void prepareStream(Intent intent) { } private void authFailed(Intent intent) { - String href = intent.getStringExtra(IContentManager.CONTENT_ID); - String error = intent.getStringExtra(IContentManager.CONTENT_TAG); + String href = intent.getStringExtra(ContentManagerBase.CONTENT_ID); + String error = intent.getStringExtra(ContentManagerBase.CONTENT_TAG); Log.e(TAG, error + ":" + href); Utils.showToast(this, error); } @@ -174,9 +174,9 @@ public void onPause() { private void registerReceiver() { Log.i(TAG, "Register receiver"); IntentFilter filter = new IntentFilter(); - filter.addAction(IContentManager.CONTENT_AUTH_START); - filter.addAction(IContentManager.CONTENT_AUTH_DONE); - filter.addAction(IContentManager.CONTENT_AUTH_ERROR); + filter.addAction(ContentManagerBase.CONTENT_AUTH_START); + filter.addAction(ContentManagerBase.CONTENT_AUTH_DONE); + filter.addAction(ContentManagerBase.CONTENT_AUTH_ERROR); LocalBroadcastManager.getInstance(this).registerReceiver(receiver, filter); } @@ -325,9 +325,9 @@ public void onPrepareLoad(Drawable placeHolderDrawable) { private void updateMediaSessionIntent() { Intent intent = new Intent(this, getVideoPlayerActivityClass()); - intent.putExtra(IContentManager.CONTENT_ID, mCurrentEpisode); + intent.putExtra(ContentManagerBase.CONTENT_ID, mCurrentEpisode); String[] others = mOtherEpisodeUrls.toArray(new String[mOtherEpisodeUrls.size()]); - intent.putExtra(IContentManager.OTHER_EPISODES, others); + intent.putExtra(ContentManagerBase.OTHER_EPISODES, others); intent.putExtra(RESUME_POSITION, playerPosition); PendingIntent pending = PendingIntent.getActivity(this, 99, intent, PendingIntent.FLAG_UPDATE_CURRENT); @@ -352,7 +352,7 @@ private void updatePlaybackState(int playbackState) { } private View.OnClickListener getNextEpisodeListener() { - IEpisodeModel next = getNextEpisode(mCurrentEpisode); + EpisodeBaseModel next = getNextEpisode(mCurrentEpisode); Log.d(TAG, "next episode:" + next); if (next != null) { return new View.OnClickListener() { @@ -366,7 +366,7 @@ public void onClick(View v) { } private View.OnClickListener getPrevEpisodeListener() { - IEpisodeModel prev = getPrevEpisode(mCurrentEpisode); + EpisodeBaseModel prev = getPrevEpisode(mCurrentEpisode); Log.d(TAG, "previous episode:" + prev); if (prev != null) { return new View.OnClickListener() { @@ -379,18 +379,18 @@ public void onClick(View v) { return null; } - private IEpisodeModel getNextEpisode(IEpisodeModel current) { + private EpisodeBaseModel getNextEpisode(EpisodeBaseModel current) { return getContentManger().findNextEpisode(mOtherEpisodeUrls, current.getHref()); } - private IEpisodeModel getPrevEpisode(IEpisodeModel current) { + private EpisodeBaseModel getPrevEpisode(EpisodeBaseModel current) { List others = new ArrayList<>(mOtherEpisodeUrls); Collections.reverse(others); return getContentManger().findNextEpisode(others, current.getHref()); } private void suggestNextEpisode() { - IEpisodeModel next = getNextEpisode(mCurrentEpisode); + EpisodeBaseModel next = getNextEpisode(mCurrentEpisode); Log.d(TAG, "Suggest next episode: " + next); if (next != null) { videoPlayerView.suggestNextEpisode(next); @@ -398,7 +398,7 @@ private void suggestNextEpisode() { } private void playNextEpisode() { - IEpisodeModel next = getNextEpisode(mCurrentEpisode); + EpisodeBaseModel next = getNextEpisode(mCurrentEpisode); Log.d(TAG, "Play next episode: " + next); if (next != null) { playEpisode(next); @@ -406,7 +406,7 @@ private void playNextEpisode() { } private void playPrevEpisode() { - IEpisodeModel next = getPrevEpisode(mCurrentEpisode); + EpisodeBaseModel next = getPrevEpisode(mCurrentEpisode); Log.d(TAG, "Play previous episode: " + next); if (next != null) { playEpisode(next); diff --git a/base/src/main/java/io/github/xwz/base/adapters/CardSelector.java b/base/src/main/java/io/github/xwz/base/adapters/CardSelector.java index 6176490..89e5027 100644 --- a/base/src/main/java/io/github/xwz/base/adapters/CardSelector.java +++ b/base/src/main/java/io/github/xwz/base/adapters/CardSelector.java @@ -4,7 +4,7 @@ import android.support.v17.leanback.widget.PresenterSelector; import io.github.xwz.base.api.CategoryModel; -import io.github.xwz.base.api.IEpisodeModel; +import io.github.xwz.base.api.EpisodeBaseModel; public class CardSelector extends PresenterSelector { final EpisodePresenter card = new EpisodePresenter(); @@ -13,11 +13,12 @@ public class CardSelector extends PresenterSelector { @Override public Presenter getPresenter(Object item) { - if (item instanceof IEpisodeModel) { - IEpisodeModel model = (IEpisodeModel) item; - if (model instanceof CategoryModel) { - return cat; - } else if (model.hasCover()) { + if (item instanceof CategoryModel) { + return cat; + } + if (item instanceof EpisodeBaseModel) { + EpisodeBaseModel model = (EpisodeBaseModel) item; + if (model.hasCover()) { return film; } } diff --git a/base/src/main/java/io/github/xwz/base/adapters/CategoryPresenter.java b/base/src/main/java/io/github/xwz/base/adapters/CategoryPresenter.java index 01d8187..ee6364a 100644 --- a/base/src/main/java/io/github/xwz/base/adapters/CategoryPresenter.java +++ b/base/src/main/java/io/github/xwz/base/adapters/CategoryPresenter.java @@ -4,7 +4,7 @@ import android.support.v17.leanback.widget.Presenter; import android.view.ViewGroup; -import io.github.xwz.base.api.IEpisodeModel; +import io.github.xwz.base.api.EpisodeBaseModel; import io.github.xwz.base.views.CategoryCardView; public class CategoryPresenter extends Presenter { @@ -18,7 +18,7 @@ public ViewHolder onCreateViewHolder(ViewGroup parent) { @Override public void onBindViewHolder(ViewHolder viewHolder, Object item) { - ((CategoryCardView) viewHolder).setEpisode((IEpisodeModel) item); + ((CategoryCardView) viewHolder).setEpisode((EpisodeBaseModel) item); } @Override diff --git a/base/src/main/java/io/github/xwz/base/adapters/EpisodePresenter.java b/base/src/main/java/io/github/xwz/base/adapters/EpisodePresenter.java index c81832b..f0792be 100644 --- a/base/src/main/java/io/github/xwz/base/adapters/EpisodePresenter.java +++ b/base/src/main/java/io/github/xwz/base/adapters/EpisodePresenter.java @@ -4,12 +4,11 @@ import android.graphics.Point; import android.support.v17.leanback.widget.ImageCardView; import android.support.v17.leanback.widget.Presenter; -import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import io.github.xwz.base.R; -import io.github.xwz.base.api.IEpisodeModel; +import io.github.xwz.base.api.EpisodeBaseModel; import io.github.xwz.base.views.EpisodeCardView; public class EpisodePresenter extends Presenter { @@ -38,7 +37,7 @@ protected ImageCardView getCardView(Context context) { @Override public void onBindViewHolder(ViewHolder viewHolder, Object item) { - ((EpisodeCardView) viewHolder).setEpisode((IEpisodeModel) item); + ((EpisodeCardView) viewHolder).setEpisode((EpisodeBaseModel) item); } @Override diff --git a/base/src/main/java/io/github/xwz/base/api/CategoryModel.java b/base/src/main/java/io/github/xwz/base/api/CategoryModel.java index f2f940b..e519724 100644 --- a/base/src/main/java/io/github/xwz/base/api/CategoryModel.java +++ b/base/src/main/java/io/github/xwz/base/api/CategoryModel.java @@ -1,179 +1,13 @@ package io.github.xwz.base.api; -import java.util.List; -import java.util.Map; - -import io.github.xwz.base.api.IEpisodeModel; - -public class CategoryModel implements IEpisodeModel { +public class CategoryModel extends EpisodeBaseModel { private String category; - private int shows; public CategoryModel(String cat) { category = cat; } - public void setEpisodeCount(int count) { - shows = count; - } - - @Override - public void setHasExtra(boolean extra) { - - } - - @Override - public void merge(IEpisodeModel ep) { - - } - - @Override - public boolean matches(String query) { - return false; - } - - @Override - public void setOtherEpisodes(Map> more) { - - } - - @Override - public Map> getOtherEpisodes() { - return null; - } - - @Override - public List getOtherEpisodeUrls(String cat) { - return null; - } - - @Override - public String getDurationText() { - return null; - } - - @Override - public void addCategory(String cat) { - - } - - @Override - public void setCategories(List cats) { - - } - - @Override - public String toString() { - return "Category: " + category; - } - - @Override - public String getSeriesTitle() { - return null; - } - - @Override - public String getHref() { - return null; - } - - @Override - public String getChannel() { - return null; - } - - @Override - public String getThumbnail() { - return null; - } - - @Override - public String getLivestream() { - return null; - } - - @Override - public String getEpisodeHouseNumber() { - return null; - } - - @Override - public List getCategories() { - return null; - } - - @Override public String getTitle() { return category; } - - @Override - public int getDuration() { - return 0; - } - - @Override - public String getRating() { - return null; - } - - @Override - public int getEpisodeCount() { - return shows; - } - - @Override - public String getDescription() { - return null; - } - - @Override - public String getRelated() { - return null; - } - - @Override - public String getAvailability() { - return null; - } - - @Override - public String getStream() { - return null; - } - - @Override - public String getCaptions() { - return null; - } - - @Override - public String getShare() { - return null; - } - - @Override - public boolean hasExtras() { - return false; - } - - @Override - public boolean hasOtherEpisodes() { - return false; - } - - @Override - public boolean isFilm() { - return false; - } - - @Override - public String getCover() { - return null; - } - - @Override - public boolean hasCover() { - return false; - } } diff --git a/base/src/main/java/io/github/xwz/base/api/EpisodeBaseModel.java b/base/src/main/java/io/github/xwz/base/api/EpisodeBaseModel.java index 7e28cca..3fd5881 100644 --- a/base/src/main/java/io/github/xwz/base/api/EpisodeBaseModel.java +++ b/base/src/main/java/io/github/xwz/base/api/EpisodeBaseModel.java @@ -7,15 +7,20 @@ import com.raizlabs.android.dbflow.annotation.Table; import com.raizlabs.android.dbflow.structure.BaseModel; +import java.io.Serializable; import java.util.ArrayList; +import java.util.HashMap; import java.util.HashSet; +import java.util.LinkedHashMap; import java.util.List; +import java.util.Map; import java.util.Set; +import io.github.xwz.base.Utils; import io.github.xwz.base.content.ContentDatabase; @Table(databaseName = ContentDatabase.NAME) -public class EpisodeBaseModel extends BaseModel { +public class EpisodeBaseModel extends BaseModel implements Serializable { @Column @PrimaryKey @@ -59,6 +64,11 @@ public class EpisodeBaseModel extends BaseModel { @Column private boolean isFilm = false; + @Column + private String related; + + private Map> others = new HashMap<>(); + public List getCategories() { return new ArrayList<>(categories); } @@ -78,11 +88,107 @@ public void update() { super.update(); } + public void addCategory(String cat) { + categories.add(cat); + } + + public void setCategories(List cats) { + categories = new HashSet<>(cats); + } + private void updateCategoriesSerialized() { Gson gson = new GsonBuilder().create(); categoriesSerialized = gson.toJson(categories); } + public String getDurationText() { + if (getRating() != null) { + return getRating() + ", " + Utils.formatMillis(getDuration() * 1000); + } else { + return Utils.formatMillis(getDuration() * 1000); + } + } + + @Override + public boolean equals(Object o) { + if (this == o) + return true; + if (!(o instanceof EpisodeBaseModel)) + return false; + EpisodeBaseModel other = (EpisodeBaseModel) o; + return other.getHref().equals(this.getHref()); + } + + public void merge(EpisodeBaseModel ep) { + this.href = ep.href == null ? this.href : ep.href; + this.seriesTitle = ep.seriesTitle == null ? this.seriesTitle : ep.seriesTitle; + this.channel = ep.channel == null ? this.channel : ep.channel; + this.thumbnail = ep.thumbnail == null ? this.thumbnail : ep.thumbnail; + this.episodeHouseNumber = ep.episodeHouseNumber == null ? this.episodeHouseNumber : ep.episodeHouseNumber; + this.categories.addAll(ep.categories); + this.title = ep.title == null ? this.title : ep.title; + this.duration = ep.duration; + this.rating = ep.rating == null ? this.rating : ep.rating; + this.episodeCount = ep.episodeCount; + this.description = ep.description == null ? this.description : ep.description; + this.cover = ep.cover == null ? this.cover : ep.cover; + this.isFilm = ep.isFilm; + this.others = ep.others.size() == 0 ? this.others : new LinkedHashMap<>(ep.others); + this.related = ep.related == null ? this.related : ep.related; + } + + protected boolean hasOther(String key) { + return others.containsKey(key); + } + + public void setOtherEpisodes(Map> more) { + others = more; + } + + public void setOtherEpisodes(String key, List more) { + others.put(key, more); + } + + public Map> getOtherEpisodes() { + return new LinkedHashMap<>(others); + } + + private List getOtherEpisodes(String cat) { + for (Map.Entry> episodes : getOtherEpisodes().entrySet()) { + if (episodes.getKey().equals(cat)) { + return episodes.getValue(); + } + } + return new ArrayList<>(); + } + + public List getOtherEpisodeUrls(String cat) { + List urls = new ArrayList<>(); + for (EpisodeBaseModel ep : getOtherEpisodes(cat)) { + urls.add(ep.getHref()); + } + return urls; + } + + public boolean hasOtherEpisodes() { + return others.size() > 0; + } + + public boolean matches(String query) { + boolean found = false; + if (getSeriesTitle() != null) { + found = found || getSeriesTitle().toLowerCase().contains(query); + } + if (getTitle() != null) { + found = found || getTitle().toLowerCase().contains(query); + } + return found; + } + + public boolean hasCover() { + return getIsFilm() && getCover() != null && getCover().length() > 0; + } + public String getSeriesTitle() { return seriesTitle; } @@ -178,4 +284,16 @@ public boolean getIsFilm() { public void setIsFilm(boolean film) { this.isFilm = film; } + + public String getRelated() { + return related; + } + + public void setRelated(String related) { + this.related = related; + } + + public String toString() { + return getHref() + ": '" + getSeriesTitle() + "' - '" + getTitle() + "'"; + } } diff --git a/base/src/main/java/io/github/xwz/base/api/HttpApiBase.java b/base/src/main/java/io/github/xwz/base/api/HttpApiBase.java index 92c4e16..1ae7e43 100644 --- a/base/src/main/java/io/github/xwz/base/api/HttpApiBase.java +++ b/base/src/main/java/io/github/xwz/base/api/HttpApiBase.java @@ -170,16 +170,16 @@ private static File createDefaultCacheDir(Context context, String path) { return cache; } - protected RadixTree buildWordsFromShows(Collection shows) { + protected RadixTree buildWordsFromShows(Collection shows) { RadixTree dict = new RadixTree<>(); - for (IEpisodeModel ep : shows) { + for (EpisodeBaseModel ep : shows) { dict.putAll(getWords(ep)); } Log.d(TAG, "dict:" + dict.size()); return dict; } - private Map getWords(IEpisodeModel episode) { + private Map getWords(EpisodeBaseModel episode) { Map words = new HashMap<>(); if (episode.getSeriesTitle() != null) { words.putAll(splitWords(episode.getSeriesTitle(), episode)); @@ -190,7 +190,7 @@ private Map getWords(IEpisodeModel episode) { return words; } - private Map splitWords(String s, IEpisodeModel episode) { + private Map splitWords(String s, EpisodeBaseModel episode) { String[] words = s.split("\\s+"); Map result = new HashMap<>(); for (String w : words) { diff --git a/base/src/main/java/io/github/xwz/base/api/IEpisodeModel.java b/base/src/main/java/io/github/xwz/base/api/IEpisodeModel.java deleted file mode 100644 index 9fac752..0000000 --- a/base/src/main/java/io/github/xwz/base/api/IEpisodeModel.java +++ /dev/null @@ -1,74 +0,0 @@ -package io.github.xwz.base.api; - -import java.io.Serializable; -import java.util.List; -import java.util.Map; - -public interface IEpisodeModel extends Serializable { - - void merge(IEpisodeModel ep); - - boolean matches(String query); - - void setOtherEpisodes(Map> more); - - void setEpisodeCount(int count); - - void setHasExtra(boolean extra); - - Map> getOtherEpisodes(); - - List getOtherEpisodeUrls(String cat); - - String getDurationText(); - - void addCategory(String cat); - - void setCategories(List cats); - - String toString(); - - String getSeriesTitle(); - - String getHref(); - - String getChannel(); - - String getThumbnail(); - - String getLivestream(); - - String getEpisodeHouseNumber(); - - List getCategories(); - - String getTitle(); - - int getDuration(); - - String getRating(); - - int getEpisodeCount(); - - String getDescription(); - - String getRelated(); - - String getAvailability(); - - String getStream(); - - String getCaptions(); - - String getShare(); - - boolean hasExtras(); - - boolean hasOtherEpisodes(); - - boolean isFilm(); - - String getCover(); - - boolean hasCover(); -} diff --git a/base/src/main/java/io/github/xwz/base/content/ContentCacheManager.java b/base/src/main/java/io/github/xwz/base/content/ContentCacheManager.java index 9de1b26..9da98b4 100644 --- a/base/src/main/java/io/github/xwz/base/content/ContentCacheManager.java +++ b/base/src/main/java/io/github/xwz/base/content/ContentCacheManager.java @@ -14,18 +14,18 @@ import java.util.List; import java.util.Map; -import io.github.xwz.base.api.IEpisodeModel; +import io.github.xwz.base.api.EpisodeBaseModel; import io.github.xwz.base.trie.RadixTree; public class ContentCacheManager { private static final String TAG = "ContentCacheManager"; private final LocalBroadcastManager mBroadcastManager; - private final Map mEpisodes = new HashMap<>(); - private List mShows = new ArrayList<>(); + private final Map mEpisodes = new HashMap<>(); + private List mShows = new ArrayList<>(); private RadixTree mDictionary = new RadixTree<>(); private final Map mStreamUrls = new HashMap<>(); - private Map> mCollections = new LinkedHashMap<>(); + private Map> mCollections = new LinkedHashMap<>(); public ContentCacheManager(Context context) { mBroadcastManager = LocalBroadcastManager.getInstance(context); @@ -34,10 +34,10 @@ public ContentCacheManager(Context context) { public void broadcastChange(String change, String tag, String id) { Intent intent = new Intent(change); if (tag != null) { - intent.putExtra(IContentManager.CONTENT_TAG, tag); + intent.putExtra(ContentManagerBase.CONTENT_TAG, tag); } if (id != null) { - intent.putExtra(IContentManager.CONTENT_ID, id); + intent.putExtra(ContentManagerBase.CONTENT_ID, id); } Log.d(TAG, "Broadcast:=> " + change); if (mBroadcastManager != null) { @@ -63,7 +63,7 @@ public void broadcastChange(String change) { broadcastChange(change, null, null); } - synchronized public List getAllShows() { + synchronized public List getAllShows() { return new ArrayList<>(mShows); } @@ -71,20 +71,20 @@ synchronized public boolean hasShows() { return !mShows.isEmpty(); } - synchronized public void putShows(Collection shows) { + synchronized public void putShows(Collection shows) { mShows = new ArrayList<>(shows); } - synchronized public void addCollection(String title, List shows) { + synchronized public void addCollection(String title, List shows) { mCollections.put(title, new ArrayList<>(shows)); } - synchronized public LinkedHashMap> getCollections() { + synchronized public LinkedHashMap> getCollections() { return new LinkedHashMap<>(mCollections); } - synchronized public void addEpisodes(Collection episodes) { - for (IEpisodeModel ep : episodes) { + synchronized public void addEpisodes(Collection episodes) { + for (EpisodeBaseModel ep : episodes) { mEpisodes.put(ep.getHref(), ep); } } @@ -97,15 +97,16 @@ synchronized public List getSuggestions(String query) { return mDictionary.getValuesWithPrefix(query); } - synchronized public void updateEpisode(IEpisodeModel ep) { + synchronized public EpisodeBaseModel updateEpisode(EpisodeBaseModel ep) { if (mEpisodes.containsKey(ep.getHref())) { - mEpisodes.get(ep.getHref()).merge(ep); + mEpisodes.get(ep.getHref()); } else { mEpisodes.put(ep.getHref(), ep); } + return mEpisodes.get(ep.getHref()); } - synchronized public IEpisodeModel getEpisode(String href) { + synchronized public EpisodeBaseModel getEpisode(String href) { if (href != null) { return mEpisodes.get(href); } diff --git a/base/src/main/java/io/github/xwz/base/content/ContentManagerBase.java b/base/src/main/java/io/github/xwz/base/content/ContentManagerBase.java index 14ae665..628fd47 100644 --- a/base/src/main/java/io/github/xwz/base/content/ContentManagerBase.java +++ b/base/src/main/java/io/github/xwz/base/content/ContentManagerBase.java @@ -1,5 +1,6 @@ package io.github.xwz.base.content; +import android.app.SearchManager; import android.content.Context; import android.content.Intent; import android.database.Cursor; @@ -12,9 +13,51 @@ import java.util.LinkedHashMap; import java.util.List; -import io.github.xwz.base.api.IEpisodeModel; +import io.github.xwz.base.api.EpisodeBaseModel; -public abstract class ContentManagerBase implements IContentManager { +public abstract class ContentManagerBase { + + public static final String CONTENT_ID = "io.github.xwz.base.CONTENT_ID"; + public static final String CONTENT_TAG = "io.github.xwz.base.CONTENT_TAG"; + + public static final String CONTENT_SHOW_LIST_FETCHING = "io.github.xwz.base.CONTENT_SHOW_LIST_FETCHING"; + public static final String CONTENT_SHOW_LIST_START = "io.github.xwz.base.CONTENT_SHOW_LIST_START"; + public static final String CONTENT_SHOW_LIST_DONE = "io.github.xwz.base.CONTENT_SHOW_LIST_DONE"; + public static final String CONTENT_SHOW_LIST_ERROR = "io.github.xwz.base.CONTENT_SHOW_LIST_ERROR"; + public static final String CONTENT_SHOW_LIST_PROGRESS = "io.github.xwz.base.CONTENT_SHOW_LIST_PROGRESS"; + + public static final String CONTENT_EPISODE_FETCHING = "io.github.xwz.base.CONTENT_EPISODE_FETCHING"; + public static final String CONTENT_EPISODE_START = "io.github.xwz.base.CONTENT_EPISODE_START"; + public static final String CONTENT_EPISODE_DONE = "io.github.xwz.base.CONTENT_EPISODE_DONE"; + public static final String CONTENT_EPISODE_ERROR = "io.github.xwz.base.CONTENT_EPISODE_ERROR"; + + public static final String CONTENT_AUTH_FETCHING = "io.github.xwz.base.CONTENT_AUTH_FETCHING"; + public static final String CONTENT_AUTH_START = "io.github.xwz.base.CONTENT_AUTH_START"; + public static final String CONTENT_AUTH_DONE = "io.github.xwz.base.CONTENT_AUTH_DONE"; + public static final String CONTENT_AUTH_ERROR = "io.github.xwz.base.CONTENT_AUTH_ERROR"; + + public static final String AUTH_FAILED_NETWORK = "AUTH_FAILED_NETWORK"; + public static final String AUTH_FAILED_TOKEN = "AUTH_FAILED_TOKEN"; + public static final String AUTH_FAILED_URL = "AUTH_FAILED_URL"; + + public static final String OTHER_EPISODES = "OTHER_EPISODES"; + public static final String MORE_LIKE_THIS = "More Like This"; + public static final String GLOBAL_SEARCH_INTENT = "GLOBAL_SEARCH_INTENT"; + + //The columns we'll include in the video database table + public static final String KEY_SERIES_TITLE = SearchManager.SUGGEST_COLUMN_TEXT_1; + public static final String KEY_TITLE = SearchManager.SUGGEST_COLUMN_TEXT_2; + + public static final String KEY_IMAGE = SearchManager.SUGGEST_COLUMN_RESULT_CARD_IMAGE; + public static final String KEY_DATA_TYPE = SearchManager.SUGGEST_COLUMN_CONTENT_TYPE; + public static final String KEY_IS_LIVE = SearchManager.SUGGEST_COLUMN_IS_LIVE; + public static final String KEY_VIDEO_WIDTH = SearchManager.SUGGEST_COLUMN_VIDEO_WIDTH; + public static final String KEY_VIDEO_HEIGHT = SearchManager.SUGGEST_COLUMN_VIDEO_HEIGHT; + public static final String KEY_PRODUCTION_YEAR = SearchManager.SUGGEST_COLUMN_PRODUCTION_YEAR; + public static final String KEY_COLUMN_DURATION = SearchManager.SUGGEST_COLUMN_DURATION; + public static final String KEY_ACTION = SearchManager.SUGGEST_COLUMN_INTENT_ACTION; + public static final String KEY_EXTRA_DATA = SearchManager.SUGGEST_COLUMN_INTENT_EXTRA_DATA; + public static final String KEY_EXTRA_NAME = SearchManager.EXTRA_DATA_KEY; private static final String TAG = "ContentManagerBase"; @@ -66,10 +109,10 @@ public void broadcastChange(String change) { mCache.broadcastChange(change); } - public List searchShows(String query) { - List results = new ArrayList<>(); + public List searchShows(String query) { + List results = new ArrayList<>(); query = query.toLowerCase(); - for (IEpisodeModel episode : mCache.getAllShows()) { + for (EpisodeBaseModel episode : mCache.getAllShows()) { if (episode.matches(query)) { results.add(episode); } @@ -85,7 +128,6 @@ public Cursor searchShowsCursor(String query) { KEY_TITLE, KEY_IMAGE, KEY_DATA_TYPE, - KEY_IS_LIVE, KEY_VIDEO_WIDTH, KEY_VIDEO_HEIGHT, KEY_PRODUCTION_YEAR, @@ -95,14 +137,13 @@ public Cursor searchShowsCursor(String query) { KEY_EXTRA_NAME }; MatrixCursor cursor = new MatrixCursor(columns); - for (IEpisodeModel ep : searchShows(query)) { + for (EpisodeBaseModel ep : searchShows(query)) { LinkedHashMap row = new LinkedHashMap(); row.put(BaseColumns._ID, ep.getHref()); row.put(KEY_SERIES_TITLE, ep.getSeriesTitle()); row.put(KEY_TITLE, ep.getTitle()); row.put(KEY_IMAGE, ep.getThumbnail()); row.put(KEY_DATA_TYPE, "video/mp4"); - row.put(KEY_IS_LIVE, ep.getLivestream()); row.put(KEY_VIDEO_WIDTH, 1280); row.put(KEY_VIDEO_HEIGHT, 720); row.put(KEY_PRODUCTION_YEAR, 2015); @@ -119,31 +160,31 @@ public List suggestions(String query) { return mCache.getSuggestions(query); } - public List getAllShows() { + public List getAllShows() { return mCache.getAllShows(); } - public List getAllShowsByCategory(String cat) { - List all = getAllShowsByCategories().get(cat); + public List getAllShowsByCategory(String cat) { + List all = getAllShowsByCategories().get(cat); if (all == null) { all = new ArrayList<>(); } return all; } - public IEpisodeModel getEpisode(String href) { + public EpisodeBaseModel getEpisode(String href) { return mCache.getEpisode(href); } - public Uri getEpisodeStreamUrl(IEpisodeModel episode) { + public Uri getEpisodeStreamUrl(EpisodeBaseModel episode) { return mCache.getEpisodeStreamUrl(episode.getHref()); } - public LinkedHashMap> getAllShowsByCategories() { + public LinkedHashMap> getAllShowsByCategories() { return cache().getCollections(); } - public IEpisodeModel findNextEpisode(List urls, String current) { + public EpisodeBaseModel findNextEpisode(List urls, String current) { String next = null; boolean found = false; for (String href : urls) { @@ -162,21 +203,28 @@ public IEpisodeModel findNextEpisode(List urls, String current) { return null; } - public void recommendEpisode(Context context, IEpisodeModel ep, RecommendationPosition position) { + public void recommendEpisode(Context context, EpisodeBaseModel ep, RecommendationPosition position) { Intent intent = new Intent(context, getRecommendationServiceClass()); - intent.putExtra(IContentManager.CONTENT_ID, ep); - intent.putExtra(IContentManager.CONTENT_TAG, position.getId()); + intent.putExtra(CONTENT_ID, ep); + intent.putExtra(CONTENT_TAG, position.getId()); context.startService(intent); } public void updateRecommendations(Context context) { - List shows = getRecommendations(); + List shows = getRecommendations(); int i = 0; - for (IEpisodeModel show : shows) { + for (EpisodeBaseModel show : shows) { Log.d(TAG, "Recommendation: " + i + ", " + show); if (i < 2) { recommendEpisode(context, show, RecommendationPosition.values()[i++]); } } } -} + + public abstract void fetchShowList(boolean force); + public abstract void fetchAuthToken(EpisodeBaseModel episode); + public abstract void fetchEpisode(EpisodeBaseModel episode); + + public abstract List getRecommendations(); + public abstract Class getRecommendationServiceClass(); +} \ No newline at end of file diff --git a/base/src/main/java/io/github/xwz/base/content/IContentManager.java b/base/src/main/java/io/github/xwz/base/content/IContentManager.java deleted file mode 100644 index 45a270d..0000000 --- a/base/src/main/java/io/github/xwz/base/content/IContentManager.java +++ /dev/null @@ -1,70 +0,0 @@ -package io.github.xwz.base.content; - -import android.app.SearchManager; -import android.content.Context; -import android.net.Uri; - -import java.util.LinkedHashMap; -import java.util.List; - -import io.github.xwz.base.api.IEpisodeModel; - -public interface IContentManager { - - String CONTENT_ID = "io.github.xwz.base.CONTENT_ID"; - String CONTENT_TAG = "io.github.xwz.base.CONTENT_TAG"; - - String CONTENT_SHOW_LIST_FETCHING = "io.github.xwz.base.CONTENT_SHOW_LIST_FETCHING"; - String CONTENT_SHOW_LIST_START = "io.github.xwz.base.CONTENT_SHOW_LIST_START"; - String CONTENT_SHOW_LIST_DONE = "io.github.xwz.base.CONTENT_SHOW_LIST_DONE"; - String CONTENT_SHOW_LIST_ERROR = "io.github.xwz.base.CONTENT_SHOW_LIST_ERROR"; - String CONTENT_SHOW_LIST_PROGRESS = "io.github.xwz.base.CONTENT_SHOW_LIST_PROGRESS"; - - String CONTENT_EPISODE_FETCHING = "io.github.xwz.base.CONTENT_EPISODE_FETCHING"; - String CONTENT_EPISODE_START = "io.github.xwz.base.CONTENT_EPISODE_START"; - String CONTENT_EPISODE_DONE = "io.github.xwz.base.CONTENT_EPISODE_DONE"; - String CONTENT_EPISODE_ERROR = "io.github.xwz.base.CONTENT_EPISODE_ERROR"; - - String CONTENT_AUTH_FETCHING = "io.github.xwz.base.CONTENT_AUTH_FETCHING"; - String CONTENT_AUTH_START = "io.github.xwz.base.CONTENT_AUTH_START"; - String CONTENT_AUTH_DONE = "io.github.xwz.base.CONTENT_AUTH_DONE"; - String CONTENT_AUTH_ERROR = "io.github.xwz.base.CONTENT_AUTH_ERROR"; - - String AUTH_FAILED_NETWORK = "AUTH_FAILED_NETWORK"; - String AUTH_FAILED_TOKEN = "AUTH_FAILED_TOKEN"; - String AUTH_FAILED_URL = "AUTH_FAILED_URL"; - - String OTHER_EPISODES = "OTHER_EPISODES"; - String MORE_LIKE_THIS = "More Like This"; - String GLOBAL_SEARCH_INTENT = "GLOBAL_SEARCH_INTENT"; - - //The columns we'll include in the video database table - String KEY_SERIES_TITLE = SearchManager.SUGGEST_COLUMN_TEXT_1; - String KEY_TITLE = SearchManager.SUGGEST_COLUMN_TEXT_2; - - String KEY_IMAGE = SearchManager.SUGGEST_COLUMN_RESULT_CARD_IMAGE; - String KEY_DATA_TYPE = SearchManager.SUGGEST_COLUMN_CONTENT_TYPE; - String KEY_IS_LIVE = SearchManager.SUGGEST_COLUMN_IS_LIVE; - String KEY_VIDEO_WIDTH = SearchManager.SUGGEST_COLUMN_VIDEO_WIDTH; - String KEY_VIDEO_HEIGHT = SearchManager.SUGGEST_COLUMN_VIDEO_HEIGHT; - String KEY_PRODUCTION_YEAR = SearchManager.SUGGEST_COLUMN_PRODUCTION_YEAR; - String KEY_COLUMN_DURATION = SearchManager.SUGGEST_COLUMN_DURATION; - String KEY_ACTION = SearchManager.SUGGEST_COLUMN_INTENT_ACTION; - String KEY_EXTRA_DATA = SearchManager.SUGGEST_COLUMN_INTENT_EXTRA_DATA; - String KEY_EXTRA_NAME = SearchManager.EXTRA_DATA_KEY; - - LinkedHashMap> getAllShowsByCategories(); - List getAllShowsByCategory(String cat); - void fetchShowList(boolean force); - IEpisodeModel getEpisode(String href); - void fetchEpisode(IEpisodeModel episode); - List suggestions(String query); - List searchShows(String query); - void fetchAuthToken(IEpisodeModel episode); - Uri getEpisodeStreamUrl(IEpisodeModel episode); - IEpisodeModel findNextEpisode(List urls, String current); - List getAllShows(); - List getRecommendations(); - void updateRecommendations(Context context); - Class getRecommendationServiceClass(); -} diff --git a/base/src/main/java/io/github/xwz/base/fragments/CategoryFragment.java b/base/src/main/java/io/github/xwz/base/fragments/CategoryFragment.java index 8ccfa50..4056bcc 100644 --- a/base/src/main/java/io/github/xwz/base/fragments/CategoryFragment.java +++ b/base/src/main/java/io/github/xwz/base/fragments/CategoryFragment.java @@ -15,11 +15,10 @@ import java.util.List; import io.github.xwz.base.Utils; -import io.github.xwz.base.adapters.CardSelector; import io.github.xwz.base.adapters.EpisodePresenter; import io.github.xwz.base.adapters.FilmPresenter; -import io.github.xwz.base.api.IEpisodeModel; -import io.github.xwz.base.content.IContentManager; +import io.github.xwz.base.api.EpisodeBaseModel; +import io.github.xwz.base.content.ContentManagerBase; public abstract class CategoryFragment extends VerticalGridFragment { private static final String TAG = "CategoryFragment"; @@ -33,7 +32,7 @@ public abstract class CategoryFragment extends VerticalGridFragment { public void onCreate(Bundle savedInstanceState) { Log.d(TAG, "onCreate"); super.onCreate(savedInstanceState); - category = getActivity().getIntent().getStringExtra(IContentManager.CONTENT_ID); + category = getActivity().getIntent().getStringExtra(ContentManagerBase.CONTENT_ID); isFilm = category.contains("Film/") || category.equals("Film"); setupFragment(); setupListeners(); @@ -49,7 +48,7 @@ private void setupFragment() { setGridPresenter(gridPresenter); setTitle(Utils.stripCategory(category)); - List all = getContentManger().getAllShowsByCategory(category); + List all = getContentManger().getAllShowsByCategory(category); ArrayObjectAdapter adapter; if (isFilm) { adapter = new ArrayObjectAdapter(new FilmPresenter(true)); @@ -80,16 +79,16 @@ private OnItemViewClickedListener getItemClickedListener() { return new OnItemViewClickedListener() { @Override public void onItemClicked(Presenter.ViewHolder itemViewHolder, Object item, RowPresenter.ViewHolder rowViewHolder, Row row) { - if (item instanceof IEpisodeModel) { + if (item instanceof EpisodeBaseModel) { Intent intent = new Intent(getActivity(), getDetailsActivityClass()); - intent.putExtra(IContentManager.CONTENT_ID, (IEpisodeModel) item); + intent.putExtra(ContentManagerBase.CONTENT_ID, (EpisodeBaseModel) item); startActivity(intent); } } }; } - protected abstract IContentManager getContentManger(); + protected abstract ContentManagerBase getContentManger(); protected abstract Class getSearchActivityClass(); diff --git a/base/src/main/java/io/github/xwz/base/fragments/DetailsFragment.java b/base/src/main/java/io/github/xwz/base/fragments/DetailsFragment.java index eda5667..17d76c8 100644 --- a/base/src/main/java/io/github/xwz/base/fragments/DetailsFragment.java +++ b/base/src/main/java/io/github/xwz/base/fragments/DetailsFragment.java @@ -30,8 +30,8 @@ import io.github.xwz.base.R; import io.github.xwz.base.Utils; import io.github.xwz.base.adapters.CardSelector; -import io.github.xwz.base.api.IEpisodeModel; -import io.github.xwz.base.content.IContentManager; +import io.github.xwz.base.api.EpisodeBaseModel; +import io.github.xwz.base.content.ContentManagerBase; import io.github.xwz.base.views.EpisodeDetailsView; public abstract class DetailsFragment extends android.support.v17.leanback.app.RowsFragment { @@ -39,8 +39,8 @@ public abstract class DetailsFragment extends android.support.v17.leanback.app.R private static final String TAG = "DetailsFragment"; private EpisodeDetailsView mDetailView; private int mHeaderHeight; - private IEpisodeModel mCurrentEpisode; - private IEpisodeModel mLoadedEpisode; + private EpisodeBaseModel mCurrentEpisode; + private EpisodeBaseModel mLoadedEpisode; private ArrayObjectAdapter otherEpisodes; private boolean loadedOtherEpisodes = false; private List mOtherEpisodeUrls = new ArrayList<>(); @@ -49,11 +49,11 @@ public abstract class DetailsFragment extends android.support.v17.leanback.app.R @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); - Log.d(TAG, "Action: " + action + ", tag: " + intent.getStringExtra(IContentManager.CONTENT_TAG)); - if (IContentManager.CONTENT_EPISODE_DONE.equals(action)) { + Log.d(TAG, "Action: " + action + ", tag: " + intent.getStringExtra(ContentManagerBase.CONTENT_TAG)); + if (ContentManagerBase.CONTENT_EPISODE_DONE.equals(action)) { updateEpisodeData(intent); } - if (IContentManager.CONTENT_EPISODE_ERROR.equals(action)) { + if (ContentManagerBase.CONTENT_EPISODE_ERROR.equals(action)) { Utils.showToast(getActivity(), "Unable to find episode details."); } } @@ -63,12 +63,12 @@ protected List getOtherEpisodeUrls() { return mOtherEpisodeUrls; } - protected abstract IContentManager getContentManger(); + protected abstract ContentManagerBase getContentManger(); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - IEpisodeModel episode = (IEpisodeModel) getActivity().getIntent().getSerializableExtra(IContentManager.CONTENT_ID); + EpisodeBaseModel episode = (EpisodeBaseModel) getActivity().getIntent().getSerializableExtra(ContentManagerBase.CONTENT_ID); if (episode == null) { episode = getEpisodeFromGlobalSearchIntent(); } @@ -79,7 +79,7 @@ public void onCreate(Bundle savedInstanceState) { } } - private void setupEpisode(IEpisodeModel episode) { + private void setupEpisode(EpisodeBaseModel episode) { mLoadedEpisode = episode; setCurrentEpisode(episode); @@ -90,14 +90,14 @@ private void setupEpisode(IEpisodeModel episode) { setupListeners(); } - private IEpisodeModel getEpisodeFromGlobalSearchIntent() { + private EpisodeBaseModel getEpisodeFromGlobalSearchIntent() { Intent intent = getActivity().getIntent(); String action = intent.getAction(); - if (IContentManager.GLOBAL_SEARCH_INTENT.equals(action)) { + if (ContentManagerBase.GLOBAL_SEARCH_INTENT.equals(action)) { Log.d(TAG, "getEpisodeFromGlobalSearchIntent"); Bundle data = intent.getExtras(); if (data != null) { - String href = data.getString(IContentManager.KEY_EXTRA_NAME); + String href = data.getString(ContentManagerBase.KEY_EXTRA_NAME); Log.d(TAG, "Search result: " + href); return getContentManger().getEpisode(href); } @@ -111,7 +111,7 @@ private void setupListeners() { setOnItemViewSelectedListener(getItemSelectedListener()); } - private void setupAdapter(IEpisodeModel episode) { + private void setupAdapter(EpisodeBaseModel episode) { ArrayObjectAdapter adapter = new ArrayObjectAdapter(new ListRowPresenter()); otherEpisodes = new ArrayObjectAdapter(new CardSelector()); otherEpisodes.add(0, episode); @@ -119,7 +119,7 @@ private void setupAdapter(IEpisodeModel episode) { setAdapter(adapter); } - private void setCurrentEpisode(IEpisodeModel episode) { + private void setCurrentEpisode(EpisodeBaseModel episode) { Log.d(TAG, "Showing details: " + episode); if (!episode.equals(mCurrentEpisode)) { mCurrentEpisode = episode; @@ -131,8 +131,8 @@ private void setCurrentEpisode(IEpisodeModel episode) { } private void updateEpisodeData(Intent intent) { - String href = intent.getStringExtra(IContentManager.CONTENT_TAG); - IEpisodeModel ep = getContentManger().getEpisode(href); + String href = intent.getStringExtra(ContentManagerBase.CONTENT_TAG); + EpisodeBaseModel ep = getContentManger().getEpisode(href); if (ep != null) { if (ep.equals(mCurrentEpisode)) { mCurrentEpisode.merge(ep); @@ -141,19 +141,19 @@ private void updateEpisodeData(Intent intent) { if (ep.equals(mLoadedEpisode) && !loadedOtherEpisodes) { updateRelatedEpisodes(ep.getOtherEpisodes()); loadedOtherEpisodes = true; - mOtherEpisodeUrls = mLoadedEpisode.getOtherEpisodeUrls(IContentManager.OTHER_EPISODES); + mOtherEpisodeUrls = mLoadedEpisode.getOtherEpisodeUrls(ContentManagerBase.OTHER_EPISODES); Log.d(TAG, "Other episodes:" + mOtherEpisodeUrls); } } } - private void updateRelatedEpisodes(Map> others) { + private void updateRelatedEpisodes(Map> others) { boolean updated = false; ArrayObjectAdapter adapter = (ArrayObjectAdapter) getAdapter(); - for (Map.Entry> list : others.entrySet()) { + for (Map.Entry> list : others.entrySet()) { String title = list.getKey(); Log.d(TAG, "More: " + title); - if (IContentManager.OTHER_EPISODES.equals(title)) { + if (ContentManagerBase.OTHER_EPISODES.equals(title)) { otherEpisodes.addAll(otherEpisodes.size(), list.getValue()); } else { ArrayObjectAdapter more = new ArrayObjectAdapter(new CardSelector()); @@ -239,12 +239,12 @@ private OnItemViewClickedListener getItemClickedListener() { @Override public void onItemClicked(Presenter.ViewHolder itemViewHolder, Object item, RowPresenter.ViewHolder rowViewHolder, Row row) { Log.d(TAG, "Clicked item:" + item); - if (item instanceof IEpisodeModel) { - IEpisodeModel ep = (IEpisodeModel) item; + if (item instanceof EpisodeBaseModel) { + EpisodeBaseModel ep = (EpisodeBaseModel) item; Intent intent = new Intent(getActivity(), getPlayerActivityClass()); - intent.putExtra(IContentManager.CONTENT_ID, ep); + intent.putExtra(ContentManagerBase.CONTENT_ID, ep); String[] others = getOtherEpisodeUrls().toArray(new String[getOtherEpisodeUrls().size()]); - intent.putExtra(IContentManager.OTHER_EPISODES, others); + intent.putExtra(ContentManagerBase.OTHER_EPISODES, others); startActivity(intent); } } @@ -256,8 +256,8 @@ private OnItemViewSelectedListener getItemSelectedListener() { @Override public void onItemSelected(Presenter.ViewHolder itemViewHolder, Object item, RowPresenter.ViewHolder rowViewHolder, Row row) { Log.d(TAG, "Selected item:" + item); - if (item instanceof IEpisodeModel) { - setCurrentEpisode((IEpisodeModel) item); + if (item instanceof EpisodeBaseModel) { + setCurrentEpisode((EpisodeBaseModel) item); } } }; @@ -277,9 +277,9 @@ public void onPause() { private void registerReceiver() { Log.i(TAG, "Register receiver"); IntentFilter filter = new IntentFilter(); - filter.addAction(IContentManager.CONTENT_EPISODE_START); - filter.addAction(IContentManager.CONTENT_EPISODE_DONE); - filter.addAction(IContentManager.CONTENT_EPISODE_ERROR); + filter.addAction(ContentManagerBase.CONTENT_EPISODE_START); + filter.addAction(ContentManagerBase.CONTENT_EPISODE_DONE); + filter.addAction(ContentManagerBase.CONTENT_EPISODE_ERROR); LocalBroadcastManager.getInstance(getActivity()).registerReceiver(receiver, filter); } } diff --git a/base/src/main/java/io/github/xwz/base/fragments/MainFragment.java b/base/src/main/java/io/github/xwz/base/fragments/MainFragment.java index 23c282a..afa36e9 100644 --- a/base/src/main/java/io/github/xwz/base/fragments/MainFragment.java +++ b/base/src/main/java/io/github/xwz/base/fragments/MainFragment.java @@ -34,8 +34,8 @@ import io.github.xwz.base.adapters.BaseArrayAdapter; import io.github.xwz.base.adapters.CardSelector; import io.github.xwz.base.api.CategoryModel; -import io.github.xwz.base.api.IEpisodeModel; -import io.github.xwz.base.content.IContentManager; +import io.github.xwz.base.api.EpisodeBaseModel; +import io.github.xwz.base.content.ContentManagerBase; public abstract class MainFragment extends BrowseFragment { @@ -49,17 +49,17 @@ public abstract class MainFragment extends BrowseFragment { public void onReceive(Context context, Intent intent) { String action = intent.getAction(); Log.d(TAG, "Action: " + action); - if (IContentManager.CONTENT_SHOW_LIST_DONE.equals(action)) { + if (ContentManagerBase.CONTENT_SHOW_LIST_DONE.equals(action)) { updateAdapter(); getContentManger().updateRecommendations(getActivity()); - } else if (IContentManager.CONTENT_SHOW_LIST_PROGRESS.equals(action)) { - String msg = intent.getStringExtra(IContentManager.CONTENT_TAG); + } else if (ContentManagerBase.CONTENT_SHOW_LIST_PROGRESS.equals(action)) { + String msg = intent.getStringExtra(ContentManagerBase.CONTENT_TAG); updateProgress(msg); } } }; - protected abstract IContentManager getContentManger(); + protected abstract ContentManagerBase getContentManger(); @Override public void onActivityCreated(Bundle savedInstanceState) { @@ -130,11 +130,11 @@ private OnItemViewClickedListener getItemClickedListener() { public void onItemClicked(Presenter.ViewHolder itemViewHolder, Object item, RowPresenter.ViewHolder rowViewHolder, Row row) { if (item instanceof CategoryModel) { Intent intent = new Intent(getActivity(), getCategoryActivityClass()); - intent.putExtra(IContentManager.CONTENT_ID, ((CategoryModel) item).getTitle()); + intent.putExtra(ContentManagerBase.CONTENT_ID, ((CategoryModel) item).getTitle()); startActivity(intent); - } else if (item instanceof IEpisodeModel) { + } else if (item instanceof EpisodeBaseModel) { Intent intent = new Intent(getActivity(), getDetailsActivityClass()); - intent.putExtra(IContentManager.CONTENT_ID, (IEpisodeModel) item); + intent.putExtra(ContentManagerBase.CONTENT_ID, (EpisodeBaseModel) item); startActivity(intent); } } @@ -148,13 +148,13 @@ public void onItemClicked(Presenter.ViewHolder itemViewHolder, Object item, RowP protected abstract Class getCategoryActivityClass(); private void updateRows(ArrayObjectAdapter adapter) { - LinkedHashMap> all = getContentManger().getAllShowsByCategories(); + LinkedHashMap> all = getContentManger().getAllShowsByCategories(); int currentRows = adapter.size(); int newRows = all.size(); List categories = new ArrayList<>(all.keySet()); for (int i = 0; i < newRows; i++) { String category = categories.get(i); - List episodes = new ArrayList<>(all.get(category)); + List episodes = new ArrayList<>(all.get(category)); if (SHOW_CATEGORY_COUNT > 0 && episodes.size() > SHOW_CATEGORY_COUNT) { CategoryModel collection = new CategoryModel(category); collection.setEpisodeCount(episodes.size()); @@ -162,11 +162,11 @@ private void updateRows(ArrayObjectAdapter adapter) { } if (i < currentRows) { // update row ListRow row = (ListRow) adapter.get(i); - row.setHeaderItem(new HeaderItem(category)); - BaseArrayAdapter items = (BaseArrayAdapter) row.getAdapter(); + row.setHeaderItem(new HeaderItem(Utils.stripCategory(category))); + BaseArrayAdapter items = (BaseArrayAdapter) row.getAdapter(); items.replaceItems(episodes); } else { // add - BaseArrayAdapter items = new BaseArrayAdapter<>(new CardSelector()); + BaseArrayAdapter items = new BaseArrayAdapter<>(new CardSelector()); items.addAll(0, episodes); HeaderItem header = new HeaderItem(Utils.stripCategory(category)); ListRow row = new ListRow(header, items); @@ -207,10 +207,10 @@ public void onPause() { private void registerReceiver() { Log.i(TAG, "Register receiver"); IntentFilter filter = new IntentFilter(); - filter.addAction(IContentManager.CONTENT_SHOW_LIST_START); - filter.addAction(IContentManager.CONTENT_SHOW_LIST_DONE); - filter.addAction(IContentManager.CONTENT_SHOW_LIST_ERROR); - filter.addAction(IContentManager.CONTENT_SHOW_LIST_PROGRESS); + filter.addAction(ContentManagerBase.CONTENT_SHOW_LIST_START); + filter.addAction(ContentManagerBase.CONTENT_SHOW_LIST_DONE); + filter.addAction(ContentManagerBase.CONTENT_SHOW_LIST_ERROR); + filter.addAction(ContentManagerBase.CONTENT_SHOW_LIST_PROGRESS); LocalBroadcastManager.getInstance(getActivity()).registerReceiver(receiver, filter); } } diff --git a/base/src/main/java/io/github/xwz/base/fragments/SearchFragment.java b/base/src/main/java/io/github/xwz/base/fragments/SearchFragment.java index 7faa5bb..7e2fc97 100644 --- a/base/src/main/java/io/github/xwz/base/fragments/SearchFragment.java +++ b/base/src/main/java/io/github/xwz/base/fragments/SearchFragment.java @@ -19,8 +19,8 @@ import io.github.xwz.base.R; import io.github.xwz.base.adapters.CardSelector; -import io.github.xwz.base.api.IEpisodeModel; -import io.github.xwz.base.content.IContentManager; +import io.github.xwz.base.api.EpisodeBaseModel; +import io.github.xwz.base.content.ContentManagerBase; public abstract class SearchFragment extends android.support.v17.leanback.app.SearchFragment implements android.support.v17.leanback.app.SearchFragment.SearchResultProvider { @@ -47,7 +47,7 @@ public ObjectAdapter getResultsAdapter() { return adapter; } - protected abstract IContentManager getContentManger(); + protected abstract ContentManagerBase getContentManger(); protected abstract Class getDetailsActivityClass(); @@ -55,9 +55,9 @@ private OnItemViewClickedListener getItemClickedListener() { return new OnItemViewClickedListener() { @Override public void onItemClicked(Presenter.ViewHolder itemViewHolder, Object item, RowPresenter.ViewHolder rowViewHolder, Row row) { - if (item instanceof IEpisodeModel) { + if (item instanceof EpisodeBaseModel) { Intent intent = new Intent(getActivity(), getDetailsActivityClass()); - intent.putExtra(IContentManager.CONTENT_ID, (IEpisodeModel) item); + intent.putExtra(ContentManagerBase.CONTENT_ID, (EpisodeBaseModel) item); startActivity(intent); } } @@ -98,7 +98,7 @@ public void setQuery(String str) { @Override public void run() { Log.d(TAG, "Searching: " + query); - List results = getContentManger().searchShows(query); + List results = getContentManger().searchShows(query); ArrayObjectAdapter row = new ArrayObjectAdapter(new CardSelector()); row.addAll(0, results); HeaderItem header = new HeaderItem(0, getResources().getString(R.string.search_results)); diff --git a/base/src/main/java/io/github/xwz/base/views/CategoryCardView.java b/base/src/main/java/io/github/xwz/base/views/CategoryCardView.java index 1e942d8..2bc6b07 100644 --- a/base/src/main/java/io/github/xwz/base/views/CategoryCardView.java +++ b/base/src/main/java/io/github/xwz/base/views/CategoryCardView.java @@ -12,7 +12,7 @@ import io.github.xwz.base.R; import io.github.xwz.base.Utils; -import io.github.xwz.base.api.IEpisodeModel; +import io.github.xwz.base.api.EpisodeBaseModel; public class CategoryCardView extends Presenter.ViewHolder { @@ -43,7 +43,7 @@ private View loadCategoryView(Context context, ViewGroup parent) { return layout; } - public void setEpisode(IEpisodeModel ep) { + public void setEpisode(EpisodeBaseModel ep) { title.setText(Utils.stripCategory(ep.getTitle())); badge.setText("" + ep.getEpisodeCount()); } diff --git a/base/src/main/java/io/github/xwz/base/views/EpisodeCardView.java b/base/src/main/java/io/github/xwz/base/views/EpisodeCardView.java index 12d64e5..5c600a7 100644 --- a/base/src/main/java/io/github/xwz/base/views/EpisodeCardView.java +++ b/base/src/main/java/io/github/xwz/base/views/EpisodeCardView.java @@ -4,12 +4,10 @@ import android.graphics.Point; import android.support.v17.leanback.widget.ImageCardView; import android.support.v17.leanback.widget.Presenter; -import android.view.View; -import android.widget.ImageView; import com.squareup.picasso.Picasso; -import io.github.xwz.base.api.IEpisodeModel; +import io.github.xwz.base.api.EpisodeBaseModel; public class EpisodeCardView extends Presenter.ViewHolder { private static final String TAG = "EpisodeCardView"; @@ -27,7 +25,7 @@ public EpisodeCardView(Context context, ImageCardView view, Point s, boolean sho canShowCover = showCover; } - public void setEpisode(IEpisodeModel ep) { + public void setEpisode(EpisodeBaseModel ep) { String series = ep.getSeriesTitle(); String title = ep.getTitle(); if (series == null || series.length() == 0) { diff --git a/base/src/main/java/io/github/xwz/base/views/EpisodeDetailsView.java b/base/src/main/java/io/github/xwz/base/views/EpisodeDetailsView.java index 9b79f0d..c37763d 100644 --- a/base/src/main/java/io/github/xwz/base/views/EpisodeDetailsView.java +++ b/base/src/main/java/io/github/xwz/base/views/EpisodeDetailsView.java @@ -12,7 +12,7 @@ import com.squareup.picasso.Transformation; import io.github.xwz.base.R; -import io.github.xwz.base.api.IEpisodeModel; +import io.github.xwz.base.api.EpisodeBaseModel; import jp.wasabeef.picasso.transformations.gpu.VignetteFilterTransformation; public class EpisodeDetailsView extends Presenter.ViewHolder { @@ -45,7 +45,7 @@ public EpisodeDetailsView(Context context, View view) { loader = new EpisodeLoader(); } - public void setEpisode(IEpisodeModel episode) { + public void setEpisode(EpisodeBaseModel episode) { episodeTitle.setText(episode.getTitle()); seriesTitle.setText(episode.getSeriesTitle()); duration.setText(episode.getDurationText()); @@ -70,20 +70,20 @@ private boolean sameTitles(String a, String b) { return false; } - public void updateEpisode(IEpisodeModel episode) { + public void updateEpisode(EpisodeBaseModel episode) { description.setText(episode.getDescription()); } - private void loadEpisodeDetails(IEpisodeModel episode) { + private void loadEpisodeDetails(EpisodeBaseModel episode) { loader.setEpisode(episode); handler.removeCallbacks(loader); handler.postDelayed(loader, LOADER_DELAY_MS); } private class EpisodeLoader implements Runnable { - private IEpisodeModel episode; + private EpisodeBaseModel episode; - public void setEpisode(IEpisodeModel ep) { + public void setEpisode(EpisodeBaseModel ep) { episode = ep; } diff --git a/base/src/main/java/io/github/xwz/base/views/VideoPlayerView.java b/base/src/main/java/io/github/xwz/base/views/VideoPlayerView.java index dd094fd..f2968ba 100644 --- a/base/src/main/java/io/github/xwz/base/views/VideoPlayerView.java +++ b/base/src/main/java/io/github/xwz/base/views/VideoPlayerView.java @@ -27,9 +27,9 @@ import java.util.Arrays; import java.util.List; +import io.github.xwz.base.api.EpisodeBaseModel; import io.github.xwz.base.player.VideoPlayer; import io.github.xwz.base.R; -import io.github.xwz.base.api.IEpisodeModel; public class VideoPlayerView { private static final String TAG = "VideoPlayerView"; @@ -149,7 +149,7 @@ private void doPauseResume() { } } - public void setEpisode(IEpisodeModel episode) { + public void setEpisode(EpisodeBaseModel episode) { episodeTitle.setText(episode.getTitle()); seriesTitle.setText(episode.getSeriesTitle()); duration.setText(episode.getDurationText()); @@ -275,7 +275,7 @@ public void run() { }, 3000); } - public void suggestNextEpisode(IEpisodeModel episode) { + public void suggestNextEpisode(EpisodeBaseModel episode) { nextEpisode.setEpisode(episode); nextEpisodeCard.setVisibility(View.VISIBLE); } diff --git a/iview/src/main/java/io/github/xwz/iview/activities/VideoPlayerActivity.java b/iview/src/main/java/io/github/xwz/iview/activities/VideoPlayerActivity.java index 9bd26c0..5b9a906 100644 --- a/iview/src/main/java/io/github/xwz/iview/activities/VideoPlayerActivity.java +++ b/iview/src/main/java/io/github/xwz/iview/activities/VideoPlayerActivity.java @@ -1,12 +1,12 @@ package io.github.xwz.iview.activities; -import io.github.xwz.base.content.IContentManager; +import io.github.xwz.base.content.ContentManagerBase; import io.github.xwz.iview.content.ContentManager; public class VideoPlayerActivity extends io.github.xwz.base.activities.VideoPlayerActivity { @Override - protected IContentManager getContentManger() { + protected ContentManagerBase getContentManger() { return ContentManager.getInstance(); } diff --git a/iview/src/main/java/io/github/xwz/iview/api/AuthApi.java b/iview/src/main/java/io/github/xwz/iview/api/AuthApi.java index 7f1745a..6eb8942 100644 --- a/iview/src/main/java/io/github/xwz/iview/api/AuthApi.java +++ b/iview/src/main/java/io/github/xwz/iview/api/AuthApi.java @@ -10,7 +10,6 @@ import io.github.xwz.iview.BuildConfig; import io.github.xwz.iview.content.ContentManager; -import io.github.xwz.base.api.IEpisodeModel; public class AuthApi extends IViewApi { @@ -52,6 +51,8 @@ private void buildAuth(String stream) { } else { Log.w(TAG, "Unable to build stream URL for:" + id); } + } else { + Log.w(TAG, "Unable to find stream URL for:" + id); } } @@ -59,7 +60,7 @@ private String updateEpisodeDetails(String href) { Log.d(TAG, "Need to update episode details"); EpisodeDetailsApi api = new EpisodeDetailsApi(getContext(), href); if (api.updateEpisode(href)) { - IEpisodeModel ep = ContentManager.cache().getEpisode(href); + EpisodeModel ep = (EpisodeModel) ContentManager.cache().getEpisode(href); return ep.getStream(); } else { ContentManager.getInstance().broadcastChange(ContentManager.CONTENT_AUTH_ERROR, ContentManager.AUTH_FAILED_URL, id); diff --git a/iview/src/main/java/io/github/xwz/iview/api/EpisodeDetailsApi.java b/iview/src/main/java/io/github/xwz/iview/api/EpisodeDetailsApi.java index 021dadd..bff6e8f 100644 --- a/iview/src/main/java/io/github/xwz/iview/api/EpisodeDetailsApi.java +++ b/iview/src/main/java/io/github/xwz/iview/api/EpisodeDetailsApi.java @@ -12,8 +12,8 @@ import java.util.List; import java.util.Map; +import io.github.xwz.base.api.EpisodeBaseModel; import io.github.xwz.iview.content.ContentManager; -import io.github.xwz.base.api.IEpisodeModel; public class EpisodeDetailsApi extends IViewApi { private static final String TAG = "EpisodeDetailsApi"; @@ -35,11 +35,12 @@ protected Void doInBackground(String... urls) { } boolean updateEpisode(String url) { - IEpisodeModel ep = fetchEpisodeDetails(url); + EpisodeModel ep = fetchEpisodeDetails(url); if (ep != null) { if (ep.getRelated() != null) { if (fetchRelatedEpisode(ep, ep.getRelated())) { - ContentManager.cache().updateEpisode(ep); + EpisodeModel existing = (EpisodeModel)ContentManager.cache().updateEpisode(ep); + existing.merge(ep); success = true; } } @@ -53,7 +54,7 @@ boolean updateEpisode(String url) { return success; } - private IEpisodeModel fetchEpisodeDetails(String url) { + private EpisodeModel fetchEpisodeDetails(String url) { Log.d(TAG, "Fetching episode details: " + url); String response = fetchUrl(buildApiUrl(url), CACHE_EXPIRY); JSONObject data = parseJSON(response); @@ -63,12 +64,12 @@ private IEpisodeModel fetchEpisodeDetails(String url) { return null; } - private boolean fetchRelatedEpisode(IEpisodeModel ep, String related) { + private boolean fetchRelatedEpisode(EpisodeModel ep, String related) { String response = fetchUrl(buildApiUrl(related), CACHE_EXPIRY); JSONObject data = parseJSON(response); try { if (data != null && data.has("index") && data.get("index") instanceof JSONArray) { - Map> others = getEpisodesFromList(data.getJSONArray("index")); + Map> others = getEpisodesFromList(data.getJSONArray("index")); ep.setOtherEpisodes(others); return true; } @@ -78,14 +79,14 @@ private boolean fetchRelatedEpisode(IEpisodeModel ep, String related) { return false; } - private Map> getEpisodesFromList(JSONArray groups) { - Map> related = new LinkedHashMap<>(); + private Map> getEpisodesFromList(JSONArray groups) { + Map> related = new LinkedHashMap<>(); for (int i = 0, k = groups.length(); i < k; i++) { try { if (groups.get(i) instanceof JSONObject) { JSONObject group = groups.getJSONObject(i); if (group.has("episodes") && group.get("episodes") instanceof JSONArray) { - List titles = new ArrayList<>(); + List titles = new ArrayList<>(); JSONArray episodes = group.getJSONArray("episodes"); for (int j = 0, m = episodes.length(); j < m; j++) { titles.add(EpisodeModel.create(episodes.getJSONObject(j))); diff --git a/iview/src/main/java/io/github/xwz/iview/api/EpisodeModel.java b/iview/src/main/java/io/github/xwz/iview/api/EpisodeModel.java index 0101244..198629c 100644 --- a/iview/src/main/java/io/github/xwz/iview/api/EpisodeModel.java +++ b/iview/src/main/java/io/github/xwz/iview/api/EpisodeModel.java @@ -4,49 +4,15 @@ import org.json.JSONException; import org.json.JSONObject; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.HashSet; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; -import java.util.Set; +import io.github.xwz.base.api.EpisodeBaseModel; -import io.github.xwz.base.Utils; -import io.github.xwz.base.api.IEpisodeModel; - -public class EpisodeModel implements IEpisodeModel { +public class EpisodeModel extends EpisodeBaseModel { private static final String TAG = "EpisodeModel"; - private String seriesTitle; - private String href; - private String format; - private String formatBgColour; - private String formatTextColour; - private String channel; - private String pubDate; - private String thumbnail; - private String livestream; - private String episodeHouseNumber; - private Set categories = new HashSet<>(); - private String title; - private int duration; - private String label; - private String rating; - private int episodeCount; - - private String description; - private String related; - private String availability; - private String stream; - private String captions; - private String share; - private boolean extras = false; + private String stream; - private Map> others = new HashMap<>(); - - public static IEpisodeModel create(JSONObject data) { + public static EpisodeModel create(JSONObject data) { EpisodeModel ep = new EpisodeModel(); ep.set(data); return ep; @@ -63,32 +29,18 @@ private static T get(JSONObject data, String key, T fallback) return value != null ? value : fallback; } - public static String getTAG() { - return TAG; - } - private void set(JSONObject data) { - seriesTitle = get(data, "seriesTitle", getSeriesTitle()); - href = get(data, "href", getHref()); - format = get(data, "format", getFormat()); - formatBgColour = get(data, "formatBgColour", getFormatBgColour()); - formatTextColour = get(data, "formatTextColour", getFormatTextColour()); - channel = get(data, "channel", getChannel()); - pubDate = get(data, "pubDate", getPubDate()); - thumbnail = get(data, "thumbnail", getThumbnail()); - livestream = get(data, "livestream", getLivestream()); - episodeHouseNumber = get(data, "episodeHouseNumber", getEpisodeHouseNumber()); - title = get(data, "title", getTitle()); - duration = getInt(data, "duration", getDuration()); - label = get(data, "label", getLabel()); - rating = get(data, "rating", getRating()); - episodeCount = getInt(data, "episodeCount", getEpisodeCount()); - - description = get(data, "description", getDescription()); - related = get(data, "related", getRelated()); - availability = get(data, "availability", getAvailability()); - captions = get(data, "captions", getCaptions()); - share = get(data, "share", getShare()); + setSeriesTitle(get(data, "seriesTitle", getSeriesTitle())); + setHref(get(data, "href", getHref())); + setChannel(get(data, "channel", getChannel())); + setThumbnail(get(data, "thumbnail", getThumbnail())); + setEpisodeHouseNumber(get(data, "episodeHouseNumber", getEpisodeHouseNumber())); + setTitle(get(data, "title", getTitle())); + setDuration(getInt(data, "duration", getDuration())); + setRating(get(data, "rating", getRating())); + setEpisodeCount(getInt(data, "episodeCount", getEpisodeCount())); + setDescription(get(data, "description", getDescription())); + setRelated(get(data, "related", getRelated())); try { stream = getStream(data); @@ -100,72 +52,14 @@ private void set(JSONObject data) { } } - private void merge(EpisodeModel ep) { - this.seriesTitle = ep.seriesTitle == null ? this.seriesTitle : ep.seriesTitle; - this.href = ep.href == null ? this.href : ep.href; - this.format = ep.format == null ? this.format : ep.format; - this.formatBgColour = ep.formatBgColour == null ? this.formatBgColour : ep.formatBgColour; - this.formatTextColour = ep.formatTextColour == null ? this.formatTextColour : ep.formatTextColour; - this.channel = ep.channel == null ? this.channel : ep.channel; - this.pubDate = ep.pubDate == null ? this.pubDate : ep.pubDate; - this.thumbnail = ep.thumbnail == null ? this.thumbnail : ep.thumbnail; - this.livestream = ep.livestream == null ? this.livestream : ep.livestream; - this.episodeHouseNumber = ep.episodeHouseNumber == null ? this.episodeHouseNumber : ep.episodeHouseNumber; - this.title = ep.title == null ? this.title : ep.title; - this.duration = ep.duration; - this.label = ep.label == null ? this.label : ep.label; - this.rating = ep.rating == null ? this.rating : ep.rating; - this.episodeCount = ep.episodeCount; - this.description = ep.description == null ? this.description : ep.description; - this.related = ep.related == null ? this.related : ep.related; - this.availability = ep.availability == null ? this.availability : ep.availability; - this.stream = ep.stream == null ? this.stream : ep.stream; - this.captions = ep.captions == null ? this.captions : ep.captions; - this.share = ep.share == null ? this.share : ep.share; - - this.categories.addAll(ep.categories); - this.others = ep.others.size() == 0 ? this.others : new LinkedHashMap<>(ep.others); - - this.extras = this.stream != null; - } - - public void setEpisodeCount(int count) { - episodeCount = count; - } - - @Override - public void setHasExtra(boolean extra) { - extras = extra; - } - - @Override - public void merge(IEpisodeModel ep) { - merge((EpisodeModel)ep); - } - - public void setOtherEpisodes(Map> more) { - others = more; - } - - public Map> getOtherEpisodes() { - return new LinkedHashMap<>(others); - } - - private List getOtherEpisodes(String cat) { - for (Map.Entry> episodes : getOtherEpisodes().entrySet()) { - if (episodes.getKey().equals(cat)) { - return episodes.getValue(); - } - } - return new ArrayList<>(); + public String getStream() { + return stream; } - public List getOtherEpisodeUrls(String cat) { - List urls = new ArrayList<>(); - for (IEpisodeModel ep : getOtherEpisodes(cat)) { - urls.add(ep.getHref()); - } - return urls; + public void merge(EpisodeModel ep) { + super.merge(ep); + this.stream = ep.stream == null ? this.stream : ep.stream; + this.extras = this.stream != null; } private String getStream(JSONObject data) throws JSONException { @@ -195,166 +89,7 @@ private static int getInt(JSONObject data, String key, int fallback) { return fallback; } - public boolean matches(String query) { - boolean found = false; - if (getSeriesTitle() != null) { - found = found || getSeriesTitle().toLowerCase().contains(query); - } - if (getTitle() != null) { - found = found || getTitle().toLowerCase().contains(query); - } - return found; - } - - private static boolean getBoolean(JSONObject data, String key, boolean fallback) { - if (data != null && key != null && data.has(key)) { - try { - return data.getBoolean(key); - } catch (JSONException e) { - //Log.d(TAG, "No boolean value for: " + key); - } - } - return fallback; - } - - @Override - public boolean equals(Object o) { - if (this == o) - return true; - if (!(o instanceof EpisodeModel)) - return false; - EpisodeModel other = (EpisodeModel) o; - return other.getHref().equals(this.getHref()); - } - - public String getDurationText() { - if (getRating() != null) { - return getRating() + ", " + Utils.formatMillis(getDuration() * 1000); - } else { - return Utils.formatMillis(getDuration() * 1000); - } - } - - public void addCategory(String cat) { - categories.add(cat); - } - - public void setCategories(List cats) { - categories = new HashSet<>(cats); - } - - public String toString() { - return getHref() + ": '" + getSeriesTitle() + "' - '" + getTitle() + "'"; - } - - public String getSeriesTitle() { - return seriesTitle; - } - - public String getHref() { - return href; - } - - public String getFormat() { - return format; - } - - public String getFormatBgColour() { - return formatBgColour; - } - - public String getFormatTextColour() { - return formatTextColour; - } - - public String getChannel() { - return channel; - } - - public String getPubDate() { - return pubDate; - } - - public String getThumbnail() { - return thumbnail; - } - - public String getLivestream() { - return livestream; - } - - public String getEpisodeHouseNumber() { - return episodeHouseNumber; - } - - public List getCategories() { - return new ArrayList<>(categories); - } - - public String getTitle() { - return title; - } - - public int getDuration() { - return duration; - } - - public String getLabel() { - return label; - } - - public String getRating() { - return rating; - } - - public int getEpisodeCount() { - return episodeCount; - } - - public String getDescription() { - return description; - } - - public String getRelated() { - return related; - } - - public String getAvailability() { - return availability; - } - - public String getStream() { - return stream; - } - - public String getCaptions() { - return captions; - } - - public String getShare() { - return share; - } - public boolean hasExtras() { return extras; } - - public boolean hasOtherEpisodes() { - return others.size() > 0; - } - - @Override - public boolean isFilm() { - return false; - } - - @Override - public String getCover() { - return null; - } - - @Override - public boolean hasCover() { - return false; - } } diff --git a/iview/src/main/java/io/github/xwz/iview/api/TvShowListApi.java b/iview/src/main/java/io/github/xwz/iview/api/TvShowListApi.java index b809bc4..ccddc0c 100644 --- a/iview/src/main/java/io/github/xwz/iview/api/TvShowListApi.java +++ b/iview/src/main/java/io/github/xwz/iview/api/TvShowListApi.java @@ -16,17 +16,17 @@ import java.util.List; import java.util.Map; -import io.github.xwz.iview.content.ContentManager; import io.github.xwz.base.ImmutableMap; -import io.github.xwz.base.api.IEpisodeModel; +import io.github.xwz.base.api.EpisodeBaseModel; +import io.github.xwz.iview.content.ContentManager; public class TvShowListApi extends IViewApi { private static final String TAG = "TvShowListApi"; private static final int CACHE_EXPIRY = 600; // 10 mins - private final Map episodes = new HashMap<>(); - private final List shows = new ArrayList<>(); - private LinkedHashMap> collections = new LinkedHashMap<>(); + private final Map episodes = new HashMap<>(); + private final List shows = new ArrayList<>(); + private LinkedHashMap> collections = new LinkedHashMap<>(); private boolean success = false; public TvShowListApi(Context context) { @@ -46,7 +46,7 @@ protected Void doInBackground(String... urls) { updateProgress(); fetchTitlesFromCollection(); - for (Map.Entry> collection : collections.entrySet()) { + for (Map.Entry> collection : collections.entrySet()) { Log.d(TAG, "Found collection: " + collection.getKey()); ContentManager.cache().addCollection(collection.getKey(), collection.getValue()); } @@ -73,8 +73,8 @@ private void updateProgress() { private void fetchTitlesFromCollection() { String response = fetchUrl(getHomeUrl(), CACHE_EXPIRY); JSONObject data = parseJSON(response); - List titles = getEpisodesFromData(data, true); - for (IEpisodeModel ep : titles) { + List titles = getEpisodesFromData(data, true); + for (EpisodeBaseModel ep : titles) { episodes.put(ep.getHref(), ep); } } @@ -82,9 +82,9 @@ private void fetchTitlesFromCollection() { private void fetchTitlesFromIndex() { String response = fetchUrl(getIndexUrl(), CACHE_EXPIRY); JSONObject data = parseJSON(response); - List titles = getEpisodesFromData(data, false); + List titles = getEpisodesFromData(data, false); Log.d(TAG, "Found " + titles.size() + " episode from index query"); - for (IEpisodeModel title : titles) { + for (EpisodeBaseModel title : titles) { if (episodes.containsKey(title.getHref())) { title.setCategories(episodes.get(title.getHref()).getCategories()); } else { @@ -97,8 +97,8 @@ private void fetchTitlesFromIndex() { private void fetchTitlesInCategory(String cat) { String response = fetchUrl(getCategoryUrl(cat), CACHE_EXPIRY); JSONObject data = parseJSON(response); - List titles = getEpisodesFromData(data, false); - for (IEpisodeModel title : titles) { + List titles = getEpisodesFromData(data, false); + for (EpisodeBaseModel title : titles) { if (episodes.containsKey(title.getHref())) { title = episodes.get(title.getHref()); } @@ -107,8 +107,8 @@ private void fetchTitlesInCategory(String cat) { } } - private List getEpisodesFromData(JSONObject data, boolean addToCollection) { - List titles = new ArrayList<>(); + private List getEpisodesFromData(JSONObject data, boolean addToCollection) { + List titles = new ArrayList<>(); if (data != null) { Iterator keys = data.keys(); while (keys.hasNext()) { @@ -126,13 +126,13 @@ private List getEpisodesFromData(JSONObject data, boolean addToCo return titles; } - private List getEpisodesFromList(JSONArray groups, boolean addToCollection) { - List titles = new ArrayList<>(); + private List getEpisodesFromList(JSONArray groups, boolean addToCollection) { + List titles = new ArrayList<>(); for (int i = 0, k = groups.length(); i < k; i++) { try { if (groups.get(i) instanceof JSONObject) { JSONObject group = groups.getJSONObject(i); - List episodes = new ArrayList<>(); + List episodes = new ArrayList<>(); if (group.has("episodes") && group.get("episodes") instanceof JSONArray) { JSONArray data = group.getJSONArray("episodes"); for (int j = 0, m = data.length(); j < m; j++) { diff --git a/iview/src/main/java/io/github/xwz/iview/content/ContentManager.java b/iview/src/main/java/io/github/xwz/iview/content/ContentManager.java index 85a5b7a..f770937 100644 --- a/iview/src/main/java/io/github/xwz/iview/content/ContentManager.java +++ b/iview/src/main/java/io/github/xwz/iview/content/ContentManager.java @@ -10,10 +10,11 @@ import java.util.Map; import io.github.xwz.base.ImmutableMap; +import io.github.xwz.base.api.EpisodeBaseModel; import io.github.xwz.base.content.ContentManagerBase; -import io.github.xwz.base.api.IEpisodeModel; import io.github.xwz.iview.api.AuthApi; import io.github.xwz.iview.api.EpisodeDetailsApi; +import io.github.xwz.iview.api.EpisodeModel; import io.github.xwz.iview.api.TvShowListApi; public class ContentManager extends ContentManagerBase { @@ -58,15 +59,16 @@ public void fetchShowList(boolean force) { } @Override - public void fetchAuthToken(IEpisodeModel episode) { - cache().broadcastChange(CONTENT_AUTH_FETCHING, episode.getHref()); - new AuthApi(getContext(), episode.getHref()).execute(episode.getStream()); + public void fetchAuthToken(EpisodeBaseModel episode) { + EpisodeModel ep = (EpisodeModel)episode; + cache().broadcastChange(CONTENT_AUTH_FETCHING, ep.getHref()); + new AuthApi(getContext(), episode.getHref()).execute(ep.getStream()); } @Override - public void fetchEpisode(IEpisodeModel episode) { + public void fetchEpisode(EpisodeBaseModel episode) { broadcastChange(CONTENT_EPISODE_FETCHING, episode.getHref()); - IEpisodeModel existing = getEpisode(episode.getHref()); + EpisodeModel existing = (EpisodeModel)getEpisode(episode.getHref()); if (existing != null && existing.hasExtras() && existing.hasOtherEpisodes()) { cache().broadcastChangeDelayed(100, CONTENT_EPISODE_DONE, episode.getHref(), null); } else { @@ -75,14 +77,14 @@ public void fetchEpisode(IEpisodeModel episode) { } @Override - public LinkedHashMap> getAllShowsByCategories() { - List shows = getAllShows(); - LinkedHashMap> all = new LinkedHashMap<>(); + public LinkedHashMap> getAllShowsByCategories() { + List shows = getAllShows(); + LinkedHashMap> all = new LinkedHashMap<>(); all.putAll(cache().getCollections()); for (Map.Entry channel : CHANNELS.entrySet()) { - List episodes = new ArrayList<>(); - for (IEpisodeModel show : shows) { + List episodes = new ArrayList<>(); + for (EpisodeBaseModel show : shows) { if (channel.getKey().equals(show.getChannel())) { episodes.add(show); } @@ -90,8 +92,8 @@ public LinkedHashMap> getAllShowsByCategories() { all.put(channel.getValue(), episodes); } for (Map.Entry cat : CATEGORIES.entrySet()) { - List episodes = new ArrayList<>(); - for (IEpisodeModel show : shows) { + List episodes = new ArrayList<>(); + for (EpisodeBaseModel show : shows) { if (show.getCategories().contains(cat.getKey())) { episodes.add(show); } @@ -102,8 +104,8 @@ public LinkedHashMap> getAllShowsByCategories() { } @Override - public List getRecommendations() { - List all = getAllShows(); + public List getRecommendations() { + List all = getAllShows(); if (all.size() > 40) { return getAllShows().subList(30, 32); } diff --git a/iview/src/main/java/io/github/xwz/iview/fragments/CategoryFragment.java b/iview/src/main/java/io/github/xwz/iview/fragments/CategoryFragment.java index 1835f30..6eb28e4 100644 --- a/iview/src/main/java/io/github/xwz/iview/fragments/CategoryFragment.java +++ b/iview/src/main/java/io/github/xwz/iview/fragments/CategoryFragment.java @@ -1,6 +1,6 @@ package io.github.xwz.iview.fragments; -import io.github.xwz.base.content.IContentManager; +import io.github.xwz.base.content.ContentManagerBase; import io.github.xwz.iview.R; import io.github.xwz.iview.activities.DetailsActivity; import io.github.xwz.iview.activities.SearchActivity; @@ -8,7 +8,7 @@ public class CategoryFragment extends io.github.xwz.base.fragments.CategoryFragment { @Override - protected IContentManager getContentManger() { + protected ContentManagerBase getContentManger() { return ContentManager.getInstance(); } diff --git a/iview/src/main/java/io/github/xwz/iview/fragments/DetailsFragment.java b/iview/src/main/java/io/github/xwz/iview/fragments/DetailsFragment.java index 7ffcd96..9e1427b 100644 --- a/iview/src/main/java/io/github/xwz/iview/fragments/DetailsFragment.java +++ b/iview/src/main/java/io/github/xwz/iview/fragments/DetailsFragment.java @@ -1,13 +1,13 @@ package io.github.xwz.iview.fragments; +import io.github.xwz.base.content.ContentManagerBase; import io.github.xwz.iview.activities.VideoPlayerActivity; import io.github.xwz.iview.content.ContentManager; -import io.github.xwz.base.content.IContentManager; public class DetailsFragment extends io.github.xwz.base.fragments.DetailsFragment { @Override - protected IContentManager getContentManger() { + protected ContentManagerBase getContentManger() { return ContentManager.getInstance(); } diff --git a/iview/src/main/java/io/github/xwz/iview/fragments/MainFragment.java b/iview/src/main/java/io/github/xwz/iview/fragments/MainFragment.java index 274f94c..b886c4e 100644 --- a/iview/src/main/java/io/github/xwz/iview/fragments/MainFragment.java +++ b/iview/src/main/java/io/github/xwz/iview/fragments/MainFragment.java @@ -3,17 +3,17 @@ import android.support.v17.leanback.widget.BrowseFrameLayout; import android.view.View; +import io.github.xwz.base.content.ContentManagerBase; import io.github.xwz.iview.R; import io.github.xwz.iview.activities.CategoryActivity; import io.github.xwz.iview.activities.DetailsActivity; import io.github.xwz.iview.activities.SearchActivity; import io.github.xwz.iview.content.ContentManager; -import io.github.xwz.base.content.IContentManager; public class MainFragment extends io.github.xwz.base.fragments.MainFragment { @Override - protected IContentManager getContentManger() { + protected ContentManagerBase getContentManger() { return ContentManager.getInstance(); } diff --git a/iview/src/main/java/io/github/xwz/iview/fragments/SearchFragment.java b/iview/src/main/java/io/github/xwz/iview/fragments/SearchFragment.java index 00559b4..ce9a080 100644 --- a/iview/src/main/java/io/github/xwz/iview/fragments/SearchFragment.java +++ b/iview/src/main/java/io/github/xwz/iview/fragments/SearchFragment.java @@ -1,13 +1,13 @@ package io.github.xwz.iview.fragments; +import io.github.xwz.base.content.ContentManagerBase; import io.github.xwz.iview.activities.DetailsActivity; import io.github.xwz.iview.content.ContentManager; -import io.github.xwz.base.content.IContentManager; public class SearchFragment extends io.github.xwz.base.fragments.SearchFragment { @Override - protected IContentManager getContentManger() { + protected ContentManagerBase getContentManger() { return ContentManager.getInstance(); } diff --git a/sbs/src/main/java/io/github/xwz/sbs/activities/VideoPlayerActivity.java b/sbs/src/main/java/io/github/xwz/sbs/activities/VideoPlayerActivity.java index 0a11953..7ba4a28 100644 --- a/sbs/src/main/java/io/github/xwz/sbs/activities/VideoPlayerActivity.java +++ b/sbs/src/main/java/io/github/xwz/sbs/activities/VideoPlayerActivity.java @@ -1,12 +1,12 @@ package io.github.xwz.sbs.activities; -import io.github.xwz.base.content.IContentManager; +import io.github.xwz.base.content.ContentManagerBase; import io.github.xwz.sbs.content.ContentManager; public class VideoPlayerActivity extends io.github.xwz.base.activities.VideoPlayerActivity { @Override - protected IContentManager getContentManger() { + protected ContentManagerBase getContentManger() { return ContentManager.getInstance(); } diff --git a/sbs/src/main/java/io/github/xwz/sbs/api/EpisodeModel.java b/sbs/src/main/java/io/github/xwz/sbs/api/EpisodeModel.java index 2fb33dc..999633a 100644 --- a/sbs/src/main/java/io/github/xwz/sbs/api/EpisodeModel.java +++ b/sbs/src/main/java/io/github/xwz/sbs/api/EpisodeModel.java @@ -1,269 +1,41 @@ package io.github.xwz.sbs.api; -import org.json.JSONException; -import org.json.JSONObject; +import io.github.xwz.base.api.EpisodeBaseModel; +import io.github.xwz.base.content.ContentManagerBase; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.HashSet; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; -import java.util.Set; - -import io.github.xwz.base.Utils; -import io.github.xwz.base.content.IContentManager; -import io.github.xwz.base.api.IEpisodeModel; - -public class EpisodeModel implements IEpisodeModel { +public class EpisodeModel extends EpisodeBaseModel { private static final String TAG = "EpisodeModel"; - - private String seriesTitle; - private String href; - private String channel; - private String thumbnail; - private String livestream; - private String episodeHouseNumber; - private Set categories = new HashSet<>(); - private String title; - private int duration; - private String rating; - private int episodeCount; - - private String description; - private String related; - private String availability; - private String stream; - private String captions; - private String share; - private String cover; - private boolean film = false; - - private boolean extras = false; private boolean fetchedRelated = false; + private boolean extras = false; - private Map> others = new HashMap<>(); - - public static IEpisodeModel create(SBSApi.Entry data) { + public static EpisodeModel create(SBSApi.Entry data) { EpisodeModel ep = new EpisodeModel(); ep.set(data); return ep; } private void set(SBSApi.Entry data) { - seriesTitle = data.series; - href = data.id; - - channel = data.getChannel(); - thumbnail = data.getThumbnail(); - livestream = null; - episodeHouseNumber = data.guid; - title = data.title; - duration = data.getDuration(); - rating = data.getRating(); - episodeCount = 0; - film = data.isFilm(); - cover = data.getCover(); - categories.addAll(data.getCategories()); - - description = data.synopsis; - } - - private void merge(EpisodeModel ep) { - this.seriesTitle = ep.seriesTitle == null ? this.seriesTitle : ep.seriesTitle; - this.href = ep.href == null ? this.href : ep.href; - this.channel = ep.channel == null ? this.channel : ep.channel; - this.thumbnail = ep.thumbnail == null ? this.thumbnail : ep.thumbnail; - this.livestream = ep.livestream == null ? this.livestream : ep.livestream; - this.episodeHouseNumber = ep.episodeHouseNumber == null ? this.episodeHouseNumber : ep.episodeHouseNumber; - this.title = ep.title == null ? this.title : ep.title; - this.duration = ep.duration; - this.rating = ep.rating == null ? this.rating : ep.rating; - this.episodeCount = ep.episodeCount; - this.description = ep.description == null ? this.description : ep.description; - this.related = ep.related == null ? this.related : ep.related; - this.availability = ep.availability == null ? this.availability : ep.availability; - this.stream = ep.stream == null ? this.stream : ep.stream; - this.captions = ep.captions == null ? this.captions : ep.captions; - this.share = ep.share == null ? this.share : ep.share; - - this.categories.addAll(ep.categories); - this.others = ep.others.size() == 0 ? this.others : new LinkedHashMap<>(ep.others); - - this.extras = this.stream != null; - } - - @Override - public void merge(IEpisodeModel ep) { - merge((EpisodeModel) ep); - } - - public void setOtherEpisodes(Map> more) { - others = more; - if (more.containsKey(IContentManager.OTHER_EPISODES)) { - if (more.get(IContentManager.OTHER_EPISODES).size() > 1) { - episodeCount = more.get(IContentManager.OTHER_EPISODES).size() + 1; - setHasExtra(true); - } - } - } - - public void setOtherEpisodes(String cat, List more) { - others.put(cat, more); - } + setSeriesTitle(data.series); + setHref(data.id); - public void setHasExtra(boolean extra) { - extras = extra; + setChannel(data.getChannel()); + setThumbnail(data.getThumbnail()); + setEpisodeHouseNumber(data.guid); + setTitle(data.title); + setDuration(data.getDuration()); + setRating(data.getRating()); + setIsFilm(data.isFilm()); + setCover(data.getCover()); + setCategories(data.getCategories()); + setDescription(data.synopsis); } public void setHasFetchedRelated(boolean fetched) { fetchedRelated = fetched; } - @Override - public void setEpisodeCount(int count) { - episodeCount = count; - } - - public Map> getOtherEpisodes() { - return new LinkedHashMap<>(others); - } - - private List getOtherEpisodes(String cat) { - for (Map.Entry> episodes : getOtherEpisodes().entrySet()) { - if (episodes.getKey().equals(cat)) { - return episodes.getValue(); - } - } - return new ArrayList<>(); - } - - public List getOtherEpisodeUrls(String cat) { - List urls = new ArrayList<>(); - for (IEpisodeModel ep : getOtherEpisodes(cat)) { - urls.add(ep.getHref()); - } - return urls; - } - - public boolean matches(String query) { - boolean found = false; - if (getSeriesTitle() != null) { - found = found || getSeriesTitle().toLowerCase().contains(query); - } - if (getTitle() != null) { - found = found || getTitle().toLowerCase().contains(query); - } - return found; - } - - private static boolean getBoolean(JSONObject data, String key, boolean fallback) { - if (data != null && key != null && data.has(key)) { - try { - return data.getBoolean(key); - } catch (JSONException e) { - //Log.d(TAG, "No boolean value for: " + key); - } - } - return fallback; - } - - @Override - public boolean equals(Object o) { - if (this == o) - return true; - if (!(o instanceof EpisodeModel)) - return false; - EpisodeModel other = (EpisodeModel) o; - return other.getHref().equals(this.getHref()); - } - - public String getDurationText() { - if (getRating() != null) { - return getRating() + ", " + Utils.formatMillis(getDuration() * 1000); - } else { - return Utils.formatMillis(getDuration() * 1000); - } - } - - public void addCategory(String cat) { - categories.add(cat); - } - - public void setCategories(List cats) { - categories = new HashSet<>(cats); - } - - public String toString() { - return getHref() + ": '" + getSeriesTitle() + "' - '" + getTitle() + "'"; - } - - public String getSeriesTitle() { - return seriesTitle; - } - - public String getHref() { - return href; - } - - public String getChannel() { - return channel; - } - - public String getThumbnail() { - return thumbnail; - } - - public String getLivestream() { - return livestream; - } - - public String getEpisodeHouseNumber() { - return episodeHouseNumber; - } - - public List getCategories() { - return new ArrayList<>(categories); - } - - public String getTitle() { - return title; - } - - public int getDuration() { - return duration; - } - - public String getRating() { - return rating; - } - - public int getEpisodeCount() { - return episodeCount; - } - - public String getDescription() { - return description; - } - - public String getRelated() { - return related; - } - - public String getAvailability() { - return availability; - } - - public String getStream() { - return stream; - } - - public String getCaptions() { - return captions; - } - - public String getShare() { - return share; + public void setHasExtra(boolean extras) { + this.extras = extras; } public boolean hasExtras() { @@ -271,21 +43,6 @@ public boolean hasExtras() { } public boolean hasOtherEpisodes() { - return fetchedRelated || others.containsKey(IContentManager.MORE_LIKE_THIS); - } - - @Override - public boolean isFilm() { - return film; - } - - @Override - public String getCover() { - return cover; - } - - @Override - public boolean hasCover() { - return isFilm() && getCover() != null && getCover().length() > 0; + return fetchedRelated || hasOther(ContentManagerBase.MORE_LIKE_THIS); } } diff --git a/sbs/src/main/java/io/github/xwz/sbs/api/SBSApi.java b/sbs/src/main/java/io/github/xwz/sbs/api/SBSApi.java index 2064de8..26987cb 100644 --- a/sbs/src/main/java/io/github/xwz/sbs/api/SBSApi.java +++ b/sbs/src/main/java/io/github/xwz/sbs/api/SBSApi.java @@ -15,8 +15,8 @@ import io.github.xwz.base.ImmutableMap; import io.github.xwz.base.Utils; -import io.github.xwz.base.api.IEpisodeModel; -import io.github.xwz.base.content.IContentManager; +import io.github.xwz.base.api.EpisodeBaseModel; +import io.github.xwz.base.content.ContentManagerBase; import io.github.xwz.sbs.content.ContentManager; public class SBSApi extends SBSApiBase { @@ -28,8 +28,8 @@ public class SBSApi extends SBSApiBase { private int page = 0; private int progress = 0; - private final List episodes = new ArrayList<>(); - private HashMap> showList = new HashMap<>(); + private final List episodes = new ArrayList<>(); + private HashMap> showList = new HashMap<>(); private boolean success = false; private static final String[] PROGRESS = new String[]{ @@ -50,28 +50,28 @@ protected Void doInBackground(String... params) { } // Build a list of shows - List shows = new ArrayList<>(); - for (List eps : showList.values()) { - IEpisodeModel show = eps.get(0); + List shows = new ArrayList<>(); + for (List eps : showList.values()) { + EpisodeBaseModel show = eps.get(0); // calculate other episodes - for (IEpisodeModel ep : eps) { - List others = new ArrayList<>(eps); + for (EpisodeBaseModel ep : eps) { + List others = new ArrayList<>(eps); others.remove(ep); - Map> more = new HashMap<>(); - more.put(IContentManager.OTHER_EPISODES, others); + Map> more = new HashMap<>(); + more.put(ContentManagerBase.OTHER_EPISODES, others); ep.setOtherEpisodes(more); - ep.setHasExtra(others.size() > 0); + ((EpisodeModel)ep).setHasExtra(others.size() > 0); } shows.add(show); } // build a list of show collections by category - Map> collections = new HashMap<>(); - for (IEpisodeModel show : shows) { + Map> collections = new HashMap<>(); + for (EpisodeBaseModel show : shows) { for (String cat : show.getCategories()) { if (!collections.containsKey(cat)) { - collections.put(cat, new ArrayList()); + collections.put(cat, new ArrayList()); } collections.get(cat).add(show); } @@ -79,7 +79,7 @@ protected Void doInBackground(String... params) { for (Map.Entry entry : getFeaturedUrls().entrySet()) { updateProgress("Loading " + Utils.stripCategory(entry.getKey()) + "..."); - List features = fetchContent(entry.getValue(), CACHE_EXPIRY); + List features = fetchContent(entry.getValue(), CACHE_EXPIRY); collections.put(entry.getKey(), features); } @@ -90,7 +90,7 @@ protected Void doInBackground(String... params) { // add collections by name for (String key : keys) { - List collection = collections.get(key); + List collection = collections.get(key); Log.d(TAG, "Found collection: " + key + " = " + collection.size()); String[] parts = key.split("/"); String name = parts.length > 1 ? parts[1] : key; @@ -141,16 +141,16 @@ protected void onPostExecute(Void v) { } private void fetchAllTitles(int page) { - List all = fetchContent(getIndexUrl(page), CACHE_EXPIRY); - for (IEpisodeModel ep : all) { + List all = fetchContent(getIndexUrl(page), CACHE_EXPIRY); + for (EpisodeBaseModel ep : all) { addToShowList(ep.getSeriesTitle(), ep); episodes.add(ep); } } - private void addToShowList(String title, IEpisodeModel ep) { + private void addToShowList(String title, EpisodeBaseModel ep) { if (!showList.containsKey(title)) { - showList.put(title, new ArrayList()); + showList.put(title, new ArrayList()); } showList.get(title).add(ep); } diff --git a/sbs/src/main/java/io/github/xwz/sbs/api/SBSApiBase.java b/sbs/src/main/java/io/github/xwz/sbs/api/SBSApiBase.java index f967724..9bc5238 100644 --- a/sbs/src/main/java/io/github/xwz/sbs/api/SBSApiBase.java +++ b/sbs/src/main/java/io/github/xwz/sbs/api/SBSApiBase.java @@ -20,8 +20,8 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; +import io.github.xwz.base.api.EpisodeBaseModel; import io.github.xwz.base.api.HttpApiBase; -import io.github.xwz.base.api.IEpisodeModel; import io.github.xwz.sbs.BuildConfig; abstract class SBSApiBase extends HttpApiBase { @@ -100,9 +100,9 @@ protected int getLastEntryCount() { return lastEntryCount; } - List fetchContent(Uri url, int staleness) { + List fetchContent(Uri url, int staleness) { InputStream response = fetchStream(url, staleness); - List all = new ArrayList<>(); + List all = new ArrayList<>(); if (response != null) { try { JsonReader reader = new JsonReader(new InputStreamReader(response, "UTF-8")); @@ -131,14 +131,14 @@ List fetchContent(Uri url, int staleness) { return all; } - private List readEntriesArray(JsonReader reader) throws IOException { + private List readEntriesArray(JsonReader reader) throws IOException { reader.beginArray(); Gson gson = new GsonBuilder().create(); - List all = new ArrayList<>(); + List all = new ArrayList<>(); while (reader.hasNext()) { try { Entry entry = gson.fromJson(reader, Entry.class); - IEpisodeModel ep = EpisodeModel.create(entry); + EpisodeModel ep = EpisodeModel.create(entry); all.add(ep); } catch (JsonSyntaxException e) { e.printStackTrace(); diff --git a/sbs/src/main/java/io/github/xwz/sbs/api/SBSRelatedApi.java b/sbs/src/main/java/io/github/xwz/sbs/api/SBSRelatedApi.java index 20416fc..edb6fe8 100644 --- a/sbs/src/main/java/io/github/xwz/sbs/api/SBSRelatedApi.java +++ b/sbs/src/main/java/io/github/xwz/sbs/api/SBSRelatedApi.java @@ -9,8 +9,8 @@ import java.util.Map; import io.github.xwz.base.ImmutableMap; -import io.github.xwz.base.api.IEpisodeModel; -import io.github.xwz.base.content.IContentManager; +import io.github.xwz.base.api.EpisodeBaseModel; +import io.github.xwz.base.content.ContentManagerBase; import io.github.xwz.sbs.content.ContentManager; public class SBSRelatedApi extends SBSApiBase { @@ -38,16 +38,16 @@ private boolean updateEpisode(String url) { if (current != null) { Log.d(TAG, "Fetched related info for: " + current); String series = current.getSeriesTitle(); - List related = fetchRelated(url); + List related = fetchRelated(url); if (related != null) { - List more = new ArrayList<>(); - for (IEpisodeModel ep : related) { + List more = new ArrayList<>(); + for (EpisodeBaseModel ep : related) { if (series != null && !series.equals(ep.getSeriesTitle())) { more.add(ep); } } if (more.size() > 0) { - current.setOtherEpisodes(IContentManager.MORE_LIKE_THIS, more); + current.setOtherEpisodes(ContentManagerBase.MORE_LIKE_THIS, more); } current.setHasExtra(true); current.setHasFetchedRelated(true); @@ -59,15 +59,15 @@ private boolean updateEpisode(String url) { return false; } - private List fetchRelated(String url) { + private List fetchRelated(String url) { String id = getIdFromUrl(url); if (id != null) { - List all = new ArrayList<>(); - List related = fetchContent(getRelatedUrl(id), CACHE_EXPIRY); - for (IEpisodeModel ep : related) { + List all = new ArrayList<>(); + List related = fetchContent(getRelatedUrl(id), CACHE_EXPIRY); + for (EpisodeBaseModel ep : related) { // load from existing cache if possible, has better data. - IEpisodeModel info = ContentManager.getInstance().getEpisode(ep.getHref()); + EpisodeBaseModel info = ContentManager.getInstance().getEpisode(ep.getHref()); if (info != null) { all.add(info); } else { diff --git a/sbs/src/main/java/io/github/xwz/sbs/content/ContentManager.java b/sbs/src/main/java/io/github/xwz/sbs/content/ContentManager.java index f2d9fa3..766b4d2 100644 --- a/sbs/src/main/java/io/github/xwz/sbs/content/ContentManager.java +++ b/sbs/src/main/java/io/github/xwz/sbs/content/ContentManager.java @@ -8,8 +8,9 @@ import java.util.Date; import java.util.List; +import io.github.xwz.base.api.EpisodeBaseModel; import io.github.xwz.base.content.ContentManagerBase; -import io.github.xwz.base.api.IEpisodeModel; +import io.github.xwz.sbs.api.EpisodeModel; import io.github.xwz.sbs.api.SBSApi; import io.github.xwz.sbs.api.SBSAuthApi; import io.github.xwz.sbs.api.SBSRelatedApi; @@ -38,9 +39,9 @@ public void fetchShowList(boolean force) { } @Override - public void fetchEpisode(IEpisodeModel episode) { + public void fetchEpisode(EpisodeBaseModel episode) { broadcastChange(CONTENT_EPISODE_FETCHING, episode.getHref()); - IEpisodeModel existing = cache().getEpisode(episode.getHref()); + EpisodeModel existing = (EpisodeModel)cache().getEpisode(episode.getHref()); if (existing != null && existing.hasExtras() && existing.hasOtherEpisodes()) { cache().broadcastChangeDelayed(100, CONTENT_EPISODE_DONE, episode.getHref(), null); } else { @@ -49,14 +50,14 @@ public void fetchEpisode(IEpisodeModel episode) { } @Override - public void fetchAuthToken(IEpisodeModel episode) { + public void fetchAuthToken(EpisodeBaseModel episode) { cache().broadcastChange(CONTENT_AUTH_FETCHING, episode.getHref()); new SBSAuthApi(getContext() , episode.getHref()).execute(episode.getHref()); } @Override - public List getRecommendations() { - List all = getAllShows(); + public List getRecommendations() { + List all = getAllShows(); if (all.size() > 40) { return getAllShows().subList(30, 32); } diff --git a/sbs/src/main/java/io/github/xwz/sbs/fragments/CategoryFragment.java b/sbs/src/main/java/io/github/xwz/sbs/fragments/CategoryFragment.java index b370e19..4e527a2 100644 --- a/sbs/src/main/java/io/github/xwz/sbs/fragments/CategoryFragment.java +++ b/sbs/src/main/java/io/github/xwz/sbs/fragments/CategoryFragment.java @@ -1,6 +1,6 @@ package io.github.xwz.sbs.fragments; -import io.github.xwz.base.content.IContentManager; +import io.github.xwz.base.content.ContentManagerBase; import io.github.xwz.sbs.R; import io.github.xwz.sbs.activities.DetailsActivity; import io.github.xwz.sbs.activities.SearchActivity; @@ -8,7 +8,7 @@ public class CategoryFragment extends io.github.xwz.base.fragments.CategoryFragment { @Override - protected IContentManager getContentManger() { + protected ContentManagerBase getContentManger() { return ContentManager.getInstance(); } diff --git a/sbs/src/main/java/io/github/xwz/sbs/fragments/DetailsFragment.java b/sbs/src/main/java/io/github/xwz/sbs/fragments/DetailsFragment.java index 9cfc7d2..5f8c60b 100644 --- a/sbs/src/main/java/io/github/xwz/sbs/fragments/DetailsFragment.java +++ b/sbs/src/main/java/io/github/xwz/sbs/fragments/DetailsFragment.java @@ -1,13 +1,13 @@ package io.github.xwz.sbs.fragments; -import io.github.xwz.base.content.IContentManager; +import io.github.xwz.base.content.ContentManagerBase; import io.github.xwz.sbs.activities.VideoPlayerActivity; import io.github.xwz.sbs.content.ContentManager; public class DetailsFragment extends io.github.xwz.base.fragments.DetailsFragment { @Override - protected IContentManager getContentManger() { + protected ContentManagerBase getContentManger() { return ContentManager.getInstance(); } diff --git a/sbs/src/main/java/io/github/xwz/sbs/fragments/MainFragment.java b/sbs/src/main/java/io/github/xwz/sbs/fragments/MainFragment.java index b0512ee..0e801a3 100644 --- a/sbs/src/main/java/io/github/xwz/sbs/fragments/MainFragment.java +++ b/sbs/src/main/java/io/github/xwz/sbs/fragments/MainFragment.java @@ -3,7 +3,7 @@ import android.support.v17.leanback.widget.BrowseFrameLayout; import android.view.View; -import io.github.xwz.base.content.IContentManager; +import io.github.xwz.base.content.ContentManagerBase; import io.github.xwz.sbs.R; import io.github.xwz.sbs.activities.CategoryActivity; import io.github.xwz.sbs.activities.DetailsActivity; @@ -14,7 +14,7 @@ public class MainFragment extends io.github.xwz.base.fragments.MainFragment { @Override - protected IContentManager getContentManger() { + protected ContentManagerBase getContentManger() { return ContentManager.getInstance(); } diff --git a/sbs/src/main/java/io/github/xwz/sbs/fragments/SearchFragment.java b/sbs/src/main/java/io/github/xwz/sbs/fragments/SearchFragment.java index 3aa80d4..38b5d89 100644 --- a/sbs/src/main/java/io/github/xwz/sbs/fragments/SearchFragment.java +++ b/sbs/src/main/java/io/github/xwz/sbs/fragments/SearchFragment.java @@ -1,6 +1,6 @@ package io.github.xwz.sbs.fragments; -import io.github.xwz.base.content.IContentManager; +import io.github.xwz.base.content.ContentManagerBase; import io.github.xwz.sbs.activities.DetailsActivity; import io.github.xwz.sbs.content.ContentManager; @@ -8,7 +8,7 @@ public class SearchFragment extends io.github.xwz.base.fragments.SearchFragment { @Override - protected IContentManager getContentManger() { + protected ContentManagerBase getContentManger() { return ContentManager.getInstance(); }