package com.ubtrobot.jimu.robotapi; import com.ubtrobot.jimu.bluetooth.ConnectionState; import com.ubtrobot.jimu.bluetooth.ConnectionStateListener; import com.ubtrobot.jimu.bluetooth.base.IPacket; import com.ubtrobot.jimu.connection.PacketsReceiveListener; import com.ubtrobot.jimu.connection.RobotConnection; import com.ubtrobot.jimu.exception.RobotActiveException; import java.util.Timer; import java.util.TimerTask; import java.util.Vector; /* loaded from: classes2.dex */ public class GlobalManager implements PacketsReceiveListener, ConnectionStateListener { private Timer a; private RobotConnection b; private DeviceManager c; private Vector d; private Vector e; private BatteryInfo f; private volatile boolean g; public GlobalManager(RobotConnection robotConnection, DeviceManager deviceManager) { this.b = robotConnection; this.c = deviceManager; this.b.a((PacketsReceiveListener) this); this.b.a((ConnectionStateListener) this); } public void c() { Timer timer = this.a; if (timer != null) { try { timer.cancel(); this.a = null; } catch (Exception e) { e.printStackTrace(); } } } @Override // com.ubtrobot.jimu.bluetooth.ConnectionStateListener public void onConnectionStateChange(String str, ConnectionState connectionState) { Vector vector; Object[] array; if (connectionState == null || (vector = this.d) == null || vector.size() == 0) { return; } synchronized (this) { array = this.d.toArray(); } for (int length = array.length - 1; length >= 0; length--) { ((IPowerStateListener) array[length]).onConnectionStateChange(str, connectionState); } } @Override // com.ubtrobot.jimu.connection.PacketsReceiveListener public void a(String str, IPacket iPacket) { if (iPacket == null) { return; } int f = iPacket.f() & 255; if (f == 5) { b(str, iPacket); } else { if (f != 39) { return; } a(iPacket); } } public void b() { Timer timer = this.a; if (timer != null) { try { timer.cancel(); } catch (Exception e) { e.printStackTrace(); } } this.a = new Timer(); this.a.schedule(new TimerTask() { // from class: com.ubtrobot.jimu.robotapi.GlobalManager.1 @Override // java.util.TimerTask, java.lang.Runnable public void run() { try { if (GlobalManager.this.g) { return; } GlobalManager.this.c.d(); } catch (Exception e2) { e2.printStackTrace(); } } }, 5000L, 30000L); } public void a(boolean z) { this.g = z; } public synchronized BatteryInfo a(IPowerStateListener iPowerStateListener) { if (this.d == null) { this.d = new Vector<>(); } if (!this.d.contains(iPowerStateListener)) { this.d.add(iPowerStateListener); } return this.f; } public void b(IPowerStateListener iPowerStateListener) { Vector vector = this.d; if (vector != null) { vector.removeElement(iPowerStateListener); } } public void b(IGlobalExceptionListener iGlobalExceptionListener) { Vector vector = this.e; if (vector != null) { vector.removeElement(iGlobalExceptionListener); } } private void b(String str, IPacket iPacket) { Vector vector; RobotActiveException a; Object[] array; if (iPacket == null || (vector = this.e) == null || vector.size() == 0 || (a = new GlobalExceptionUtil().a(iPacket, str)) == null) { return; } synchronized (this) { array = this.e.toArray(); } if (array == null || array.length == 0) { return; } for (int length = array.length - 1; length >= 0; length--) { ((IGlobalExceptionListener) array[length]).onGlobalException(a); } } public synchronized void a(IGlobalExceptionListener iGlobalExceptionListener) { if (this.e == null) { this.e = new Vector<>(); } if (!this.e.contains(iGlobalExceptionListener)) { this.e.add(iGlobalExceptionListener); } } public BatteryInfo a() { return this.f; } private void a(IPacket iPacket) { Object[] array; if (iPacket == null) { return; } byte[] g = iPacket.g(); if (g.length == 1) { return; } BatteryInfo batteryInfo = new BatteryInfo(g[2], g[3], g[0] != 0, g[1] == 1); BatteryInfo batteryInfo2 = this.f; if (batteryInfo2 != null && batteryInfo2.c() == batteryInfo.c() && this.f.d() == batteryInfo.d() && this.f.a() == batteryInfo.a() && this.f.b() == batteryInfo.b()) { return; } this.f = batteryInfo; Vector vector = this.d; if (vector == null || vector.size() == 0) { return; } synchronized (this) { array = this.d.toArray(); } for (int length = array.length - 1; length >= 0; length--) { ((IPowerStateListener) array[length]).onPowerStateChanged(batteryInfo); } } }