Files
assistant-android/app/src/main/java/com/gh/gamecenter/NewsActivity.java
2016-05-21 18:23:11 +08:00

1356 lines
62 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.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.support.v4.util.ArrayMap;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.text.Spanned;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.webkit.JavascriptInterface;
import android.webkit.WebSettings;
import android.webkit.WebSettings.LayoutAlgorithm;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.android.volley.NoConnectionError;
import com.android.volley.Response;
import com.android.volley.TimeoutError;
import com.android.volley.VolleyError;
import com.gh.base.AppController;
import com.gh.base.BaseActivity;
import com.gh.common.constant.Config;
import com.gh.common.constant.Constants;
import com.gh.common.util.DialogUtils;
import com.gh.common.util.DisplayUtils;
import com.gh.common.util.FileUtils;
import com.gh.common.util.ImageUtils;
import com.gh.common.util.MD5Utils;
import com.gh.common.util.NetworkUtils;
import com.gh.common.util.NewsUtils;
import com.gh.common.util.PackageUtils;
import com.gh.common.util.PlatformUtils;
import com.gh.common.util.TimestampUtils;
import com.gh.common.util.Utils;
import com.gh.common.view.DownloadDialog;
import com.gh.download.DataWatcher;
import com.gh.download.DownloadEntry;
import com.gh.download.DownloadManager;
import com.gh.gamecenter.entity.ApkEntity;
import com.gh.gamecenter.entity.DismissEntity;
import com.gh.gamecenter.entity.GameEntity;
import com.gh.gamecenter.entity.NewsDetailsEntity;
import com.gh.gamecenter.entity.NewsEntity;
import com.gh.gamecenter.eventbus.EBNetworkState;
import com.gh.gamecenter.eventbus.EBPWDismiss;
import com.gh.gamecenter.eventbus.EBPackage;
import com.gh.gamecenter.eventbus.EBPutUrl;
import com.gh.gamecenter.eventbus.EBRedDot;
import com.gh.gamecenter.manager.ConcernManager;
import com.gh.gamecenter.manager.DataCollectionManager;
import com.gh.gamecenter.manager.PackageManager;
import com.gh.gamecenter.volley.extended.JsonArrayExtendedRequest;
import com.gh.gamecenter.volley.extended.JsonObjectExtendedRequest;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.tendcloud.tenddata.TCAgent;
import org.json.JSONArray;
import org.json.JSONObject;
import java.lang.reflect.Type;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Random;
import de.greenrobot.event.EventBus;
/**
* 文章详情页面 要启动该页面 需要传入一下参数 放入 EssayEntity中传过来 文章 id 文章标题 title 文章发表时间 time
*
* @author 黄壮华
*/
public class NewsActivity extends BaseActivity implements OnClickListener {
private LinearLayout essaydetails_ll_loading, essaydetails_ll_bottom,
reuse_no_connection;
private NewsDetailsEntity entity;
private TextView actionbar_tv_title, essaydetails_tv_download,
essaydetails_tv_per;
private ImageView essaydetails_iv_share;
private ProgressBar essaydetails_progressbar;
private GameEntity gameEntity;
private DownloadEntry downloadEntry;
private RecyclerView essaydetails_rv_show;
private NewsAdapter adapter;
private LinearLayoutManager linearLayoutManager;
private String entrance;
private String newsId;
private long start = 0L;
private boolean isSentReport = false;
private boolean isDestroy = false;
private DismissEntity dismissEntity;
private ArrayMap<String, String> statusMap;
private ConcernManager concernManager;
private Handler handler = new Handler();
private DataWatcher dataWatcher = new DataWatcher() {
@Override
public void onDataChanged(
HashMap<String, DownloadEntry> downloadingEntries) {
if (gameEntity != null && gameEntity.getApk().size() == 1) {
String url = gameEntity.getApk().get(0).getUrl();
for (Map.Entry<String, DownloadEntry> entry : downloadingEntries
.entrySet()) {
if (url.equals(entry.getValue().getUrl())) {
if (!"pause".equals(statusMap.get(entry.getValue()
.getUrl()))) {
downloadEntry = entry.getValue();
invalidate();
}
break;
}
}
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View contentView = View.inflate(this, R.layout.activity_essaydetails,
null);
init(contentView);
dismissEntity = new DismissEntity(false);
concernManager = new ConcernManager(getApplicationContext());
DisplayMetrics outMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(outMetrics);
entrance = (String) getIntent().getExtras().get("entrance");
newsId = getIntent().getStringExtra("newsId");
if (newsId == null) {
entity = (NewsDetailsEntity) getIntent().getExtras().get("entity");
actionbar_tv_title.setText(entity.getType());
}
statusMap = new ArrayMap<String, String>();
WebView essaydetails_webView = (WebView) findViewById(R.id.essaydetails_webView);
if (NetworkUtils.isWifiConnected(this)) {
essaydetails_webView.setVisibility(View.INVISIBLE);
if (newsId != null) {
essaydetails_webView.loadUrl("http://news.ghzhushou.com/" + newsId + ".html");
} else {
essaydetails_webView.loadUrl("http://news.ghzhushou.com/" + entity.getId() + ".html");
}
} else {
essaydetails_webView.setVisibility(View.GONE);
}
essaydetails_ll_bottom.setOnClickListener(this);
reuse_no_connection.setOnClickListener(this);
essaydetails_tv_download.setOnClickListener(this);
essaydetails_iv_share.setOnClickListener(this);
essaydetails_progressbar.setOnClickListener(this);
essaydetails_tv_per.setOnClickListener(this);
essaydetails_rv_show.setHasFixedSize(true);
linearLayoutManager = new LinearLayoutManager(this);
essaydetails_rv_show.setLayoutManager(linearLayoutManager);
adapter = new NewsAdapter();
essaydetails_rv_show.setAdapter(adapter);
if (newsId != null) {
getNewDigest(newsId);
} else {
getContent();
}
start = Calendar.getInstance().getTimeInMillis();
if (Config.isShow) {
essaydetails_ll_bottom.setVisibility(View.VISIBLE);
} else {
essaydetails_ll_bottom.setVisibility(View.GONE);
}
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)) {
essaydetails_rv_show.scrollToPosition(0);
}
}
});
}
}
private void getNewDigest(final String news_id) {
JsonObjectExtendedRequest request = new JsonObjectExtendedRequest(
Config.HOST + "v1d45/news/" + news_id + "/digest",
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Gson gson = new Gson();
NewsEntity newsEntity = gson.fromJson(
response.toString(), NewsEntity.class);
NewsDetailsEntity essayEntity = new NewsDetailsEntity();
essayEntity.setTitle(newsEntity.getTitle());
essayEntity.setType(newsEntity.getType());
essayEntity.setId(news_id);
entity = essayEntity;
actionbar_tv_title.setText(entity.getType());
getContent();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// 无网络连接和访问超时
if (error.getClass().equals(NoConnectionError.class)
|| error.getClass().equals(TimeoutError.class)) {
if (!isDestroy) {
essaydetails_rv_show.setVisibility(View.GONE);
essaydetails_ll_loading.setVisibility(View.GONE);
essaydetails_ll_bottom.setVisibility(View.GONE);
reuse_no_connection.setVisibility(View.VISIBLE);
}
}
}
});
AppController.addToRequestQueue(request, NewsActivity.class);
}
private long[] mHits = new long[2];
@Override
public void onClick(View v) {
if (v == essaydetails_tv_download) {
if (gameEntity != null && !gameEntity.getApk().isEmpty()) {
if (NetworkUtils.isWifiConnected(this)) {
if (gameEntity.getApk().size() == 1) {
if (essaydetails_tv_download.getText().toString()
.contains("启动")) {
Map<String, Object> kv = new HashMap<String, Object>();
kv.put("版本", gameEntity.getApk().get(0)
.getPlatform());
TCAgent.onEvent(NewsActivity.this, "游戏启动",
gameEntity.getName(), kv);
PackageUtils.launchApplicationByPackageName(
getApplicationContext(), gameEntity
.getApk().get(0).getPackageName());
} else {
ApkEntity apkEntity = gameEntity.getApk().get(0);
String msg = FileUtils.isCanDownload(apkEntity
.getSize());
if (TextUtils.isEmpty(msg)) {
Map<String, Object> kv = new HashMap<String, Object>();
kv.put("版本", apkEntity.getPlatform());
kv.put("状态", "下载开始");
TCAgent.onEvent(NewsActivity.this, "游戏下载",
gameEntity.getName(), kv);
Map<String, Object> kv2 = new HashMap<String, Object>();
kv2.put("版本", apkEntity.getPlatform());
kv2.put("状态", "下载开始");
kv2.put("位置", entrance + "-文章详情-开始");
TCAgent.onEvent(NewsActivity.this, "游戏下载位置",
gameEntity.getName(), kv2);
Map<String, Object> kv3 = new HashMap<String, Object>();
kv3.put(entrance, "下载数");
kv3.put(entrance, "下载开始");
TCAgent.onEvent(NewsActivity.this, "应用数据",
gameEntity.getName(), kv3);
Map<String, Object> map = new HashMap<String, Object>();
map.put("game", gameEntity.getName());
map.put("method", "正常");
map.put("platform",
PlatformUtils.getInstance(
getApplicationContext())
.getPlatformName(
gameEntity.getApk()
.get(0)
.getPlatform()));
map.put("status", "开始");
map.put("location", "新闻详情:" + entity.getTitle());
map.put("from", entrance);
map.put("network",
NetworkUtils.getConnectedType(this));
map.put("createdOn",
System.currentTimeMillis() / 1000);
DataCollectionManager.onEvent(this, "download",
map);
DownloadEntry entry = new DownloadEntry();
entry.setUrl(apkEntity.getUrl());
entry.setName(gameEntity.getName());
entry.setPath(FileUtils.getDownloadPath(
NewsActivity.this,
MD5Utils.getContentMD5(gameEntity
.getName()
+ "_"
+ System.currentTimeMillis())
+ ".apk"));
HashMap<String, String> meta = new HashMap<String, String>();
meta.put("ETag", apkEntity.getEtag());
meta.put("icon", gameEntity.getIcon());
meta.put("platform", apkEntity.getPlatform());
meta.put("gameId", gameEntity.getId());
meta.put("entrance", entrance + "-文章详情");
meta.put("location",
"新闻详情:" + entity.getTitle());
entry.setMeta(meta);
DownloadManager.getInstance(
getApplicationContext()).add(entry);
EventBus.getDefault().post(new EBRedDot(1));
essaydetails_tv_download
.setVisibility(View.GONE);
essaydetails_progressbar
.setVisibility(View.VISIBLE);
essaydetails_tv_per.setVisibility(View.VISIBLE);
essaydetails_progressbar.setProgress(0);
essaydetails_tv_per.setText("0.0%");
statusMap.put(entry.getUrl(), "downloading");
} else {
toast(msg);
}
}
} else {
if (!dismissEntity.isShow()) {
dismissEntity.setShow(true);
DownloadDialog.getInstance(NewsActivity.this)
.showPopupWindow(v, gameEntity,
entrance + "-文章详情", statusMap,
"新闻详情:" + entity.getTitle());
}
}
} else {
final Dialog dialog = new Dialog(this);
View view = View.inflate(this,
R.layout.search_history_delete_dialog, null);
TextView title = (TextView) view
.findViewById(R.id.delete_dialog_title);
title.setText("警告");
TextView content = (TextView) view
.findViewById(R.id.delete_dialog_message);
content.setText("您当前的网络为2G/3G/4G下载将会消耗移动流量是否继续下载");
TextView cancel = (TextView) view
.findViewById(R.id.delete_dialog_cancel);
cancel.setText("取消");
cancel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
TextView confirem = (TextView) view
.findViewById(R.id.delete_dialog_confirm);
confirem.setText("继续");
confirem.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
if (gameEntity.getApk().size() == 1) {
if (essaydetails_tv_download.getText()
.toString().contains("启动")) {
Map<String, Object> kv = new HashMap<String, Object>();
kv.put("版本", gameEntity.getApk().get(0)
.getPlatform());
TCAgent.onEvent(NewsActivity.this, "游戏启动",
gameEntity.getName(), kv);
PackageUtils
.launchApplicationByPackageName(
getApplicationContext(),
gameEntity.getApk().get(0)
.getPackageName());
} else {
ApkEntity apkEntity = gameEntity.getApk()
.get(0);
String msg = FileUtils
.isCanDownload(apkEntity.getSize());
if (TextUtils.isEmpty(msg)) {
Map<String, Object> kv = new HashMap<String, Object>();
kv.put("版本", apkEntity.getPlatform());
kv.put("状态", "下载开始");
TCAgent.onEvent(NewsActivity.this,
"游戏下载", gameEntity.getName(),
kv);
Map<String, Object> kv2 = new HashMap<String, Object>();
kv2.put("版本", apkEntity.getPlatform());
kv2.put("状态", "下载开始");
kv2.put("位置", entrance + "-文章详情-开始");
TCAgent.onEvent(NewsActivity.this,
"游戏下载位置", gameEntity.getName(),
kv2);
Map<String, Object> map = new HashMap<String, Object>();
map.put("game", gameEntity.getName());
map.put("method", "正常");
map.put("platform",
PlatformUtils
.getInstance(
getApplicationContext())
.getPlatformName(
gameEntity
.getApk()
.get(0)
.getPlatform()));
map.put("status", "开始");
map.put("location",
"新闻详情:" + entity.getTitle());
map.put("from", entrance);
map.put("network",
NetworkUtils
.getConnectedType(NewsActivity.this));
map.put("createdOn",
System.currentTimeMillis() / 1000);
DataCollectionManager.onEvent(
NewsActivity.this, "download",
map);
DownloadEntry entry = new DownloadEntry();
entry.setUrl(apkEntity.getUrl());
entry.setName(gameEntity.getName());
entry.setPath(FileUtils
.getDownloadPath(
NewsActivity.this,
MD5Utils.getContentMD5(gameEntity
.getName()
+ "_"
+ System.currentTimeMillis())
+ ".apk"));
HashMap<String, String> meta = new HashMap<String, String>();
meta.put("ETag", apkEntity.getEtag());
meta.put("icon", gameEntity.getIcon());
meta.put("platform",
apkEntity.getPlatform());
meta.put("gameId", gameEntity.getId());
meta.put("entrance", entrance + "-文章详情");
meta.put("location",
"新闻详情:" + entity.getTitle());
entry.setMeta(meta);
DownloadManager.getInstance(
getApplicationContext()).add(
entry);
EventBus.getDefault().post(
new EBRedDot(1));
essaydetails_tv_download
.setVisibility(View.GONE);
essaydetails_progressbar
.setVisibility(View.VISIBLE);
essaydetails_tv_per
.setVisibility(View.VISIBLE);
essaydetails_progressbar.setProgress(0);
essaydetails_tv_per.setText("0.0%");
statusMap.put(entry.getUrl(),
"downloading");
} else {
toast(msg);
}
}
} else {
if (!dismissEntity.isShow()) {
dismissEntity.setShow(true);
DownloadDialog.getInstance(
NewsActivity.this).showPopupWindow(
v, gameEntity, entrance + "-文章详情",
statusMap, "新闻详情:" + entity.getTitle());
}
}
}
});
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(view);
dialog.show();
}
} else {
toast("稍等片刻~!游戏正在上传中...");
}
} else if (v == essaydetails_iv_share) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("location", "分享");
map.put("createOn", System.currentTimeMillis() / 1000);
map.put("news", entity.getTitle());
map.put("page", "新闻详情");
DataCollectionManager.onEvent(this, "click-item", map);
String url = "http://news.ghzhushou.com/" + entity.getId()
+ ".html";
showShare(url, entity.getTitle(), gameEntity.getIcon(), entrance,
"新闻");
} else if (v == essaydetails_progressbar || v == essaydetails_tv_per) {
String str = essaydetails_tv_per.getText().toString();
if ("继续".equals(str)) {
statusMap.put(downloadEntry.getUrl(), "downloading");
essaydetails_tv_per.setText(downloadEntry.getPercent() + "%");
Message msg = Message.obtain();
msg.what = Constants.CONTINUE_DOWNLOAD_TASK;
msg.obj = downloadEntry.getUrl();
DownloadManager.getInstance(getApplicationContext()).put(downloadEntry.getUrl(),
System.currentTimeMillis());
DownloadManager.getInstance(getApplicationContext()).sendMessageDelayed(msg, 1000);
} else if ("安装".equals(str)) {
String path = downloadEntry.getPath();
PackageManager manager = new PackageManager(
getApplicationContext());
if (manager.launchSetup(path)) {
startActivity(PackageUtils.getInstallIntent(path));
} else {
DialogUtils.showDialog(NewsActivity.this, path);
}
} else if("等待".equals(str)){
} else {
statusMap.put(downloadEntry.getUrl(), "pause");
essaydetails_tv_per.setText("继续");
Message msg = Message.obtain();
msg.what = Constants.PAUSE_DOWNLOAD_TASK;
msg.obj = downloadEntry.getUrl();
DownloadManager.getInstance(getApplicationContext()).put(downloadEntry.getUrl(),
System.currentTimeMillis());
DownloadManager.getInstance(getApplicationContext()).sendMessageDelayed(msg, 1000);
}
} else if (v == reuse_no_connection) {
essaydetails_rv_show.setVisibility(View.VISIBLE);
essaydetails_ll_loading.setVisibility(View.VISIBLE);
essaydetails_ll_bottom.setVisibility(View.VISIBLE);
reuse_no_connection.setVisibility(View.GONE);
handler.postDelayed(runnable, 1000);
}
}
Runnable runnable = new Runnable() {
@Override
public void run() {
if (newsId != null) {
getNewDigest(newsId);
} else {
getContent();
}
}
};
private void getContent() {
JsonObjectExtendedRequest request = new JsonObjectExtendedRequest(
Config.HOST + "v1d45/news/" + entity.getId() + "/detail",
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
if (!isDestroy) {
Gson gson = new Gson();
NewsDetailsEntity essayEntity = gson.fromJson(response.toString(), NewsDetailsEntity.class);
essayEntity.setTitle(entity.getTitle());
if (entity.getTime() != null) {
essayEntity.setTime(entity.getTime());
}
essayEntity.setType(entity.getType());
if (entity.getId() != null) {
essayEntity.setId(entity.getId());
}
entity = essayEntity;
adapter.notifyDataSetChanged();
// 获取相关推荐
getNewsMore();
// 获取游戏数据
getGameDetails();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Utils.log(error.toString());
if (error.networkResponse != null) {
Utils.log(new String(error.networkResponse.data));
}
// 无网络连接和访问超时
if (error.getClass().equals(NoConnectionError.class)
|| error.getClass().equals(TimeoutError.class)) {
if (!isDestroy) {
essaydetails_rv_show.setVisibility(View.GONE);
essaydetails_ll_loading.setVisibility(View.GONE);
essaydetails_ll_bottom.setVisibility(View.GONE);
reuse_no_connection.setVisibility(View.VISIBLE);
}
}
}
});
AppController.addToRequestQueue(request, NewsActivity.class);
}
private void getNewsMore() {
JsonArrayExtendedRequest request = new JsonArrayExtendedRequest(
TimestampUtils.addTimestamp(Config.HOST + "v1d45/news/"
+ entity.getId() + "/suggestion", Constants.NEWS_CD),
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Utils.log(response);
if (!isDestroy) {
Type listType = new TypeToken<ArrayList<NewsEntity>>() {}.getType();
Gson gson = new Gson();
List<NewsEntity> list = gson.fromJson(
response.toString(), listType);
// 去除与当前文章重复的文章
for (int i = 0, size = list.size(); i < size; i++) {
if (entity.getId().equals(list.get(i).getId())) {
list.remove(i);
break;
}
}
List<NewsEntity> more = new ArrayList<NewsEntity>();
// TODO 随机三篇文章
int[] index = new int[list.size() > 3 ? 3 : list
.size()];
Random random = new Random(System
.currentTimeMillis());
for (int i = 0; i < index.length; i++) {
if (i == 0) {
index[i] = random.nextInt(list.size());
} else {
index[i] = random(random, index, i,
list.size());
}
}
for (int i = 0; i < index.length; i++) {
more.add(list.get(index[i]));
}
entity.setMore(more);
adapter.notifyItemChanged(0);
adapter.notifyItemRangeInserted(1, entity.getMore().size());
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Utils.log(error);
}
});
AppController.addToRequestQueue(request, NewsActivity.class);
}
private int random(Random random, int[] index, int i, int size) {
int seed = random.nextInt(size);
for (int j = 0; j < i; j++) {
if (index[j] == seed) {
return random(random, index, i, size);
}
}
return seed;
}
private void getGameDetails() {
JsonObjectExtendedRequest gameRequest = new JsonObjectExtendedRequest(
TimestampUtils.addTimestamp(Config.HOST + "v1d45/game/"
+ entity.getGame() + "/news_digest", Constants.GAME_CD),
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
if (!isDestroy) {
processingGameData(response.toString());
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
AppController.addToRequestQueue(gameRequest, NewsActivity.class);
}
private void processingGameData(String data) {
Gson gson = new Gson();
gameEntity = gson.fromJson(data, GameEntity.class);
boolean isInstalled = false;
if (gameEntity.getApk() != null
&& gameEntity.getApk().size() == 1
&& PackageManager.isInstalled(gameEntity.getApk().get(0)
.getPackageName())) {
isInstalled = true;
}
if (TextUtils.isEmpty(gameEntity.getDownloadAddWord())) {
essaydetails_tv_download.setVisibility(View.VISIBLE);
essaydetails_progressbar.setVisibility(View.GONE);
essaydetails_tv_per.setVisibility(View.GONE);
if (isInstalled) {
essaydetails_tv_download.setText("启动《" + gameEntity.getName() + "");
} else {
essaydetails_tv_download.setText("下载《" + gameEntity.getName() + "");
}
} else {
essaydetails_tv_download.setVisibility(View.VISIBLE);
essaydetails_progressbar.setVisibility(View.GONE);
essaydetails_tv_per.setVisibility(View.GONE);
if (isInstalled) {
essaydetails_tv_download.setText("启动《" + gameEntity.getName() + "" + gameEntity.getDownloadAddWord());
} else {
essaydetails_tv_download.setText("下载《" + gameEntity.getName() + "" + gameEntity.getDownloadAddWord());
}
}
if (gameEntity.getApk() == null) {
essaydetails_ll_bottom.setVisibility(View.GONE);
} else if (Config.isShow) {
essaydetails_ll_bottom.setVisibility(View.VISIBLE);
String url = gameEntity.getApk().get(0).getUrl();
for (DownloadEntry entry : DownloadManager.getInstance(getApplicationContext()).getAll()) {
if (url.equals(entry.getUrl())) {
downloadEntry = entry;
essaydetails_tv_download.setVisibility(View.GONE);
essaydetails_progressbar.setVisibility(View.VISIBLE);
essaydetails_tv_per.setVisibility(View.VISIBLE);
invalidate();
break;
}
}
}
adapter.notifyItemChanged(0);
}
private void showSuccessDialog() {
final Dialog dialog = new Dialog(this);
View view = View.inflate(this, R.layout.gamedetails_attention_dialog,
null);
TextView title = (TextView) view.findViewById(R.id.dialog_title);
title.setText("关注成功");
TextView confirm = (TextView) view.findViewById(R.id.dialog_confirm);
confirm.setText("我知道了");
confirm.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
TextView message = (TextView) view.findViewById(R.id.dialog_message);
message.setText("游戏的最新动态消息会优先提醒您,包括攻略、资讯、开服信息、开测信息以及最新的插件。");
view.findViewById(R.id.dialog_rl_cancel).setVisibility(View.GONE);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(view);
dialog.show();
}
private void showCancelDialog(final TextView attention) {
final Dialog dialog = new Dialog(this);
View view = View.inflate(this, R.layout.gamedetails_attention_dialog,
null);
TextView title = (TextView) view.findViewById(R.id.dialog_title);
title.setText("取消关注");
TextView cancel = (TextView) view.findViewById(R.id.dialog_cancel);
cancel.setText("取消");
cancel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
TextView message = (TextView) view.findViewById(R.id.dialog_message);
Spanned content = Html
.fromHtml("取消关注游戏后,您将无法及时收到游戏的<font color='#ff0000'>攻略</font>、<font color='#ff0000'>资讯</font>等最新动态提醒,您确定取消吗?");
message.setText(content);
view.findViewById(R.id.dialog_rl_confirm).setVisibility(View.VISIBLE);
view.findViewById(R.id.dialog_confirm).setOnClickListener(
new OnClickListener() {
@Override
public void onClick(View v) {
Map<String, Object> kv = new HashMap<String, Object>();
kv.put("状态", "取消关注");
TCAgent.onEvent(NewsActivity.this, "游戏关注",
gameEntity.getName(), kv);
Map<String, Object> map = new HashMap<String, Object>();
map.put("game", gameEntity.getName());
map.put("type", "关注");
map.put("createdOn", System.currentTimeMillis() / 1000);
DataCollectionManager.onEvent(NewsActivity.this,
"concern", map);
concernManager.deleteConcern(gameEntity.getId());
dialog.dismiss();
attention.setText("关注");
attention
.setBackgroundResource(R.drawable.textview_red_style);
attention.setTextColor(0xffffffff);
}
});
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(view);
dialog.show();
}
public class JsInterface {
private Context context;
private ArrayList<String> imgs = new ArrayList<String>();
public JsInterface(Context context) {
this.context = context;
}
@JavascriptInterface
public void openImage(String url) {
int current = 0;
for (int i = 0, size = imgs.size(); i < size; i++) {
if (url.equals(imgs.get(i))) {
current = i;
}
}
Intent checkIntent = new Intent(context, ViewImageActivity.class);
checkIntent.putExtra("urls", imgs);
checkIntent.putExtra("current", current);
checkIntent.putExtra("ScaleType", "FIT_CENTER");
context.startActivity(checkIntent);
}
@JavascriptInterface
public void addImage(String url) {
if (!imgs.contains(url)) {
imgs.add(url);
}
}
@JavascriptInterface
public void skip(String id, String type) {
if ("game".equals(type)) {
Intent intent = new Intent(NewsActivity.this,
GameDetailsActivity.class);
intent.putExtra("gameId", id);
intent.putExtra("entrance", entrance);
startActivity(intent);
} else if ("news".equals(type)) {
Intent intent = new Intent(NewsActivity.this,
NewsActivity.class);
intent.putExtra("newsId", id);
intent.putExtra("entrance", entrance);
startActivity(intent);
}
}
}
private class NewsViewHolder extends RecyclerView.ViewHolder implements OnClickListener {
private LinearLayout essaydetails_ll_top;
private View essaydetails_line;
private ImageView essaydetails_icon;
private TextView essaydetails_name, essaydetails_info,
essaydetails_attention, essaydetails_title, essaydetails_time,
essaydetails_author, essaydetails_tv_more;
private WebView essaydetails_wv_content;
private LinearLayout news_item_ll;
private TextView news_type, news_title;
public NewsViewHolder(View view, int viewType) {
super(view);
if (viewType == 0) {
essaydetails_ll_top = (LinearLayout) view.findViewById(R.id.essaydetails_ll_top);
essaydetails_ll_top.setOnClickListener(this);
essaydetails_line = view.findViewById(R.id.essaydetails_line);
essaydetails_icon = (ImageView) view.findViewById(R.id.essaydetails_icon);
essaydetails_name = (TextView) view.findViewById(R.id.essaydetails_name);
essaydetails_info = (TextView) view.findViewById(R.id.essaydetails_info);
essaydetails_attention = (TextView) view.findViewById(R.id.essaydetails_attention);
essaydetails_attention.setOnClickListener(this);
essaydetails_title = (TextView) view.findViewById(R.id.essaydetails_title);
essaydetails_time = (TextView) view.findViewById(R.id.essaydetails_time);
essaydetails_author = (TextView) view.findViewById(R.id.essaydetails_author);
essaydetails_wv_content = (WebView) view.findViewById(R.id.essaydetails_wv_content);
essaydetails_tv_more = (TextView) view.findViewById(R.id.essaydetails_tv_more);
} else if (viewType == 1) {
news_type = (TextView) view.findViewById(R.id.news_type);
news_title = (TextView) view.findViewById(R.id.news_title);
news_item_ll = (LinearLayout) view.findViewById(R.id.news_item_ll);
}
}
@Override
public void onClick(View v) {
if (v == essaydetails_ll_top) {
Intent intent = new Intent(NewsActivity.this,
GameDetailsActivity.class);
intent.putExtra("gameId", gameEntity.getId());
intent.putExtra("entrance", entrance);
startActivity(intent);
} else if (v == essaydetails_attention) {
if ("关注".equals(essaydetails_attention.getText().toString())) {
Map<String, Object> kv = new HashMap<String, Object>();
kv.put("状态", "关注");
TCAgent.onEvent(NewsActivity.this, "游戏关注", gameEntity.getName(), kv);
Map<String, Object> kv2 = new HashMap<String, Object>();
kv2.put("点击", "关注");
TCAgent.onEvent(NewsActivity.this, "插件数据", gameEntity.getName(), kv2);
Map<String, Object> map = new HashMap<String, Object>();
map.put("game", gameEntity.getName());
map.put("type", "关注");
map.put("createdOn", System.currentTimeMillis() / 1000);
DataCollectionManager.onEvent(NewsActivity.this, "concern", map);
concernManager.addByEntity(gameEntity);
essaydetails_attention.setText("取消关注");
essaydetails_attention.setBackgroundResource(R.drawable.border_red_bg);
essaydetails_attention.setTextColor(0xffbc2132);
showSuccessDialog();
} else {
Map<String, Object> kv2 = new HashMap<String, Object>();
kv2.put("点击", "取消关注");
TCAgent.onEvent(NewsActivity.this, "插件数据", gameEntity.getName(), kv2);
showCancelDialog(essaydetails_attention);
}
}
}
}
private class NewsAdapter extends RecyclerView.Adapter<NewsViewHolder> {
@Override
public NewsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view;
if (viewType == 0) {
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.essaydetails_item_top, parent, false);
} else if (viewType == 1) {
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.essaydetails_more_news_item, parent, false);
} else {
RelativeLayout relativeLayout = new RelativeLayout(NewsActivity.this);
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(
LayoutParams.MATCH_PARENT, DisplayUtils.dip2px(getApplicationContext(), 48));
if (!Config.isShow) {
params.height = 0;
}
relativeLayout.setLayoutParams(params);
view = relativeLayout;
}
return new NewsViewHolder(view, viewType);
}
@Override
public void onBindViewHolder(NewsViewHolder holder, int position) {
if (position == 0) {
if (gameEntity != null && !gameEntity.getName().contains("光环助手")) {
holder.essaydetails_ll_top.setVisibility(View.VISIBLE);
holder.essaydetails_line.setVisibility(View.VISIBLE);
ImageUtils.getInstance(getApplicationContext()).displayFile(gameEntity.getIcon(), holder.essaydetails_icon);
holder.essaydetails_name.setText(gameEntity.getName());
if (concernManager.isConcern(gameEntity.getId())) {
holder.essaydetails_attention.setText("取消关注");
holder.essaydetails_attention.setBackgroundResource(R.drawable.border_red_bg);
holder.essaydetails_attention.setTextColor(0xffbc2132);
} else {
holder.essaydetails_attention.setText("关注");
holder.essaydetails_attention.setBackgroundResource(R.drawable.textview_red_style);
holder.essaydetails_attention.setTextColor(0xffffffff);
}
if (gameEntity.getApk() != null && !gameEntity.getApk().isEmpty()) {
for (int i = 0, size = gameEntity.getApk().size(); i < size; i++) {
ApkEntity apkEntity = gameEntity.getApk().get(i);
if ("9u".equals(apkEntity.getPlatform())) {
holder.essaydetails_info.setText("V" + apkEntity.getVersion() + " | " + apkEntity.getSize());
break;
}
if (i == size - 1) {
holder.essaydetails_info.setText("V" + apkEntity.getVersion() + " | " + apkEntity.getSize());
}
}
}
} else {
holder.essaydetails_ll_top.setVisibility(View.GONE);
holder.essaydetails_line.setVisibility(View.GONE);
}
holder.essaydetails_title.setText(entity.getTitle());
SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd", Locale.getDefault());
try {
long today = format.parse(format.format(new Date())).getTime();
long day = Long.valueOf(entity.getTime() + "000");
if (day >= today && day < today + 86400 * 1000) {
format.applyPattern("HH:mm");
holder.essaydetails_time.setText("今天 " + format.format(day));
} else if (day >= today - 86400 * 1000 && day < today) {
format.applyPattern("HH:mm");
holder.essaydetails_time.setText("昨天 " + format.format(day));
} else {
format.applyPattern("yyyy年MM月dd日 HH:mm");
holder.essaydetails_time .setText(format.format(day));
}
} catch (ParseException e) {
e.printStackTrace();
format.applyPattern("yyyy年MM月dd日 HH:mm");
holder.essaydetails_time.setText(format.format(Long.valueOf(entity.getTime() + "000")));
}
if (entity.getAuthor() != null) {
holder.essaydetails_author.setText(entity.getAuthor());
}
if (holder.essaydetails_wv_content.getTag() == null) {
holder.essaydetails_wv_content.addJavascriptInterface(new JsInterface(NewsActivity.this), "imagelistener");
WebSettings webSettings = holder.essaydetails_wv_content.getSettings();
webSettings.setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);
webSettings.setJavaScriptEnabled(true);
holder.essaydetails_wv_content.loadDataWithBaseURL(null, entity.getContent(), "text/html", "utf-8", null);
holder.essaydetails_wv_content .setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view,
String url) {
view.loadUrl("javascript:(function(){"
+ "var imgs = document.getElementsByTagName(\"img\");"
+ "for(var i = 0; i < imgs.length - 1; i++) {"
+ " window.imagelistener.addImage(imgs[i].src);"
+ "}"
+ "for(var i = 0; i < imgs.length - 1; i++) {"
+ " imgs[i].onclick = function() {"
+ " window.imagelistener.openImage(this.src);"
+ " }"
+ "}"
+ "var as = document.getElementsByTagName(\"a\");"
+ "for(var i = 0; i < as.length; i++) {"
+ " as[i].onclick = function() {"
+ " window.imagelistener.skip(this.id, this.type);"
+ " }"
+ "}"
+ "})()");
super.onPageFinished(view, url);
if (essaydetails_ll_loading != null) {
essaydetails_ll_loading.setVisibility(View.GONE);
essaydetails_rv_show.setVisibility(View.VISIBLE);
}
}
});
holder.essaydetails_wv_content.setTag("show");
}
if (entity.getMore() != null && entity.getMore().size() != 0) {
holder.essaydetails_tv_more.setVisibility(View.VISIBLE);
} else {
holder.essaydetails_tv_more.setVisibility(View.GONE);
}
} else if (entity.getMore() != null && position >= 1 && position < 1 + entity.getMore().size()) {
final NewsEntity newsEntity = entity.getMore().get(position - 1);
holder.news_type.setBackgroundResource(NewsUtils
.getDrawableIdByType(newsEntity.getType()));
holder.news_type.setText(newsEntity.getType());
holder.news_title.setText(newsEntity.getTitle());
holder.news_item_ll.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(NewsActivity.this,
NewsActivity.class);
intent.putExtra("newsId", newsEntity.getId());
intent.putExtra("entrance", entrance);
startActivity(intent);
}
});
}
}
@Override
public int getItemCount() {
if (entity == null || entity.getAuthor() == null) {
return 0;
}
int moreSize = entity.getMore() == null ? 0 : entity.getMore().size();
return 2 + moreSize;
}
@Override
public int getItemViewType(int position) {
if (position == 0) {
return 0;
} else if (entity.getMore() != null && position >= 1 && position < 1 + entity.getMore().size()) {
return 1;
} else {
return 5;
}
}
}
public void onEventMainThread(EBPackage busFour) {
if (gameEntity != null && gameEntity.getApk().size() == 1) {
String packageName = gameEntity.getApk().get(0).getPackageName();
if (packageName.equals(busFour.getPackageName())) {
if ("安装".equals(busFour.getType())) {
essaydetails_tv_download.setVisibility(View.VISIBLE);
essaydetails_progressbar.setVisibility(View.GONE);
essaydetails_tv_per.setVisibility(View.GONE);
essaydetails_tv_download.setText("启动《"
+ gameEntity.getName() + "");
} else if ("卸载".equals(busFour.getType())) {
essaydetails_tv_download.setVisibility(View.VISIBLE);
essaydetails_progressbar.setVisibility(View.GONE);
essaydetails_tv_per.setVisibility(View.GONE);
essaydetails_tv_download.setText("下载《"
+ gameEntity.getName() + "");
}
}
}
}
public void onEventMainThread(EBNetworkState busNetworkState) {
if (busNetworkState.isNetworkConnected()) {
if (reuse_no_connection.getVisibility() == View.VISIBLE) {
essaydetails_rv_show.setVisibility(View.VISIBLE);
essaydetails_ll_loading.setVisibility(View.VISIBLE);
essaydetails_ll_bottom.setVisibility(View.VISIBLE);
reuse_no_connection.setVisibility(View.GONE);
handler.postDelayed(runnable, 1000);
}
}
}
public void onEventMainThread(EBPWDismiss dismiss) {
if (dismissEntity != null) {
dismissEntity.setShow(false);
}
}
private void invalidate() {
essaydetails_progressbar
.setProgress((int) (downloadEntry.getPercent() * 10));
switch (downloadEntry.getStatus()) {
case downloading:
essaydetails_tv_per.setText(downloadEntry.getPercent() + "%");
break;
case pause:
case timeout:
case neterror:
essaydetails_tv_per.setText("继续");
break;
case waiting:
essaydetails_tv_per.setText("等待");
break;
case done:
EventBus.getDefault().post(
new EBPutUrl(gameEntity.getApk().get(0).getPackageName(),
downloadEntry.getUrl()));
essaydetails_tv_per.setText("安装");
break;
case cancel:
case hijack:
essaydetails_tv_download.setVisibility(View.VISIBLE);
essaydetails_progressbar.setVisibility(View.GONE);
essaydetails_tv_per.setVisibility(View.GONE);
if (TextUtils.isEmpty(gameEntity.getDownloadAddWord())) {
essaydetails_tv_download.setText("下载《" + gameEntity.getName() + "");
} else {
essaydetails_tv_download.setText("下载《" + gameEntity.getName() + "" + gameEntity.getDownloadAddWord());
}
break;
default:
break;
}
}
@Override
public void onResume() {
super.onResume();
if (gameEntity != null && gameEntity.getApk().size() == 1) {
if (PackageManager.isInstalled(gameEntity.getApk().get(0).getPackageName())) {
essaydetails_tv_download.setVisibility(View.VISIBLE);
essaydetails_progressbar.setVisibility(View.GONE);
essaydetails_tv_per.setVisibility(View.GONE);
if (TextUtils.isEmpty(gameEntity.getDownloadAddWord())) {
essaydetails_tv_download.setText("启动《" + gameEntity.getName() + "");
} else {
essaydetails_tv_download.setText("启动《" + gameEntity.getName() + "" + gameEntity.getDownloadAddWord());
}
} else {
String url = gameEntity.getApk().get(0).getUrl();
for (DownloadEntry entry : DownloadManager.getInstance(getApplicationContext()).getAll()) {
if (url.equals(entry.getUrl())) {
downloadEntry = entry;
essaydetails_tv_download.setVisibility(View.GONE);
essaydetails_progressbar.setVisibility(View.VISIBLE);
essaydetails_tv_per.setVisibility(View.VISIBLE);
invalidate();
break;
}
}
}
}
DownloadManager.getInstance(this).addObserver(dataWatcher);
}
@Override
public void finish() {
long end = Calendar.getInstance().getTimeInMillis();
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秒";
}
// 阅读报告
Map<String, Object> kv = new HashMap<String, Object>();
kv.put(entrance, "阅读数");
kv.put(entrance, "用时:" + cost);
TCAgent.onEvent(NewsActivity.this, "文章数据", entity.getTitle(), kv);
if ("主页-卡牌头条".equals(entrance)) {
Map<String, Object> kv2 = new HashMap<String, Object>();
kv2.put(entity.getTitle(), "阅读数");
kv2.put(entity.getTitle(), "用时:" + cost);
TCAgent.onEvent(this, "主页数据", "卡牌头条", kv2);
} else if ("主页-卡牌攻略".equals(entrance)) {
Map<String, Object> kv2 = new HashMap<String, Object>();
kv2.put(entity.getTitle(), "阅读数");
kv2.put(entity.getTitle(), "用时:" + cost);
TCAgent.onEvent(this, "主页数据", "卡牌攻略", kv2);
}
Map<String, Object> map = new HashMap<String, Object>();
map.put("title", entity.getTitle());
map.put("type", entity.getType());
map.put("author", entity.getAuthor());
if (gameEntity != null) {
map.put("game", gameEntity.getName());
}
map.put("time", seconds);
map.put("createdOn", System.currentTimeMillis() / 1000);
map.put("from", entrance);
DataCollectionManager.onEvent(this, "news", map);
super.finish();
}
@Override
protected void onPause() {
super.onPause();
statusMap.clear();
DownloadManager.getInstance(this).removeObserver(dataWatcher);
if (!isSentReport) {
long end = Calendar.getInstance().getTimeInMillis();
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秒";
}
// 阅读报告
Map<String, Object> kv = new HashMap<String, Object>();
kv.put("用时", cost);
TCAgent.onEvent(NewsActivity.this, "阅读文章", entity.getTitle(), kv);
isSentReport = true;
}
}
@Override
protected void onDestroy() {
super.onDestroy();
isDestroy = true;
AppController.canclePendingRequests(NewsActivity.class);
essaydetails_ll_loading = null;
essaydetails_ll_bottom = null;
reuse_no_connection = null;
entity = null;
actionbar_tv_title = null;
essaydetails_tv_download = null;
essaydetails_tv_per = null;
essaydetails_iv_share = null;
essaydetails_progressbar = null;
gameEntity = null;
downloadEntry = null;
essaydetails_rv_show = null;
adapter = null;
linearLayoutManager = null;
entrance = null;
dismissEntity = null;
statusMap = null;
handler = null;
dataWatcher = null;
}
}