jimu-decompiled/sources/com/ubt/jimu/blockly/command/UnityCommandController.java
2025-05-13 19:24:51 +02:00

535 lines
22 KiB
Java

package com.ubt.jimu.blockly.command;
import android.R;
import android.content.Intent;
import android.media.AudioManager;
import android.text.TextUtils;
import android.util.Log;
import com.ubt.jimu.JimuApplication;
import com.ubt.jimu.base.cache.SharePreferenceHelper;
import com.ubt.jimu.base.entities.Course;
import com.ubt.jimu.blockly.Utils;
import com.ubt.jimu.blockly.command.result.QueryResult;
import com.ubt.jimu.blockly.exception.BlocklyEvent;
import com.ubt.jimu.blockly.exception.JimuRobotMessage;
import com.ubt.jimu.blockly.feature.blockly.JimuSensor;
import com.ubt.jimu.blockly.feature.sensor.DeviceDirection;
import com.ubt.jimu.blockly.feature.sensor.DirectionSensorEventListerCompact;
import com.ubt.jimu.blockly.feature.sensor.ManagerHelper;
import com.ubt.jimu.blockly.feature.sensor.SensorObservable;
import com.ubt.jimu.blockly.view.DialogBlocklyWebView;
import com.ubt.jimu.course.repository.CourseResult;
import com.ubt.jimu.unity.ModelType;
import com.ubt.jimu.unity.bluetooth.ConnectionStatus;
import com.ubt.jimu.unity.bluetooth.UnityActivity;
import com.ubt.jimu.utils.ExternalOverFroyoUtils;
import com.ubt.jimu.utils.JsonHelper;
import com.ubtech.utils.StringUtils;
import com.ubtech.utils.XLog;
import com.ubtrobot.jimu.bluetooth.proxy.BluetoothProxy;
import java.io.File;
import java.util.List;
import org.greenrobot.eventbus.EventBus;
import org.json.JSONException;
import org.json.JSONObject;
/* loaded from: classes.dex */
public class UnityCommandController {
public static final String CONNECTION_BLE_CALLBACK = "ConnectBLECallBack";
public static final String DELETE_PROGRAM = "deleteProgram";
public static final String FRAGMENT_TAG_BLOCKLY = "tag_blockly";
public static final String OPEN_LOGIC_PROGRAMMING = "OpenLogicProgramming";
public static final String REFRESH_ALL_SERVO = "refreshAllServo";
public static final String RESULT_FAILED = "1|failure";
public static final String RESULT_SUCCESS = "1|success";
public static final String SHOW_EXCEPTION_CALLBACK = "JsShowExceptionCallback";
private UnityActivity activity;
AudioManager am;
private DialogBlocklyWebView blockly;
private int code;
private DirectionSensorEventListerCompact compactListener;
private String connenctDevice;
private Course course;
boolean hasMagnetic;
private ManagerHelper managerHelper;
private int moduleType;
private long serverModelId;
private final String TAG = UnityCommandController.class.getSimpleName();
private final String BLE_DISCONNECT_NOTITY = "BLEDisconnectNotity";
private final String JS_SHOW_EXCEPTION = "JsShowException";
private final String COMMON_TIPS = "CommonTips";
private final String CHARGE_PROTECTED = "ChargeProtected";
private final String LOGIC_CMD_RESULT = "LogicCMDResult";
private final String BLUE_SETTING = "OpenAndroidBLESetting";
private final String CANCEL_WAIT_DIALOG = "JsExceptionWaitResult";
private final String SUCCESS = CourseResult.SUCCESS;
private boolean open = false;
private boolean isBlocklyRunning = false;
public UnityCommandController(UnityActivity unityActivity) {
this.activity = unityActivity;
this.managerHelper = new ManagerHelper(this.activity);
this.am = (AudioManager) unityActivity.getSystemService("audio");
}
private void SetSendXTState(int i) {
if (this.connenctDevice != null) {
BluetoothProxy.c().a(this.connenctDevice, i == 1);
}
}
private void bleState(String str) {
}
private void blocklyResult(String str, int i, String str2, String str3) {
DialogBlocklyWebView dialogBlocklyWebView = this.blockly;
if (dialogBlocklyWebView != null) {
dialogBlocklyWebView.wakeupBlockly(str, i, str2, str3);
}
}
private void chargeProtected(String str) {
DialogBlocklyWebView dialogBlocklyWebView = this.blockly;
if (dialogBlocklyWebView != null) {
dialogBlocklyWebView.chargeProtected(str);
}
}
private void commonTips(String str) {
JimuRobotMessage jimuRobotMessage = (JimuRobotMessage) JsonHelper.a(str, (Class<?>) JimuRobotMessage.class);
if (this.blockly == null) {
this.activity.showToast(jimuRobotMessage);
} else {
EventBus.b().b(new BlocklyEvent(1001, jimuRobotMessage));
}
}
private void deleteProgram(String str) {
XLog.a(this.TAG, "删除案例");
}
private String formatResult(String str) {
QueryResult queryResult = (QueryResult) JsonHelper.a(str, (Class<?>) QueryResult.class);
if (queryResult == null) {
return null;
}
List<JimuSensor> infrared = queryResult.getInfrared();
if (infrared != null) {
for (JimuSensor jimuSensor : infrared) {
jimuSensor.setResult(JimuSensor.convertInfraredValueToLevel2(jimuSensor.getResult()));
}
}
String a = JsonHelper.a(queryResult);
SensorObservable.getInstance().setData(queryResult);
return a;
}
private void initCompactSensorEventListener() {
if (this.compactListener == null) {
this.compactListener = new DirectionSensorEventListerCompact(new DirectionSensorEventListerCompact.ActionListener() { // from class: com.ubt.jimu.blockly.command.UnityCommandController.3
@Override // com.ubt.jimu.blockly.feature.sensor.DirectionSensorEventListerCompact.ActionListener
public void onBottomUp() {
if (UnityCommandController.this.blockly != null) {
UnityCommandController.this.blockly.refreshDirection(DeviceDirection.DOWN);
}
}
@Override // com.ubt.jimu.blockly.feature.sensor.DirectionSensorEventListerCompact.ActionListener
public void onChange(float[] fArr) {
}
@Override // com.ubt.jimu.blockly.feature.sensor.DirectionSensorEventListerCompact.ActionListener
public void onLeftUp() {
if (UnityCommandController.this.blockly != null) {
UnityCommandController.this.blockly.refreshDirection(DeviceDirection.LEFT);
}
}
@Override // com.ubt.jimu.blockly.feature.sensor.DirectionSensorEventListerCompact.ActionListener
public void onNormal() {
if (UnityCommandController.this.blockly != null) {
UnityCommandController.this.blockly.refreshDirection(DeviceDirection.NONE);
}
}
@Override // com.ubt.jimu.blockly.feature.sensor.DirectionSensorEventListerCompact.ActionListener
public void onRightUp() {
if (UnityCommandController.this.blockly != null) {
UnityCommandController.this.blockly.refreshDirection(DeviceDirection.RIGHT);
}
}
@Override // com.ubt.jimu.blockly.feature.sensor.DirectionSensorEventListerCompact.ActionListener
public void onShake(long j, float[] fArr) {
if (UnityCommandController.this.blockly != null) {
UnityCommandController.this.blockly.refreshDirection(DeviceDirection.SWING);
}
}
@Override // com.ubt.jimu.blockly.feature.sensor.DirectionSensorEventListerCompact.ActionListener
public void onTopUp() {
if (UnityCommandController.this.blockly != null) {
UnityCommandController.this.blockly.refreshDirection(DeviceDirection.UP);
}
}
});
}
this.managerHelper.registerListener(this.compactListener, 1, 3);
}
private void notifyBleState(String str) {
DialogBlocklyWebView dialogBlocklyWebView = this.blockly;
if (dialogBlocklyWebView != null) {
dialogBlocklyWebView.blueConnectCallBack(str);
}
}
/* JADX WARN: Multi-variable type inference failed */
private void onBlocklyResult(String str) {
try {
JSONObject jSONObject = new JSONObject(str);
String string = jSONObject.getString(UnityActivity.RESULT);
String replace = jSONObject.getString("cmd").replace(BlocklyCommandController.BASE_REQUEST_URL, "");
String[] split = replace.split("\\|");
int i = 1;
i = 1;
JSONObject jSONObject2 = new JSONObject(split[split.length - 1]);
if (BlocklyCommandController.QUERY.equals(split[0])) {
JSONObject jSONObject3 = new JSONObject(string);
String string2 = jSONObject3.has("state") ? jSONObject3.getString("state") : "";
string = (jSONObject3.has(UnityActivity.RESULT) ? jSONObject3.getInt(UnityActivity.RESULT) : 1) == 0 ? RESULT_FAILED : RESULT_SUCCESS;
i = CourseResult.SUCCESS.equals(string2);
} else if ("queryRobotStatus".equals(split[0])) {
JSONObject jSONObject4 = new JSONObject(string);
if (jSONObject4.has("state")) {
i = jSONObject4.optInt("state");
}
} else if (TextUtils.isEmpty(string) || !string.contains(CourseResult.SUCCESS)) {
i = 0;
}
blocklyResult(jSONObject2.getString("branchId"), i, replace, string);
} catch (JSONException e) {
e.printStackTrace();
} catch (Exception e2) {
e2.printStackTrace();
}
}
private void onDirectionResult(JSONObject jSONObject, String[] strArr) {
try {
JSONObject jSONObject2 = new JSONObject();
int i = 0;
if (!this.compactListener.directionNotChange()) {
String string = new JSONObject(strArr[1]).getString("direction");
if (DeviceDirection.SWING.getValue().equals(string)) {
if (!this.compactListener.isShaking()) {
}
i = 1;
} else {
DeviceDirection deviceDirection = this.compactListener.getDeviceDirection();
if (deviceDirection != null) {
if (!deviceDirection.getValue().equals(string)) {
}
i = 1;
}
}
}
jSONObject2.put("state", CourseResult.SUCCESS);
jSONObject2.put(UnityActivity.RESULT, i);
jSONObject.put(UnityActivity.RESULT, jSONObject2.toString());
onBlocklyResult(jSONObject.toString());
} catch (JSONException e) {
e.printStackTrace();
}
}
private void onInfraredTimerResult(JSONObject jSONObject, String str) {
try {
if (jSONObject.has(UnityActivity.RESULT)) {
String string = jSONObject.getString(UnityActivity.RESULT);
if (TextUtils.isEmpty(string)) {
return;
}
String[] split = string.split("\\|");
if (split.length <= 1 || !CourseResult.SUCCESS.equals(split[0])) {
return;
}
this.blockly.showInfraredRef(formatResult(split[1]), str);
}
} catch (JSONException e) {
e.printStackTrace();
Log.e(this.TAG, e.getMessage());
}
}
private void onLogicCMDResult(String str) {
try {
JSONObject jSONObject = new JSONObject(str);
String string = jSONObject.getString(UnityActivity.RESULT);
String[] split = jSONObject.getString("cmd").replace(BlocklyCommandController.BASE_REQUEST_URL, "").split("\\|");
if ("failure".equals(string) && !BlocklyCommandController.QUERY_ALL_SENSOR.equals(split[0])) {
onBlocklyResult(str);
return;
}
if (split[0].equals(BlocklyCommandController.QUERY) && split[1].contains("direction")) {
onDirectionResult(jSONObject, split);
} else if (BlocklyCommandController.QUERY_ALL_SENSOR.equals(split[0])) {
onInfraredTimerResult(jSONObject, split.length >= 2 ? split[2] : "");
} else {
onBlocklyResult(str);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
/* JADX INFO: Access modifiers changed from: private */
public synchronized void open(String str) {
if (this.isBlocklyRunning) {
return;
}
int i = new SharePreferenceHelper().getInt(SharePreferenceHelper.SP_KEY_CONNECTION_STATUS, ConnectionStatus.DIS_CONNECT.getStatus());
if (this.blockly == null) {
this.blockly = DialogBlocklyWebView.getInstance(str, i, this.serverModelId, this.moduleType, this.code, this.activity.size, this.course);
register();
}
if (this.activity.getFragmentManager().findFragmentByTag(FRAGMENT_TAG_BLOCKLY) == null) {
if (this.blockly.isAdded()) {
XLog.a(this.TAG, "show blockly");
this.activity.getFragmentManager().beginTransaction().show(this.blockly).commit();
} else {
XLog.a(this.TAG, "add blockly");
this.activity.getFragmentManager().beginTransaction().add(R.id.content, this.blockly, FRAGMENT_TAG_BLOCKLY).commit();
}
} else if (TextUtils.isEmpty(str)) {
UnityActivity.communityWithUnity(this.activity, JimuBlocklyCommand.EXIT_LOGIC_VIEW, "");
return;
} else {
XLog.a(this.TAG, "show blockly");
this.blockly.updateJimuRobot(str);
this.activity.getFragmentManager().beginTransaction().show(this.blockly).commit();
}
this.isBlocklyRunning = true;
}
private synchronized void openBlockly(final String str) {
if (new File((ExternalOverFroyoUtils.a(JimuApplication.l(), (ModelType) null) + "data/blockly/") + Utils.BLOCKLY_NEW + File.separator + "teachMaterial/index.html").exists()) {
open(str);
} else {
Utils.unZipBlockly(new Utils.IUnzipBlockly() { // from class: com.ubt.jimu.blockly.command.UnityCommandController.1
@Override // com.ubt.jimu.blockly.Utils.IUnzipBlockly
public void unZipFailed() {
XLog.b(UnityCommandController.this.TAG, "openBlockly unZipFailed 无法解压Blockly到指定目录");
UnityActivity.communityWithUnity(UnityCommandController.this.activity, JimuBlocklyCommand.EXIT_LOGIC_VIEW, "");
}
@Override // com.ubt.jimu.blockly.Utils.IUnzipBlockly
public void unZipSuccessed() {
UnityCommandController.this.open(str);
}
});
}
this.am.requestAudioFocus(new AudioManager.OnAudioFocusChangeListener() { // from class: com.ubt.jimu.blockly.command.UnityCommandController.2
@Override // android.media.AudioManager.OnAudioFocusChangeListener
public void onAudioFocusChange(int i) {
XLog.a(UnityCommandController.this.TAG, "audiomanager focusChange.");
}
}, 3, 2);
}
private void register() {
initCompactSensorEventListener();
}
private void showException(String str) {
XLog.b(this.TAG, "showException:" + str);
if (this.blockly != null) {
EventBus.b().b(new BlocklyEvent(1002, str));
}
}
private void unRegister() {
this.managerHelper.unRegisterListener(this.compactListener);
}
public void blocklyCallUnity(String str) {
try {
JSONObject jSONObject = new JSONObject(str);
String string = jSONObject.has("funcName") ? jSONObject.getString("funcName") : null;
if (JimuBlocklyCommand.EXIT_LOGIC_VIEW.equals(string)) {
this.open = false;
closeBlockly();
unRegister();
this.activity.setIsBack(true);
return;
}
if (JimuBlocklyCommand.CONNECT_BLE.equals(string)) {
hideBlockly();
return;
}
if (JimuBlocklyCommand.BLE_STATE.equals(string)) {
if (TextUtils.isEmpty(this.connenctDevice)) {
bleState("0");
return;
} else {
bleState("1");
return;
}
}
if (JimuBlocklyCommand.SET_SEND_XT_STATE.equals(string)) {
SetSendXTState(Integer.parseInt(jSONObject.getString("arg")));
} else if ("setServoMode".equals(string)) {
hideBlockly();
}
} catch (Exception e) {
Log.e(this.TAG, e.getMessage());
e.printStackTrace();
}
}
public void closeBlockly() {
XLog.a(this.TAG, "close blockly");
this.isBlocklyRunning = false;
this.open = false;
if (this.blockly != null) {
synchronized (this) {
if (this.blockly != null) {
this.blockly.stopQueryTimer();
this.activity.getFragmentManager().beginTransaction().remove(this.blockly).commit();
this.blockly = null;
XLog.a(this.TAG, "销毁 DilogBlocklyWebview");
}
}
}
this.am.abandonAudioFocus(null);
}
public synchronized void hideBlockly() {
if (this.blockly != null) {
this.isBlocklyRunning = false;
this.activity.getFragmentManager().beginTransaction().hide(this.blockly).commit();
XLog.a(this.TAG, "隐藏 DilogBlocklyWebview");
}
}
public void loadJavascript(String str, String... strArr) {
DialogBlocklyWebView dialogBlocklyWebView = this.blockly;
if (dialogBlocklyWebView == null) {
return;
}
dialogBlocklyWebView.setJSResult(str, strArr);
}
public void notifyConnectionState(String str) {
if (this.blockly != null && StringUtils.f(str)) {
this.blockly.setJSResult("BLEStateChanged", str);
}
}
public void notifyKeyboardHidden(String str) {
DialogBlocklyWebView dialogBlocklyWebView = this.blockly;
if (dialogBlocklyWebView == null || !this.isBlocklyRunning) {
return;
}
dialogBlocklyWebView.notifyKeyboardHidden(str);
}
public boolean onUnityCallback(String str, String str2) {
if ("CommonTips".equals(str)) {
commonTips(str2);
}
if (!this.open && !OPEN_LOGIC_PROGRAMMING.equals(str)) {
return false;
}
if (OPEN_LOGIC_PROGRAMMING.equals(str)) {
this.open = true;
openBlockly(str2);
} else if ("ConnectBLECallBack".equals(str)) {
openBlockly(str2);
notifyBleState(str2);
} else if ("BLEDisconnectNotity".equals(str)) {
notifyBleState("{\"blueState\":\"3\"}");
notifyConnectionState("3");
EventBus.b().b(new BlocklyEvent(1003, str2));
} else if ("JsShowException".equals(str)) {
showException(str2);
} else if ("ChargeProtected".equals(str)) {
chargeProtected(str2);
} else if ("LogicCMDResult".equals(str)) {
onLogicCMDResult(str2);
} else if ("JsExceptionWaitResult".equals(str)) {
this.blockly.hideWait();
} else if ("OpenAndroidBLESetting".equals(str)) {
this.activity.startActivity(new Intent("android.settings.BLUETOOTH_SETTINGS"));
} else if ("refreshAllServo".equals(str)) {
openRefreshAllServo(str2);
} else {
if (!"deleteProgram".equals(str)) {
return false;
}
deleteProgram(str2);
}
return true;
}
public void openRefreshAllServo(String str) {
this.activity.getFragmentManager().beginTransaction().show(this.blockly).commit();
this.blockly.refreshAllServo(str);
try {
JSONObject jSONObject = new JSONObject(str);
this.blockly.refreshAllServo(jSONObject.has("commonServo") ? jSONObject.getString("commonServo") : "", jSONObject.has("circleServo") ? jSONObject.getString("circleServo") : "");
} catch (Exception e) {
e.printStackTrace();
}
}
public void setCode(int i) {
this.code = i;
}
public void setConnenctDevice(String str) {
try {
this.connenctDevice = str;
int status = (TextUtils.isEmpty(this.connenctDevice) ? ConnectionStatus.DIS_CONNECT : ConnectionStatus.CONNECTED).getStatus();
if (this.blockly != null) {
this.blockly.setBlueState(status);
}
} catch (Exception e) {
Log.e(this.TAG, e.getMessage());
e.printStackTrace();
}
}
public void setCourse(Course course) {
this.course = course;
}
public void setModuleType(int i) {
this.moduleType = i;
}
public void setServerModelId(long j) {
this.serverModelId = j;
}
public synchronized void showBlockly() {
if (this.blockly != null) {
this.activity.getFragmentManager().beginTransaction().show(this.blockly).commit();
}
}
public void stopBlockly() {
DialogBlocklyWebView dialogBlocklyWebView = this.blockly;
if (dialogBlocklyWebView == null) {
return;
}
dialogBlocklyWebView.jsShowException("EnterBackground");
}
public void updateBluetoothState(String str) {
if (this.blockly != null && TextUtils.isDigitsOnly(str)) {
this.blockly.setBlueState(Integer.valueOf(str).intValue());
}
}
}