package com.hanju.video.app.ui.recommend;
|
|
import android.content.Context;
|
import android.content.DialogInterface;
|
import android.content.Intent;
|
import android.content.SharedPreferences;
|
import android.content.pm.PackageInfo;
|
import android.content.pm.PackageManager.NameNotFoundException;
|
import android.graphics.Canvas;
|
import android.graphics.Rect;
|
import android.os.Bundle;
|
import android.provider.Settings;
|
|
import androidx.annotation.NonNull;
|
import androidx.fragment.app.Fragment;
|
import androidx.recyclerview.widget.GridLayoutManager;
|
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
|
import androidx.recyclerview.widget.LinearLayoutManager;
|
import androidx.recyclerview.widget.RecyclerView;
|
|
import android.util.Log;
|
import android.view.LayoutInflater;
|
import android.view.View;
|
import android.view.View.OnClickListener;
|
import android.view.ViewGroup;
|
import android.widget.GridView;
|
import android.widget.ImageView;
|
import android.widget.LinearLayout;
|
import android.widget.TextView;
|
|
import com.bumptech.glide.Glide;
|
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.entity.HomeSpecial;
|
import com.hanju.video.app.entity.video.HomeNav;
|
import com.hanju.video.app.util.UserUtil;
|
import com.lcjian.library.RetainViewFragment;
|
import com.lcjian.library.content.ConnectivityChangeHelper;
|
import com.lcjian.library.content.ConnectivityChangeHelper.OnConnectivityChangeListener;
|
import com.lcjian.library.util.SystemCommon;
|
import com.lcjian.library.util.cache.DiskLruCache;
|
import com.lcjian.library.util.common.StorageUtils;
|
import com.lcjian.library.util.common.StringUtils;
|
import com.lcjian.library.widget.RatioLayout;
|
import com.lzj.gallery.library.views.BannerViewPager;
|
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
|
import com.umeng.analytics.MobclickAgent;
|
import com.hanju.video.app.BasicTextHttpResponseHandler;
|
import com.hanju.video.app.HttpApiUtil;
|
import com.hanju.video.app.R;
|
import com.hanju.video.app.entity.HomeAd;
|
import com.hanju.video.app.entity.HomeType;
|
import com.hanju.video.app.entity.HomeTypeItem;
|
import com.hanju.video.app.entity.HomeVideo;
|
import com.hanju.video.app.entity.VideoInfo;
|
import com.hanju.video.app.entity.VideoType;
|
import com.hanju.video.app.entity.ad.ExpressAdContainer;
|
import com.hanju.video.app.ui.category.bean.HotStar;
|
import com.hanju.video.app.ui.media.VideoDetailActivity;
|
import com.hanju.video.app.ui.mine.BrowserActivity;
|
import com.hanju.video.app.util.HanJuConstant;
|
import com.hanju.video.app.util.CustomShareDialog;
|
import com.hanju.video.app.util.DimenUtils;
|
import com.hanju.video.app.util.ad.AdUtil;
|
import com.hanju.video.app.util.ad.ExpressAdManager;
|
import com.hanju.video.app.util.ui.DividerItemDecoration;
|
import com.hanju.video.app.util.ui.GlideUtil;
|
import com.ysh.wpc.appupdate.GoReview;
|
import com.ysh.wpc.appupdate.service.DownLoadFileService;
|
|
import org.apache.http.Header;
|
import org.json.JSONArray;
|
import org.json.JSONException;
|
import org.json.JSONObject;
|
|
import java.io.File;
|
import java.io.IOException;
|
import java.text.SimpleDateFormat;
|
import java.util.ArrayList;
|
import java.util.Date;
|
import java.util.HashSet;
|
import java.util.Iterator;
|
import java.util.List;
|
import java.util.Set;
|
|
import static com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions.withCrossFade;
|
|
public class RecommendFragment extends RetainViewFragment implements
|
OnClickListener {
|
|
private SwipeRefreshLayout rl_recommend;
|
|
private RecyclerView rv_recommend;
|
|
private RecommendNewAdapter mRecommendNewAdapter;
|
|
private HomeSpecialAdapter mHomeSpecialAdapter;
|
|
private List mHomeTypes;
|
|
private List<HomeAd> mHomeAds;
|
|
private List<HomeSpecial> mHomeSpecials;
|
|
private DiskLruCache cache;
|
|
private ConnectivityChangeHelper mChangeHelper;
|
|
private TextView tv_no_network;
|
|
private TextView tv_notice;
|
|
private HomeNav type;
|
|
private int position;
|
|
private ImageView iv_right_ad;
|
|
private boolean isBottom = false;
|
|
boolean isReview;
|
|
private Context context;
|
|
private int page = 1;
|
|
private GridView gv_special;
|
|
private boolean isLoad;
|
|
private View loading;
|
|
private int totalCount = 0;
|
|
private Set<String> emptyHomeTypes = new HashSet<>();
|
|
public static Fragment newInstance(HomeNav type, String position) {
|
if (Integer.parseInt(position) < 2) {
|
RecommendFragment fragment = new RecommendFragment();
|
Bundle bundle = new Bundle();
|
bundle.putSerializable("videotype", type);
|
bundle.putInt("position", Integer.parseInt(position));
|
fragment.setArguments(bundle);
|
return fragment;
|
} else {
|
CategoryRecommendFragment fragment = new CategoryRecommendFragment();
|
Bundle bundle = new Bundle();
|
bundle.putSerializable("videotype", type);
|
bundle.putString("id", type.getId());
|
fragment.setArguments(bundle);
|
return fragment;
|
}
|
}
|
|
@Override
|
public void onCreate(Bundle savedInstanceState) {
|
super.onCreate(savedInstanceState);
|
context = getActivity().getApplicationContext();
|
try {
|
cache = DiskLruCache.open(
|
new File(StorageUtils.getCacheDirectory(context)
|
.toString(), "http"), getVersionNum(context),
|
1, 1024 * 1024);
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
|
|
}
|
|
@Override
|
public void onSaveInstanceState(Bundle outState) {
|
outState.putString("flash back", "show");
|
super.onSaveInstanceState(outState);
|
}
|
|
public static int getVersionNum(Context context) {
|
try {
|
PackageInfo pi = context.getPackageManager().getPackageInfo(
|
context.getPackageName(), 0);
|
return pi.versionCode;
|
} catch (NameNotFoundException e) {
|
e.printStackTrace();
|
return 1;
|
}
|
}
|
|
@Override
|
public int getContentResource() {
|
return R.layout.recommend_fragment;
|
}
|
|
private String getDay(long time) {
|
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
|
String date = format.format(new Date(time));
|
return date;
|
}
|
|
|
//填充数据
|
private void setAdContent() {
|
//没有广告,添加广告
|
if (position != 0)//只有推荐才有广告
|
return;
|
if (mRecommendNewAdapter.getAdCount() == 0) {
|
if (adList != null && adList.size() > 0) {
|
mRecommendNewAdapter.addAdToPosition(adList.get(0), 3);
|
}
|
mRecommendNewAdapter.notifyDataSetChanged();
|
}
|
}
|
|
private BannerViewPager vp_recommend;
|
private LinearLayout ll_ad_containner;
|
private RatioLayout rl_recommend_top;
|
|
|
RecyclerView.ItemDecoration itemDecorationCategory = new RecyclerView.ItemDecoration() {
|
@Override
|
public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
|
super.onDraw(c, parent, state);
|
}
|
|
@Override
|
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
|
super.onDrawOver(c, parent, state);
|
}
|
|
@Override
|
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
|
super.getItemOffsets(outRect, view, parent, state);
|
int index = parent.getChildAdapterPosition(view);
|
int total = parent.getAdapter().getItemCount();
|
int minWidth = com.lcjian.library.util.common.DimenUtils.dipToPixels(10, view.getContext());
|
outRect.bottom = 2 * minWidth;
|
}
|
};
|
|
private void initBannerAndTypes() {
|
List<View> viewList = new ArrayList<>();
|
View view = LayoutInflater.from(getContext()).inflate(R.layout.recommend_top, null);
|
vp_recommend = view.findViewById(R.id.vp_recommend);
|
ll_ad_containner = view.findViewById(R.id.ll_ad_containner);
|
rl_recommend_top = view.findViewById(R.id.rl_recommend_top);
|
gv_special = view.findViewById(R.id.gv_special);
|
if (position == 1)
|
rl_recommend_top.setRatio(0.56f);
|
viewList.add(view);
|
// rv_category = new RecyclerView(getContext());
|
// ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
|
// rv_category.setLayoutParams(params);
|
// GridLayoutManager layoutManager = new GridLayoutManager(getContext(), 4);
|
// layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
|
// @Override
|
// public int getSpanSize(int position) {
|
// return 1;
|
// }
|
// });
|
// rv_category.setLayoutManager(layoutManager);
|
// rv_category.addItemDecoration(itemDecorationCategory);
|
// rv_category.setAdapter(new RecommendCategoryAdapter(videoTypeList));
|
// viewList.add(rv_category);
|
mRecommendNewAdapter.setHeaderView(viewList);
|
}
|
|
@Override
|
public void onCreateView(View contentView, Bundle savedInstanceState) {
|
tv_no_network = contentView.findViewById(R.id.tv_no_network);
|
tv_no_network.setOnClickListener(this);
|
// 公告
|
tv_notice = contentView.findViewById(R.id.include_notice);
|
mChangeHelper = new ConnectivityChangeHelper(context,
|
new OnConnectivityChangeListener() {
|
@Override
|
public void onNetworkUnAvailable() {
|
if (!StringUtils.isBlank(content)) {
|
tv_notice.setVisibility(View.VISIBLE);
|
} else {
|
tv_notice.setVisibility(View.GONE);
|
}
|
tv_no_network.setVisibility(View.VISIBLE);
|
}
|
|
@Override
|
public void onWiFiAvailable() {
|
if (!StringUtils.isBlank(content)) {
|
tv_notice.setVisibility(View.VISIBLE);
|
} else {
|
tv_notice.setVisibility(View.GONE);
|
}
|
tv_no_network.setVisibility(View.GONE);
|
}
|
|
@Override
|
public void onMobileAvailable() {
|
if (!StringUtils.isBlank(content)) {
|
tv_notice.setVisibility(View.VISIBLE);
|
} else {
|
tv_notice.setVisibility(View.GONE);
|
}
|
tv_no_network.setVisibility(View.GONE);
|
}
|
});
|
|
int width = (int) SystemCommon.getScreenWidth(context);
|
|
rl_recommend = contentView.findViewById(R.id.rl_recommend);
|
rv_recommend = contentView
|
.findViewById(R.id.rv_recommend);
|
rv_recommend.addOnScrollListener(new RecyclerView.OnScrollListener() {
|
@Override
|
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
|
super.onScrollStateChanged(recyclerView, newState);
|
LinearLayoutManager manager = (LinearLayoutManager) recyclerView.getLayoutManager();
|
int first = manager.findFirstVisibleItemPosition();
|
int last = manager.findLastVisibleItemPosition();
|
int total = manager.getItemCount();
|
if (newState == RecyclerView.SCROLL_STATE_IDLE) {
|
if ((!isLoad) && last == total - 1 && mHomeTypes.size() > 0 && totalCount > mHomeTypes.size() + (emptyHomeTypes==null?0: emptyHomeTypes.size())) {
|
Log.i("RecommendFragment", "加载更多");
|
page++;
|
isLoad = true;
|
// tv_loading.setText("正在加载更多数据");
|
// iv_loading.setVisibility(View.VISIBLE);
|
loading.setVisibility(View.VISIBLE);
|
getHomeType(page);
|
}
|
}
|
}
|
|
@Override
|
public void onScrolled(RecyclerView recyclerView, int dx, final int dy) {
|
super.onScrolled(recyclerView, dx, dy);
|
}
|
});
|
|
rv_recommend.setLayoutManager(new LinearLayoutManager(context));
|
DividerItemDecoration decoration = new DividerItemDecoration();
|
decoration.setSize(DimenUtils.dip2px(rv_recommend.getContext(), 0));
|
rv_recommend.addItemDecoration(decoration);
|
|
rl_recommend.setClickable(false);
|
|
iv_right_ad = contentView.findViewById(R.id.iv_right_ad);
|
|
type = (HomeNav) getArguments().getSerializable("videotype");
|
position = (Integer) getArguments().get("position");
|
|
|
isReview = GoReview.getGoReview(context, HanJuConstant.GO_REVIEW_KEY);
|
|
|
mHomeTypes = new ArrayList<HomeType>();
|
mHomeAds = new ArrayList<>();
|
mHomeSpecials = new ArrayList<>();
|
|
rl_recommend.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
|
@Override
|
public void onRefresh() {
|
if (position == 0) {
|
getHotStars();
|
}
|
page = 1;
|
//加载广告
|
loadAd();
|
getHomeAd();
|
getRecommendSearchSpecial();
|
Log.i("mResult", "onCreate---setOnRefreshListener---loadAD1");
|
}
|
});
|
|
if (mHomeTypes == null)
|
mHomeTypes = new ArrayList();
|
mRecommendNewAdapter = new RecommendNewAdapter(getActivity(), mHomeTypes);
|
loading = LayoutInflater.from(getContext()).inflate(R.layout.item_loading, null);
|
loading.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
|
loading.setVisibility(View.GONE);
|
mRecommendNewAdapter.setFooterView(loading);
|
rv_recommend.setAdapter(mRecommendNewAdapter);
|
|
setAdContent();
|
initBannerAndTypes();
|
//加载缓存数据
|
// loadCacheData();
|
rv_recommend.postDelayed(new Runnable() {
|
|
@Override
|
public void run() {
|
if (getActivity() != null) {
|
rl_recommend.setRefreshing(true);
|
page = 1;
|
getHomeAd();
|
getRecommendSearchSpecial();
|
Log.i("mResult", "onCreate---postDelayed---loadAD1");
|
if (position == 0)
|
getHotStars();
|
}
|
}
|
}, 500);
|
|
//加载广告
|
loadAd();
|
|
getNotice();
|
if (position == 0) {
|
getAdRecommendRight();
|
}
|
}
|
|
private List<ExpressAdContainer> adList;
|
|
private void loadAd() {
|
try {
|
ExpressAdManager recommendAdManager = new ExpressAdManager(AdUtil.AD_TYPE.csj, context);
|
recommendAdManager.loadRecommendLargeAd(new ExpressAdManager.IAdLoadListener() {
|
@Override
|
public void onSuccess(final List<ExpressAdContainer> adList) {
|
RecommendFragment.this.adList = new ArrayList<>();
|
for (ExpressAdContainer ad : adList) {
|
ExpressAdManager.renderAd(getActivity(), ad, new ExpressAdManager.IAdRenderListener() {
|
@Override
|
public void onRenderSuccess(List<ExpressAdContainer> adList1) {
|
RecommendFragment.this.adList.add(adList1.get(0));
|
if (RecommendFragment.this.adList.size() == adList.size()) {
|
if (mRecommendNewAdapter.getAdCount() == 0 && mHomeTypes.size() > 0) {//没有填充广告
|
setAdContent();
|
}
|
}
|
}
|
|
@Override
|
public void onRenderFail(List<ExpressAdContainer> adList) {
|
|
}
|
}, new ExpressAdManager.IAdEventListener() {
|
@Override
|
public void closeAd(ExpressAdContainer ad) {
|
mRecommendNewAdapter.removeAd(ad);
|
}
|
});
|
}
|
}
|
});
|
} catch (Exception e) {
|
|
}
|
}
|
|
/**
|
* 设置广告数据
|
*/
|
private void setHomeAdsData() {
|
|
List<String> urlList = new ArrayList<>();
|
for (HomeAd ad : mHomeAds)
|
urlList.add(ad.getPicture());
|
|
|
if (!vp_recommend.hasAdapter()) {
|
|
vp_recommend.initBanner(urlList, true, 0.4375f)//开启3D画廊效果
|
.addPageMargin(-14, 16)//参数1page之间的间距,参数2中间item距离边界的间距
|
.addPointMargin(5)//指示器点间距
|
.addStartTimer(3)//自动轮播3秒间隔
|
.addPointBottom(0)//底部间距
|
.addRoundCorners(12)//圆角
|
.finishConfig()//这句必须加
|
.addBannerListener(new BannerViewPager.OnClickBannerListener() {
|
@Override
|
public void onBannerClick(int position) {
|
if (position >= mHomeAds.size())
|
return;
|
Intent intent = null;
|
if (mHomeAds.get(position).getLinkType() == 1) {
|
intent = new Intent(getContext(),
|
VideoDetailActivity.class);
|
intent.putExtra("video_info", mHomeAds.get(position)
|
.getVideo());
|
} else if (mHomeAds.get(position).getLinkType() == 5) {
|
try {
|
JSONObject object = new JSONObject(mHomeAds
|
.get(position).getParams()
|
.replace("\\", ""));
|
String thumb = object.optString("thumb");
|
String title = object.optString("title");
|
String desc = object.optString("desc");
|
String path = object.optString("path");
|
String username = object.optString("username");
|
String url = object.optString("url");
|
|
CustomShareDialog.Builder builder = new CustomShareDialog.Builder(getActivity());
|
builder.setMessage(desc);
|
builder.setUrl(url);
|
builder.setContentImage(thumb);
|
builder.setTitle(title);
|
builder.setPath(path);
|
builder.setUserName(username);
|
builder.setNegativeButton("取消分享", new DialogInterface.OnClickListener() {
|
@Override
|
public void onClick(DialogInterface dialog, int which) {
|
dialog.dismiss();
|
}
|
});
|
builder.create().show();
|
} catch (Exception e) {
|
|
}
|
|
} else {
|
try {
|
intent = new Intent(getContext(), Class
|
.forName(mHomeAds.get(position).getClazz()));
|
JSONObject object = new JSONObject(mHomeAds
|
.get(position).getParams()
|
.replace("\\", ""));
|
if (mHomeAds.get(position).getClazz().contains("FXBrowserActivity") && object.optString("url").endsWith(".apk")) {
|
// startDownLoadFile(object.optString("url"));
|
return;
|
}
|
if (mHomeAds.get(position).getParams() != null) {
|
@SuppressWarnings("unchecked")
|
Iterator<String> its = object.keys();
|
while (its.hasNext()) {
|
String key = its.next();
|
String value = object.optString(key);
|
intent.putExtra(key, value);
|
intent.putExtra("isPush", true);
|
}
|
}
|
} catch (ClassNotFoundException e) {
|
e.printStackTrace();
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
|
}
|
if (intent != null)
|
getContext().startActivity(intent);
|
}
|
|
});
|
} else {
|
vp_recommend.notifyDataSetChanged();
|
}
|
}
|
|
/**
|
* 设置首页专题数据
|
*/
|
private void setHomeSpecialData() {
|
if (mHomeSpecials == null || mHomeSpecials.size() == 0) {
|
gv_special.setVisibility(View.GONE);
|
return;
|
} else {
|
gv_special.setVisibility(View.VISIBLE);
|
}
|
if (gv_special.getAdapter() == null) {
|
mHomeSpecialAdapter = new HomeSpecialAdapter(mHomeSpecials, getActivity());
|
gv_special.setAdapter(mHomeSpecialAdapter);
|
} else
|
mHomeSpecialAdapter.notifyDataSetChanged();
|
}
|
|
private void loadCacheData() {
|
if (cache != null && position == 0) {
|
{
|
DiskLruCache.Snapshot snapshot = null;
|
try {
|
snapshot = cache.get(getKey("getHomeAd") + type.getId());
|
if (snapshot != null) {
|
List<HomeAd> adList = new ArrayList<HomeAd>();
|
String str = snapshot.getString(0);
|
try {
|
JSONArray array = new JSONArray(str);
|
HomeAd ad;
|
for (int i = 0; i < array.length(); i++) {
|
ad = new HomeAd();
|
ad.setId(array.optJSONObject(i).optString("Id"));
|
ad.setPicture(array.optJSONObject(i).optString(
|
"Picture"));
|
ad.setTitle(array.optJSONObject(i).optString(
|
"Title"));
|
ad.setClazz(array.optJSONObject(i).optString(
|
"Clazz"));
|
ad.setParams(array.optJSONObject(i).optString(
|
"Params"));
|
ad.setLinkType(Integer
|
.parseInt(array.optJSONObject(i)
|
.optString("LinkType")));
|
VideoInfo videoInfo = new VideoInfo();
|
videoInfo
|
.setId(array.optJSONObject(i)
|
.optJSONObject("Video")
|
.optString("Id"));
|
videoInfo.setShare(array.optJSONObject(i)
|
.optJSONObject("Video")
|
.optString("Share"));
|
videoInfo.setThirdType(array.optJSONObject(i)
|
.optJSONObject("Video")
|
.optString("ThirdType"));
|
ad.setVideo(videoInfo);
|
adList.add(ad);
|
|
}
|
if (adList != null && adList.size() > 0) {
|
mHomeAds.clear();
|
mHomeAds.addAll(adList);
|
setHomeAdsData();
|
}
|
} catch (JSONException e) {
|
e.printStackTrace();
|
}
|
|
}
|
} catch (Exception e) {
|
e.printStackTrace();
|
} finally {
|
if (snapshot != null) {
|
snapshot.close();
|
}
|
}
|
}
|
{
|
DiskLruCache.Snapshot snapshot = null;
|
try {
|
snapshot = cache.get(getKey("getHomeType") + type.getId());
|
if (snapshot != null) {
|
Gson gson = new GsonBuilder().setFieldNamingPolicy(
|
FieldNamingPolicy.UPPER_CAMEL_CASE).create();
|
List<HomeType> homeTypes = gson.fromJson(
|
snapshot.getString(0),
|
new TypeToken<List<HomeType>>() {
|
}.getType());
|
mHomeTypes.clear();
|
if (homeTypes != null) {
|
mHomeTypes.addAll(homeTypes);
|
// mRecommendNewAdapter.notifyDataSetChanged();
|
}
|
}
|
} catch (Exception e) {
|
e.printStackTrace();
|
} finally {
|
if (snapshot != null) {
|
snapshot.close();
|
}
|
}
|
}
|
{
|
DiskLruCache.Snapshot snapshot = null;
|
try {
|
snapshot = cache.get(getKey("getRecommendSearchSpecial") + type.getId());
|
if (snapshot != null) {
|
Gson gson = new GsonBuilder().setFieldNamingPolicy(
|
FieldNamingPolicy.UPPER_CAMEL_CASE).create();
|
List<HomeSpecial> homeSpecials = gson.fromJson(
|
snapshot.getString(0),
|
new TypeToken<List<HomeSpecial>>() {
|
}.getType());
|
mHomeSpecials.clear();
|
if (homeSpecials != null) {
|
mHomeSpecials.addAll(homeSpecials);
|
// mRecommendNewAdapter.notifyDataSetChanged();
|
}
|
}
|
} catch (Exception e) {
|
e.printStackTrace();
|
} finally {
|
if (snapshot != null) {
|
snapshot.close();
|
}
|
}
|
}
|
}
|
|
}
|
|
|
private void getAdRecommendRight() {
|
HttpApiUtil.getAdRecommendRight(context, new BasicTextHttpResponseHandler() {
|
@Override
|
public void onSuccessPerfect(int statusCode, Header[] headers, JSONObject jsonObject) throws Exception {
|
if (jsonObject.getBoolean("IsPost")) {
|
if (position == 0) {
|
String img = jsonObject.optJSONObject("Data").optString("img");
|
String mstatus = jsonObject.optJSONObject("Data").optString("mstatus");
|
final String linkType = jsonObject.optJSONObject("Data").optString("linkType");
|
final String webLink = jsonObject.optJSONObject("Data").optString("webLink");
|
final String downloadLink = jsonObject.optJSONObject("Data").optString("downloadLink");
|
Glide.with(context).load(img).into(iv_right_ad);
|
if (mstatus.equalsIgnoreCase("0")) {
|
iv_right_ad.setVisibility(View.GONE);
|
} else {
|
iv_right_ad.setVisibility(View.VISIBLE);
|
}
|
|
iv_right_ad.setOnClickListener(new OnClickListener() {
|
@Override
|
public void onClick(View v) {
|
if (linkType.equalsIgnoreCase("0")) {
|
Intent intent = new Intent(context, BrowserActivity.class);
|
intent.putExtra("url", webLink);
|
startActivity(intent);
|
} else {
|
try {
|
Intent intent = new Intent(context,
|
DownLoadFileService.class);
|
context.stopService(intent);
|
} catch (Exception e) {
|
}
|
try {
|
Bundle bundle = new Bundle();
|
bundle.putString("downloadurl",
|
downloadLink);
|
Intent intent = new Intent(context,
|
DownLoadFileService.class);
|
intent.putExtras(bundle);
|
context.startService(intent);
|
} catch (Exception e) {
|
}
|
}
|
}
|
});
|
} else {
|
iv_right_ad.setVisibility(View.GONE);
|
}
|
}
|
}
|
|
@Override
|
public void onFailure(int statusCode, Header[] headers, String responseString, Throwable
|
throwable) {
|
super.onFailure(statusCode, headers, responseString, throwable);
|
}
|
});
|
}
|
|
private String getKey(String method) {
|
return new Md5FileNameGenerator().generate(method);
|
}
|
|
@Override
|
public void onResume() {
|
super.onResume();
|
mChangeHelper.registerReceiver();
|
MobclickAgent.onPageStart("主页面—推荐");
|
}
|
|
@Override
|
public void onPause() {
|
super.onPause();
|
mChangeHelper.unregisterReceiver();
|
MobclickAgent.onPageEnd("主页面—推荐");
|
}
|
|
private String content;
|
|
private void getNotice() {
|
SharedPreferences preferences = context.getSharedPreferences(
|
"user", Context.MODE_PRIVATE);
|
String uid = preferences.getString("uid", "");
|
HttpApiUtil.getNotice(context, uid,
|
new BasicTextHttpResponseHandler() {
|
|
@Override
|
public void onSuccessPerfect(int statusCode,
|
Header[] headers, JSONObject jsonObject)
|
throws Exception {
|
if (jsonObject.getBoolean("IsPost")) {
|
JSONObject obj = jsonObject.optJSONObject("Data");
|
content = obj.optString("Content");
|
// 0-不显示
|
// 1-显示
|
final String url = obj.optString("Url");
|
if (!StringUtils.isBlank(content)) {
|
tv_notice.setVisibility(View.VISIBLE);
|
tv_notice.setText(content);
|
if (!StringUtils.isBlank(url)) {
|
tv_notice
|
.setOnClickListener(new OnClickListener() {
|
|
@Override
|
public void onClick(View v) {
|
startActivity(new Intent(
|
context,
|
BrowserActivity.class)
|
.putExtra("url", url));
|
|
}
|
});
|
}
|
} else {
|
tv_notice.setVisibility(View.GONE);
|
}
|
|
}
|
}
|
});
|
}
|
|
private void getHomeAd() {
|
SharedPreferences preferences = context.getSharedPreferences(
|
"user", Context.MODE_PRIVATE);
|
String uid = preferences.getString("uid", "");
|
HttpApiUtil.getHomeAd(context, uid, type.getId(), type.getDataKey(),
|
new BasicTextHttpResponseHandler() {
|
@Override
|
public void onSuccessPerfect(int statusCode,
|
Header[] headers, JSONObject jsonObject)
|
throws Exception {
|
if (jsonObject.getBoolean("IsPost")) {
|
Gson gson = new GsonBuilder()
|
.setPrettyPrinting()
|
.setFieldNamingPolicy(
|
FieldNamingPolicy.UPPER_CAMEL_CASE)
|
.create();
|
List<HomeAd> homeAds = gson.fromJson(jsonObject
|
.getJSONObject("Data").getJSONArray("data")
|
.toString(), new TypeToken<List<HomeAd>>() {
|
}.getType());
|
|
if (homeAds != null) {
|
mHomeAds.clear();
|
mHomeAds.addAll(homeAds);
|
}
|
|
setHomeAdsData();
|
|
|
if (position == 0) {
|
DiskLruCache.Editor editor = cache
|
.edit(getKey("getHomeAd") + type.getId());
|
editor.set(0, jsonObject.getJSONObject("Data")
|
.getJSONArray("data").toString());
|
editor.commit();
|
}
|
}
|
}
|
|
@Override
|
public void onFinish() {
|
getHomeType(page);
|
}
|
});
|
}
|
|
|
/**
|
* 获取专题
|
*/
|
private void getRecommendSearchSpecial() {
|
HttpApiUtil.getRecommendSearchSpecial(context, new BasicTextHttpResponseHandler() {
|
@Override
|
public void onSuccessPerfect(int statusCode,
|
Header[] headers, JSONObject jsonObject)
|
throws Exception {
|
if (jsonObject.getBoolean("IsPost")) {
|
Gson gson = new GsonBuilder()
|
.create();
|
JSONArray data = jsonObject.optJSONArray("Data");
|
|
List<HomeSpecial> homeSpecials = gson.fromJson(data.toString(), new TypeToken<List<HomeSpecial>>() {
|
}.getType());
|
|
if (homeSpecials != null) {
|
mHomeSpecials.clear();
|
mHomeSpecials.addAll(homeSpecials);
|
}
|
setHomeSpecialData();
|
// //缓存
|
if (position == 0) {
|
DiskLruCache.Editor editor = cache
|
.edit(getKey("getRecommendSearchSpecial") + type.getId());
|
editor.set(0, data
|
.toString());
|
editor.commit();
|
}
|
}
|
}
|
|
@Override
|
public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
|
super.onFailure(statusCode, headers, responseString, throwable);
|
}
|
|
@Override
|
public void onFinish() {
|
}
|
});
|
}
|
|
/**
|
* 获取影片整体信息
|
*/
|
|
private void getHomeType(int page) {
|
SharedPreferences preferences = context.getSharedPreferences(
|
"user", Context.MODE_PRIVATE);
|
String uid = preferences.getString("uid", "");
|
if (page == 1) {
|
if (emptyHomeTypes == null)
|
emptyHomeTypes = new HashSet<>();
|
emptyHomeTypes.clear();
|
}
|
HttpApiUtil.getHomeType(context, uid, type.getId(), type.getDataKey(), page, 5,
|
new BasicTextHttpResponseHandler() {
|
@Override
|
public void onSuccessPerfect(int statusCode,
|
Header[] headers, JSONObject jsonObject)
|
throws Exception {
|
if (jsonObject.getBoolean("IsPost")) {
|
Gson gson = new GsonBuilder().setVersion(1.0).setFieldNamingPolicy(
|
FieldNamingPolicy.UPPER_CAMEL_CASE)
|
.create();
|
JSONObject data = jsonObject.getJSONObject("Data");
|
totalCount = data.optInt("count");
|
List<HomeType> homeTypes = new ArrayList<>();
|
JSONArray array = data.getJSONArray("data");
|
for (int i = 0; i < array.length(); i++) {
|
HomeType type = new HomeType();
|
JSONObject obj = array.optJSONObject(i);
|
type.setColumns(obj.optInt("Columns"));
|
// type.setColumns(3);
|
type.setId(obj.optString("Id"));
|
type.setName(obj.optString("Name"));
|
type.setHasMore(obj.optString("HasMore"));
|
type.setActivity(obj.optString("Activity"));
|
type.setParams(obj.optString("Params"));
|
type.setIcon(obj.optString("Icon"));
|
|
JSONArray homeArray = obj
|
.optJSONArray("HomeVideoList");
|
List<HomeVideo> homeList = new ArrayList<HomeVideo>();
|
if (homeArray != null && homeArray.length() > 0) {
|
for (int j = 0; j < homeArray.length(); j++) {
|
homeList.add(gson.fromJson(homeArray
|
.optJSONObject(j).toString(),
|
HomeVideo.class));
|
}
|
}
|
type.setHomeVideoList(homeList);
|
JSONArray itemArray = obj
|
.optJSONArray("ItemTypeList");
|
List<HomeTypeItem> itemList = new ArrayList<HomeTypeItem>();
|
if (itemArray != null && itemArray.length() > 0) {
|
for (int k = 0; k < itemArray.length(); k++) {
|
if (itemArray.optJSONObject(k) != null) {
|
itemList.add(gson.fromJson(
|
itemArray.optJSONObject(k)
|
.toString(),
|
HomeTypeItem.class));
|
}
|
}
|
}
|
type.setItemTypeList(itemList);
|
if (!type.getName().equalsIgnoreCase("美女"))
|
if (type.getHomeVideoList() != null && type.getHomeVideoList().size() > 0)
|
homeTypes.add(type);
|
else
|
emptyHomeTypes.add(type.getId());
|
}
|
|
if (page == 1) {
|
//清除老数据
|
mHomeTypes.clear();
|
mRecommendNewAdapter.removeAllAd();
|
mHomeTypes.addAll(homeTypes);
|
} else {
|
mHomeTypes.addAll(homeTypes);
|
}
|
//填充数据
|
setAdContent();
|
if (position == 0) {
|
DiskLruCache.Editor editor = cache
|
.edit(getKey("getHomeType") + type.getId());
|
editor.set(0, jsonObject.getJSONObject("Data")
|
.getJSONArray("data").toString());
|
editor.commit();
|
}
|
mRecommendNewAdapter.notifyDataSetChanged();
|
}
|
}
|
|
@Override
|
public void onFinish() {
|
rl_recommend.setRefreshing(false);
|
if (page > 1 && loading != null) {
|
loading.setVisibility(View.GONE);
|
isLoad = false;
|
}
|
|
try {
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
}
|
|
@Override
|
public void onFailure(int statusCode, Header[] headers,
|
byte[] responseBytes, Throwable throwable) {
|
super.onFailure(statusCode, headers, responseBytes,
|
throwable);
|
}
|
});
|
}
|
|
@Override
|
public void onClick(View v) {
|
switch (v.getId()) {
|
case R.id.tv_no_network: {
|
Intent intent = new Intent(Settings.ACTION_SETTINGS);
|
startActivity(intent);
|
}
|
break;
|
default:
|
break;
|
}
|
}
|
|
@Override
|
public void onDestroy() {
|
super.onDestroy();
|
}
|
|
private List<HomeVideo> adList1 = new ArrayList<>();
|
|
|
List<HotStar> hotStars = new ArrayList<>();
|
|
private void getHotStars() {
|
SharedPreferences preferences = context.getSharedPreferences(
|
"user", Context.MODE_PRIVATE);
|
String uid = preferences.getString("uid", "");
|
HttpApiUtil.getRecommendStars(context, uid,
|
new BasicTextHttpResponseHandler() {
|
@Override
|
public void onSuccessPerfect(int statusCode,
|
Header[] headers, JSONObject jsonObject)
|
throws Exception {
|
if (jsonObject.getBoolean("IsPost")) {
|
hotStars.clear();
|
Gson gson = new GsonBuilder().setFieldNamingPolicy(
|
FieldNamingPolicy.UPPER_CAMEL_CASE)
|
.create();
|
List<HotStar> hotStarList = gson.fromJson(
|
jsonObject.getJSONObject("Data")
|
.getJSONArray("data").toString(),
|
new TypeToken<List<HotStar>>() {
|
}.getType());
|
hotStars.addAll(hotStarList);
|
|
//插入明星列表
|
if (hotStars != null && hotStars.size() > 0) {
|
HomeType homeType = new HomeType();
|
homeType.setName("明星合辑");
|
homeType.setColumns(4);
|
homeType.setHotStars(hotStars);
|
homeType.setActivity("");
|
homeType.setBeizhu("");
|
homeType.setParams("");
|
homeType.setHasMore("true");
|
homeType.setId("9998");
|
mHomeTypes.add(homeType);
|
mRecommendNewAdapter.notifyDataSetChanged();
|
}
|
}
|
}
|
|
@Override
|
public void onFailure(int statusCode, Header[] headers,
|
byte[] responseBytes, Throwable throwable) {
|
super.onFailure(statusCode, headers, responseBytes, throwable);
|
}
|
|
@Override
|
public void onFinish() {
|
}
|
});
|
|
}
|
|
class RecommendCategoryAdapter extends RecyclerView.Adapter {
|
|
private List<VideoType> videoTypeList;
|
|
public RecommendCategoryAdapter(List<VideoType> videoTypeList) {
|
this.videoTypeList = videoTypeList;
|
}
|
|
@NonNull
|
@Override
|
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
|
return new MyViewHolder(LayoutInflater.from(getContext()).inflate(R.layout.item_recommend_category, null, true));
|
}
|
|
@Override
|
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
|
VideoType videoType = videoTypeList.get(position);
|
MyViewHolder myViewHolder = (MyViewHolder) holder;
|
myViewHolder.tv_name.setText(videoType.getName());
|
GlideUtil.loadCircle(videoType.getIcon(), myViewHolder.iv_icon, getContext());
|
}
|
|
@Override
|
public int getItemCount() {
|
return videoTypeList.size();
|
}
|
|
class MyViewHolder extends RecyclerView.ViewHolder {
|
private ImageView iv_icon;
|
private TextView tv_name;
|
|
public MyViewHolder(@NonNull View itemView) {
|
super(itemView);
|
iv_icon = itemView.findViewById(R.id.iv_icon);
|
tv_name = itemView.findViewById(R.id.tv_name);
|
}
|
}
|
}
|
|
}
|