242 lines
8.8 KiB
Java
242 lines
8.8 KiB
Java
package com.gh.gamecenter;
|
||
|
||
import android.content.Context;
|
||
import android.content.Intent;
|
||
import android.net.Uri;
|
||
import android.os.Bundle;
|
||
import android.support.annotation.NonNull;
|
||
import android.text.TextUtils;
|
||
import android.view.KeyEvent;
|
||
import android.view.View;
|
||
import android.view.ViewGroup;
|
||
import android.webkit.DownloadListener;
|
||
import android.webkit.WebChromeClient;
|
||
import android.webkit.WebSettings;
|
||
import android.webkit.WebView;
|
||
import android.webkit.WebViewClient;
|
||
import android.widget.ImageView;
|
||
import android.widget.ProgressBar;
|
||
import android.widget.RelativeLayout;
|
||
import android.widget.TextView;
|
||
|
||
import com.gh.base.BaseActivity;
|
||
import com.gh.common.util.DisplayUtils;
|
||
import com.gh.common.util.EntranceUtils;
|
||
import com.gh.common.util.ShareUtils;
|
||
import com.gh.gamecenter.entity.CommentnumEntity;
|
||
import com.gh.gamecenter.entity.ToolBoxEntity;
|
||
import com.gh.gamecenter.retrofit.RetrofitManager;
|
||
import com.jakewharton.rxbinding.view.RxView;
|
||
import com.tencent.tauth.Tencent;
|
||
|
||
import java.util.List;
|
||
import java.util.concurrent.TimeUnit;
|
||
|
||
import butterknife.BindView;
|
||
import rx.android.schedulers.AndroidSchedulers;
|
||
import rx.functions.Action1;
|
||
import rx.schedulers.Schedulers;
|
||
|
||
/**
|
||
* Created by khy on 2016/10/18.
|
||
*/
|
||
public class WebActivity extends BaseActivity implements View.OnClickListener{
|
||
|
||
@BindView(R.id.news_webview)
|
||
WebView webView;
|
||
@BindView(R.id.web_progressbar)
|
||
ProgressBar progressBar;
|
||
@BindView(R.id.web_comment)
|
||
TextView webComment;
|
||
@BindView(R.id.news_bottom)
|
||
RelativeLayout newsBottom;
|
||
@BindView(R.id.actionbar_tv_title)
|
||
TextView newsTitle;
|
||
|
||
private ImageView mShareIv;
|
||
|
||
private String newsId;
|
||
private int commentNum;
|
||
|
||
private boolean mIsTools;
|
||
private ToolBoxEntity mToolBoxEntity;
|
||
|
||
@NonNull
|
||
public static Intent getWebIntent(Context context) {
|
||
Intent intent = new Intent(context, WebActivity.class);
|
||
intent.putExtra("gameName", "使用条款与免责声明");
|
||
intent.putExtra("url", "http://api.ghzs666.com/disclaimer");
|
||
return intent;
|
||
}
|
||
|
||
@Override
|
||
protected int getLayoutId() {
|
||
return R.layout.activity_web;
|
||
}
|
||
|
||
@Override
|
||
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
|
||
super.onActivityResult(requestCode, resultCode, data);
|
||
// 刷新评论数
|
||
if (resultCode == 1001 && progressBar.getVisibility() == View.VISIBLE && newsId != null) {
|
||
webComment.setText("查看评论(" + data.getExtras().getInt("commentNum") + ")");
|
||
} else 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);
|
||
String webUrl;
|
||
String webTitle;
|
||
|
||
mIsTools = getIntent().getBooleanExtra("isTools", false);
|
||
if (getIntent().getBundleExtra("data") != null) {
|
||
Bundle data = getIntent().getBundleExtra("data");
|
||
webUrl = data.getString("url");
|
||
webTitle = "";
|
||
} else {
|
||
webUrl = getIntent().getStringExtra("url");
|
||
webTitle = getIntent().getStringExtra("gameName");
|
||
newsId = getIntent().getStringExtra("newsId");
|
||
}
|
||
|
||
if (mIsTools) {
|
||
mToolBoxEntity = getIntent().getParcelableExtra("ToolBoxEntity");
|
||
mShareIv = new ImageView(this);
|
||
mShareIv.setImageResource(R.drawable.menu_ic_share);
|
||
mShareIv.setOnClickListener(this);
|
||
mShareIv.setVisibility(View.VISIBLE);
|
||
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
|
||
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
|
||
params.addRule(RelativeLayout.CENTER_VERTICAL);
|
||
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
|
||
RelativeLayout reuse_actionbar = (RelativeLayout) mContentView.findViewById(
|
||
R.id.reuse_actionbar);
|
||
reuse_actionbar.addView(mShareIv, params);
|
||
}
|
||
|
||
|
||
initTitle(webTitle);
|
||
|
||
webView.loadUrl(webUrl);
|
||
|
||
WebSettings settings = webView.getSettings();
|
||
settings.setJavaScriptEnabled(true);
|
||
settings.setSupportZoom(true);
|
||
|
||
//用webview打开url
|
||
webView.setWebViewClient(new WebViewClient() {
|
||
// @Override
|
||
// public boolean shouldOverrideUrlLoading(WebView view, String url) {
|
||
// view.loadUrl(url);
|
||
// return true;
|
||
// }
|
||
|
||
// @Override
|
||
// public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
|
||
// 广告拦截操作
|
||
// if (!url.contains(xxxx)) {
|
||
// return new WebResourceResponse(null, null, null);
|
||
// }
|
||
// return null;
|
||
// }
|
||
});
|
||
|
||
// 页面里的下载跳转到自带浏览器
|
||
webView.setDownloadListener(new DownloadListener() {
|
||
@Override
|
||
public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
|
||
Uri uri = Uri.parse(url);
|
||
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
|
||
startActivity(intent);
|
||
}
|
||
});
|
||
|
||
//设置加载进度条
|
||
webView.setWebChromeClient(new WebChromeClient() {
|
||
|
||
@Override
|
||
public void onProgressChanged(WebView view, int newProgress) {
|
||
progressBar.setProgress(newProgress);
|
||
if (newProgress == 100) {
|
||
progressBar.setVisibility(View.GONE);
|
||
if (newsBottom.getVisibility() == View.GONE && newsId != null) {
|
||
newsBottom.setVisibility(View.VISIBLE);
|
||
}
|
||
} else {
|
||
if (progressBar.getVisibility() == View.GONE) {
|
||
progressBar.setVisibility(View.VISIBLE);
|
||
}
|
||
}
|
||
}
|
||
|
||
@Override
|
||
public void onReceivedTitle(WebView view, String title) {
|
||
super.onReceivedTitle(view, title);
|
||
if (TextUtils.isEmpty(newsId) && TextUtils.isEmpty(newsTitle.getText().toString()) && !mIsTools) {
|
||
newsTitle.setEllipsize(TextUtils.TruncateAt.END);
|
||
newsTitle.setPadding(DisplayUtils.dip2px(getApplication(), 30), 0, DisplayUtils.dip2px(getApplication(), 30), 0);
|
||
newsTitle.setSingleLine();
|
||
newsTitle.setText(title);
|
||
|
||
}
|
||
}
|
||
});
|
||
|
||
if (newsId != null) {
|
||
getNewsCommentNum();
|
||
}
|
||
}
|
||
|
||
public void getNewsCommentNum() {
|
||
RetrofitManager.getComment()
|
||
.getNewsCommentnum(newsId)
|
||
.subscribeOn(Schedulers.io())
|
||
.observeOn(AndroidSchedulers.mainThread())
|
||
.subscribe(new com.gh.gamecenter.retrofit.Response<List<CommentnumEntity>>() {
|
||
|
||
@Override
|
||
public void onNext(List<CommentnumEntity> response) {
|
||
super.onNext(response);
|
||
if (response.size() > 0 && response.get(0).getNum() > 0) {
|
||
commentNum = response.get(0).getNum();
|
||
webComment.setText("查看评论(" + response.get(0).getNum() + ")");
|
||
}
|
||
}
|
||
});
|
||
}
|
||
|
||
@Override
|
||
public void onClick(View v) {
|
||
if (v == webComment) {
|
||
Intent intent = new Intent(this, MessageDetailActivity.class);
|
||
intent.putExtra("newsId", newsId);
|
||
intent.putExtra("commentNum", commentNum);
|
||
intent.putExtra(EntranceUtils.KEY_ENTRANCE, mEntrance + "+(光环浏览器)");
|
||
startActivityForResult(intent, 1001);
|
||
} else if (v == mShareIv) {
|
||
RxView.clicks(mShareIv)
|
||
.throttleFirst(1, TimeUnit.SECONDS)
|
||
.subscribe(new Action1<Void>() {
|
||
@Override
|
||
public void call(Void aVoid) {
|
||
showShare(mToolBoxEntity.getUrl(), mToolBoxEntity.getDes(), mToolBoxEntity.getIcon(), mToolBoxEntity.getName(), null, true);
|
||
}
|
||
});
|
||
}
|
||
}
|
||
|
||
@Override
|
||
public boolean onKeyDown(int keyCode, KeyEvent event) {
|
||
if (keyCode == KeyEvent.KEYCODE_BACK && webView.canGoBack()) {
|
||
webView.goBack();// 返回前一个页面
|
||
return true;
|
||
}
|
||
return super.onKeyDown(keyCode, event);
|
||
}
|
||
|
||
}
|