package com.hanju.video.app.ui.media;
|
|
import android.app.ProgressDialog;
|
import android.content.Context;
|
import android.content.Intent;
|
import android.content.SharedPreferences;
|
import android.graphics.Bitmap;
|
import android.graphics.Color;
|
import android.graphics.Rect;
|
import android.os.Bundle;
|
import android.os.Handler;
|
import android.util.Log;
|
import android.view.View;
|
import android.widget.FrameLayout;
|
import android.widget.ImageView;
|
import android.widget.LinearLayout;
|
import android.widget.TextView;
|
import android.widget.Toast;
|
|
import com.bumptech.glide.Glide;
|
import com.bumptech.glide.request.RequestOptions;
|
import com.google.gson.FieldNamingPolicy;
|
import com.google.gson.Gson;
|
import com.google.gson.GsonBuilder;
|
import com.google.gson.reflect.TypeToken;
|
import com.hanju.video.app.util.JsonUtil;
|
import com.hanju.video.app.util.http.BasicTextHttpResponseHandler;
|
import com.hanju.video.app.util.http.HttpApiUtil;
|
import com.hanju.video.app.R;
|
import com.hanju.video.app.entity.video.Play;
|
import com.hanju.video.app.entity.video.PlayUrl;
|
import com.hanju.video.app.entity.video.Playlocation;
|
import com.hanju.video.app.entity.video.VideoDetailInfo;
|
import com.hanju.video.app.entity.video.VideoInfo;
|
import com.hanju.video.app.entity.video.VideoResource;
|
import com.hanju.video.app.entity.ad.ExpressAdContainer;
|
import com.hanju.video.app.ui.login.LoginActivity;
|
import com.hanju.video.app.ui.video.EpisodeNewAdapter;
|
import com.hanju.video.app.ui.video.IVideoClickListener;
|
import com.hanju.video.app.ui.video.VideoColumn2Adapter;
|
import com.hanju.video.app.util.video.VideoUtil;
|
import com.hanju.video.app.util.ad.AdUtil;
|
import com.hanju.video.app.util.ad.ExpressAdManager;
|
import com.hanju.lib.library.RetainViewFragment;
|
import com.hanju.lib.library.util.SingleToast;
|
import com.hanju.lib.library.util.common.DimenUtils;
|
import com.hanju.lib.library.util.common.StringUtils;
|
import com.nostra13.universalimageloader.core.DisplayImageOptions;
|
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
|
|
import org.apache.http.Header;
|
import org.json.JSONObject;
|
|
import java.util.List;
|
|
import androidx.recyclerview.widget.LinearLayoutManager;
|
import androidx.recyclerview.widget.RecyclerView;
|
import de.greenrobot.event.EventBus;
|
|
/**
|
* 节目选集列表,可设置电视剧6列;电影,综艺1列
|
*/
|
public class EpisodeFragment extends RetainViewFragment implements View.OnClickListener {
|
private int mStart;
|
private int mEnd;
|
private int mPlayingPosition;
|
private VideoInfo mVideoInfo = null;
|
private RecyclerView rv_episode, rv_video_related_video, rv_video_guess_like;
|
|
private ImageView iv_moive_img;
|
|
private TextView tv_star_name, tv_update_time, tv_cancle_follow, tv_title, tv_desc, tv_play_num, tv_score;
|
|
private LinearLayout ll_add_attention;
|
|
private FrameLayout fl_native_ad1;
|
|
private EpisodeNewAdapter episodeAdapter;
|
|
private ProgressDialog pd;
|
|
private LinearLayout ll_score;
|
private per.wsj.library.AndRatingBar arb_score;
|
|
final String TAG = "EpisodeFragment";
|
|
private LinearLayout ll_indicator;
|
|
private String resourceId;
|
|
public static DisplayImageOptions option = new DisplayImageOptions.Builder()
|
.showImageForEmptyUri(R.drawable.from_other)
|
.showImageOnFail(R.drawable.from_other)
|
.showImageOnLoading(R.drawable.from_other)
|
.resetViewBeforeLoading(false) // default
|
.cacheInMemory(true) // default
|
.cacheOnDisk(true) // default
|
.considerExifParams(false) // default
|
.imageScaleType(ImageScaleType.EXACTLY) // default
|
.bitmapConfig(Bitmap.Config.ARGB_8888) // default
|
.handler(new Handler()) // default
|
.build();
|
|
public static EpisodeFragment newInstance(VideoInfo videoInfo,
|
int playingPosition, String resourceId, int start, int end) {
|
EpisodeFragment episodeFragment = new EpisodeFragment();
|
Bundle args = new Bundle();
|
args.putSerializable("video_info", videoInfo);
|
args.putInt("playing_position", playingPosition);
|
args.putInt("start", start);
|
args.putInt("end", end);
|
args.putString("resourceId", resourceId);
|
episodeFragment.setArguments(args);
|
return episodeFragment;
|
}
|
|
@Override
|
public int getContentResource() {
|
return R.layout.fragment_episode;
|
}
|
|
|
/**
|
* 设置视频信息
|
*/
|
private void setVideoInfo() {
|
if (mVideoInfo == null)
|
return;
|
tv_title.setText(mVideoInfo.getName());
|
tv_desc.setText(mVideoInfo.getIntroduction() == null ? "" : mVideoInfo.getIntroduction());
|
|
if (!StringUtils.isBlank(mVideoInfo.getScore())) {
|
tv_score.setText("评分:" + mVideoInfo.getScore());
|
tv_score.setVisibility(View.VISIBLE);
|
ll_score.setVisibility(View.VISIBLE);
|
arb_score.setRating(Float.parseFloat(mVideoInfo.getScore()) / 2);
|
} else {
|
tv_score.setVisibility(View.GONE);
|
ll_score.setVisibility(View.GONE);
|
}
|
tv_play_num.setText(VideoUtil.getWatchCountShortName(mVideoInfo.getWatchCount()) + " 次播放");
|
|
setIndicatorSelectedEvent();
|
ll_indicator.post(new Runnable() {
|
@Override
|
public void run() {
|
ll_indicator.getChildAt(0).performClick();
|
}
|
});
|
guessLike();
|
getRelativeVideos();
|
}
|
|
public class SpacesItemDecoration extends RecyclerView.ItemDecoration {
|
private int space;
|
|
public SpacesItemDecoration(int space) {
|
this.space = space;
|
}
|
|
@Override
|
public void getItemOffsets(Rect outRect, View view,
|
RecyclerView parent, RecyclerView.State state) {
|
int position = parent.getChildAdapterPosition(view);
|
outRect.left = space;
|
if (mVideoInfo != null && position == mVideoInfo.getVideoDetailList().size() - 1)
|
outRect.right = space;
|
}
|
}
|
|
private void loadEpisode() {
|
|
int type = 0;
|
|
if (mVideoInfo.getShowType() == 1) {
|
// 综艺或者电影
|
type = EpisodeNewAdapter.TYPE_ZONGYI;
|
|
} else if (mVideoInfo.getShowType() == 2) {
|
// 电视剧动漫
|
type = EpisodeNewAdapter.TYPE_DIANSHIJU;
|
}
|
episodeAdapter = new EpisodeNewAdapter(getContext(), mVideoInfo, type, mPlayingPosition, new EpisodeNewAdapter.ISelectVideoEpisodeListener() {
|
@Override
|
public void onClick(int position, VideoDetailInfo detailInfo) {
|
VideoDetailInfo videoDetailInfo = detailInfo;
|
mPlayingPosition = position;
|
episodeAdapter.setPlayingPosition(mPlayingPosition);
|
refresh();
|
Playlocation playlocation = new Playlocation();
|
playlocation.setPosition(position);
|
EventBus.getDefault().post(playlocation);
|
getUrl(videoDetailInfo);
|
}
|
});
|
LinearLayoutManager ms = new LinearLayoutManager(getContext());
|
ms.setOrientation(LinearLayoutManager.HORIZONTAL);
|
|
rv_episode.setHasFixedSize(true);
|
rv_episode.setNestedScrollingEnabled(false);
|
|
rv_episode.setLayoutManager(ms);
|
rv_episode.addItemDecoration(new SpacesItemDecoration(DimenUtils.dip2px(getContext(), 10)));
|
}
|
|
private void setFollowData(boolean attention) {
|
if (attention) {
|
tv_cancle_follow.setText("已追剧");
|
} else {
|
tv_cancle_follow.setText("追剧");
|
}
|
}
|
|
@Override
|
public void onCreateView(View contentView, Bundle savedInstanceState) {
|
Log.i(TAG, "onCreateView");
|
EventBus.getDefault().register(this);
|
mVideoInfo = (VideoInfo) getArguments()
|
.getSerializable("video_info");
|
mPlayingPosition = getArguments().getInt("playing_position");
|
mStart = getArguments().getInt("start");
|
mEnd = getArguments().getInt("end");
|
resourceId = getArguments().getString("resourceId");
|
|
pd = new ProgressDialog(getContext());
|
pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);// 设置进度条的形式为圆形转动的进度条
|
pd.setCancelable(true);// 设置是否可以通过点击Back键取消
|
pd.setCanceledOnTouchOutside(false);// 设置在点击Dialog外是否取消Dialog进度条
|
pd.setMessage("网络加载中·······");
|
|
|
ll_add_attention = contentView.findViewById(R.id.ll_add_attention);
|
tv_title = contentView.findViewById(R.id.tv_title);
|
tv_desc = contentView.findViewById(R.id.tv_desc);
|
tv_desc.setOnClickListener(new View.OnClickListener() {
|
@Override
|
public void onClick(View v) {
|
tv_desc.setMaxLines(100);
|
}
|
});
|
tv_score = contentView.findViewById(R.id.tv_score);
|
ll_score = contentView.findViewById(R.id.ll_score);
|
arb_score = contentView.findViewById(R.id.arb_score);
|
|
tv_score = contentView.findViewById(R.id.tv_score);
|
tv_play_num = contentView.findViewById(R.id.tv_play_num);
|
|
ll_indicator = contentView.findViewById(R.id.ll_indicator);
|
|
|
if (mVideoInfo.getAttention() == null) {
|
ll_add_attention.setVisibility(View.GONE);
|
} else {
|
ll_add_attention.setVisibility(View.VISIBLE);
|
iv_moive_img = contentView.findViewById(R.id.iv_moive_img);
|
tv_star_name = contentView.findViewById(R.id.tv_star_name);
|
tv_update_time = contentView.findViewById(R.id.tv_update_time);
|
tv_cancle_follow = contentView.findViewById(R.id.tv_cancle_follow);
|
try {
|
Glide.with(getActivity().getApplicationContext()).load(mVideoInfo.getAttention().getMoviePicture())
|
.apply(new RequestOptions().centerCrop().placeholder(R.drawable.ic_default).error(R.drawable.ic_default))
|
.into(iv_moive_img);
|
} catch (IllegalArgumentException e) {
|
e.printStackTrace();
|
}
|
tv_star_name.setText(mVideoInfo.getAttention().getMovieName() + "");
|
tv_update_time.setText(mVideoInfo.getAttention().getUpdateInfo() + "");
|
setFollowData(mVideoInfo.getAttention().isAttention());
|
|
tv_cancle_follow.setOnClickListener(new View.OnClickListener() {
|
@Override
|
public void onClick(View v) {
|
SharedPreferences sp = getContext().getSharedPreferences("user", Context.MODE_PRIVATE);
|
String uid = sp.getString("uid", "");
|
String loginid = sp.getString("LoginUid", "");
|
if (StringUtils.isEmpty(loginid)) {
|
Toast.makeText(ll_add_attention.getContext(), "登录成功后才能进行追剧!", Toast.LENGTH_LONG).show();
|
getContext().startActivity(new Intent(ll_add_attention.getContext(), LoginActivity.class));
|
return;
|
}
|
if (mVideoInfo.getAttention().isAttention()) {//取消关注
|
cancleAttention(uid, loginid);
|
} else {//关注
|
addAttention(uid, loginid);
|
}
|
}
|
});
|
}
|
|
setVideoInfo();
|
|
|
fl_native_ad1 = contentView
|
.findViewById(R.id.fl_native_ad_1);
|
rv_episode = contentView.findViewById(R.id.rv_episode);
|
rv_video_related_video = contentView.findViewById(R.id.rv_video_related_video);
|
rv_video_guess_like = contentView.findViewById(R.id.rv_video_guess_like);
|
|
|
rv_video_guess_like.setHasFixedSize(true);
|
rv_video_guess_like.setNestedScrollingEnabled(false);
|
rv_video_related_video.setHasFixedSize(true);
|
rv_video_related_video.setNestedScrollingEnabled(false);
|
|
|
loadEpisode();
|
refresh();
|
loadAD1();
|
if (mVideoInfo.getVideoDetailList() != null && mVideoInfo.getVideoDetailList().size() > 1) {
|
rv_episode.setVisibility(View.VISIBLE);
|
} else
|
rv_episode.setVisibility(View.GONE);
|
}
|
|
|
public void onEventMainThread(Playlocation playlocation) {
|
mPlayingPosition = playlocation.getPosition();
|
loadEpisode();
|
refresh();
|
}
|
|
private void addAttention(String uid, String loginId) {
|
HttpApiUtil.addAttention(ll_add_attention.getContext(), uid, loginId, mVideoInfo.getId(), new BasicTextHttpResponseHandler() {
|
@Override
|
public void onSuccessPerfect(int statusCode, Header[] headers, JSONObject jsonObject) throws Exception {
|
if (jsonObject.optBoolean("IsPost")) {
|
SingleToast.showToast(ll_add_attention.getContext(), "追剧成功!");
|
mVideoInfo.getAttention().setAttention(true);
|
setFollowData(mVideoInfo.getAttention().isAttention());
|
|
}
|
}
|
});
|
}
|
|
private void cancleAttention(String uid, String loginId) {
|
HttpApiUtil.cancelAttention(ll_add_attention.getContext(), uid, loginId, mVideoInfo.getId(), new BasicTextHttpResponseHandler() {
|
@Override
|
public void onSuccessPerfect(int statusCode, Header[] headers, JSONObject jsonObject) throws Exception {
|
if (jsonObject.optBoolean("IsPost")) {
|
SingleToast.showToast(ll_add_attention.getContext(), "取消追剧成功!");
|
mVideoInfo.getAttention().setAttention(false);
|
setFollowData(mVideoInfo.getAttention().isAttention());
|
}
|
}
|
});
|
}
|
|
@Override
|
public void onResume() {
|
super.onResume();
|
}
|
|
@Override
|
public void onDestroyView() {
|
super.onDestroyView();
|
EventBus.getDefault().unregister(this);
|
}
|
|
@Override
|
public void onPause() {
|
super.onPause();
|
}
|
|
private void getUrl(VideoDetailInfo info) {
|
SharedPreferences preferences = getContext().getSharedPreferences(
|
"user", Context.MODE_PRIVATE);
|
String uid = preferences.getString("uid", "");
|
HttpApiUtil.getPlayUrl(getContext(), uid, info.getType(), mVideoInfo.getId(),
|
info.getId(), resourceId, info.geteId(),
|
new BasicTextHttpResponseHandler() {
|
|
@Override
|
public void onSuccessPerfect(int statusCode,
|
Header[] headers, JSONObject jsonObject)
|
throws Exception {
|
if (jsonObject.getBoolean("IsPost")) {
|
|
VideoResource resource = JsonUtil.videoGson.fromJson(
|
jsonObject.getJSONObject("Data")
|
.getJSONObject("Resource")
|
.toString(),
|
new TypeToken<VideoResource>() {
|
}.getType());
|
PlayUrl info = new PlayUrl();
|
info.setResource(resource);
|
info.setPlayType(Integer.parseInt(jsonObject
|
.getJSONObject("Data")
|
.optString("PlayType")));
|
info.setUrl(jsonObject.getJSONObject("Data")
|
.optString("Url"));
|
info.setParams(jsonObject.getJSONObject("Data")
|
.optString("Params"));
|
Play play = new Play();
|
play.setPlayUrl(info);
|
EventBus.getDefault().post(play);
|
}else{
|
Toast.makeText(getContext(), jsonObject.optString("Error"), Toast.LENGTH_SHORT).show();
|
}
|
}
|
|
@Override
|
public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
|
super.onFailure(statusCode, headers, responseString, throwable);
|
|
}
|
|
@Override
|
public void onFinish() {
|
super.onFinish();
|
if (pd != null && pd.isShowing())
|
pd.dismiss();
|
}
|
});
|
}
|
|
public boolean isPlaying() {
|
return mStart <= mPlayingPosition && mPlayingPosition < mEnd;
|
}
|
|
/**
|
* 刷新adapter
|
*/
|
public void refresh() {
|
if (rv_episode.getAdapter() != null)
|
rv_episode.getAdapter().notifyDataSetChanged();
|
else
|
rv_episode.postDelayed(new Runnable() {
|
@Override
|
public void run() {
|
Log.i(TAG, "refresh");
|
rv_episode.setAdapter(episodeAdapter);
|
if (mPlayingPosition > 4)
|
rv_episode.scrollToPosition(mPlayingPosition);
|
}
|
}, 500);
|
|
|
}
|
|
// 加载播放页第一条广告,先设置加载上下文环境和条件
|
private void loadAD1() {
|
ExpressAdManager expressAdManager = null;
|
try {
|
expressAdManager = new ExpressAdManager(AdUtil.AD_TYPE.gdt2, getContext());
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
|
if (expressAdManager == null)
|
return;
|
|
expressAdManager.loadVideoDetailPlayerBottomAd(new ExpressAdManager.IAdLoadListener() {
|
@Override
|
public void onSuccess(List<ExpressAdContainer> adList) {
|
if (adList != null && adList.size() > 0) {
|
fl_native_ad1.setVisibility(View.VISIBLE);
|
final ExpressAdContainer ad = adList.get(0);
|
if (getActivity() == null)
|
return;
|
|
ExpressAdManager.renderAd(getActivity(), adList.get(0), new ExpressAdManager.IAdRenderListener() {
|
@Override
|
public void onRenderSuccess(List<ExpressAdContainer> adList) {
|
ExpressAdManager.fillAd(adList.get(0), fl_native_ad1);
|
}
|
|
@Override
|
public void onRenderFail(List<ExpressAdContainer> adList) {
|
|
}
|
}, new ExpressAdManager.IAdEventListener() {
|
@Override
|
public void closeAd(ExpressAdContainer ad) {
|
fl_native_ad1.removeAllViews();
|
}
|
});
|
|
} else {
|
fl_native_ad1.setVisibility(View.GONE);
|
}
|
}
|
});
|
}
|
|
|
@Override
|
public void onClick(View v) {
|
SharedPreferences preferences = getContext()
|
.getSharedPreferences("user", Context.MODE_PRIVATE);
|
switch (v.getId()) {
|
|
|
}
|
}
|
|
|
public void onEventMainThread(VideoInfo videoInfo) {
|
mVideoInfo = videoInfo;
|
if (mVideoInfo.getVideoDetailList() != null && mVideoInfo.getVideoDetailList().size() > 1) {
|
rv_episode.setVisibility(View.VISIBLE);
|
} else
|
rv_episode.setVisibility(View.GONE);
|
rv_episode.setAdapter(episodeAdapter);
|
}
|
|
|
private void setIndicatorSelected(int index) {
|
|
for (int i = 0; i < ll_indicator.getChildCount(); i++) {
|
LinearLayout child = (LinearLayout) ll_indicator.getChildAt(i);
|
TextView tv = (TextView) child.getChildAt(0);
|
View view = child.getChildAt(1);
|
if (i == index) {
|
tv.setTextColor(getContext().getColor(R.color.theme));
|
view.setVisibility(View.VISIBLE);
|
} else {
|
tv.setTextColor(Color.parseColor("#FF999999"));
|
view.setVisibility(View.GONE);
|
}
|
}
|
}
|
|
private void setIndicatorSelectedEvent() {
|
|
for (int i = 0; i < ll_indicator.getChildCount(); i++) {
|
final int p = i;
|
View view = ll_indicator.getChildAt(i);
|
view.setOnClickListener(new View.OnClickListener() {
|
@Override
|
public void onClick(View v) {
|
rv_episode.setVisibility(View.GONE);
|
rv_video_related_video.setVisibility(View.GONE);
|
rv_video_guess_like.setVisibility(View.GONE);
|
|
|
switch (p) {
|
case 0:
|
setIndicatorSelected(0);
|
rv_episode.setVisibility(View.VISIBLE);
|
break;
|
case 1:
|
setIndicatorSelected(1);
|
rv_video_related_video.setVisibility(View.VISIBLE);
|
break;
|
case 2:
|
setIndicatorSelected(2);
|
rv_video_guess_like.setVisibility(View.VISIBLE);
|
break;
|
}
|
}
|
});
|
|
|
}
|
}
|
|
|
private void getRelativeVideos() {
|
SharedPreferences preferences = getContext().getSharedPreferences(
|
"user", Context.MODE_PRIVATE);
|
String uid = preferences.getString("uid", "");
|
HttpApiUtil.getRelativeVideos(getContext(), uid,
|
mVideoInfo.getId(), new BasicTextHttpResponseHandler() {
|
|
@Override
|
public void onSuccessPerfect(int statusCode,
|
Header[] headers, JSONObject jsonObject)
|
throws Exception {
|
if (jsonObject.getBoolean("IsPost")) {
|
|
final List<VideoInfo> videoInfos = JsonUtil.videoGson.fromJson(
|
jsonObject.getJSONObject("Data")
|
.getJSONArray("data").toString(),
|
new TypeToken<List<VideoInfo>>() {
|
}.getType());
|
|
if (videoInfos == null || videoInfos.size() == 0) {
|
} else {
|
final VideoColumn2Adapter adapter = new VideoColumn2Adapter(getActivity(), getContext(), videoInfos, false, 2, new IVideoClickListener() {
|
@Override
|
public void onClick(VideoInfo video) {
|
VideoDetailActivity.ChangeVideoEvent changeVideoEvent = new VideoDetailActivity.ChangeVideoEvent();
|
changeVideoEvent.setVideoInfo(video);
|
changeVideoEvent.setTag("VideoPlayerFragment");
|
EventBus.getDefault().post(changeVideoEvent);
|
}
|
});
|
adapter.initRecyclerViewDisplayWidthColumn2(rv_video_related_video);
|
|
if (getActivity() == null)
|
return;
|
getActivity().runOnUiThread(new Runnable() {
|
@Override
|
public void run() {
|
if (rv_video_related_video != null) {
|
rv_video_related_video.setAdapter(adapter);
|
}
|
}
|
});
|
}
|
}
|
}
|
});
|
}
|
|
|
private void guessLike() {
|
SharedPreferences preferences = getContext().getSharedPreferences(
|
"user", Context.MODE_PRIVATE);
|
String uid = preferences.getString("uid", "");
|
HttpApiUtil.guessLike(getContext(), uid, mVideoInfo.getId(),
|
new BasicTextHttpResponseHandler() {
|
|
@Override
|
public void onSuccessPerfect(int statusCode,
|
Header[] headers, JSONObject jsonObject)
|
throws Exception {
|
if (jsonObject.getBoolean("IsPost")) {
|
|
List<VideoInfo> videoInfos = JsonUtil.videoGson.fromJson(
|
jsonObject.getJSONObject("Data")
|
.getJSONArray("data").toString(),
|
new TypeToken<List<VideoInfo>>() {
|
}.getType());
|
final VideoColumn2Adapter adapter = new VideoColumn2Adapter(getActivity(), getContext(), videoInfos, false, 2, new IVideoClickListener() {
|
@Override
|
public void onClick(VideoInfo video) {
|
VideoDetailActivity.ChangeVideoEvent changeVideoEvent = new VideoDetailActivity.ChangeVideoEvent();
|
changeVideoEvent.setVideoInfo(video);
|
changeVideoEvent.setTag("VideoPlayerFragment");
|
EventBus.getDefault().post(changeVideoEvent);
|
}
|
});
|
adapter.initRecyclerViewDisplayWidthColumn2(rv_video_guess_like);
|
if (getActivity() != null)
|
getActivity().runOnUiThread(new Runnable() {
|
@Override
|
public void run() {
|
if (rv_video_guess_like != null)
|
rv_video_guess_like
|
.setAdapter(adapter);
|
}
|
});
|
}
|
}
|
});
|
}
|
|
|
}
|