Files
assistant-android/app/src/main/java/com/gh/gamecenter/NewsDetailActivity.java

772 lines
32 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.gh.gamecenter;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.SystemClock;
import android.support.v4.view.MotionEventCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.gh.base.BaseActivity;
import com.gh.common.constant.Config;
import com.gh.common.util.ApkActiveUtils;
import com.gh.common.util.DataCollectionUtils;
import com.gh.common.util.DataUtils;
import com.gh.common.util.DialogUtils;
import com.gh.common.util.DisplayUtils;
import com.gh.common.util.EntranceUtils;
import com.gh.common.util.FileUtils;
import com.gh.common.util.GameUtils;
import com.gh.common.util.NetworkUtils;
import com.gh.common.util.PackageUtils;
import com.gh.common.util.ShareUtils;
import com.gh.common.view.DownloadDialog;
import com.gh.common.view.VerticalItemDecoration;
import com.gh.download.DataWatcher;
import com.gh.download.DownloadEntity;
import com.gh.download.DownloadManager;
import com.gh.gamecenter.entity.ApkEntity;
import com.gh.gamecenter.entity.GameEntity;
import com.gh.gamecenter.entity.NewsEntity;
import com.gh.gamecenter.eventbus.EBConcernChanged;
import com.gh.gamecenter.eventbus.EBDownloadStatus;
import com.gh.gamecenter.eventbus.EBNetworkState;
import com.gh.gamecenter.eventbus.EBPackage;
import com.gh.gamecenter.manager.PackageManager;
import com.gh.gamecenter.newsdetail.NewsDetailAdapter;
import com.gh.gamecenter.retrofit.Response;
import com.gh.gamecenter.retrofit.RetrofitManager;
import com.jakewharton.rxbinding.view.RxView;
import com.tencent.tauth.Tencent;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import butterknife.BindView;
import retrofit2.HttpException;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;
/**
* 文章详情页面 要启动该页面 需要传入一下参数 放入 EssayEntity中传过来 文章 id 文章标题 title 文章发表时间 time
*
* @author 黄壮华
*/
public class NewsDetailActivity extends BaseActivity implements OnClickListener {
@BindView(R.id.actionbar_tv_title)
TextView actionbar_tv_title;
@BindView(R.id.news_detail_rv_show)
RecyclerView detail_rv_show;
@BindView(R.id.news_detail_ll_bottom)
LinearLayout detail_ll_bottom;
@BindView(R.id.news_detail_ll_dowload)
RelativeLayout detail_ll_dowload;
@BindView(R.id.news_detail_tv_download)
TextView detail_tv_download;
@BindView(R.id.news_detail_pb_progressbar)
ProgressBar detail_pb_progressbar;
@BindView(R.id.news_detail_tv_per)
TextView detail_tv_per;
@BindView(R.id.reuse_ll_loading)
LinearLayout reuse_ll_loading;
@BindView(R.id.reuse_no_connection)
LinearLayout reuse_no_connection;
@BindView(R.id.reuse_none_data)
LinearLayout reuse_none_data;
@BindView(R.id.reuse_tv_none_data)
TextView reuse_tv_none_data;
@BindView(R.id.news_detail_ll_coment)
RelativeLayout detail_ll_comment;
private ImageView iv_share;
private NewsDetailAdapter adapter;
private String newsId;
Runnable runnable = new Runnable() {
@Override
public void run() {
if (newsId != null) {
getNewsDigest(newsId);
} else {
String id = getIntent().getStringExtra("id");
String type = getIntent().getStringExtra("type");
String title = getIntent().getStringExtra("title");
if (type != null) {
actionbar_tv_title.setText(type);
}
adapter.setId(id);
adapter.setType(type);
adapter.setTitle(title);
adapter.getNewsDetail();
}
}
};
private long start = 0L;
private boolean isSentReport = false;
private float Y11, Y12, Y21, Y22;
private float X11, X12, X21, X22;
private boolean isSecondDown = false;
private double R11;
private int scrollSize = 300;//滑动距离超过300触发事件放大缩小字体
private SharedPreferences sp;
private int fontsize;
private GameEntity gameEntity;
private DownloadEntity mDownloadEntity;
private Handler handler = new Handler();
private String downloadOffText;
private DataWatcher dataWatcher = new DataWatcher() {
@Override
public void onDataChanged(DownloadEntity downloadEntity) {
if (gameEntity != null && gameEntity.getApk().size() == 1) {
String url = gameEntity.getApk().get(0).getUrl();
if (url.equals(downloadEntity.getUrl())) {
if (!"pause".equals(DownloadManager.getInstance(NewsDetailActivity.this).
getStatus(downloadEntity.getUrl()))) {
mDownloadEntity = downloadEntity;
invalidate();
}
}
}
}
};
private long[] mHits = new long[2];
/**
* 启动新闻详情页面
*/
public static void startNewsDetailActivity(Context context, NewsEntity newsEntity, String entrance) {
if (!TextUtils.isEmpty(newsEntity.getLink())) {
Intent intent = new Intent(context, WebActivity.class);
intent.putExtra("url", newsEntity.getLink());
intent.putExtra("gameName", newsEntity.getGameName());
intent.putExtra("newsId", newsEntity.getId());
intent.putExtra(EntranceUtils.KEY_ENTRANCE, entrance + "+(消息详情[" + newsEntity.getGameName() + "])");
context.startActivity(intent);
} else {
Intent intent = new Intent(context, NewsDetailActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("id", newsEntity.getId());
intent.putExtra("title", newsEntity.getTitle());
intent.putExtra("type", newsEntity.getType());
intent.putExtra(EntranceUtils.KEY_ENTRANCE, entrance);
context.startActivity(intent);
}
}
// @Override
// protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// super.onActivityResult(requestCode, resultCode, data);
// // 刷新评论数
// if (resultCode == 1001 ) {
// adapter.commentChange(data.getExtras().getInt("commentNum"));
// }
// }
@Override
protected int getLayoutId() {
return R.layout.activity_news_detail;
}
//接收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("");
// 添加分享图标
iv_share = new ImageView(this);
iv_share.setImageResource(R.drawable.ic_share);
iv_share.setOnClickListener(this);
iv_share.setVisibility(View.GONE);
iv_share.setPadding(DisplayUtils.dip2px(this, 13), DisplayUtils.dip2px(this, 11)
, DisplayUtils.dip2px(this, 11), DisplayUtils.dip2px(this, 13));
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
DisplayUtils.dip2px(this, 48), DisplayUtils.dip2px(this, 48));
params.addRule(RelativeLayout.CENTER_VERTICAL);
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
RelativeLayout reuse_actionbar = (RelativeLayout) mContentView.findViewById(
R.id.reuse_actionbar);
reuse_actionbar.addView(iv_share, params);
// DisplayMetrics outMetrics = new DisplayMetrics();
// getWindowManager().getDefaultDisplay().getMetrics(outMetrics);
detail_rv_show.setHasFixedSize(true);
detail_rv_show.setLayoutManager(new LinearLayoutManager(this));
detail_rv_show.addItemDecoration(new VerticalItemDecoration(this, 8, false));
adapter = new NewsDetailAdapter(this, this, mEntrance);
detail_rv_show.setAdapter(adapter);
newsId = getIntent().getStringExtra("newsId");
if (getIntent().getBundleExtra("data") != null) {
newsId = getIntent().getBundleExtra("data").getString("newsId");
}
if (newsId == null) {
String id = getIntent().getStringExtra("id");
String type = getIntent().getStringExtra("type");
String title = getIntent().getStringExtra("title");
if (type != null) {
actionbar_tv_title.setText(type);
}
adapter.setId(id);
adapter.setType(type);
adapter.setTitle(title);
adapter.getNewsDetail();
iv_share.setVisibility(View.VISIBLE);
} else {
getNewsDigest(newsId);
}
start = System.currentTimeMillis();
View view = getWindow().findViewById(Window.ID_ANDROID_CONTENT);
if (view != null) {
view.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
System.arraycopy(mHits, 1, mHits, 0, mHits.length - 1);
mHits[mHits.length - 1] = SystemClock.uptimeMillis();
if (mHits[0] >= (SystemClock.uptimeMillis() - 1000)) {
detail_rv_show.scrollToPosition(0);
}
}
});
}
sp = getSharedPreferences(Config.PREFERENCE, Activity.MODE_PRIVATE);
fontsize = sp.getInt("fontsize", 1);
if (fontsize == 0) {
fontsize = 1;
}
detail_ll_bottom.setOnClickListener(this);
detail_tv_download.setOnClickListener(this);
detail_pb_progressbar.setOnClickListener(this);
detail_tv_per.setOnClickListener(this);
reuse_no_connection.setOnClickListener(this);
detail_ll_comment.setOnClickListener(this);
// 防抖处理
RxView.clicks(iv_share)
.throttleFirst(1, TimeUnit.SECONDS)
.subscribe(new Action1<Void>() {
@Override
public void call(Void aVoid) {
if (adapter != null && adapter.getNewsDetailEntity() != null) {
Map<String, Object> kv = new HashMap<>();
kv.put("名字", adapter.getNewsDetailEntity().getTitle());
kv.put("位置", "分享");
DataUtils.onEvent(NewsDetailActivity.this, "点击", "新闻详情", kv);
DataCollectionUtils.uploadClick(NewsDetailActivity.this, "分享", "新闻详情"
, adapter.getNewsDetailEntity().getTitle());
String url = "http://www.ghzs666.com/article/" +
adapter.getNewsDetailEntity().getId() + ".html";
if (gameEntity == null) {
showShare(url, adapter.getNewsDetailEntity().getTitle(),
"http://image.ghzs666.com/pic/57d604808ab49e467d8b4568.png",
adapter.getNewsDetailEntity().getTitle(), null, false);
} else {
showShare(url, gameEntity.getName(), gameEntity.getIcon(),
adapter.getNewsDetailEntity().getTitle(), gameEntity.getTag(), false);
}
}
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
handler.removeCallbacksAndMessages(null);
}
@Override
protected void onPause() {
super.onPause();
if (!isSentReport) {
long end = System.currentTimeMillis();
int seconds = (int) ((end - start) / 1000);
String cost;
if (seconds < 5) {
cost = "小于5秒";
} else if (seconds < 30) {
cost = "5秒30秒";
} else if (seconds < 60) {
cost = "30秒60秒";
} else {
cost = "大于60秒";
}
if (adapter.getNewsDetailEntity() != null) {
// 阅读报告
Map<String, Object> kv = new HashMap<>();
kv.put("用时", cost);
DataUtils.onEvent(NewsDetailActivity.this, "阅读文章", adapter.getNewsDetailEntity().getTitle(), kv);
// 阅读报告
Map<String, Object> kv1 = new HashMap<>();
kv1.put(mEntrance, "阅读数");
kv1.put(mEntrance, "用时:" + cost);
DataUtils.onEvent(NewsDetailActivity.this, "文章数据", adapter.getNewsDetailEntity().getTitle(), kv1);
if (seconds > 0) {
DataCollectionUtils.uploadNews(this, adapter.getNewsDetailEntity(), gameEntity, seconds, mEntrance);
}
}
isSentReport = true;
}
DownloadManager.getInstance(this).removeObserver(dataWatcher);
}
@Override
protected void onResume() {
super.onResume();
if (gameEntity != null
&& gameEntity.getApk() != null
&& gameEntity.getApk().size() == 1) {
initDownload(true);
}
DownloadManager.getInstance(this).addObserver(dataWatcher);
}
@Override
public void loadDone(Object obj) {
if (reuse_ll_loading != null) {
reuse_ll_loading.setVisibility(View.GONE);
detail_rv_show.setVisibility(View.VISIBLE);
}
getGameDetail((String) obj);
}
@Override
public void loadError() {
detail_rv_show.setVisibility(View.GONE);
reuse_ll_loading.setVisibility(View.GONE);
detail_ll_bottom.setVisibility(View.GONE);
detail_rv_show.setPadding(0, 0, 0, 0);
reuse_no_connection.setVisibility(View.VISIBLE);
}
private void getNewsDigest(final String newsId) {
RetrofitManager.getApi().getNewsDigest(newsId)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Response<NewsEntity>() {
@Override
public void onResponse(NewsEntity response) {
if (response.getType() != null) {
actionbar_tv_title.setText(response.getType());
}
adapter.setId(newsId);
adapter.setType(response.getType());
adapter.setTitle(response.getTitle());
adapter.getNewsDetail();
iv_share.setVisibility(View.VISIBLE);
}
@Override
public void onFailure(HttpException e) {
detail_rv_show.setVisibility(View.GONE);
reuse_ll_loading.setVisibility(View.GONE);
detail_ll_bottom.setVisibility(View.GONE);
detail_rv_show.setPadding(0, 0, 0, 0);
reuse_no_connection.setVisibility(View.VISIBLE);
}
});
}
/**
* 增加手势监听,控制字体大小
*
* @author 柯皓源
*/
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
switch (ev.getAction() & MotionEventCompat.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
isSecondDown = false;
int pointerIndex = MotionEventCompat.findPointerIndex(ev, MotionEventCompat.getPointerId(ev, 0));
Y11 = MotionEventCompat.getY(ev, pointerIndex);
X11 = MotionEventCompat.getX(ev, pointerIndex);
break;
case MotionEvent.ACTION_UP:
final int actionUpIndex = MotionEventCompat.getActionIndex(ev);
int pointerIndex2 = MotionEventCompat.findPointerIndex(ev, MotionEventCompat.getPointerId(ev, actionUpIndex));
Y12 = MotionEventCompat.getY(ev, pointerIndex2);
X12 = MotionEventCompat.getX(ev, pointerIndex2);
float X2 = Math.abs(X12 - X22);
float Y2 = Math.abs(Y12 - Y22);
float R2 = (X2 * X2) + (Y2 * Y2);
double R12 = Math.sqrt(R2);
if (isSecondDown) {
if ((R11 - R12) > scrollSize && fontsize > 1) {
fontsize--;
adapter.setFontSize(fontsize);
sp.edit().putInt("fontsize", fontsize).apply();
String fontSizeText = getFontSize(fontsize);
Toast.makeText(this, fontSizeText, Toast.LENGTH_SHORT).show();
}
if ((R11 - R12) < -scrollSize && fontsize < 4) {
fontsize++;
adapter.setFontSize(fontsize);
sp.edit().putInt("fontsize", fontsize).apply();
String fontSizeText = getFontSize(fontsize);
Toast.makeText(this, fontSizeText, Toast.LENGTH_SHORT).show();
}
}
break;
case MotionEvent.ACTION_MOVE:
if (isSecondDown) {
return true;
}
break;
case MotionEventCompat.ACTION_POINTER_DOWN:
isSecondDown = true;
final int pointerIndexDown = MotionEventCompat.getActionIndex(ev);
int pointerIndex1 = MotionEventCompat.findPointerIndex(ev, MotionEventCompat.getPointerId(ev, pointerIndexDown));
Y21 = MotionEventCompat.getY(ev, pointerIndex1);
X21 = MotionEventCompat.getX(ev, pointerIndex1);
float X1 = Math.abs(X11 - X21);
float Y1 = Math.abs(Y11 - Y21);
float R1 = (X1 * X1) + (Y1 * Y1);
R11 = Math.sqrt(R1);
break;
case MotionEventCompat.ACTION_POINTER_UP:
final int pointerIndexUp = MotionEventCompat.getActionIndex(ev);
int pointerIndex21 = MotionEventCompat.findPointerIndex(ev, MotionEventCompat.getPointerId(ev, pointerIndexUp));
Y22 = MotionEventCompat.getY(ev, pointerIndex21);
X22 = MotionEventCompat.getX(ev, pointerIndex21);
break;
}
return super.dispatchTouchEvent(ev);
} @Override
public void onClick(final View v) {
if (v == reuse_no_connection) {
detail_rv_show.setVisibility(View.VISIBLE);
reuse_ll_loading.setVisibility(View.VISIBLE);
reuse_no_connection.setVisibility(View.GONE);
handler.postDelayed(runnable, 1000);
} else if (v == detail_tv_download) {
if (gameEntity != null && !gameEntity.getApk().isEmpty()) {
if (gameEntity.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, gameEntity, mEntrance, "新闻详情:" + adapter.getTitle());
}
} else {
toast("稍等片刻~!游戏正在上传中...");
}
} else if (v == detail_pb_progressbar || v == detail_tv_per) {
String str = detail_tv_per.getText().toString();
if ("下载中".equals(str)) {
Intent intent = DownloadManagerActivity.getDownloadMangerIntent(this,
gameEntity.getApk().get(0).getUrl(), mEntrance + "(新闻详情[" + adapter.getTitle() + "])");
startActivity(intent);
} else if ("安装".equals(str)) {
PackageUtils.launchSetup(this, mDownloadEntity.getPath());
}
} else if (v == detail_ll_comment) {
Intent intent = new Intent(this, MessageDetailActivity.class);
intent.putExtra("commentNum", -1);
intent.putExtra("newsId", adapter.getNewsDetailEntity().getId());
intent.putExtra("openSoftInput", true);
intent.putExtra(EntranceUtils.KEY_ENTRANCE, mEntrance + "(新闻详情[" + adapter.getTitle() + "])");
startActivity(intent);
}
}
private String getFontSize(int i) {
switch (i) {
case 1:
return "小字号";
case 2:
return "中字号";
case 3:
return "大字号";
case 4:
return "特大字号";
default:
return "未知字号";
}
}
private void getGameDetail(String gameId) {
if (TextUtils.isEmpty(gameId)) {
detail_ll_bottom.setVisibility(View.GONE);
detail_rv_show.setPadding(0, 0, 0, 0);
return;
}
RetrofitManager.getApi().getGameNewsDigest(gameId)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Response<GameEntity>() {
@Override
public void onResponse(GameEntity response) {
ApkActiveUtils.filterHideApk(response);
gameEntity = response;
adapter.setGameEntity(gameEntity);
adapter.notifyItemInserted(1);
downloadOffText = gameEntity.getDownloadOffText();
initDownload(true);
}
});
}
// 关注事件
@Subscribe(threadMode = ThreadMode.MAIN)
public void onEventMainThread(EBConcernChanged changed) {
if (gameEntity != null && changed.isSingle() && changed.getGameId().equals(gameEntity.getId())) {
adapter.notifyItemChanged(1);
}
}
@Subscribe(threadMode = ThreadMode.MAIN)
public void onEventMainThread(EBNetworkState busNetworkState) {
if (busNetworkState.isNetworkConnected()) {
if (reuse_no_connection.getVisibility() == View.VISIBLE) {
detail_rv_show.setVisibility(View.VISIBLE);
reuse_ll_loading.setVisibility(View.VISIBLE);
detail_ll_bottom.setVisibility(View.VISIBLE);
detail_rv_show.setPadding(0, 0, 0, DisplayUtils.dip2px(getApplicationContext(), 60));
reuse_no_connection.setVisibility(View.GONE);
handler.postDelayed(runnable, 1000);
}
}
}
protected void initDownload(boolean isCheck) {
if (Config.isShow(this)) {
detail_ll_bottom.setVisibility(View.VISIBLE);
detail_rv_show.setPadding(0, 0, 0,
DisplayUtils.dip2px(getApplicationContext(), 60));
} else {
detail_ll_bottom.setVisibility(View.GONE);
detail_rv_show.setPadding(0, 0, 0, 0);
}
if (gameEntity != null && "光环助手".equals(gameEntity.getName())) {
if ("光环助手".equals(gameEntity.getName())) {
detail_ll_dowload.setVisibility(View.GONE);
} else {
detail_ll_bottom.setVisibility(View.GONE);
detail_rv_show.setPadding(0, 0, 0, 0);
}
} else if (gameEntity == null || gameEntity.getApk().isEmpty()) {
detail_tv_download.setVisibility(View.VISIBLE);
detail_pb_progressbar.setVisibility(View.GONE);
detail_tv_per.setVisibility(View.GONE);
if (TextUtils.isEmpty(downloadOffText)) {
detail_tv_download.setText("暂无下载");
} else {
detail_tv_download.setText(downloadOffText);
}
detail_tv_download.setBackgroundResource(R.drawable.game_item_btn_pause_style);
detail_tv_download.setTextColor(0xFF999999);
detail_tv_download.setClickable(false);
} else {
detail_tv_download.setVisibility(View.VISIBLE);
detail_pb_progressbar.setVisibility(View.GONE);
detail_tv_per.setVisibility(View.GONE);
boolean isInstalled = false;
if (gameEntity.getApk() != null && gameEntity.getApk().size() == 1
&& PackageManager.isInstalled(gameEntity.getApk().get(0).getPackageName())) {
isInstalled = true;
}
if (isInstalled) {
if (PackageManager.isCanUpdate(gameEntity.getId(), gameEntity.getApk().get(0).getPackageName())) {
detail_tv_download.setBackgroundResource(
R.drawable.game_item_btn_download_style);
detail_tv_download.setText("更新");
} else {
if (gameEntity.getTag() != null && gameEntity.getTag().size() != 0
&& !TextUtils.isEmpty(gameEntity.getApk().get(0).getGhVersion())
&& !PackageUtils.isSignature(this, gameEntity.getApk().get(0).getPackageName())) {
detail_tv_download.setBackgroundResource(
R.drawable.game_item_btn_plugin_style);
detail_tv_download.setText("插件化");
} else {
detail_tv_download.setBackgroundResource(
R.drawable.game_item_btn_launch_style);
detail_tv_download.setText("启动");
}
}
} else {
String status = GameUtils.getDownloadBtnText(this, gameEntity);
if ("插件化".equals(status)) {
detail_tv_download.setBackgroundResource(R.drawable.game_item_btn_plugin_style);
} else if ("打开".equals(status)) {
detail_tv_download.setBackgroundResource(R.drawable.game_item_btn_launch_style);
} else {
detail_tv_download.setBackgroundResource(R.drawable.game_item_btn_download_style);
}
detail_tv_download.setText(status);
}
}
if (isCheck && gameEntity != null
&& gameEntity.getApk() != null
&& gameEntity.getApk().size() == 1) {
String url = gameEntity.getApk().get(0).getUrl();
DownloadEntity downloadEntity = DownloadManager.getInstance(getApplicationContext()).get(url);
if (downloadEntity != null) {
mDownloadEntity = downloadEntity;
detail_tv_download.setVisibility(View.GONE);
detail_pb_progressbar.setVisibility(View.VISIBLE);
detail_tv_per.setVisibility(View.VISIBLE);
invalidate();
}
}
}
private void invalidate() {
detail_pb_progressbar.setProgress((int) (mDownloadEntity.getPercent() * 10));
detail_tv_per.setTextColor(0xFFFFFFFF);
switch (mDownloadEntity.getStatus()) {
case downloading:
case pause:
case timeout:
case neterror:
case waiting:
detail_tv_per.setText("下载中");
break;
case done:
detail_tv_per.setText("安装");
if (mDownloadEntity.isPluggable()
&& PackageManager.isInstalled(mDownloadEntity.getPackageName())) {
detail_pb_progressbar.setProgressDrawable(getResources().getDrawable(R.drawable.progressbar_plugin_radius_style));
} else {
detail_pb_progressbar.setProgressDrawable(getResources().getDrawable(R.drawable.progressbar_normal_radius_style));
}
break;
case cancel:
case hijack:
case notfound:
initDownload(false);
break;
default:
break;
}
}
// 接收下载被删除消息
@Subscribe(threadMode = ThreadMode.MAIN)
public void onEvent(EBDownloadStatus status) {
if ("delete".equals(status.getStatus())
&& gameEntity != null
&& gameEntity.getApk() != null
&& gameEntity.getApk().size() == 1) {
String url = gameEntity.getApk().get(0).getUrl();
if (url.equals(status.getUrl())) {
initDownload(false);
}
}
}
// 接受安装、卸载消息
@Subscribe(threadMode = ThreadMode.MAIN)
public void onEventMainThread(EBPackage busFour) {
if (gameEntity != null
&& gameEntity.getApk() != null
&& gameEntity.getApk().size() == 1) {
String packageName = gameEntity.getApk().get(0).getPackageName();
if (packageName.equals(busFour.getPackageName())) {
initDownload(false);
}
}
}
private void download() {
String str = detail_tv_download.getText().toString();
if (str.contains("启动")) {
DataUtils.onGameLaunchEvent(this, gameEntity.getName(), gameEntity.getApk().get(0).getPlatform(), "新闻详情");
PackageUtils.launchApplicationByPackageName(this, gameEntity.getApk().get(0).getPackageName());
} else {
String method;
if (str.contains("更新")) {
method = "更新";
} else if (str.contains("插件化")) {
method = "插件化";
} else {
method = "下载";
}
ApkEntity apkEntity = gameEntity.getApk().get(0);
String msg = FileUtils.isCanDownload(this, apkEntity.getSize());
if (TextUtils.isEmpty(msg)) {
DataUtils.onGameDownloadEvent(this, gameEntity.getName(), apkEntity.getPlatform(), mEntrance, "下载开始");
DownloadManager.createDownload(this, apkEntity, gameEntity, method, mEntrance, "新闻详情:" + adapter.getTitle());
detail_tv_download.setVisibility(View.GONE);
detail_pb_progressbar.setVisibility(View.VISIBLE);
detail_tv_per.setVisibility(View.VISIBLE);
detail_pb_progressbar.setProgress(0);
detail_tv_per.setText("0.0%");
DownloadManager.getInstance(NewsDetailActivity.this).putStatus(apkEntity.getUrl(), "downloading");
} else {
toast(msg);
}
}
}
}