365 lines
13 KiB
Java
365 lines
13 KiB
Java
package com.ubt.jimu.controller.manager;
|
|
|
|
import android.content.Context;
|
|
import android.content.res.TypedArray;
|
|
import android.os.Build;
|
|
import android.text.TextUtils;
|
|
import android.util.SparseArray;
|
|
import com.ubt.jimu.R;
|
|
import com.ubt.jimu.base.entities.RobotLite;
|
|
import com.ubt.jimu.base.util.PathHelper;
|
|
import com.ubt.jimu.controller.data.action.Action;
|
|
import com.ubt.jimu.controller.data.action.ActionIcon;
|
|
import com.ubt.jimu.controller.data.action.ActionSequence;
|
|
import com.ubt.jimu.controller.data.action.MotorData;
|
|
import com.ubt.jimu.controller.data.action.TurnData;
|
|
import com.ubt.jimu.controller.data.widget.ActionWidgetData;
|
|
import com.ubt.jimu.controller.data.widget.JockstickDataConverter;
|
|
import com.ubt.jimu.transport.model.TransportFile;
|
|
import com.unity3d.ads.metadata.MediationMetaData;
|
|
import java.io.File;
|
|
import java.io.FileInputStream;
|
|
import java.io.IOException;
|
|
import java.util.ArrayList;
|
|
import java.util.Collections;
|
|
import java.util.Comparator;
|
|
import java.util.HashMap;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import java.util.TreeMap;
|
|
import java.util.concurrent.CopyOnWriteArrayList;
|
|
import org.xmlpull.v1.XmlPullParser;
|
|
import org.xmlpull.v1.XmlPullParserException;
|
|
import org.xmlpull.v1.XmlPullParserFactory;
|
|
|
|
/* loaded from: classes.dex */
|
|
public class ActionManager {
|
|
private static ActionManager e;
|
|
private Context a;
|
|
private List<ActionSequence> b = new CopyOnWriteArrayList();
|
|
private final Map<String, ActionIcon> c = new HashMap();
|
|
private RobotLite d;
|
|
|
|
private ActionManager(Context context) {
|
|
this.a = context.getApplicationContext();
|
|
d();
|
|
}
|
|
|
|
public static synchronized ActionManager a(Context context) {
|
|
ActionManager actionManager;
|
|
synchronized (ActionManager.class) {
|
|
if (e == null) {
|
|
e = new ActionManager(context);
|
|
}
|
|
actionManager = e;
|
|
}
|
|
return actionManager;
|
|
}
|
|
|
|
private void c() {
|
|
List<ActionSequence> list = this.b;
|
|
if (list != null) {
|
|
list.clear();
|
|
}
|
|
}
|
|
|
|
private void d() {
|
|
String[] stringArray = this.a.getResources().getStringArray(R.array.action_icon_key);
|
|
TypedArray obtainTypedArray = this.a.getResources().obtainTypedArray(R.array.action_icon_drawable);
|
|
for (int i = 0; i < stringArray.length; i++) {
|
|
this.c.put(stringArray[i], new ActionIcon(stringArray[i], obtainTypedArray.getResourceId(i, 0)));
|
|
}
|
|
}
|
|
|
|
public void b() {
|
|
if (this.d != null) {
|
|
c();
|
|
a(this.d.isOfficial());
|
|
}
|
|
}
|
|
|
|
private void c(XmlPullParser xmlPullParser, Action action) {
|
|
String attributeValue = xmlPullParser.getAttributeValue(null, TransportFile.TYPE_DIY_SHOW);
|
|
if (TextUtils.isEmpty(attributeValue)) {
|
|
return;
|
|
}
|
|
String[] split = attributeValue.split(";");
|
|
if (split.length > 0) {
|
|
ArrayList arrayList = new ArrayList(split.length);
|
|
for (String str : split) {
|
|
arrayList.add(Integer.valueOf(str.trim()));
|
|
}
|
|
action.a(arrayList);
|
|
}
|
|
}
|
|
|
|
public void a(RobotLite robotLite) {
|
|
this.d = robotLite;
|
|
b();
|
|
}
|
|
|
|
public ActionIcon b(String str) {
|
|
Map<String, ActionIcon> map = this.c;
|
|
if (map == null || map.size() <= 0) {
|
|
return null;
|
|
}
|
|
return this.c.get(str);
|
|
}
|
|
|
|
private void b(boolean z) {
|
|
File[] listFiles;
|
|
File file = new File(PathHelper.getActionDir(this.a, this.d, z));
|
|
if (!file.exists() || !file.isDirectory() || (listFiles = file.listFiles()) == null || listFiles.length <= 0) {
|
|
return;
|
|
}
|
|
a(listFiles, z);
|
|
}
|
|
|
|
private void d(XmlPullParser xmlPullParser, Action action) {
|
|
String attributeValue = xmlPullParser.getAttributeValue(null, "turns");
|
|
if (TextUtils.isEmpty(attributeValue)) {
|
|
return;
|
|
}
|
|
String[] split = attributeValue.split(";");
|
|
TreeMap treeMap = new TreeMap();
|
|
for (String str : split) {
|
|
String[] split2 = str.split("\\$");
|
|
if (split2.length > 1) {
|
|
String[] split3 = split2[1].split(",");
|
|
int intValue = Integer.valueOf(split3[0].trim()).intValue();
|
|
if (intValue == 2) {
|
|
intValue = -1;
|
|
}
|
|
treeMap.put(Integer.valueOf(split2[0].trim()), new TurnData(intValue, Integer.valueOf(split3[1].trim()).intValue()));
|
|
}
|
|
}
|
|
action.c(treeMap);
|
|
}
|
|
|
|
public ActionSequence a(String str) {
|
|
List<ActionSequence> list = this.b;
|
|
if (list == null || list.size() <= 0) {
|
|
return null;
|
|
}
|
|
for (ActionSequence actionSequence : this.b) {
|
|
if (actionSequence.e().equals(str)) {
|
|
return actionSequence;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
private void a(boolean z) {
|
|
if (z) {
|
|
b(false);
|
|
b(true);
|
|
} else {
|
|
b(true);
|
|
}
|
|
List<ActionSequence> list = this.b;
|
|
if (list == null || list.size() <= 1) {
|
|
return;
|
|
}
|
|
a aVar = new Comparator() { // from class: com.ubt.jimu.controller.manager.a
|
|
@Override // java.util.Comparator
|
|
public final int compare(Object obj, Object obj2) {
|
|
return ActionManager.a((ActionSequence) obj, (ActionSequence) obj2);
|
|
}
|
|
};
|
|
if (Build.VERSION.SDK_INT >= 24) {
|
|
this.b.sort(aVar);
|
|
return;
|
|
}
|
|
ArrayList arrayList = new ArrayList(this.b);
|
|
Collections.sort(arrayList, aVar);
|
|
this.b.clear();
|
|
this.b.addAll(arrayList);
|
|
}
|
|
|
|
private int c(String str) {
|
|
if (!TextUtils.isEmpty(str)) {
|
|
try {
|
|
return Integer.valueOf(str.trim()).intValue();
|
|
} catch (NumberFormatException e2) {
|
|
e2.printStackTrace();
|
|
}
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
private void b(ActionSequence actionSequence, XmlPullParser xmlPullParser) {
|
|
actionSequence.e(xmlPullParser.getAttributeValue(null, MediationMetaData.KEY_NAME));
|
|
a(actionSequence, xmlPullParser.getAttributeValue(null, "showName"));
|
|
actionSequence.f(xmlPullParser.getAttributeValue(null, "robotID"));
|
|
actionSequence.c(xmlPullParser.getAttributeValue(null, JockstickDataConverter.ID));
|
|
actionSequence.b(xmlPullParser.getAttributeValue(null, "icon"));
|
|
actionSequence.a(Long.valueOf(xmlPullParser.getAttributeValue(null, "createTime")).longValue());
|
|
}
|
|
|
|
private void b(XmlPullParser xmlPullParser, Action action) {
|
|
String attributeValue = xmlPullParser.getAttributeValue(null, "rotas");
|
|
if (TextUtils.isEmpty(attributeValue)) {
|
|
return;
|
|
}
|
|
String[] split = attributeValue.split(";");
|
|
TreeMap treeMap = new TreeMap();
|
|
for (String str : split) {
|
|
String[] split2 = str.split("\\$");
|
|
if (split2.length > 1) {
|
|
treeMap.put(Integer.valueOf(split2[0].trim()), Integer.valueOf(split2[1].trim()));
|
|
}
|
|
}
|
|
action.b(treeMap);
|
|
}
|
|
|
|
static /* synthetic */ int a(ActionSequence actionSequence, ActionSequence actionSequence2) {
|
|
if (actionSequence.j() != actionSequence2.j()) {
|
|
return actionSequence.j() ? 1 : -1;
|
|
}
|
|
long b = actionSequence2.b() - actionSequence.b();
|
|
if (b > 0) {
|
|
return 1;
|
|
}
|
|
return b < 0 ? -1 : 0;
|
|
}
|
|
|
|
private void a(File[] fileArr, boolean z) {
|
|
ActionSequence a;
|
|
for (File file : fileArr) {
|
|
if (file.isFile() && (a = a(file)) != null) {
|
|
ActionIcon b = b(a.c());
|
|
if (b != null) {
|
|
a.a(b.a());
|
|
}
|
|
a.a(!z);
|
|
String soundPath = PathHelper.getSoundPath(this.a, this.d, a);
|
|
if (!TextUtils.isEmpty(soundPath)) {
|
|
a.d(soundPath);
|
|
}
|
|
this.b.add(a);
|
|
}
|
|
}
|
|
}
|
|
|
|
private ActionSequence a(File file) {
|
|
try {
|
|
FileInputStream fileInputStream = new FileInputStream(file);
|
|
try {
|
|
XmlPullParser newPullParser = XmlPullParserFactory.newInstance().newPullParser();
|
|
newPullParser.setInput(fileInputStream, "utf-8");
|
|
ActionSequence actionSequence = null;
|
|
for (int eventType = newPullParser.getEventType(); eventType != 1; eventType = newPullParser.next()) {
|
|
if (eventType != 0) {
|
|
if (eventType == 2 && "Root".equals(newPullParser.getName())) {
|
|
String attributeValue = newPullParser.getAttributeValue(null, "nodeType");
|
|
if ("head".equals(attributeValue)) {
|
|
b(actionSequence, newPullParser);
|
|
} else if ("body".equals(attributeValue)) {
|
|
a(actionSequence, newPullParser);
|
|
}
|
|
}
|
|
} else {
|
|
actionSequence = new ActionSequence();
|
|
}
|
|
}
|
|
fileInputStream.close();
|
|
return actionSequence;
|
|
} catch (Throwable th) {
|
|
try {
|
|
throw th;
|
|
} catch (Throwable th2) {
|
|
try {
|
|
fileInputStream.close();
|
|
} catch (Throwable th3) {
|
|
th.addSuppressed(th3);
|
|
}
|
|
throw th2;
|
|
}
|
|
}
|
|
} catch (IOException | NumberFormatException | XmlPullParserException e2) {
|
|
e2.printStackTrace();
|
|
return null;
|
|
}
|
|
}
|
|
|
|
private void a(ActionSequence actionSequence, XmlPullParser xmlPullParser) {
|
|
Action action = new Action();
|
|
action.a(c(xmlPullParser.getAttributeValue(null, "index")));
|
|
int c = c(xmlPullParser.getAttributeValue(null, "sportTime"));
|
|
if (c > -1) {
|
|
action.b(c);
|
|
}
|
|
int c2 = c(xmlPullParser.getAttributeValue(null, "waitTime"));
|
|
if (c2 > -1) {
|
|
action.c(c2);
|
|
}
|
|
b(xmlPullParser, action);
|
|
d(xmlPullParser, action);
|
|
a(xmlPullParser, action);
|
|
c(xmlPullParser, action);
|
|
actionSequence.a(action);
|
|
}
|
|
|
|
private void a(XmlPullParser xmlPullParser, Action action) {
|
|
String attributeValue = xmlPullParser.getAttributeValue(null, "motors");
|
|
if (TextUtils.isEmpty(attributeValue)) {
|
|
return;
|
|
}
|
|
String[] split = attributeValue.split(";");
|
|
TreeMap treeMap = new TreeMap();
|
|
for (String str : split) {
|
|
String[] split2 = str.split("\\$");
|
|
if (split2.length > 1) {
|
|
String[] split3 = split2[1].split(",");
|
|
if (split3.length >= 3) {
|
|
int intValue = Integer.valueOf(split3[0].trim()).intValue();
|
|
if (intValue == 2) {
|
|
intValue = -1;
|
|
}
|
|
treeMap.put(Integer.valueOf(split2[0].trim()), new MotorData(intValue, Integer.valueOf(split3[1].trim()).intValue(), Integer.valueOf(split3[2].trim()).intValue()));
|
|
} else if (split3.length >= 2) {
|
|
int intValue2 = Integer.valueOf(split3[0].trim()).intValue();
|
|
if (intValue2 == 2) {
|
|
intValue2 = -1;
|
|
}
|
|
treeMap.put(Integer.valueOf(split2[0].trim()), new MotorData(intValue2, Integer.valueOf(split3[1].trim()).intValue(), 6553500));
|
|
} else if (split3.length <= 1) {
|
|
treeMap.put(Integer.valueOf(split2[0].trim()), new MotorData(0, 0, 6553500));
|
|
}
|
|
}
|
|
}
|
|
action.a(treeMap);
|
|
}
|
|
|
|
private void a(ActionSequence actionSequence, String str) {
|
|
if (TextUtils.isEmpty(str)) {
|
|
return;
|
|
}
|
|
String[] split = str.split(";");
|
|
SparseArray<String> sparseArray = new SparseArray<>(split.length);
|
|
for (String str2 : split) {
|
|
String[] split2 = str2.split("\\$");
|
|
if (split2.length > 1) {
|
|
sparseArray.put(Integer.valueOf(split2[0]).intValue(), split2[1]);
|
|
}
|
|
}
|
|
actionSequence.a(sparseArray);
|
|
}
|
|
|
|
public List<ActionWidgetData> a() {
|
|
List<ActionSequence> list = this.b;
|
|
if (list == null || list.size() <= 0) {
|
|
return null;
|
|
}
|
|
ArrayList arrayList = new ArrayList(this.b.size());
|
|
for (ActionSequence actionSequence : this.b) {
|
|
ActionWidgetData actionWidgetData = new ActionWidgetData();
|
|
actionWidgetData.setActionID(actionSequence.e());
|
|
actionWidgetData.setActionNm(actionSequence.h());
|
|
actionWidgetData.setActionSequence(actionSequence);
|
|
arrayList.add(actionWidgetData);
|
|
}
|
|
return arrayList;
|
|
}
|
|
}
|