494 lines
18 KiB
Java
494 lines
18 KiB
Java
package com.gh.gamecenter;
|
|
|
|
import android.app.Activity;
|
|
import android.content.Context;
|
|
import android.content.Intent;
|
|
import android.os.Bundle;
|
|
import android.os.Message;
|
|
import android.support.annotation.NonNull;
|
|
import android.support.v7.widget.LinearLayoutManager;
|
|
import android.support.v7.widget.RecyclerView;
|
|
import android.text.TextUtils;
|
|
import android.view.View;
|
|
import android.widget.LinearLayout;
|
|
import android.widget.TextView;
|
|
|
|
import com.ethanhua.skeleton.Skeleton;
|
|
import com.ethanhua.skeleton.ViewSkeletonScreen;
|
|
import com.gh.base.BaseActivity;
|
|
import com.gh.base.OnRequestCallBackListener;
|
|
import com.gh.common.util.ApkActiveUtils;
|
|
import com.gh.common.util.DetailDownloadUtils;
|
|
import com.gh.common.util.DeviceTokenUtils;
|
|
import com.gh.common.util.EntranceUtils;
|
|
import com.gh.common.util.LibaoUtils;
|
|
import com.gh.common.view.DownloadProgressBar;
|
|
import com.gh.common.view.VerticalItemDecoration;
|
|
import com.gh.download.DownloadManager;
|
|
import com.gh.gamecenter.adapter.LibaoDetailAdapter;
|
|
import com.gh.gamecenter.adapter.viewholder.DetailViewHolder;
|
|
import com.gh.gamecenter.entity.ApkEntity;
|
|
import com.gh.gamecenter.entity.GameEntity;
|
|
import com.gh.gamecenter.entity.LibaoEntity;
|
|
import com.gh.gamecenter.entity.LibaoStatusEntity;
|
|
import com.gh.gamecenter.entity.MeEntity;
|
|
import com.gh.gamecenter.entity.UserDataLibaoEntity;
|
|
import com.gh.gamecenter.eventbus.EBDownloadStatus;
|
|
import com.gh.gamecenter.eventbus.EBPackage;
|
|
import com.gh.gamecenter.eventbus.EBUISwitch;
|
|
import com.gh.gamecenter.retrofit.Response;
|
|
import com.gh.gamecenter.retrofit.RetrofitManager;
|
|
import com.halo.assistant.HaloApp;
|
|
import com.lightgame.download.DataWatcher;
|
|
import com.lightgame.download.DownloadEntity;
|
|
import com.lightgame.utils.Utils;
|
|
|
|
import org.greenrobot.eventbus.Subscribe;
|
|
import org.greenrobot.eventbus.ThreadMode;
|
|
|
|
import java.text.SimpleDateFormat;
|
|
import java.util.Calendar;
|
|
import java.util.Date;
|
|
import java.util.List;
|
|
import java.util.Locale;
|
|
|
|
import butterknife.BindView;
|
|
import io.reactivex.android.schedulers.AndroidSchedulers;
|
|
import io.reactivex.schedulers.Schedulers;
|
|
import retrofit2.HttpException;
|
|
|
|
import static com.gh.gamecenter.R.id.reuse_tv_none_data;
|
|
|
|
/**
|
|
* Created by khy on 2016/12/13.
|
|
*/
|
|
public class LibaoDetailActivity extends BaseActivity implements LibaoDetailAdapter.OnCodeScrollListener,
|
|
OnRequestCallBackListener {
|
|
|
|
@BindView(R.id.libaodetail_rv_show)
|
|
RecyclerView mLibaoDetailRv;
|
|
@BindView(R.id.reuse_ll_loading)
|
|
LinearLayout mLlLoading;
|
|
@BindView(R.id.reuse_no_connection)
|
|
LinearLayout mNoConnection;
|
|
@BindView(reuse_tv_none_data)
|
|
TextView mNoneDataTv;
|
|
@BindView(R.id.reuse_none_data)
|
|
LinearLayout mNoneData;
|
|
@BindView(R.id.detail_progressbar)
|
|
DownloadProgressBar mDownloadPb;
|
|
@BindView(R.id.list_skeleton)
|
|
View mListSkeleton;
|
|
|
|
private ViewSkeletonScreen mSkeleton;
|
|
|
|
private LibaoDetailAdapter mAdapter;
|
|
|
|
private LibaoEntity mLibaoEntity;
|
|
private DownloadEntity mDownloadEntity;
|
|
private GameEntity mGameEntity;
|
|
|
|
private boolean mIsScroll;
|
|
|
|
private String mListStatus; // 记录列表领取状态(防止状态在详情改变导致列表状态改变)
|
|
private String mName;
|
|
private String mTitle;
|
|
protected String mDownloadAddWord;
|
|
protected String mDownloadOffText;
|
|
|
|
@Override
|
|
protected void handleMessage(Message msg) {
|
|
super.handleMessage(msg);
|
|
if (msg.what == 0) {
|
|
long time = Utils.getTime(LibaoDetailActivity.this) - 5; // -5 防止获取服务器时间延迟发生的计算错误()
|
|
Calendar calendar = Calendar.getInstance();
|
|
Date date = new Date(time * 1000);
|
|
calendar.setTime(date);
|
|
calendar.set(Calendar.HOUR_OF_DAY, 24);
|
|
calendar.set(Calendar.SECOND, 0);
|
|
calendar.set(Calendar.MINUTE, 0);
|
|
calendar.set(Calendar.MILLISECOND, 0);
|
|
|
|
long targetTime = calendar.getTimeInMillis();
|
|
long delayedTime = targetTime - time * 1000;
|
|
if (BuildConfig.DEBUG) {
|
|
toast("领取时间剩余: " + (delayedTime / 1000) + " 秒");
|
|
}
|
|
mBaseHandler.sendEmptyMessageDelayed(1, delayedTime);
|
|
} else if (msg.what == 1) {
|
|
String beforeStatus = mLibaoEntity.getBeforeStatus();
|
|
if ("ling".equals(beforeStatus)) {
|
|
mLibaoEntity.setStatus("repeatLinged"); // 可以重复领取
|
|
} else {
|
|
mLibaoEntity.setStatus("repeatTaoed"); // 可以重复领取
|
|
}
|
|
mAdapter.notifyItemChanged(0);
|
|
}
|
|
}
|
|
|
|
private DataWatcher dataWatcher = new DataWatcher() {
|
|
@Override
|
|
public void onDataChanged(DownloadEntity downloadEntity) {
|
|
if (mGameEntity != null && mGameEntity.getApk().size() == 1) {
|
|
String url = mGameEntity.getApk().get(0).getUrl();
|
|
if (url.equals(downloadEntity.getUrl())) {
|
|
if (!"pause".equals(DownloadManager.getInstance(LibaoDetailActivity.this).
|
|
getStatus(downloadEntity.getUrl()))) {
|
|
mDownloadEntity = downloadEntity;
|
|
DetailDownloadUtils.detailInvalidate(getDetailViewHolder());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
};
|
|
|
|
|
|
private DetailViewHolder getDetailViewHolder() {
|
|
// 每次获取需要重新创建, 防止数据刷新
|
|
return new DetailViewHolder(mContentView, mGameEntity, mDownloadEntity, mDownloadOffText, mDownloadAddWord,
|
|
false, mEntrance, mName, mTitle, null); // 下载按钮ViewHolder
|
|
}
|
|
|
|
@NonNull
|
|
public static Intent getIntent(Context context, LibaoEntity libaoEntity, String entrance) {
|
|
Intent intent = new Intent(context, LibaoDetailActivity.class);
|
|
HaloApp.put(LibaoEntity.TAG, libaoEntity);
|
|
intent.putExtra(EntranceUtils.KEY_ENTRANCE, entrance);
|
|
return intent;
|
|
}
|
|
|
|
@NonNull
|
|
public static Intent getIntentById(Context context, String id, String entrance) {
|
|
Intent intent = new Intent(context, LibaoDetailActivity.class);
|
|
intent.putExtra(EntranceUtils.KEY_ENTRANCE, entrance);
|
|
intent.putExtra(EntranceUtils.KEY_ID, id);
|
|
return intent;
|
|
}
|
|
|
|
@Override
|
|
protected void onSaveInstanceState(Bundle outState) {
|
|
super.onSaveInstanceState(outState);
|
|
HaloApp.put(LibaoEntity.TAG, mAdapter.getLibaoEntity());
|
|
}
|
|
|
|
@Override
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
super.onCreate(savedInstanceState);
|
|
mName = getString(R.string.libao_detail);
|
|
setNavigationTitle(mName);
|
|
mLibaoEntity = (LibaoEntity) HaloApp.get(LibaoEntity.TAG, false);
|
|
|
|
mIsScroll = true;
|
|
|
|
mSkeleton = Skeleton.bind(mListSkeleton).shimmer(false).load(R.layout.activity_libaodetail_skeleton).show();
|
|
mAdapter = new LibaoDetailAdapter(this, this, this, mLibaoEntity, mDownloadPb, mEntrance);
|
|
mLibaoDetailRv.setLayoutManager(new LinearLayoutManager(this) {
|
|
@Override
|
|
public boolean canScrollVertically() {
|
|
return mIsScroll;
|
|
}
|
|
});
|
|
mLibaoDetailRv.addItemDecoration(new VerticalItemDecoration(this, 8, true));
|
|
mLibaoDetailRv.setAdapter(mAdapter);
|
|
|
|
mNoConnection.setOnClickListener(v -> {
|
|
mSkeleton.show();
|
|
mNoConnection.setVisibility(View.GONE);
|
|
mLlLoading.setVisibility(View.VISIBLE);
|
|
mBaseHandler.postDelayed(() -> getGameDigest(), 1000);
|
|
});
|
|
|
|
if (mLibaoEntity == null) {
|
|
String id = getIntent().getStringExtra(EntranceUtils.KEY_ID);
|
|
if (!TextUtils.isEmpty(id)) {
|
|
getLibaoDigest(id);
|
|
}
|
|
} else {
|
|
getGameDigest();
|
|
if (!mLibaoEntity.isActive()) {
|
|
checkLibaoStatus();
|
|
}
|
|
}
|
|
}
|
|
|
|
@Override
|
|
protected void onPause() {
|
|
super.onPause();
|
|
DownloadManager.getInstance(this).removeObserver(dataWatcher);
|
|
}
|
|
|
|
@Override
|
|
protected void onResume() {
|
|
super.onResume();
|
|
if (mGameEntity != null
|
|
&& mGameEntity.getApk().size() == 1) {
|
|
DetailDownloadUtils.detailInitDownload(getDetailViewHolder(), true);
|
|
}
|
|
DownloadManager.getInstance(this).addObserver(dataWatcher);
|
|
}
|
|
|
|
@Override
|
|
protected int getLayoutId() {
|
|
return R.layout.activity_libaodetail;
|
|
|
|
}
|
|
|
|
// 检查礼包是否可以重复领取
|
|
private void checkLibaoStatus() {
|
|
if (mLibaoEntity != null && mLibaoEntity.getRepeat() > 0 && mLibaoEntity.getMe() != null
|
|
&& mLibaoEntity.getMe().getUserDataLibaoList() != null) {
|
|
|
|
mTitle = mLibaoEntity.getName();
|
|
String status = mLibaoEntity.getStatus();
|
|
String beforeStatus = mLibaoEntity.getBeforeStatus();
|
|
|
|
MeEntity userData = mLibaoEntity.getMe();
|
|
List<UserDataLibaoEntity> userDataLibaoList = userData.getUserDataLibaoList();
|
|
|
|
int repeat = mLibaoEntity.getRepeat();
|
|
int curStatusCount = 0; // 当前状态的领取/淘号数量
|
|
|
|
for (UserDataLibaoEntity userDataLibaoEntity : userDataLibaoList) {
|
|
if (beforeStatus != null && beforeStatus.equals(userDataLibaoEntity.getType())) {
|
|
curStatusCount++;
|
|
}
|
|
}
|
|
|
|
// 由领取到淘号的状态转换
|
|
if (repeat <= curStatusCount || curStatusCount == 0) { // 当前领取/淘号总数是否超过总重复领取次数 / 当前状态的礼包没有领取/淘号过
|
|
if (curStatusCount == 0 && ("ling".equals(beforeStatus) || "tao".equals(beforeStatus))) {
|
|
if (isCanLing()) { // 恢复原始状态
|
|
mLibaoEntity.setStatus(beforeStatus);
|
|
mAdapter.notifyItemChanged(0);
|
|
}
|
|
}
|
|
return;
|
|
}
|
|
|
|
|
|
if ((("linged").equals(status) || ("taoed").equals(status)) &&
|
|
("ling".equals(beforeStatus) || "tao".equals(beforeStatus))) { //检查是否到了重复领取时间
|
|
mListStatus = mLibaoEntity.getStatus();
|
|
if (isCanLing()) {
|
|
mBaseHandler.sendEmptyMessage(1);
|
|
} else {
|
|
if ("ling".equals(beforeStatus)) {
|
|
mLibaoEntity.setStatus("repeatLing"); // 预备重复领取
|
|
} else {
|
|
mLibaoEntity.setStatus("repeatTao"); // 预备重复领取
|
|
}
|
|
mAdapter.notifyItemChanged(0);
|
|
|
|
DeviceTokenUtils.syncServerTime(LibaoDetailActivity.this);
|
|
mBaseHandler.sendEmptyMessageDelayed(0, 5000);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 检查是否可以领取/淘号
|
|
* 最后领取/淘号时间不是当天
|
|
*/
|
|
public boolean isCanLing() {
|
|
List<UserDataLibaoEntity> userDataLibaoList = mLibaoEntity.getMe().getUserDataLibaoList();
|
|
UserDataLibaoEntity userDataLibaoEntity = userDataLibaoList.get(userDataLibaoList.size() - 1);
|
|
SimpleDateFormat formatDay = new SimpleDateFormat("dd", Locale.CHINA);
|
|
long lingTime = userDataLibaoEntity.getTime() * 1000;
|
|
long curTime = Utils.getTime(this) * 1000;
|
|
int lingDay = Integer.parseInt(formatDay.format(lingTime));
|
|
int curDay = Integer.parseInt(formatDay.format(curTime));
|
|
if (curDay != lingDay || curTime - lingTime > 24 * 60 * 60 * 1000) {
|
|
return true;
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
private void getLibaoDigest(final String libaoId) {
|
|
RetrofitManager.getInstance(this).getApi().getLibaoDigest(libaoId)
|
|
.subscribeOn(Schedulers.io())
|
|
.observeOn(AndroidSchedulers.mainThread())
|
|
.subscribe(new Response<LibaoEntity>() {
|
|
@Override
|
|
public void onResponse(LibaoEntity response) {
|
|
mLibaoEntity = response;
|
|
mLibaoEntity.setActive(true); // addLibaoDetail设置userData
|
|
mAdapter.setLibaoEntity(mLibaoEntity);
|
|
mAdapter.notifyDataSetChanged();
|
|
getGameDigest();
|
|
getLibaoStatus(libaoId);
|
|
checkLibaoStatus();
|
|
}
|
|
});
|
|
}
|
|
|
|
private void getLibaoStatus(String libaoId) {
|
|
LibaoUtils.getLibaoStatus(this, libaoId, new LibaoUtils.PostLibaoListener() {
|
|
@Override
|
|
public void postSucced(Object response) {
|
|
List<LibaoStatusEntity> statusList = (List<LibaoStatusEntity>) response;
|
|
|
|
LibaoUtils.initLiBaoEntity(statusList.get(0), mLibaoEntity);
|
|
|
|
mAdapter.notifyDataSetChanged();
|
|
}
|
|
|
|
@Override
|
|
public void postFailed(Throwable error) {
|
|
|
|
}
|
|
});
|
|
}
|
|
|
|
// 获取游戏摘要
|
|
private void getGameDigest() {
|
|
if (mLibaoEntity.getGame() == null) return;
|
|
String gameId = mLibaoEntity.getGame().getId();
|
|
RetrofitManager.getInstance(this).getApi().getGameNewsDigest(gameId)
|
|
.map(ApkActiveUtils.filterMapper)
|
|
.subscribeOn(Schedulers.io())
|
|
.observeOn(AndroidSchedulers.mainThread())
|
|
.subscribe(new Response<GameEntity>() {
|
|
@Override
|
|
public void onResponse(GameEntity response) {
|
|
ApkActiveUtils.filterHideApk(response);
|
|
mGameEntity = response;
|
|
mAdapter.setGameEntity(mGameEntity);
|
|
|
|
mDownloadAddWord = mGameEntity.getDownloadAddWord();
|
|
mDownloadOffText = mGameEntity.getDownloadOffText();
|
|
DetailDownloadUtils.detailInitDownload(getDetailViewHolder(), true);
|
|
|
|
mAdapter.addLibaoDetail(LibaoDetailActivity.this, true);
|
|
}
|
|
|
|
@Override
|
|
public void onFailure(HttpException e) {
|
|
if (e != null && e.code() == 404) {
|
|
mAdapter.addLibaoDetail(LibaoDetailActivity.this, false);
|
|
} else {
|
|
loadError();
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
@Override
|
|
public void isScroll(boolean isScroll) {
|
|
mIsScroll = isScroll;
|
|
}
|
|
|
|
@Override
|
|
public boolean handleBackPressed() {
|
|
// 重置领取状态
|
|
if (mLibaoEntity == null) {
|
|
return false;
|
|
}
|
|
String status = mLibaoEntity.getStatus();
|
|
if (!TextUtils.isEmpty(status) && status.contains("repeat")) {
|
|
mLibaoEntity.setStatus(mListStatus);
|
|
}
|
|
return false;
|
|
}
|
|
|
|
@Override
|
|
public void loadDone() {
|
|
if (mLlLoading != null) {
|
|
mLlLoading.setVisibility(View.GONE);
|
|
mLibaoDetailRv.setVisibility(View.VISIBLE);
|
|
}
|
|
|
|
// 从存号箱进入 获取userData后 检查领取状态(是否可以重复领取/重复淘号)
|
|
if (mLibaoEntity.isActive() && mLibaoEntity.getMe() != null) {
|
|
|
|
if ("ling".equals(mLibaoEntity.getStatus()) || "tao".equals(mLibaoEntity.getStatus())) {
|
|
List<UserDataLibaoEntity> userLibaooList = mLibaoEntity.getMe().getUserDataLibaoList();
|
|
if (userLibaooList != null && userLibaooList.size() > 0) {
|
|
UserDataLibaoEntity userDataLibaoEntity = userLibaooList.get(userLibaooList.size() - 1);
|
|
if ("ling".equals(userDataLibaoEntity.getType())) {
|
|
mLibaoEntity.setStatus("linged");
|
|
} else {
|
|
mLibaoEntity.setStatus("taoed");
|
|
}
|
|
}
|
|
}
|
|
|
|
checkLibaoStatus();
|
|
}
|
|
|
|
mSkeleton.hide();
|
|
}
|
|
|
|
@Override
|
|
public void loadDone(Object obj) { // 在当前详情页领取获取淘号的数据
|
|
if (obj != null) {
|
|
Intent data = new Intent();
|
|
data.putExtra(UserDataLibaoEntity.TAG, (UserDataLibaoEntity) obj);
|
|
setResult(Activity.RESULT_OK, data);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void loadError() {
|
|
mLibaoDetailRv.setVisibility(View.GONE);
|
|
mLlLoading.setVisibility(View.GONE);
|
|
mLibaoDetailRv.setPadding(0, 0, 0, 0);
|
|
mNoConnection.setVisibility(View.VISIBLE);
|
|
|
|
mSkeleton.hide();
|
|
}
|
|
|
|
@Override
|
|
public void loadEmpty() {
|
|
mLibaoDetailRv.setVisibility(View.GONE);
|
|
mLlLoading.setVisibility(View.GONE);
|
|
mNoneData.setVisibility(View.VISIBLE);
|
|
mLibaoDetailRv.setPadding(0, 0, 0, 0);
|
|
if (mAdapter.getLibaoDetailEntity() == null) {
|
|
mNoneDataTv.setText("页面不见了~");
|
|
toast("内容可能已被删除");
|
|
} else {
|
|
mNoneDataTv.setText("天了噜~页面不见了");
|
|
}
|
|
|
|
mSkeleton.hide();
|
|
}
|
|
|
|
// 领取/淘号失败,根据返回的时间重新倒数
|
|
@Subscribe(threadMode = ThreadMode.MAIN)
|
|
public void onEventMainThread(EBUISwitch swith) {
|
|
if (LibaoUtils.REFRESH_LIBAO_TIME.equals(swith.getFrom())) {
|
|
mBaseHandler.sendEmptyMessageDelayed(1, swith.getPosition());
|
|
}
|
|
}
|
|
|
|
// 接收下载被删除消息
|
|
@Subscribe(threadMode = ThreadMode.MAIN)
|
|
public void onEvent(EBDownloadStatus status) {
|
|
if ("delete".equals(status.getStatus())
|
|
&& mGameEntity != null
|
|
&& mGameEntity.getApk().size() == 1) {
|
|
String url = mGameEntity.getApk().get(0).getUrl();
|
|
if (url.equals(status.getUrl())) {
|
|
DetailDownloadUtils.detailInitDownload(getDetailViewHolder(), false);
|
|
}
|
|
}
|
|
}
|
|
|
|
// 接受安装、卸载消息
|
|
@Subscribe(threadMode = ThreadMode.MAIN)
|
|
public void onEventMainThread(EBPackage busFour) {
|
|
if (mGameEntity != null
|
|
&& mGameEntity.getApk().size() > 0) {
|
|
for (ApkEntity apkEntity : mGameEntity.getApk()) {
|
|
String packageName = apkEntity.getPackageName();
|
|
if (packageName.equals(busFour.getPackageName())) {
|
|
ApkActiveUtils.filterHideApk(mGameEntity);
|
|
DetailDownloadUtils.detailInitDownload(getDetailViewHolder(), false);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|