Files
assistant-android/app/src/main/java/com/gh/gamecenter/GameDetailActivity.java
kehaoyuan ad2656a24b ...
2017-09-26 18:39:42 +08:00

616 lines
26 KiB
Java

package com.gh.gamecenter;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.facebook.drawee.view.SimpleDraweeView;
import com.gh.base.BaseActivity;
import com.gh.base.adapter.FragmentAdapter;
import com.gh.common.util.ApkActiveUtils;
import com.gh.common.util.AppDebugConfig;
import com.gh.common.util.CheckLoginUtils;
import com.gh.common.util.ConcernUtils;
import com.gh.common.util.DataCollectionUtils;
import com.gh.common.util.DataUtils;
import com.gh.common.util.DetailDownloadUtils;
import com.gh.common.util.DialogUtils;
import com.gh.common.util.EntranceUtils;
import com.gh.common.util.ImageUtils;
import com.gh.common.util.ShareUtils;
import com.gh.download.DownloadManager;
import com.gh.gamecenter.adapter.viewholder.DetailViewHolder;
import com.gh.gamecenter.entity.ApkEntity;
import com.gh.gamecenter.entity.GameDetailEntity;
import com.gh.gamecenter.entity.GameEntity;
import com.gh.gamecenter.entity.UserDataEntity;
import com.gh.gamecenter.eventbus.EBConcernChanged;
import com.gh.gamecenter.eventbus.EBDownloadStatus;
import com.gh.gamecenter.eventbus.EBPackage;
import com.gh.gamecenter.eventbus.EBReuse;
import com.gh.gamecenter.gamedetail.FuliFragment;
import com.gh.gamecenter.gamedetail.XinXiFragment;
import com.gh.gamecenter.retrofit.Response;
import com.gh.gamecenter.retrofit.RetrofitManager;
import com.jakewharton.rxbinding.view.RxView;
import com.lightgame.download.DataWatcher;
import com.lightgame.download.DownloadEntity;
import com.lightgame.utils.Utils;
import com.lightgame.view.NoScrollableViewPager;
import com.tencent.tauth.Tencent;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.HttpException;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;
/**
* Created by khy on 2017/3/24.
* 游戏详情适配器
*/
public class GameDetailActivity extends BaseActivity {
public static final String SKIP_XINXI = "skipXinxi";
public static final String SKIP_FULI = "skipFuli";
public static final String OPEN_APPBAR = "openappbar";
public static final String SCROLL_TO_KAIFU = "scrollToKaiFu";
@BindView(R.id.gamedetail_vp)
NoScrollableViewPager mViewPager;
@BindView(R.id.reuse_ll_loading)
LinearLayout mLoading;
@BindView(R.id.reuse_no_connection)
LinearLayout mNoConnection;
@BindView(R.id.actionbar_tv_title)
TextView mTitle;
@BindView(R.id.gamedetail_iv_thumb)
SimpleDraweeView mGameIcon;
@BindView(R.id.gamedetail_tv_name)
TextView mGameName;
@BindView(R.id.gamedetail_tv_info)
TextView mGameInfo;
@BindView(R.id.gamedetail_tv_concern)
TextView mGameConcern;
@BindView(R.id.gamedetail_tabbar)
LinearLayout mTabBar;
@BindView(R.id.gamedetail_tabbar_fuli_tv)
TextView mTanBarFuLiTv;
@BindView(R.id.gamedetail_tabbar_xinxi_tv)
TextView mTanBarXinXiTv;
@BindView(R.id.gamedetail_appbar)
AppBarLayout mAppBarLayout;
@BindView(R.id.gamedetail_kaifu_hint)
View mKaifuHint;
ImageView mShareIv;
private String mGameId;
private GameEntity mGameEntity;
private DownloadEntity mDownloadEntity;
private GameDetailEntity mGameDetailEntity;
private String downloadAddWord;
private String downloadOffText;
private String name; // 页面位置
private String title; // 页面标题
private String shareCode;
private boolean mIsTouchScreen = false;
private boolean mIsShowKaifuHint;
private boolean mIsScrollToKaiFu;
private int mCurVpPosition;
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(GameDetailActivity.this).
getStatus(downloadEntity.getUrl()))) {
mDownloadEntity = downloadEntity;
DetailDownloadUtils.detailInvalidate(getDetailViewHolder());
}
}
}
}
};
private DetailViewHolder getDetailViewHolder() {
// 每次获取需要重新创建, 防止数据刷新
return new DetailViewHolder(mContentView, mGameEntity, mDownloadEntity, downloadOffText, downloadAddWord,
false, mEntrance, name, title); // 下载按钮ViewHolder
}
/**
* 启动游戏详情页面
*/
public static void startGameDetailActivity(Context context, GameEntity gameEntity, String entrance) {
Intent intent = new Intent(context, GameDetailActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(EntranceUtils.KEY_ENTRANCE, entrance);
intent.putExtra(GameEntity.class.getSimpleName(), gameEntity);
context.startActivity(intent);
}
/**
* 启动游戏详情页面
*/
public static void startGameDetailActivity(Context context, String gameId, String entrance) {
Intent intent = new Intent(context, GameDetailActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("gameId", gameId);
intent.putExtra(EntranceUtils.KEY_ENTRANCE, entrance);
context.startActivity(intent);
}
@Override
protected int getLayoutId() {
return R.layout.activity_gamedetail;
}
//接收QQ或者QQ空间分享回调
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == com.tencent.connect.common.Constants.REQUEST_QQ_SHARE
|| requestCode == com.tencent.connect.common.Constants.REQUEST_QZONE_SHARE) {
Tencent.onActivityResultData(requestCode, resultCode, data, ShareUtils.getInstance(this).QqShareListener);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initTitle("");
mShareIv = (ImageView) LayoutInflater.from(this).inflate(R.layout.menu_action_share, null);
RelativeLayout reuse_actionbar = mContentView.findViewById(R.id.reuse_actionbar);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
params.addRule(RelativeLayout.CENTER_VERTICAL);
reuse_actionbar.addView(mShareIv);
mViewPager.setScrollable(true);
name = "游戏详情";
mGameId = getIntent().getStringExtra("gameId");
mEntrance = getIntent().getStringExtra(EntranceUtils.KEY_ENTRANCE);
if (getIntent().getBundleExtra("data") != null) {
mGameId = getIntent().getBundleExtra("data").getString("gameId");
}
if (mGameId == null) {
mGameEntity = getIntent().getParcelableExtra(GameEntity.class.getSimpleName());
if (mGameEntity != null) {
mGameId = mGameEntity.getId();
mTitle.setText(mGameEntity.getName());
title = mGameEntity.getName();
}
}
if (AppDebugConfig.IS_DEBUG) {
AppDebugConfig.logMethodWithParams(this, getIntent().toUri(Intent.URI_INTENT_SCHEME), mGameId, mGameEntity);
}
if (mGameEntity != null) {
getGameDetail();
} else if (mGameId != null) {
getGameDigest();
} else {
mLoading.setVisibility(View.GONE);
mNoConnection.setVisibility(View.VISIBLE);
}
mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
mCurVpPosition = position;
if (position == 0) {
if (mIsShowKaifuHint) {
mKaifuHint.setVisibility(View.VISIBLE);
}
mTanBarFuLiTv.setTextColor(Color.WHITE);
mTanBarFuLiTv.setBackgroundResource(R.drawable.tabbar_left_bg);
mTanBarXinXiTv.setTextColor(Color.BLACK);
mTanBarXinXiTv.setBackgroundDrawable(new ColorDrawable(0));
} else {
mKaifuHint.setVisibility(View.GONE);
mTanBarXinXiTv.setTextColor(Color.WHITE);
mTanBarXinXiTv.setBackgroundResource(R.drawable.tabbar_right_bg);
mTanBarFuLiTv.setTextColor(Color.BLACK);
mTanBarFuLiTv.setBackgroundDrawable(new ColorDrawable(0));
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
mAppBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
@Override
public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
int total = appBarLayout.getTotalScrollRange();
if (Math.abs(verticalOffset) > total / 2) {
mTitle.setVisibility(View.VISIBLE);
} else {
mTitle.setVisibility(View.GONE);
}
if (Math.abs(verticalOffset) == total && mIsScrollToKaiFu) {
mIsScrollToKaiFu = false;
EventBus.getDefault().post(new EBReuse("scrollToKaiFu"));
}
}
});
// 防抖处理
RxView.clicks(mShareIv)
.throttleFirst(1, TimeUnit.SECONDS)
.subscribe(new Action1<Void>() {
@Override
public void call(Void aVoid) {
if (TextUtils.isEmpty(shareCode)) return;
Map<String, Object> kv = new HashMap<>();
kv.put("点击", "分享");
DataUtils.onEvent(GameDetailActivity.this, "插件数据", mGameEntity.getName(), kv);
DataCollectionUtils.uploadClick(GameDetailActivity.this, "分享", "游戏详情", mGameEntity.getName());
String url = "http://www.ghzs666.com/game/" + shareCode;
showShare(url, mGameEntity.getName(), mGameEntity.getIcon(), null, mGameEntity.getTag(), false);
}
});
}
@Override
protected void onPause() {
super.onPause();
DownloadManager.getInstance(this).removeObserver(dataWatcher);
}
@Override
protected void onResume() {
super.onResume();
if (mGameEntity != null
&& mGameEntity.getApk() != null
&& mGameEntity.getApk().size() == 1) {
DetailDownloadUtils.detailInitDownload(getDetailViewHolder(), true);
}
DownloadManager.getInstance(this).addObserver(dataWatcher);
}
private void initGameDetailTop() {
mLoading.setVisibility(View.GONE);
mAppBarLayout.setVisibility(View.VISIBLE);
if (mGameDetailEntity.getUserData() != null && mGameDetailEntity.getUserData().isGameConcerned()) {
mGameConcern.setText("取消关注");
mGameConcern.setBackgroundResource(R.drawable.border_red_bg);
mGameConcern.setTextColor(Color.parseColor("#ff4147"));
} else {
mGameConcern.setText("关注");
mGameConcern.setBackgroundResource(R.drawable.textview_concern_red_style);
mGameConcern.setTextColor(0xffffffff);
}
mGameName.setText(mGameEntity.getName());
ImageUtils.Companion.display(mGameIcon, mGameEntity.getIcon());
if (mGameEntity.getApk() == null || mGameEntity.getApk().isEmpty()) {
mGameInfo.setText("");
} else {
ApkEntity apkEntity = mGameEntity.getApk().get(0);
mGameInfo.setText(String.format("V%s | %s", apkEntity.getVersion(), apkEntity.getSize()));
}
}
// 获取游戏摘要
private void getGameDigest() {
RetrofitManager.getInstance(this).getApi().getGameDigest(mGameId)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Response<GameEntity>() {
@Override
public void onResponse(GameEntity response) {
ApkActiveUtils.filterHideApk(response);
mGameEntity = response;
mTitle.setText(mGameEntity.getName());
mGameId = response.getId();
title = mGameEntity.getName();
getGameDetail();
}
@Override
public void onFailure(HttpException e) {
mLoading.setVisibility(View.GONE);
mNoConnection.setVisibility(View.VISIBLE);
}
});
}
// 获取游戏详情
public void getGameDetail() {
RetrofitManager.getInstance(this).getApi().getGameDetail(mGameId)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Response<GameDetailEntity>() {
@Override
public void onError(Throwable e) {
super.onError(e);
mLoading.setVisibility(View.GONE);
mNoConnection.setVisibility(View.VISIBLE);
Utils.log("onError::getGameDetail" + e.toString() + "&&id::" + mGameId);
}
@Override
public void onResponse(GameDetailEntity response) {
mGameDetailEntity = response;
// 过滤过期公告
if (response.getNotice() != null && response.getNotice().size() > 0) {
long l = System.currentTimeMillis();
for (int i = 0; i < response.getNotice().size(); i++) {
if (l > (response.getNotice().get(i).getOvertime() * 1000)) {
response.getNotice().remove(i);
i--;
}
}
}
downloadAddWord = response.getDownloadAddWord();
downloadOffText = response.getDownloadOffText();
shareCode = response.getShareCode();
Bundle args = new Bundle();
args.putParcelable(GameEntity.class.getSimpleName(), mGameEntity);
args.putParcelable(GameDetailEntity.class.getSimpleName(), response);
List<Fragment> list = new ArrayList<>();
FuliFragment fuliFragment = new FuliFragment();
fuliFragment.setArguments(args);
list.add(fuliFragment);
XinXiFragment xinXiFragment = new XinXiFragment();
xinXiFragment.setArguments(args);
list.add(xinXiFragment);
mViewPager.setAdapter(new FragmentAdapter(getSupportFragmentManager(), list));
initGameDetailTop();
DetailDownloadUtils.detailInitDownload(getDetailViewHolder(), true);
if (mGameDetailEntity.getUserData() == null || !mGameDetailEntity.getUserData().isGameConcerned()) {
mTanBarXinXiTv.performClick();
}
}
});
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
mIsTouchScreen = true;
break;
case MotionEvent.ACTION_UP:
mIsTouchScreen = false;
break;
}
return super.dispatchTouchEvent(ev);
}
@Subscribe(threadMode = ThreadMode.MAIN)
public void onEventMainThread(EBReuse reuse) {
if (SKIP_XINXI.equals(reuse.getType())) {
mViewPager.setCurrentItem(1);
} else if (OPEN_APPBAR.equals(reuse.getType()) && !mIsTouchScreen) {
mAppBarLayout.setExpanded(true, true);
} else if (SKIP_FULI.equals(reuse.getType())) {
mViewPager.setCurrentItem(0);
} else if ("hideKaifuHint".equals(reuse.getType())) {
mIsShowKaifuHint = false;
mKaifuHint.setVisibility(View.GONE);
} else if ("showKaifuHint".equals(reuse.getType())) {
mIsShowKaifuHint = true;
if (mCurVpPosition == 0) {
mKaifuHint.post(new Runnable() {
@Override
public void run() {
mKaifuHint.setVisibility(View.VISIBLE);
}
});
}
}
}
// 接收下载被删除消息
@Subscribe(threadMode = ThreadMode.MAIN)
public void onEvent(EBDownloadStatus status) {
if ("delete".equals(status.getStatus())
&& mGameEntity != null
&& mGameEntity.getApk() != 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() != 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);
}
}
}
}
// 关注事件
@Subscribe(threadMode = ThreadMode.MAIN)
public void onEventMainThread(EBConcernChanged changed) {
if (mGameDetailEntity != null &&mGameEntity != null && changed.isSingle() && changed.getGameId().equals(mGameEntity.getId())) {
if (mGameDetailEntity.getUserData() == null) {
mGameDetailEntity.setUserData(new UserDataEntity());
}
mGameDetailEntity.getUserData().setGameConcerned(changed.isConcern());
initGameDetailTop();
}
}
@OnClick({R.id.detail_tv_download, R.id.detail_pb_progressbar, R.id.reuse_no_connection
, R.id.detail_tv_per, R.id.gamedetail_tabbar_xinxi_tv, R.id.gamedetail_tabbar_fuli_tv
, R.id.gamedetail_kaifu_hint, R.id.gamedetail_tv_concern})
public void onClick(View v) {
switch (v.getId()) {
case R.id.gamedetail_tabbar_fuli_tv:
mViewPager.setCurrentItem(0);
break;
case R.id.gamedetail_tabbar_xinxi_tv:
mViewPager.setCurrentItem(1);
break;
case R.id.reuse_no_connection:
mLoading.setVisibility(View.VISIBLE);
mNoConnection.setVisibility(View.GONE);
if (mGameEntity != null) {
getGameDetail();
} else if (mGameId != null) {
getGameDigest();
}
break;
case R.id.gamedetail_tv_concern:
CheckLoginUtils.checkLogin(GameDetailActivity.this, new CheckLoginUtils.OnLoggenInListener() {
@Override
public void onLoggedIn() {
mGameConcern.setEnabled(false);
if ("关注".equals(mGameConcern.getText().toString())) {
// 添加关注
ConcernUtils.INSTANCE.postConcernGameId(GameDetailActivity.this, mGameEntity.getId()
, new ConcernUtils.onConcernListener() {
@Override
public void onSuccess() {
Map<String, Object> kv = new HashMap<>();
kv.put("状态", "关注");
DataUtils.onEvent(GameDetailActivity.this, "游戏关注", mGameEntity.getName(), kv);
Map<String, Object> kv2 = new HashMap<>();
kv2.put("点击", "关注");
DataUtils.onEvent(GameDetailActivity.this, "插件数据", mGameEntity.getName(), kv2);
DataCollectionUtils.uploadConcern(GameDetailActivity.this, mGameEntity.getName(), mGameEntity.getId(), "关注");
mGameConcern.setText("取消关注");
mGameConcern.setBackgroundResource(R.drawable.border_red_bg);
mGameConcern.setTextColor(Color.parseColor("#ff4147"));
toast("关注成功");
mGameConcern.setEnabled(true);
}
@Override
public void onError() {
mGameConcern.setEnabled(true);
toast("关注失败");
}
});
} else {
Map<String, Object> kv2 = new HashMap<>();
kv2.put("点击", "取消关注");
DataUtils.onEvent(GameDetailActivity.this, "插件数据", mGameEntity.getName(), kv2);
DialogUtils.showCancelDialog(GameDetailActivity.this, new DialogUtils.ConfirmListener() {
@Override
public void onConfirm() {
// 取消关注
ConcernUtils.INSTANCE.deleteConcernData(GameDetailActivity.this, mGameEntity.getId()
, new ConcernUtils.onConcernListener() {
@Override
public void onSuccess() {
Map<String, Object> kv = new HashMap<>();
kv.put("状态", "取消关注");
DataUtils.onEvent(GameDetailActivity.this, "游戏关注", mGameEntity.getName(), kv);
DataCollectionUtils.uploadConcern(GameDetailActivity.this,
mGameEntity.getName(), mGameEntity.getId(), "取消关注");
mGameConcern.setText("关注");
mGameConcern.setBackgroundResource(R.drawable.textview_concern_red_style);
mGameConcern.setTextColor(0xffffffff);
mGameConcern.setEnabled(true);
}
@Override
public void onError() {
mGameConcern.setEnabled(true);
toast("取消关注失败");
}
});
}
}, new DialogUtils.CancelListener() {
@Override
public void onCancel() {
mGameConcern.setEnabled(true);
}
});
}
}
});
break;
case R.id.gamedetail_kaifu_hint:
mAppBarLayout.setExpanded(false, true);
mIsScrollToKaiFu = true;
break;
}
}
}