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.MotionEvent; import android.view.View; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.ProgressBar; import android.widget.TextView; import android.widget.Toast; import com.facebook.drawee.view.SimpleDraweeView; import com.gh.base.AppController; import com.gh.base.BaseActivity; import com.gh.common.constant.Config; import com.gh.common.util.ApkActiveUtils; import com.gh.common.util.ConcernUtils; import com.gh.common.util.DataCollectionUtils; import com.gh.common.util.DataUtils; import com.gh.common.util.DialogUtils; import com.gh.common.util.EntranceUtils; import com.gh.common.util.FileUtils; import com.gh.common.util.GameUtils; import com.gh.common.util.ImageUtils; import com.gh.common.util.NetworkUtils; import com.gh.common.util.PackageUtils; import com.gh.common.util.ShareUtils; import com.gh.common.util.Utils; import com.gh.common.view.DownloadDialog; import com.gh.common.view.NoScrollableViewPager; import com.gh.download.DataWatcher; import com.gh.download.DownloadEntity; import com.gh.download.DownloadManager; import com.gh.gamecenter.adapter.FragmentAdapter; import com.gh.gamecenter.entity.ApkEntity; import com.gh.gamecenter.entity.GameDetailEntity; import com.gh.gamecenter.entity.GameEntity; 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.manager.ConcernManager; import com.gh.gamecenter.manager.PackageManager; import com.gh.gamecenter.retrofit.Response; import com.gh.gamecenter.retrofit.RetrofitManager; import com.jakewharton.rxbinding.view.RxView; import com.tencent.tauth.Tencent; 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"; @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_share) ImageView mShareIv; @BindView(R.id.detail_ll_bottom) LinearLayout mDownloadBottom; @BindView(R.id.detail_tv_download) TextView mDownloadTv; @BindView(R.id.detail_tv_per) TextView mDownloadPer; @BindView(R.id.detail_pb_progressbar) ProgressBar mDownloadPb; @BindView(R.id.gamedetail_appbar) AppBarLayout mAppBarLayout; private ConcernManager mConcernManager; private String mGameId; private GameEntity mGameEntity; private DownloadEntity mDownloadEntity; private String downloadAddWord; private String downloadOffText; private String name; // 页面位置 private String title; // 页面标题 private String shareCode; private boolean mIsTouchScreen = false; 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; invalidate(); } } } } }; /** * 启动游戏详情页面 */ public static void startGameDetailActivity(Context context, GameEntity gameEntity, String entrance) { AppController.put("GameEntity", gameEntity); Intent intent = new Intent(context, GameDetailActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.putExtra(EntranceUtils.KEY_ENTRANCE, entrance); 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); init(""); mViewPager.setScrollable(true); mConcernManager = new ConcernManager(GameDetailActivity.this); 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 = (GameEntity) AppController.get("GameEntity", false); if (mGameEntity != null) { mGameId = mGameEntity.getId(); mTitle.setText(mGameEntity.getName()); title = mGameEntity.getName(); } } 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) { if (position == 0) { mTanBarFuLiTv.setTextColor(Color.WHITE); mTanBarFuLiTv.setBackgroundResource(R.drawable.gamedetail_tag_select_bg); mTanBarXinXiTv.setTextColor(Color.BLACK); mTanBarXinXiTv.setBackgroundDrawable(new ColorDrawable(0)); } else { mTanBarXinXiTv.setTextColor(Color.WHITE); mTanBarXinXiTv.setBackgroundResource(R.drawable.gamedetail_tag_unselect_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); } } }); } @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) { initDownload(true); } DownloadManager.getInstance(this).addObserver(dataWatcher); } private void initGameDetailTop() { mLoading.setVisibility(View.GONE); mAppBarLayout.setVisibility(View.VISIBLE); if (mConcernManager.isConcern(mGameEntity.getId())) { 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.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())); } mGameConcern.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { final TextView concern = (TextView) v; if ("关注".equals(concern.getText().toString())) { Map kv = new HashMap<>(); kv.put("状态", "关注"); DataUtils.onEvent(GameDetailActivity.this, "游戏关注", mGameEntity.getName(), kv); Map kv2 = new HashMap<>(); kv2.put("点击", "关注"); DataUtils.onEvent(GameDetailActivity.this, "插件数据", mGameEntity.getName(), kv2); DataCollectionUtils.uploadConcern(GameDetailActivity.this, mGameEntity.getName(), mGameEntity.getId(), "关注"); mConcernManager.addByEntity(mGameEntity); concern.setText("取消关注"); concern.setBackgroundResource(R.drawable.border_red_bg); concern.setTextColor(Color.parseColor("#ff4147")); Toast.makeText(GameDetailActivity.this, "关注成功", Toast.LENGTH_SHORT).show(); // 添加关注 ConcernUtils.postConcernGameId(GameDetailActivity.this, mGameEntity.getId()); } else { Map kv2 = new HashMap<>(); kv2.put("点击", "取消关注"); DataUtils.onEvent(GameDetailActivity.this, "插件数据", mGameEntity.getName(), kv2); DialogUtils.showCancelDialog(GameDetailActivity.this, new DialogUtils.ConfirmListener() { @Override public void onConfirm() { Map kv = new HashMap<>(); kv.put("状态", "取消关注"); DataUtils.onEvent(GameDetailActivity.this, "游戏关注", mGameEntity.getName(), kv); DataCollectionUtils.uploadConcern(GameDetailActivity.this, mGameEntity.getName(), mGameEntity.getId(), "取消关注"); mConcernManager.deleteConcern(mGameEntity.getId()); concern.setText("关注"); concern.setBackgroundResource(R.drawable.textview_concern_red_style); concern.setTextColor(0xffffffff); // 取消关注 ConcernUtils.deleteConcernData(GameDetailActivity.this, mGameEntity.getId()); } }); } } }); } // 获取游戏摘要 private void getGameDigest() { RetrofitManager.getApi().getGameDigest(mGameId) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Response() { @Override public void onResponse(GameEntity response) { ApkActiveUtils.filterHideApk(response); mGameEntity = response; AppController.put("GameEntity", response); mTitle.setText(mGameEntity.getName()); mGameId = response.getId(); title = mGameEntity.getName(); getGameDetail(); } @Override public void onFailure(HttpException e) { Utils.log("=======onError::getGameDetail"); mLoading.setVisibility(View.GONE); mNoConnection.setVisibility(View.VISIBLE); } }); } // 获取游戏详情 public void getGameDetail() { RetrofitManager.getApi().getGameDetail(mGameId) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Response() { @Override public void onError(Throwable e) { super.onError(e); Utils.log("=======onError::getGameDetail" + e.toString()); } @Override public void onResponse(GameDetailEntity 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--; } } } AppController.put("GameDetailEntity", response); downloadAddWord = response.getDownloadAddWord(); downloadOffText = response.getDownloadOffText(); shareCode = response.getShareCode(); List list = new ArrayList<>(); list.add(new FuliFragment()); list.add(new XinXiFragment()); mViewPager.setAdapter(new FragmentAdapter(getSupportFragmentManager(), list)); initGameDetailTop(); initDownload(true); if (!mConcernManager.isConcern(mGameId)) { mTanBarXinXiTv.performClick(); } } @Override public void onFailure(HttpException e) { mLoading.setVisibility(View.GONE); mNoConnection.setVisibility(View.VISIBLE); } }); } @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); } 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); } } // 接收下载被删除消息 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())) { initDownload(false); } } } // 接受安装、卸载消息 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); initDownload(false); } } } } @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_share}) public void onClick(View v) { if (v == mDownloadTv) { if (mGameEntity != null && !mGameEntity.getApk().isEmpty()) { if (mGameEntity.getApk().size() == 1) { if (NetworkUtils.isWifiConnected(this)) { download(); } else { DialogUtils.showDownloadDialog(this, new DialogUtils.ConfirmListener() { @Override public void onConfirm() { download(); } }); } } else { DownloadDialog.getInstance(this).showPopupWindow(v, mGameEntity, mEntrance, name + ":" + title); } } else { toast("稍等片刻~!游戏正在上传中..."); } } else if (v == mDownloadPb || v == mDownloadPer) { String str = mDownloadPer.getText().toString(); if ("下载中".equals(str)) { Intent intent = DownloadManagerActivity.getDownloadMangerIntent(this, mGameEntity.getApk().get(0).getUrl(), mEntrance + "+(" + name + "[" + title + "])"); startActivity(intent); } else if ("安装".equals(str)) { PackageUtils.launchSetup(this, mDownloadEntity.getPath()); } } else if (v == mTanBarFuLiTv) { mViewPager.setCurrentItem(0); } else if (v == mTanBarXinXiTv) { mViewPager.setCurrentItem(1); } else if (v == mShareIv) { if (TextUtils.isEmpty(shareCode)) return; // 防抖处理 RxView.clicks(mShareIv) .throttleFirst(1, TimeUnit.SECONDS) .subscribe(new Action1() { @Override public void call(Void aVoid) { Map kv = new HashMap<>(); kv.put("点击", "分享"); DataUtils.onEvent(GameDetailActivity.this, "插件数据", mGameEntity.getName(), kv); DataCollectionUtils.uploadClick(GameDetailActivity.this, "分享", "游戏详情", mGameEntity.getName()); String url = "http://www.ghzhushou.com/game/" + shareCode; showShare(url, mGameEntity.getName(), mGameEntity.getIcon(), null, mGameEntity.getTag(), false); } }); } else if (v == mNoConnection) { mLoading.setVisibility(View.VISIBLE); mNoConnection.setVisibility(View.GONE); if (mGameEntity != null) { getGameDetail(); } else if (mGameId != null) { getGameDigest(); } } } private void download() { String str = mDownloadTv.getText().toString(); if (str.contains("启动")) { DataUtils.onGameLaunchEvent(this, mGameEntity.getName(), mGameEntity.getApk().get(0).getPlatform(), name); PackageUtils.launchApplicationByPackageName(this, mGameEntity.getApk().get(0).getPackageName()); } else { String method; if (str.contains("更新")) { method = "更新"; } else if (str.contains("插件化")) { method = "插件化"; } else { method = "下载"; } ApkEntity apkEntity = mGameEntity.getApk().get(0); String msg = FileUtils.isCanDownload(this, apkEntity.getSize()); if (TextUtils.isEmpty(msg)) { DataUtils.onGameDownloadEvent(this, mGameEntity.getName(), apkEntity.getPlatform(), mEntrance, "下载开始"); DownloadManager.createDownload(this, apkEntity, mGameEntity, method, mEntrance, name + ":" + title); mDownloadTv.setVisibility(View.GONE); mDownloadPb.setVisibility(View.VISIBLE); mDownloadPer.setVisibility(View.VISIBLE); mDownloadPb.setProgress(0); mDownloadPer.setText("0.0%"); DownloadManager.getInstance(GameDetailActivity.this).putStatus(apkEntity.getUrl(), "downloading"); } else { toast(msg); } } } private void invalidate() { mDownloadPb.setProgress((int) (mDownloadEntity.getPercent() * 10)); mDownloadPer.setTextColor(0xFFFFFFFF); switch (mDownloadEntity.getStatus()) { case downloading: case pause: case timeout: case neterror: case waiting: mDownloadPer.setText("下载中"); break; case done: mDownloadPer.setText("安装"); if (mDownloadEntity.isPluggable() && PackageManager.isInstalled(mDownloadEntity.getPackageName())) { mDownloadPb.setProgressDrawable(getResources().getDrawable(R.drawable.progressbar_plugin_radius_style)); } else { mDownloadPb.setProgressDrawable(getResources().getDrawable(R.drawable.progressbar_normal_radius_style)); } break; case cancel: case hijack: case notfound: initDownload(false); break; default: break; } } protected void initDownload(boolean isCheck) { if (Config.isShow(this)) { mDownloadBottom.setVisibility(View.VISIBLE); } else { mDownloadBottom.setVisibility(View.GONE); } if (mGameEntity != null && "光环助手".equals(mGameEntity.getName())) { mDownloadBottom.setVisibility(View.GONE); } else if (mGameEntity == null || mGameEntity.getApk().isEmpty()) { mDownloadTv.setVisibility(View.VISIBLE); mDownloadPb.setVisibility(View.GONE); mDownloadPer.setVisibility(View.GONE); if (TextUtils.isEmpty(downloadOffText)) { mDownloadTv.setText("暂无下载"); } else { mDownloadTv.setText(downloadOffText); } mDownloadTv.setBackgroundResource(R.drawable.game_item_btn_pause_style); mDownloadTv.setTextColor(0xFF999999); mDownloadTv.setClickable(false); } else { mDownloadTv.setVisibility(View.VISIBLE); mDownloadPb.setVisibility(View.GONE); mDownloadPer.setVisibility(View.GONE); boolean isInstalled = false; if (mGameEntity.getApk() != null && mGameEntity.getApk().size() == 1 && PackageManager.isInstalled(mGameEntity.getApk().get(0).getPackageName())) { isInstalled = true; } if (isInstalled) { if (PackageManager.isCanUpdate(mGameEntity.getId(), mGameEntity.getApk().get(0).getPackageName())) { if (TextUtils.isEmpty(downloadAddWord)) { mDownloadTv.setBackgroundResource( R.drawable.game_item_btn_download_style); mDownloadTv.setText(String.format("更新《%s》", mGameEntity.getName())); } else { mDownloadTv.setBackgroundResource( R.drawable.game_item_btn_download_style); mDownloadTv.setText(String.format("更新《%s》%s", mGameEntity.getName(), downloadAddWord)); } } else { if (mGameEntity.getTag() != null && mGameEntity.getTag().size() != 0 && !TextUtils.isEmpty(mGameEntity.getApk().get(0).getGhVersion()) && !PackageUtils.isSignature(this, mGameEntity.getApk().get(0).getPackageName())) { if (TextUtils.isEmpty(downloadAddWord)) { mDownloadTv.setBackgroundResource( R.drawable.game_item_btn_plugin_style); mDownloadTv.setText(String.format("插件化《%s》", mGameEntity.getName())); } else { mDownloadTv.setBackgroundResource( R.drawable.game_item_btn_plugin_style); mDownloadTv.setText(String.format("插件化《%s》%s", mGameEntity.getName(), downloadAddWord)); } } else { if (TextUtils.isEmpty(downloadAddWord)) { mDownloadTv.setBackgroundResource( R.drawable.game_item_btn_launch_style); mDownloadTv.setText(String.format("启动《%s》", mGameEntity.getName())); } else { mDownloadTv.setBackgroundResource( R.drawable.game_item_btn_launch_style); mDownloadTv.setText(String.format("启动《%s》%s", mGameEntity.getName(), downloadAddWord)); } } } } else { String status = GameUtils.getDownloadBtnText(this, mGameEntity); if ("插件化".equals(status)) { mDownloadTv.setBackgroundResource(R.drawable.game_item_btn_plugin_style); } else if ("打开".equals(status)) { mDownloadTv.setBackgroundResource(R.drawable.game_item_btn_launch_style); } else { mDownloadTv.setBackgroundResource(R.drawable.game_item_btn_download_style); } if (TextUtils.isEmpty(downloadAddWord)) { mDownloadTv.setText(String.format(status + "《%s》", mGameEntity.getName())); } else { mDownloadTv.setText(String.format(status + "《%s》%s", mGameEntity.getName(), downloadAddWord)); } } } if (isCheck && mGameEntity != null && mGameEntity.getApk() != null && mGameEntity.getApk().size() == 1) { String url = mGameEntity.getApk().get(0).getUrl(); DownloadEntity downloadEntity = DownloadManager.getInstance(getApplicationContext()).get(url); if (downloadEntity != null) { mDownloadEntity = downloadEntity; mDownloadTv.setVisibility(View.GONE); mDownloadPb.setVisibility(View.VISIBLE); mDownloadPer.setVisibility(View.VISIBLE); invalidate(); } } } }