|
|
|
package com.agg.h5game;
|
|
|
|
|
|
|
|
import android.app.Activity;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.content.pm.ActivityInfo;
|
|
|
|
import android.content.res.Configuration;
|
|
|
|
import android.net.Uri;
|
|
|
|
import android.os.Build;
|
|
|
|
import android.os.Bundle;
|
|
|
|
import android.os.Handler;
|
|
|
|
import android.text.TextUtils;
|
|
|
|
import android.view.KeyEvent;
|
|
|
|
import android.view.Window;
|
|
|
|
import android.view.WindowManager;
|
|
|
|
import android.webkit.JavascriptInterface;
|
|
|
|
import android.webkit.WebSettings;
|
|
|
|
import android.webkit.WebView;
|
|
|
|
import android.webkit.WebViewClient;
|
|
|
|
|
|
|
|
import com.agg.h5game.tools.AggH5Log;
|
|
|
|
import com.agg.h5game.tools.AggH5Tools;
|
|
|
|
import com.agg.h5game.tools.ParamsTools;
|
|
|
|
import com.stss.sdk.InitResult;
|
|
|
|
import com.stss.sdk.PayResult;
|
|
|
|
import com.stss.sdk.STSSAggGame;
|
|
|
|
import com.stss.sdk.STSSAggSdkListener;
|
|
|
|
import com.stss.sdk.constant.STSSAggCode;
|
|
|
|
import com.stss.sdk.verify.STSSUToken;
|
|
|
|
|
|
|
|
public class AggH5MainActivity extends Activity {
|
|
|
|
private final String TAG = "STSSAgg";
|
|
|
|
private Activity mContext;
|
|
|
|
|
|
|
|
private WebView webView;
|
|
|
|
private String h5game = "https://m.baidu.com";
|
|
|
|
private Boolean isToLogin = false;
|
|
|
|
|
|
|
|
private STSSUToken currToken = null;
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
|
requestWindowFeature(Window.FEATURE_NO_TITLE);
|
|
|
|
Window window = getWindow();
|
|
|
|
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
|
|
|
|
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
|
|
|
|
window.setStatusBarColor(getResources().getColor(R.color.status_bar_color));
|
|
|
|
}
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
mContext = this;
|
|
|
|
|
|
|
|
setContentView(R.layout.activity_main);
|
|
|
|
initView();
|
|
|
|
initSDK();
|
|
|
|
|
|
|
|
String gameUrl = AggH5Tools.getMetaData(mContext, "h5game");
|
|
|
|
if (!TextUtils.isEmpty(gameUrl)) {
|
|
|
|
h5game = gameUrl;
|
|
|
|
}
|
|
|
|
String newH5Url = h5game + "?" + ParamsTools.getParams(mContext, currToken);
|
|
|
|
AggH5Log.d(TAG, newH5Url);
|
|
|
|
webView.loadUrl(newH5Url);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void initView() {
|
|
|
|
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
|
|
|
|
webView = findViewById(R.id.webview);
|
|
|
|
webView.addJavascriptInterface(new AggH5AndroidToJs(), "AggH5Game");
|
|
|
|
|
|
|
|
//页面缩放,适应手机屏幕
|
|
|
|
webView.getSettings().setJavaScriptEnabled(true);
|
|
|
|
webView.getSettings().setUseWideViewPort(true);
|
|
|
|
webView.getSettings().setLoadWithOverviewMode(false);
|
|
|
|
webView.getSettings().setDomStorageEnabled(true);
|
|
|
|
webView.getSettings().setDefaultTextEncodingName("UTF-8");
|
|
|
|
webView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
|
|
|
|
|
|
|
|
//加载页面时如果不加改代码,页面会跳转到系统自带浏览器显示。
|
|
|
|
webView.setWebViewClient(new WebViewClient() {
|
|
|
|
@Override
|
|
|
|
public boolean shouldOverrideUrlLoading(WebView view, String url) {
|
|
|
|
AggH5Log.d(TAG, "url for load: " + url);
|
|
|
|
try {
|
|
|
|
if (url.endsWith("qnsdkyhxy2.html") || url.endsWith("qnsdkysxy2.html")) {
|
|
|
|
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
|
|
|
|
startActivity(intent);
|
|
|
|
return false;
|
|
|
|
} else if (url.startsWith("http:") || url.startsWith("https:")) {
|
|
|
|
//view.loadUrl(url);
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
|
|
|
|
startActivity(intent);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onPageFinished(WebView view, String url) {
|
|
|
|
super.onPageFinished(view, url);
|
|
|
|
AggH5Log.d(TAG, "url for finish: " + url);
|
|
|
|
login();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private void initSDK() {
|
|
|
|
STSSAggGame.setSDKListener(mSdkListener);
|
|
|
|
STSSAggGame.init(mContext);
|
|
|
|
STSSAggGame.onCreate();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onKeyDown(int keyCode, KeyEvent event) {
|
|
|
|
if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
|
|
|
|
STSSAggGame.exit();
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private STSSAggSdkListener mSdkListener = new STSSAggSdkListener() {
|
|
|
|
// 状态回调(一般只用于日志打印)
|
|
|
|
@Override
|
|
|
|
public void onResult(final int code, final String msg) {
|
|
|
|
super.onResult(code, msg);
|
|
|
|
AggH5Log.e(TAG, "onResult(); code=" + code + ", msg=" + msg);
|
|
|
|
switch (code) {
|
|
|
|
case STSSAggCode.CODE_INIT_SUCCESS:
|
|
|
|
AggH5Log.d(TAG, "onResult 初始化成功");
|
|
|
|
break;
|
|
|
|
case STSSAggCode.CODE_INIT_FAIL:
|
|
|
|
AggH5Log.d(TAG, "onResult 初始化失败");
|
|
|
|
break;
|
|
|
|
case STSSAggCode.CODE_LOGIN_FAIL:
|
|
|
|
AggH5Log.d(TAG, "onResult 登录失败");
|
|
|
|
break;
|
|
|
|
case STSSAggCode.CODE_LOGIN_SUCCESS:
|
|
|
|
AggH5Log.d(TAG, "onResult 登录成功");
|
|
|
|
break;
|
|
|
|
case STSSAggCode.CODE_LOGOUT_SUCCESS:
|
|
|
|
AggH5Log.d(TAG, "onResult 注销成功");
|
|
|
|
break;
|
|
|
|
case STSSAggCode.CODE_EXIT_SUCCESS:
|
|
|
|
AggH5Log.d(TAG, "onResult 退出成功");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 初始化回调
|
|
|
|
@Override
|
|
|
|
public void onInitResult(InitResult result) {
|
|
|
|
super.onInitResult(result);
|
|
|
|
AggH5Log.e(TAG, "onInitResult(); result.extension=" + result.getExtension());
|
|
|
|
if (result.isSDKExit()) {
|
|
|
|
AggH5Log.d(TAG, "onInitResult->有退出框显示");
|
|
|
|
}
|
|
|
|
login();
|
|
|
|
}
|
|
|
|
|
|
|
|
// 登录成功回调
|
|
|
|
@Override
|
|
|
|
public void onAuthResult(final STSSUToken authResult) {
|
|
|
|
super.onAuthResult(authResult);
|
|
|
|
final String msg = "UserID=" + authResult.getGame_uid() + ", Token=" + authResult.getToken();
|
|
|
|
AggH5Log.e(TAG, "onAuthResult(); " + msg);
|
|
|
|
AggH5MainActivity.this.runOnUiThread(new Runnable() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
if (authResult.isSuc()) {
|
|
|
|
currToken = authResult;
|
|
|
|
runOnUiThread(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
String newH5Url = h5game + "?" + ParamsTools.getParams(mContext, currToken);
|
|
|
|
AggH5Log.d(TAG, newH5Url);
|
|
|
|
webView.loadUrl(newH5Url);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
//showToast("获取Token失败");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// 支付回调
|
|
|
|
@Override
|
|
|
|
public void onPayResult(final PayResult result) {
|
|
|
|
super.onPayResult(result);
|
|
|
|
AggH5Log.e(TAG, "onPayResult(); result.productName=" + result.getProductName());
|
|
|
|
AggH5MainActivity.this.runOnUiThread(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
//showToast("支付成功,商品:" + result.getProductName());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// 注销回调
|
|
|
|
@Override
|
|
|
|
public void onLogout() {
|
|
|
|
super.onLogout();
|
|
|
|
currToken = null;
|
|
|
|
AggH5Log.e(TAG, "onLogout();");
|
|
|
|
AggH5MainActivity.this.runOnUiThread(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
//showToast("个人中心退出帐号成功");
|
|
|
|
AggH5Log.d(TAG, "实现游戏的切换或注销帐号的流程,比如回到登陆界面");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// 退出回调
|
|
|
|
@Override
|
|
|
|
public void onExitResult() {
|
|
|
|
super.onExitResult();
|
|
|
|
AggH5MainActivity.this.finish();
|
|
|
|
System.exit(0);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
private void login() {
|
|
|
|
if (!isToLogin) {
|
|
|
|
isToLogin = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (currToken != null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
new Handler().postDelayed(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
runOnUiThread(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
STSSAggGame.login();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}, 1000);
|
|
|
|
}
|
|
|
|
|
|
|
|
@JavascriptInterface
|
|
|
|
public void logout() {
|
|
|
|
STSSAggGame.logout();
|
|
|
|
}
|
|
|
|
|
|
|
|
@JavascriptInterface
|
|
|
|
public void exit() {
|
|
|
|
STSSAggGame.exit();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void onActivityResult(int requestCode, int resultCode, Intent data) {
|
|
|
|
STSSAggGame.onActivityResult(requestCode, resultCode, data);
|
|
|
|
super.onActivityResult(requestCode, resultCode, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void onStart() {
|
|
|
|
STSSAggGame.onStart();
|
|
|
|
super.onStart();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void onPause() {
|
|
|
|
STSSAggGame.onPause();
|
|
|
|
webView.onPause();
|
|
|
|
super.onPause();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void onResume() {
|
|
|
|
STSSAggGame.onResume();
|
|
|
|
webView.onResume();
|
|
|
|
super.onResume();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void onNewIntent(Intent newIntent) {
|
|
|
|
STSSAggGame.onNewIntent(newIntent);
|
|
|
|
super.onNewIntent(newIntent);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void onStop() {
|
|
|
|
STSSAggGame.onStop();
|
|
|
|
super.onStop();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void onDestroy() {
|
|
|
|
STSSAggGame.onDestroy();
|
|
|
|
super.onDestroy();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void onRestart() {
|
|
|
|
STSSAggGame.onRestart();
|
|
|
|
super.onRestart();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void onConfigurationChanged(Configuration newConfig) {
|
|
|
|
super.onConfigurationChanged(newConfig);
|
|
|
|
STSSAggGame.onConfigurationChanged(newConfig);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 如果是unity开发的游戏,需要把onBackPressed和onKeyDown注释掉
|
|
|
|
public void onBackPressed() {
|
|
|
|
AggH5Log.d(TAG, "OnBackPressed.");
|
|
|
|
STSSAggGame.onBackPressed();
|
|
|
|
STSSAggGame.exit();
|
|
|
|
// super.onBackPressed();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
|
|
|
|
STSSAggGame.onRequestPermissionsResult(requestCode, permissions, grantResults);
|
|
|
|
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
|
|
|
|
}
|
|
|
|
} |
|
|
\ No newline at end of file |
...
|
...
|
|