Files
assistant-android/app/src/main/java/com/gh/gamecenter/geetest/GtDialog.java
2017-01-17 11:06:08 +08:00

331 lines
9.5 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.geetest;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.view.Gravity;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.webkit.JavascriptInterface;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.gh.common.util.Utils;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.Iterator;
/**
* 验证对话框
*
* @author dreamzsm@gmail.com
*
*/
public class GtDialog extends Dialog {
private String baseURL = "https://static.geetest.com/static/appweb/app-index.html";
// private String baseURL = "http://192.168.1.158:8721";
protected static final String ACTIVITY_TAG="GtDialog";
private TelephonyManager tm;
private Context mContext;
private String mParamsString;
private String product = "embed";
private String language = "zh-cn";
private String mTitle = "";
private Boolean debug = false;
private Dialog mDialog = this;
private int mWidth;
private int mHeight;
private int mTimeout = 10000;//默认10000ms
private GTWebView webView;
public Boolean isShowing = false;
public void setBaseURL(String url) {
this.baseURL = url;
}
public void setDebug(Boolean debug) {
this.debug = debug;
}
public void setProduct(String product) {
this.product = product;
}
//支持"zh-cn","zh-hk","zh-tw","ko-kr","ja-jp","en-us".默认"zh-cn".
public void setLanguage(String lang) {
this.language = lang;
}
//验证标题, 默认无标题, 不宜过长.
public void setGTTitle(String title) { this.mTitle = title;}
public void setTimeout(int timeout) {
this.mTimeout = timeout;
}
@Override
public void onDetachedFromWindow() {
super.onDetachedFromWindow();
}
public void stopLoading() {
webView.stopLoading();
}
public GtDialog (Context context, JSONObject params) {
super(context);
mContext = context;
tm = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
mParamsString = unwrappingParameters(params);
init(context);
}
private void init(Context context) {
webView = new GTWebView(context);
webView.setGtWebViewListener(new GTWebView.GtWebViewListener() {
@Override
public void gtCallReady(Boolean status) {
if (gtListener != null) {
gtListener.gtCallReady(status);
}
}
@Override
public void gtError() {
if (gtListener != null) {
gtError();
}
}
});
webView.addJavascriptInterface(new JSInterface(), "JSInterface");
String pathUrl = getPathUrl(mParamsString);
String gt_mobile_req_url = baseURL + pathUrl;
Utils.log("验证模块拼接后的url: " + gt_mobile_req_url);
webView.loadUrl(gt_mobile_req_url);
webView.buildLayer();
}
private String unwrappingParameters(JSONObject params) {
Iterator<String> iter = params.keys();
String paramString = "";
while (iter.hasNext()) {
String key = iter.next();
try {
if (iter.hasNext()) {
String comp = key + "=" + params.getString(key) + "&";
paramString = paramString + comp;
}
else {
String comp = key + "=" + params.getString(key);
paramString = paramString + comp;
}
} catch (JSONException e) {
e.printStackTrace();
}
}
return paramString;
}
private String getPathUrl(String paramsString) {
mWidth = getDeviceWidth();
mHeight = getDeviceHeight();
float scale = getDeviceScale();
return "?" + paramsString
// + "&challenge=" + this.challenge
// + "&success=" + (this.success ? 1 : 0)
+ "&imei=" + tm.getDeviceId()
+ "&mType=" + Build.MODEL
+ "&osType=" + "android"
+ "&osVerInt=" + Build.VERSION.RELEASE
+ "&gsdkVerCode=" + "2.16.12.15.1"
+ "&title=" + this.mTitle //验证标题,不宜过长
+ "&lang=" + this.language //支持"zh-cn","zh-hk","zh-tw","ko-kr","ja-jp","en-us".默认"zh-cn"
+ "&debug=" + this.debug
+ "&width=" + (int)(mWidth / scale + 1.5f);//1.5f: fix blank on the webview
}
private float getDeviceScale() {
return getContext().getResources().getDisplayMetrics().density;
}
private int getDeviceWidth() {
int height = DimenTool.getHeightPx(getContext());
int width = DimenTool.getWidthPx(getContext());
float scale = getDeviceScale();
final int WIDTH = 290;
if (height < width) {
width = height * 3 / 4;
}
width = width * 4 / 5;
if ((int)(width / scale + 0.5f) < WIDTH) {
width = (int)((WIDTH - 0.5f) * scale);
}
return width;
}
private int getDeviceHeight() {
int height = DimenTool.getHeightPx(getContext());
int width = DimenTool.getWidthPx(getContext());
float scale = getContext().getResources().getDisplayMetrics().density;
final int HEIGHT = 500;
return (int)(HEIGHT * scale);
}
public interface GtListener {
//通知native验证已准备完毕
void gtCallReady(Boolean status); // true准备完成/false未准备完成
//通知native关闭验证
void gtCallClose();
//通知javascript发生严重错误
void gtError();
//通知native验证结果并准备二次验证
void gtResult(boolean success, String result);
}
private GtListener gtListener;
public void setGtListener(GtListener listener) {
gtListener = listener;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
RelativeLayout contentView = new RelativeLayout(getContext());
contentView.addView(webView);
TextView textView = new TextView(getContext());
textView.setBackgroundColor(Color.WHITE);
textView.setGravity(Gravity.CENTER);
textView.setText("完成验证即可领取礼包");
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(getDeviceWidth(), (int)((float)getDeviceWidth()/7.5));
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
contentView.addView(textView, params);
setContentView(contentView);
final LayoutParams layoutParams = webView.getLayoutParams();
layoutParams.width = mWidth;
layoutParams.height = LayoutParams.WRAP_CONTENT;
webView.setLayoutParams(layoutParams);
LayoutParams contentViewParams = contentView.getLayoutParams();
contentViewParams.height = (int) ((float)getDeviceWidth() * 0.97);
contentViewParams.width = getDeviceWidth();
contentView.setLayoutParams(contentViewParams);
}
@Override
public void show() {
isShowing = true;
super.show();
}
@Override
public void dismiss() {
isShowing = false;
webView.stopLoading();
webView.removeJavascriptInterface("JSInterface");
webView.removeAllViews();
webView.destroy();
super.dismiss();
}
public class JSInterface {
@JavascriptInterface
public void gtCallBack(String code, String result, String message) {
final int fCode;
final String fResult = result;
final String fMessage = message;
try {
fCode = Integer.parseInt(code);
((Activity)mContext).runOnUiThread(new Runnable() {
@Override
public void run() {
if (fCode == 1) {
dismiss();
if (gtListener != null) {
gtListener.gtResult(true, fResult);
}
} else {
if (gtListener != null) {
gtListener.gtResult(false, fResult);
}
}
}
});
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@JavascriptInterface
public void gtCloseWindow() {
dismiss();
if (gtListener != null) {
gtListener.gtCallClose();
}
}
@JavascriptInterface
public void gtReady() {
((Activity)mContext).runOnUiThread(new Runnable() {
@Override
public void run() {
mDialog.show();
}
});
if (gtListener != null) {
gtListener.gtCallReady(true);
}
}
@JavascriptInterface
public void gtError() {
if (gtListener != null) {
gtListener.gtError();
}
}
}
}