package com.ubt.jimu.blockly.command; import android.text.TextUtils; import android.util.Log; import android.webkit.WebView; import com.alibaba.android.arouter.facade.Postcard; import com.alibaba.android.arouter.launcher.ARouter; import com.google.gson.reflect.TypeToken; import com.ubt.jimu.base.cache.Cache; import com.ubt.jimu.base.db.diy.DiyDBModel; import com.ubt.jimu.base.db.diy.DiyHelper; import com.ubt.jimu.base.entities.Robot; import com.ubt.jimu.blockly.JimuRobot; import com.ubt.jimu.blockly.Utils; import com.ubt.jimu.blockly.bean.BlocklyProject; import com.ubt.jimu.blockly.command.result.JavaResultBean; import com.ubt.jimu.blockly.command.result.SaveBean; import com.ubt.jimu.blockly.command.result.XmlListBean; import com.ubt.jimu.blockly.exception.BlocklyEvent; import com.ubt.jimu.course.repository.JimuCourse; import com.ubt.jimu.course.view.JimuTaskListActivity; import com.ubt.jimu.discover.view.CourseListActivity; import com.ubt.jimu.diy.view.DiyBrowseActivity; import com.ubt.jimu.diy.view.DiyFanActivity; import com.ubt.jimu.unity.bluetooth.UnityActivity; import com.ubt.jimu.utils.JsonHelper; import com.ubt.jimu.utils.LogUtils; import com.ubt.jimu.utils.SPUtils; import com.ubtech.utils.FileHelper; import com.ubtrobot.log.ALog; import java.io.File; import java.io.UnsupportedEncodingException; import java.lang.reflect.Type; import java.net.URLDecoder; import java.net.URLEncoder; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Timer; import java.util.TimerTask; import org.greenrobot.eventbus.EventBus; import org.json.JSONException; import org.json.JSONObject; /* loaded from: classes.dex */ public class JimuBlocklyCommand { public static final String BLE_STATE = "BLEState"; public static final String CONNECT_BLE = "ConnectBLE"; public static final String EXIT_LOGIC_VIEW = "ExitLogicView"; public static final String LOGIC_CMD = "LogicCMD"; public static final String SET_SEND_XT_STATE = "setSendXTState"; public static final String SET_SERVO_MODE = "setServoMode"; public static final String WAKEUP_BLOCKLY = "continueSteps"; private UnityActivity context; private TimerTask infraredTask; private JimuRobot jimuRobot; private int moduleType; private String queryCallback; private String queryUrl; private Timer timer; private WebView webView; private final String BLE_CONNECT_CALLBACK = JimuUnityCommand.BLE_CONNECT_CALLBACK; protected final long INFRARED_INTERVAL = 200; protected String tag = JimuBlocklyCommand.class.getSimpleName(); private ArrayList projects = new ArrayList<>(); private class InfraredTimerTask extends TimerTask { private String url; public InfraredTimerTask(String str) { this.url = str; } @Override // java.util.TimerTask, java.lang.Runnable public void run() { JimuBlocklyCommand.this.unityComm(this.url); } } public JimuBlocklyCommand(WebView webView, UnityActivity unityActivity, JimuRobot jimuRobot) { this.context = unityActivity; this.webView = webView; this.jimuRobot = jimuRobot; } private boolean isNameExist(String str, String str2, String str3) { JavaResultBean javaResultBean = new JavaResultBean<>(); if (TextUtils.isEmpty(str)) { javaResultBean.retCode = JavaResultBean.RESULT_EXCEPTION; javaResultBean.retMsg = ""; setJSResult(javaResultBean, str3, true); return true; } String b = FileHelper.b(this.jimuRobot.getBlocklyProject("blockly") + this.jimuRobot.getModelId() + ".config", (String) null); if (!TextUtils.isEmpty(b)) { ArrayList arrayList = (ArrayList) JsonHelper.a(b, new TypeToken>() { // from class: com.ubt.jimu.blockly.command.JimuBlocklyCommand.3 }.getType()); if (arrayList == null) { arrayList = new ArrayList(); } if (arrayList.size() > 0) { Iterator it = arrayList.iterator(); while (it.hasNext()) { BlocklyProject blocklyProject = (BlocklyProject) it.next(); if (blocklyProject.getXmlName().equals(str) && !blocklyProject.getXmlId().equals(str2)) { break; } } } } return false; } private String writeConfigFile(String str, String str2, String str3, String str4) { String str5 = this.jimuRobot.getBlocklyProject("blockly") + this.jimuRobot.getModelId() + ".config"; BlocklyProject blocklyProject = null; String b = FileHelper.b(str5, (String) null); boolean isEmpty = TextUtils.isEmpty(b); ArrayList arrayList = (ArrayList) JsonHelper.a(b, new TypeToken>() { // from class: com.ubt.jimu.blockly.command.JimuBlocklyCommand.4 }.getType()); if (arrayList == null) { arrayList = new ArrayList(); } if (TextUtils.isEmpty(str3)) { str3 = System.currentTimeMillis() + ""; File file = new File(this.jimuRobot.getBlocklyProject("blockly")); if (!file.exists()) { file.mkdirs(); } BlocklyProject blocklyProject2 = new BlocklyProject(); blocklyProject2.setXmlId(str3); blocklyProject2.setXmlName(str); blocklyProject2.setBlocklyVersion(str4); blocklyProject2.setIsDefault(false); arrayList.add(0, blocklyProject2); this.projects.add(0, blocklyProject2); } else { Iterator it = arrayList.iterator(); while (true) { if (!it.hasNext()) { break; } BlocklyProject blocklyProject3 = (BlocklyProject) it.next(); if (blocklyProject3.getXmlId().equals(str3)) { blocklyProject3.setXmlName(str); blocklyProject = blocklyProject3; break; } } if (blocklyProject != null) { arrayList.remove(blocklyProject); arrayList.add(0, blocklyProject); } } FileHelper.a(str5, JsonHelper.a(arrayList), "utf-8"); if (isEmpty) { try { if (!TextUtils.isEmpty(this.jimuRobot.getModelType())) { Integer.parseInt(this.jimuRobot.getModelType()); } } catch (NumberFormatException e) { e.printStackTrace(); Log.e(this.tag, "保存文件:" + e.getMessage()); } } return str3; } public void bleState() { UnityActivity.communityWithUnity(this.context, BLE_STATE, ""); } public void closeWindow() { UnityActivity unityActivity; UnityActivity.communityWithUnity(this.context, EXIT_LOGIC_VIEW, ""); if (8194 == this.context.getBlocklyType()) { JimuCourse jimuCourse = Cache.getInstance().getJimuCourse(); Robot robot = Cache.getInstance().getRobot(); if (robot != null && jimuCourse != null && (unityActivity = this.context) != null) { JimuTaskListActivity.start(unityActivity, Cache.getInstance().getPackageId(), robot.getModelId(), jimuCourse.getId()); } } else if (8193 == this.context.getBlocklyType()) { LogUtils.c("返回首页"); ARouter.b().a("/page/main").t(); } else if (8197 == this.context.getBlocklyType()) { CourseListActivity.start(this.context, "AstroBot"); } else if (8195 == this.context.getBlocklyType()) { DiyFanActivity.start(this.context, DiyFanActivity.uuid); } else if (8196 == this.context.getBlocklyType() || 8192 == this.context.getBlocklyType()) { SPUtils.b(String.format(DiyBrowseActivity.DIYSTEP, this.context.getModelID()), -2); ARouter.b().a("/diy/detail").t(); } else { ARouter.b().a("/page/main").t(); } stopSensorTimer(""); } public void confirm(String str) { String[] split; FileHelper.a(Utils.baseUrl + "status.txt", "", (String) null); if (TextUtils.isEmpty(str) || (split = str.split("\\|")) == null || split.length < 2) { return; } try { Utils.setJSResult(this.webView, "continueSteps", URLEncoder.encode(new JSONObject(split[1]).optString("branchId"), "utf-8"), "1"); } catch (JSONException e) { e.printStackTrace(); } catch (Exception e2) { e2.printStackTrace(); } } public void connectedBLE() { UnityActivity.communityWithUnity(this.context, CONNECT_BLE, ""); } public void deleteXml(String str, String str2) { boolean z; ALog.a(this.tag).d("deleteXml"); Iterator it = this.projects.iterator(); while (true) { if (!it.hasNext()) { z = false; break; } BlocklyProject next = it.next(); if (next.getXmlId().equals(str)) { z = next.getIsDefault(); break; } } JavaResultBean javaResultBean = new JavaResultBean<>(); if (z) { javaResultBean.retCode = JavaResultBean.RESULT_FAILED; javaResultBean.retMsg = "can not delete sample project."; } else { File file = new File(this.jimuRobot.getBlocklyProject("blockly") + str + ".xml"); if (file.exists()) { file.delete(); javaResultBean.retCode = "0000"; } else { javaResultBean.retCode = "0000"; javaResultBean.retMsg = "file not exit"; } String str3 = this.jimuRobot.getBlocklyProject("blockly") + this.jimuRobot.getModelId() + ".config"; ArrayList arrayList = (ArrayList) JsonHelper.a(FileHelper.b(str3, (String) null), new TypeToken>() { // from class: com.ubt.jimu.blockly.command.JimuBlocklyCommand.5 }.getType()); if (arrayList == null) { arrayList = new ArrayList(); } Iterator it2 = arrayList.iterator(); while (true) { if (!it2.hasNext()) { break; } BlocklyProject blocklyProject = (BlocklyProject) it2.next(); if (blocklyProject.getXmlId().equals(str)) { arrayList.remove(blocklyProject); break; } } FileHelper.a(str3, JsonHelper.a(arrayList), "utf-8"); } setJSResult(javaResultBean, str2, false); } public JimuRobot getJimuRobot() { return this.jimuRobot; } public int getModuleType() { return this.moduleType; } public String getQueryCallback() { return this.queryCallback; } public String getQueryUrl() { return this.queryUrl; } public void heartBeat(String str) { UnityActivity.communityWithUnity(this.context, SET_SEND_XT_STATE, str); } public void notifyBLEState(String str) { EventBus.b().b(new BlocklyEvent(1006, str)); setJSResult(JimuUnityCommand.BLE_CONNECT_CALLBACK, str, true); } public void queryInfrared(String str, String str2) { unityComm(str); } /* JADX WARN: Type inference failed for: r1v4, types: [T, com.ubt.jimu.blockly.command.result.XmlListBean] */ public void readXml(String str, String str2) { BlocklyProject blocklyProject; String str3; ALog.a(this.tag).d("readXml"); Iterator it = this.projects.iterator(); while (true) { if (!it.hasNext()) { blocklyProject = null; break; } else { blocklyProject = it.next(); if (blocklyProject.getXmlId().equals(str)) { break; } } } JavaResultBean javaResultBean = new JavaResultBean<>(); if (blocklyProject != null) { if (blocklyProject.getIsDefault()) { str3 = this.jimuRobot.getSamplePath() + str + ".xml"; } else { str3 = this.jimuRobot.getBlocklyProject("blockly") + str + ".xml"; } javaResultBean.retCode = "0000"; } else { javaResultBean.retCode = JavaResultBean.RESULT_FAILED; javaResultBean.retMsg = "xmlId not in projects"; str3 = ""; } javaResultBean.result = new XmlListBean(str3); setJSResult(javaResultBean, str2, true); } /* JADX WARN: Type inference failed for: r4v3, types: [T, com.ubt.jimu.blockly.command.result.SaveBean] */ public void saveXml(String str, String str2, String str3, String str4, String str5) { ALog.a(this.tag).d("saveXml"); if (TextUtils.isEmpty(str3)) { isNameExist(str, str3, str5); } String writeConfigFile = writeConfigFile(str, str2, str3, str4); JavaResultBean javaResultBean = new JavaResultBean<>(); if (FileHelper.a(this.jimuRobot.getBlocklyProject("blockly") + writeConfigFile + ".xml", str2, (String) null)) { javaResultBean.retCode = "0000"; } else { javaResultBean.retCode = JavaResultBean.RESULT_FAILED; } ?? saveBean = new SaveBean(); saveBean.xmlId = writeConfigFile; javaResultBean.result = saveBean; setJSResult(javaResultBean, str5, true); Iterator it = this.projects.iterator(); while (it.hasNext()) { BlocklyProject next = it.next(); if (!TextUtils.isEmpty(next.getXmlId())) { next.getXmlId().equals(writeConfigFile); } } Integer.parseInt(this.jimuRobot.getModelType()); } public void servoSet(String str) { try { FileHelper.a(Utils.baseUrl + "status.txt", "", (String) null); Utils.setJSResult(this.webView, "continueSteps", URLEncoder.encode(str, "utf-8"), "1"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); Log.e(this.tag, e.getMessage()); } } protected void setJSResult(String str, String str2, boolean z) { if (z) { try { str2 = URLEncoder.encode(str2, "utf-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); Log.e(this.tag, e.getMessage()); return; } } final String str3 = "javascript:" + str + "('" + str2 + "')"; this.webView.post(new Runnable() { // from class: com.ubt.jimu.blockly.command.JimuBlocklyCommand.1 @Override // java.lang.Runnable public void run() { JimuBlocklyCommand.this.webView.loadUrl(str3); } }); } public void setJimuRobot(JimuRobot jimuRobot) { } public void setModuleType(int i) { this.moduleType = i; } public void setServoMode() { DiyDBModel queryForUUid; if (!"1".equals(this.jimuRobot.getModelType()) || (queryForUUid = DiyHelper.getInstance().queryForUUid(this.jimuRobot.getModelId())) == null) { return; } this.context.setIsBack(true); Postcard a = ARouter.b().a("/controller/servosettings"); a.a("isFromUnity", true); a.a("robotLite", queryForUUid.getRobotLite()); a.a(this.context, 107); } public void startSensorTimer(String str, String str2) { ALog.a(this.tag).d("startSensorTimer"); if (TextUtils.isEmpty(str)) { return; } this.queryUrl = str; this.queryCallback = str2; String str3 = "jimu://queryAllSensor"; String[] split = str.split("\\|"); if (split != null && split.length >= 1) { str3 = ("jimu://queryAllSensor|") + split[1]; } if (split != null && split.length >= 2) { str3 = str3 + "|" + str2; } stopSensorTimer(""); this.timer = new Timer(); this.infraredTask = new InfraredTimerTask(str3); this.timer.schedule(this.infraredTask, 50L, 200L); } public void stopSensorTimer(String str) { ALog.a(this.tag).d("stopSensorTimer"); TimerTask timerTask = this.infraredTask; if (timerTask != null) { timerTask.cancel(); this.infraredTask = null; } Timer timer = this.timer; if (timer != null) { timer.cancel(); this.timer.purge(); this.timer = null; } } public void unityComm(String str) { JSONObject jSONObject = new JSONObject(); try { jSONObject.put("funcName", LOGIC_CMD); jSONObject.put("arg", URLDecoder.decode(str, "UTF-8")); UnityActivity.communityWithUnity(this.context, jSONObject.toString()); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (JSONException e2) { e2.printStackTrace(); } } /* JADX WARN: Type inference failed for: r0v10, types: [T, com.ubt.jimu.blockly.command.result.XmlListBean] */ /* JADX WARN: Type inference failed for: r0v8, types: [T, com.ubt.jimu.blockly.command.result.XmlListBean] */ public void xmlList(String str) { ALog.a(this.tag).d("xmlList"); this.projects.clear(); File file = new File(this.jimuRobot.getBlocklyProject("blockly") + this.jimuRobot.getModelId() + ".config"); File file2 = new File(this.jimuRobot.getSamplePath() + this.jimuRobot.getModelId() + ".config"); JavaResultBean javaResultBean = new JavaResultBean<>(); javaResultBean.retCode = "0000"; if (file.exists() || file2.exists()) { String b = FileHelper.b(file.getAbsolutePath(), (String) null); String b2 = FileHelper.b(file2.getAbsolutePath(), (String) null); Type type = new TypeToken>() { // from class: com.ubt.jimu.blockly.command.JimuBlocklyCommand.6 }.getType(); ArrayList arrayList = (ArrayList) JsonHelper.a(b, type); ArrayList arrayList2 = (ArrayList) JsonHelper.a(b2, type); if (arrayList2 != null) { this.projects.addAll(arrayList2); } if (arrayList != null) { this.projects.addAll(arrayList); } javaResultBean.result = new XmlListBean(this.projects); } else { javaResultBean.result = new XmlListBean(new ArrayList()); } setJSResult(javaResultBean, str, false); } private void setJSResult(JavaResultBean javaResultBean, String str, boolean z) { String a = JsonHelper.a(javaResultBean); if (z) { try { a = URLEncoder.encode(a, "utf-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); return; } } final String str2 = "javascript:" + str + "('" + a + "')"; this.webView.post(new Runnable() { // from class: com.ubt.jimu.blockly.command.JimuBlocklyCommand.2 @Override // java.lang.Runnable public void run() { JimuBlocklyCommand.this.webView.loadUrl(str2); } }); } }