270 lines
10 KiB
Java
Executable File
270 lines
10 KiB
Java
Executable File
package com.hnjc.wjtx;
|
|
|
|
import android.app.Activity;
|
|
import android.app.AlertDialog;
|
|
import android.content.Context;
|
|
import android.content.SharedPreferences;
|
|
import android.content.res.Resources;
|
|
import android.graphics.drawable.Drawable;
|
|
import android.os.Build;
|
|
import android.os.Bundle;
|
|
import android.os.VibrationEffect;
|
|
import android.os.Vibrator;
|
|
import android.util.Log;
|
|
import android.view.KeyEvent;
|
|
import android.widget.FrameLayout;
|
|
import android.widget.ImageView;
|
|
import android.widget.Toast;
|
|
|
|
import com.hnjc.wjtx.util.AssetsUtil;
|
|
import com.hnjc.wjtx.util.IdUtil;
|
|
import com.hnjc.wjtx.util.StorageUtil;
|
|
import com.hnjc.wjtx.util.StringUtil;
|
|
|
|
import org.egret.egretnativeandroid.EgretNativeAndroid;
|
|
import org.json.JSONException;
|
|
import org.json.JSONObject;
|
|
|
|
//Android项目发布设置详见doc目录下的README_ANDROID.md
|
|
|
|
public class MainActivity extends Activity {
|
|
private final String TAG = "MainActivity";
|
|
private EgretNativeAndroid nativeAndroid;
|
|
private ImageView launchScreenImageView = null;
|
|
private FrameLayout rootLayout = null;
|
|
private Vibrator vibrator;
|
|
|
|
|
|
@Override
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
super.onCreate(savedInstanceState);
|
|
|
|
String preloadPath = AssetsUtil.getDiskFileDir(this, this.getString(R.string.preload_path));
|
|
String gameUrl = this.getString(R.string.game_url);
|
|
|
|
nativeAndroid = new EgretNativeAndroid(this);
|
|
if (!nativeAndroid.checkGlEsVersion()) {
|
|
Toast.makeText(this, "This device does not support OpenGL ES 2.0.",
|
|
Toast.LENGTH_LONG).show();
|
|
return;
|
|
}
|
|
|
|
nativeAndroid.config.showFPS = false;
|
|
nativeAndroid.config.fpsLogTime = 30;
|
|
nativeAndroid.config.disableNativeRender = false;
|
|
nativeAndroid.config.clearCache = false;
|
|
nativeAndroid.config.loadingTimeout = 0;
|
|
nativeAndroid.config.immersiveMode = true;
|
|
nativeAndroid.config.useCutout = true;
|
|
nativeAndroid.config.preloadPath = preloadPath;
|
|
|
|
setExternalInterfaces();
|
|
|
|
if (!nativeAndroid.initialize(gameUrl)) {
|
|
Toast.makeText(this, "Initialize native failed.",
|
|
Toast.LENGTH_LONG).show();
|
|
return;
|
|
}
|
|
setContentView(nativeAndroid.getRootFrameLayout());
|
|
rootLayout = nativeAndroid.getRootFrameLayout();
|
|
showLoadingView();
|
|
vibrator = (Vibrator) this.getSystemService(Context.VIBRATOR_SERVICE);
|
|
}
|
|
|
|
@Override
|
|
protected void onPause() {
|
|
super.onPause();
|
|
nativeAndroid.pause();
|
|
}
|
|
|
|
@Override
|
|
protected void onResume() {
|
|
super.onResume();
|
|
nativeAndroid.resume();
|
|
}
|
|
|
|
@Override
|
|
public boolean onKeyDown(final int keyCode, final KeyEvent keyEvent) {
|
|
if (keyCode == KeyEvent.KEYCODE_BACK) {
|
|
//声明并初始化弹出对象
|
|
AlertDialog.Builder builder=new AlertDialog.Builder(this);
|
|
builder.setTitle("提示:");
|
|
builder.setMessage("是否退出");
|
|
//设置确认按钮
|
|
builder.setNegativeButton("确定", (dialog, which) -> {
|
|
nativeAndroid.exitGame();
|
|
finish();//退出程序
|
|
});
|
|
//设置取消按钮
|
|
builder.setPositiveButton("取消",null);
|
|
//显示弹框
|
|
builder.show();
|
|
return true;
|
|
} else {
|
|
return super.onKeyDown(keyCode, keyEvent);
|
|
}
|
|
|
|
}
|
|
|
|
private void setExternalInterfaces() {
|
|
nativeAndroid.setExternalInterface("sendToNative", message -> {
|
|
Log.d(TAG, "Get message: " + message);
|
|
nativeAndroid.callExternalInterface("sendToJS", "Get message: " + message);
|
|
});
|
|
nativeAndroid.setExternalInterface("removeNativeLoading", message -> {
|
|
Log.d(TAG, "removeNativeLoading: " + message);
|
|
hideLoadingView();
|
|
});
|
|
nativeAndroid.setExternalInterface("setLocalStorage", message -> {
|
|
Log.d(TAG, "setLocalStorage: " + message);
|
|
try{
|
|
JSONObject jsonObject = new JSONObject(message);
|
|
String key = jsonObject.getString("key");
|
|
String val = jsonObject.getString("val");
|
|
SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE);
|
|
StorageUtil.writeString(sharedPref, key, val);
|
|
}
|
|
catch (JSONException e) {
|
|
Log.e(TAG, " onState message failed to analyze");
|
|
}
|
|
|
|
|
|
});
|
|
nativeAndroid.setExternalInterface("getLocalStorage", message -> {
|
|
Log.d(TAG, "getLocalStorage: " + message);
|
|
SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE);
|
|
String val = StorageUtil.readString(sharedPref, message);
|
|
nativeAndroid.callExternalInterface("getLocalStorage", val);
|
|
});
|
|
nativeAndroid.setExternalInterface("removeLocalStorage", message -> {
|
|
Log.d(TAG, "removeLocalStorage: " + message);
|
|
SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE);
|
|
StorageUtil.removeString(sharedPref, message);
|
|
});
|
|
/**
|
|
* 震动须添加权限
|
|
* <uses-permission android:name="android.permission.VIBRATE"/>
|
|
* @param type {int} 震动类型 0: 短震动, 1: 长震动
|
|
* */
|
|
nativeAndroid.setExternalInterface("vibrate", message -> {
|
|
Log.d(TAG, "vibrate: " + message);
|
|
message = StringUtil.isBlank(message) ? "0" : message;
|
|
int type = Integer.getInteger(message);
|
|
long time = type == 0 ? 15 : 500;
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
|
vibrator.vibrate(VibrationEffect.createOneShot(time, VibrationEffect.DEFAULT_AMPLITUDE));
|
|
} else {
|
|
//deprecated in API 26
|
|
vibrator.vibrate(time);
|
|
}
|
|
});
|
|
nativeAndroid.setExternalInterface("showToast", message -> {
|
|
Log.d(TAG, "showToast: " + message);
|
|
Toast.makeText(MainActivity.this, message, Toast.LENGTH_LONG).show();
|
|
});
|
|
nativeAndroid.setExternalInterface("getUid", message -> {
|
|
//TODO: 平台的登陆
|
|
JSONObject obj = new JSONObject();
|
|
try {
|
|
obj.put("openid", IdUtil.getUid(this));
|
|
obj.put("token", "");
|
|
// 如果其他登陆过程中的错误, 则传入一个大于0的errorcode
|
|
obj.put("errorcode", 1);
|
|
nativeAndroid.callExternalInterface("sendUidToJS", obj.toString());
|
|
} catch (JSONException e) {
|
|
e.printStackTrace();
|
|
nativeAndroid.callExternalInterface("loginCancel", "");
|
|
}
|
|
|
|
});
|
|
nativeAndroid.setExternalInterface("@onState", message -> {
|
|
Log.e(TAG, "Get @onState: " + message);
|
|
try{
|
|
JSONObject jsonObject = new JSONObject(message);
|
|
String state = jsonObject.getString("state");
|
|
handleStateEvent(state);
|
|
}
|
|
catch (JSONException e) {
|
|
Log.e(TAG, " onState message failed to analyze");
|
|
}
|
|
|
|
});
|
|
nativeAndroid.setExternalInterface("@onError", message -> Log.e(TAG, "Get @onError: " + message));
|
|
// 支付
|
|
nativeAndroid.setExternalInterface("pay", message -> {
|
|
|
|
try {
|
|
JSONObject jsonObject = new JSONObject(message);
|
|
String cpOrderId = jsonObject.getString("orderId");
|
|
String productCode = jsonObject.getString("productCode");
|
|
int count = jsonObject.getInt("count");
|
|
//TODO: 平台的支付
|
|
JSONObject obj = new JSONObject();
|
|
try {
|
|
obj.put("errcode", 0);
|
|
obj.put("errmsg", "");
|
|
nativeAndroid.callExternalInterface("payResult", obj.toString());
|
|
} catch (JSONException e) {
|
|
e.printStackTrace();
|
|
}
|
|
} catch (JSONException e) {
|
|
e.printStackTrace();
|
|
} catch ( Exception e ) {
|
|
e.printStackTrace();
|
|
}
|
|
});
|
|
/**
|
|
* isCreateRole: isNew,
|
|
* roleCreateTime: Date.now(),
|
|
* serverId: giant.Core.serverVO.id,
|
|
* serverName: giant.Core.serverVO.name,
|
|
* userRoleId: giant.Core.hero.id,
|
|
* userRoleName: giant.Core.hero.name,
|
|
* userRoleBalance: giant.Core.hero.coin,
|
|
* vipLevel: giant.Core.hero.vipLevel,
|
|
* userRoleLevel: giant.Core.hero.level,
|
|
* partyId: 0,
|
|
* partyName: giant.Core.hero.camp,
|
|
* gameRoleGender: giant.Core.hero.gender,
|
|
* gameRolePower: giant.Core.hero.power,
|
|
*/
|
|
nativeAndroid.setExternalInterface("reportRoleInfo", message -> {
|
|
Log.i(TAG, "Get reportRoleInfo: " + message);
|
|
});
|
|
}
|
|
|
|
@Override
|
|
protected void onDestroy() {
|
|
super.onDestroy();
|
|
}
|
|
|
|
private void showLoadingView() {
|
|
launchScreenImageView = new ImageView(this);
|
|
launchScreenImageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
|
|
Resources res = getResources();
|
|
Drawable drawable = res.getDrawable(R.drawable.m_loading);
|
|
launchScreenImageView.setImageDrawable(drawable);
|
|
|
|
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,
|
|
FrameLayout.LayoutParams.MATCH_PARENT);
|
|
rootLayout.addView(launchScreenImageView, params);
|
|
}
|
|
private void handleStateEvent(String state) {
|
|
switch (state) {
|
|
case "running":
|
|
// hideLoadingView();
|
|
return;
|
|
default:
|
|
Log.i(TAG, state);
|
|
}
|
|
}
|
|
private void hideLoadingView() {
|
|
rootLayout.removeView(launchScreenImageView);
|
|
Drawable drawable = launchScreenImageView.getDrawable();
|
|
launchScreenImageView.setImageDrawable(null);
|
|
drawable.setCallback(null);
|
|
launchScreenImageView = null;
|
|
}
|
|
|
|
}
|