Skip to content

Commit

Permalink
Refactor into a base model
Browse files Browse the repository at this point in the history
  • Loading branch information
xwz committed Sep 17, 2015
1 parent f3f356a commit 97e8092
Show file tree
Hide file tree
Showing 39 changed files with 471 additions and 1,120 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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}.
Expand Down Expand Up @@ -83,19 +83,19 @@ public abstract class VideoPlayerActivity extends BaseActivity implements Surfac

private boolean ready = false;

private IEpisodeModel mCurrentEpisode;
private EpisodeBaseModel mCurrentEpisode;
private List<String> mOtherEpisodeUrls;
private long resumePosition;

private final BroadcastReceiver receiver = new BroadcastReceiver() {
@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);
}
}
Expand All @@ -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);
Expand All @@ -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;
Expand All @@ -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);
Expand All @@ -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);
}
Expand All @@ -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);
}

Expand Down Expand Up @@ -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);
Expand All @@ -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() {
Expand All @@ -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() {
Expand All @@ -379,34 +379,34 @@ 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<String> 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);
}
}

private void playNextEpisode() {
IEpisodeModel next = getNextEpisode(mCurrentEpisode);
EpisodeBaseModel next = getNextEpisode(mCurrentEpisode);
Log.d(TAG, "Play next episode: " + next);
if (next != null) {
playEpisode(next);
}
}

private void playPrevEpisode() {
IEpisodeModel next = getPrevEpisode(mCurrentEpisode);
EpisodeBaseModel next = getPrevEpisode(mCurrentEpisode);
Log.d(TAG, "Play previous episode: " + next);
if (next != null) {
playEpisode(next);
Expand Down
13 changes: 7 additions & 6 deletions base/src/main/java/io/github/xwz/base/adapters/CardSelector.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
Loading

0 comments on commit 97e8092

Please sign in to comment.