package com.ubt.jimu.connect.presenter; import android.bluetooth.BluetoothDevice; import android.content.Context; import android.content.DialogInterface; import android.location.LocationManager; import android.os.Build; import android.provider.Settings; import android.util.Log; import com.afunx.ble.blelitelib.utils.BleUtils; import com.tencent.bugly.BuglyStrategy; import com.ubt.jimu.JimuApplication; import com.ubt.jimu.R; import com.ubt.jimu.base.cache.Cache; import com.ubt.jimu.base.cache.SharePreferenceHelper; import com.ubt.jimu.base.entities.ActiveStat; import com.ubt.jimu.base.entities.ApiResult; import com.ubt.jimu.base.entities.User; import com.ubt.jimu.base.http.ApiClient; import com.ubt.jimu.base.http.ApiConstants; import com.ubt.jimu.base.http.service.PackageRobotService; import com.ubt.jimu.connect.contract.ConnectContract$Presenter; import com.ubt.jimu.connect.contract.ConnectContract$View; import com.ubt.jimu.connect.model.MatchResultCallback; import com.ubt.jimu.connect.model.ModelMatchModel; import com.ubt.jimu.connect.model.ReadAllServoModel; import com.ubt.jimu.connect.model.UpdateModelImp; import com.ubt.jimu.unity.bluetooth.UnityActivity; import com.ubt.jimu.utils.RxSchedulers; import com.ubtech.permission.JimuPermissionRequest; import com.ubtech.permission.PermissionRequestListener; import com.ubtech.utils.XLog; import com.ubtech.view.dialog.SimpleDialog; import com.ubtrobot.jimu.bluetooth.Cancellable; import com.ubtrobot.jimu.bluetooth.ConnectCallback; import com.ubtrobot.jimu.bluetooth.base.discover.ScanResult; import com.ubtrobot.jimu.bluetooth.base.discover.ScannedHubEmitter; import com.ubtrobot.jimu.connection.RequestException; import com.ubtrobot.jimu.robotapi.BoardInfo; import com.ubtrobot.jimu.robotapi.JimuException; import com.ubtrobot.jimu.robotapi.JimuManager; import com.ubtrobot.jimu.robotapi.JimuScanner; import com.ubtrobot.jimu.robotapi.ServoAngleReadInfo; import com.ubtrobot.log.ALog; import com.ubtrobot.ubtlib.analytics.JimuAnalytics; import io.reactivex.Observer; import io.reactivex.disposables.Disposable; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import org.json.JSONObject; /* loaded from: classes.dex */ public class ConnectPresenterImpl implements ConnectContract$Presenter { private ConnectContract$View a; private JimuScanner b; private Cancellable c; private JimuManager d; private Context e; private SharePreferenceHelper f; private volatile boolean g; private String i; private int j; private volatile int h = -1; private boolean k = false; private BluetoothDevice l = null; private ExecutorService m = Executors.newSingleThreadExecutor(); private ScannedHubEmitter n = new ScannedHubEmitter() { // from class: com.ubt.jimu.connect.presenter.ConnectPresenterImpl.8 @Override // com.ubtrobot.jimu.bluetooth.base.discover.ScannedHubEmitter public void a(ScanResult scanResult) { if (ConnectPresenterImpl.this.h != 0) { Log.w("ConnectPresenterImpl", "searched a robot not in scan state"); } else { ConnectPresenterImpl.this.a.a(scanResult); } } @Override // com.ubtrobot.jimu.bluetooth.base.discover.ScannedHubEmitter public void a(int i, String str) { ALog.a("ConnectPresenterImpl").d("scaned onFail:" + i + " " + str); } @Override // com.ubtrobot.jimu.bluetooth.base.discover.ScannedHubEmitter public void a() { ALog.a("ConnectPresenterImpl").d("Scan timeout"); } }; public ConnectPresenterImpl(Context context, ConnectContract$View connectContract$View) { this.g = true; this.a = connectContract$View; this.a.a(this); this.e = context; this.b = new JimuScanner(context); this.d = JimuApplication.l().f(); this.f = new SharePreferenceHelper(); this.g = this.f.getBoolean(SharePreferenceHelper.SP_KEY_AUTO_CONNECT, true).booleanValue(); ALog.a("ConnectPresenterImpl").d("mIsAutoConnect:" + this.g); } @Override // com.ubt.jimu.connect.contract.ConnectContract$Presenter public void k() { if (c()) { p(); } else { new SimpleDialog.Builder(this.e).b(R.string.app_need_bluetooth_function).a(R.string.deny, true).a(new DialogInterface.OnClickListener(this) { // from class: com.ubt.jimu.connect.presenter.ConnectPresenterImpl.10 @Override // android.content.DialogInterface.OnClickListener public void onClick(DialogInterface dialogInterface, int i) { dialogInterface.dismiss(); } }).d(R.string.allow).b(new DialogInterface.OnClickListener() { // from class: com.ubt.jimu.connect.presenter.ConnectPresenterImpl.9 @Override // android.content.DialogInterface.OnClickListener public void onClick(DialogInterface dialogInterface, int i) { ConnectPresenterImpl.this.a.R(); dialogInterface.dismiss(); } }).a().show(); } } @Override // com.ubt.jimu.connect.contract.ConnectContract$Presenter public BluetoothDevice l() { return this.l; } @Override // com.ubt.jimu.connect.contract.ConnectContract$Presenter public int m() { return this.h; } @Override // com.ubt.jimu.connect.contract.ConnectContract$Presenter public void p() { JimuPermissionRequest.b(this.e, new PermissionRequestListener() { // from class: com.ubt.jimu.connect.presenter.ConnectPresenterImpl.7 @Override // com.ubtech.permission.PermissionRequestListener public void onDenied() { Log.w("ConnectPresenterImpl", "User denied the location permission request"); } @Override // com.ubtech.permission.PermissionRequestListener public void onGranted() { if (ConnectPresenterImpl.this.e()) { ConnectPresenterImpl.this.a.z(); } else { ConnectPresenterImpl.this.b(); } } }); } @Override // com.ubt.jimu.connect.contract.ConnectContract$Presenter public void q() { Cancellable cancellable = this.c; if (cancellable != null) { cancellable.cancel(); } } @Override // com.ubt.jimu.connect.contract.ConnectContract$Presenter public void r() { a(false, (String) null, (String) null); } /* JADX INFO: Access modifiers changed from: private */ public boolean e() { return Build.VERSION.SDK_INT >= 29 && !a(this.e); } /* JADX INFO: Access modifiers changed from: private */ public void f() { this.m.execute(new Runnable() { // from class: com.ubt.jimu.connect.presenter.ConnectPresenterImpl.2 @Override // java.lang.Runnable public void run() { final ModelMatchModel modelMatchModel = new ModelMatchModel(); modelMatchModel.a(ConnectPresenterImpl.this.i, ConnectPresenterImpl.this.k, new MatchResultCallback() { // from class: com.ubt.jimu.connect.presenter.ConnectPresenterImpl.2.1 @Override // com.ubt.jimu.connect.model.MatchResultCallback public void a(int i, BoardInfo boardInfo) { if (i == -4 || i == -3) { Cache.getInstance().setBoardInfo(boardInfo); ConnectPresenterImpl.this.a.a(ConnectPresenterImpl.this.i, ConnectPresenterImpl.this.k); return; } if (i == -2 || i == -1) { if (ConnectPresenterImpl.this.h != 2) { ALog.a("ConnectPresenterImpl").d("match fail!but not on STATE_CONNECTED, so not show fail view"); return; } ConnectPresenterImpl.this.a(); ALog.a("ConnectPresenterImpl").e("Device is not jimu or read code fail! Disconnect and update page to connect fail!", new Object[0]); ConnectPresenterImpl.this.a.b0(); ConnectPresenterImpl.this.r(); return; } if (i != 0) { Log.e("ConnectPresenterImpl", "Invalid match result!"); return; } Cache.getInstance().setBoardInfo(boardInfo); if (ModelMatchModel.a(ConnectPresenterImpl.this.k, ConnectPresenterImpl.this.i)) { ConnectPresenterImpl.this.a.a(ConnectPresenterImpl.this.i, ConnectPresenterImpl.this.k); return; } UpdateModelImp b = UpdateModelImp.b(); if (!b.c(boardInfo)) { ConnectPresenterImpl.this.a(modelMatchModel, boardInfo); } else if (b.d(boardInfo) || b.b(boardInfo)) { ConnectPresenterImpl.this.a.a(ConnectPresenterImpl.this.i, ConnectPresenterImpl.this.k); } else { ConnectPresenterImpl.this.a(modelMatchModel, boardInfo); } } }); } }); } /* JADX INFO: Access modifiers changed from: private */ public void h() { this.m.execute(new Runnable() { // from class: com.ubt.jimu.connect.presenter.ConnectPresenterImpl.4 @Override // java.lang.Runnable public void run() { ModelMatchModel modelMatchModel = new ModelMatchModel(); try { if (!ConnectPresenterImpl.this.d.h()) { ConnectPresenterImpl.this.d.c(); Log.e("ConnectPresenterImpl", "connect device is not JIMU"); ConnectPresenterImpl.this.a.b0(); return; } ConnectPresenterImpl.this.d.n(); try { Thread.sleep(2000L); } catch (InterruptedException unused) { Thread.currentThread().interrupt(); } BoardInfo b = modelMatchModel.b(); Cache.getInstance().setBoardInfo(b); if (b.l()) { ConnectPresenterImpl.this.a(); ConnectPresenterImpl.this.a.O(); return; } int b2 = ConnectPresenterImpl.this.b(b); if (b2 == 0) { ConnectPresenterImpl.this.a(); ConnectPresenterImpl.this.a.G(); return; } if (b2 > 1) { ConnectPresenterImpl.this.a(); ConnectPresenterImpl.this.a.O(); return; } for (Map.Entry entry : b.h().entrySet()) { if (entry.getValue().b() != null && entry.getValue().b().size() != 0 && (entry.getKey().intValue() == 14 || entry.getKey().intValue() == 8)) { ConnectPresenterImpl.this.a(); ConnectPresenterImpl.this.a.Y(); return; } } ConnectPresenterImpl.this.a.c(-1); } catch (JimuException e) { Log.e("ConnectPresenterImpl", "Read board info fail!", e); ConnectPresenterImpl.this.a.b0(); } catch (RequestException e2) { Log.e("ConnectPresenterImpl", "Read board info fail!", e2); ConnectPresenterImpl.this.a.b0(); } } }); } @Override // com.ubt.jimu.connect.contract.ConnectContract$Presenter public void b(String str) { ALog.a("ConnectPresenterImpl").d("setModelId:" + str); this.i = str; } @Override // com.ubt.jimu.connect.contract.ConnectContract$Presenter public void c(String str, String str2) { SharePreferenceHelper sharePreferenceHelper = new SharePreferenceHelper(); if (str != null) { ALog.a("ConnectPresenterImpl").d("save connect success model. model:" + str2 + " mac:" + str); sharePreferenceHelper.put(str2, str); } } @Override // com.ubt.jimu.connect.contract.ConnectContract$Presenter public boolean d() { return this.d.g(); } @Override // com.ubt.jimu.connect.contract.ConnectContract$Presenter public boolean g() { return this.g; } @Override // com.ubt.jimu.connect.contract.ConnectContract$Presenter public void b(boolean z) { this.g = z; ALog.a("ConnectPresenterImpl").d("mIsAutoConnect:" + this.g); } private boolean c() { return BleUtils.isBluetoothEnabled(); } /* JADX INFO: Access modifiers changed from: private */ public int b(BoardInfo boardInfo) { int size = boardInfo.i() != null ? 0 + boardInfo.i().size() : 0; for (Map.Entry entry : boardInfo.h().entrySet()) { if (entry.getValue().b() != null) { size += entry.getValue().b().size(); } } return size; } @Override // com.ubt.jimu.connect.contract.ConnectContract$Presenter public void a(boolean z) { this.k = z; } @Override // com.ubt.jimu.connect.contract.ConnectContract$Presenter public void a(int i) { this.j = i; } @Override // com.ubt.jimu.connect.contract.ConnectContract$Presenter public void a(final BluetoothDevice bluetoothDevice) { ALog.a("ConnectPresenterImpl").d("mBluetoothState:" + this.h); this.h = 1; q(); if (!c()) { this.h = 3; this.l = null; this.a.b0(); r(); return; } this.a.i(); this.d.a(bluetoothDevice.getAddress(), new ConnectCallback() { // from class: com.ubt.jimu.connect.presenter.ConnectPresenterImpl.1 @Override // com.ubtrobot.jimu.bluetooth.ConnectCallback public void a(int i, String str) { ALog.a("ConnectPresenterImpl").d("connect fail! fail msg:" + i + " " + str); if (ConnectPresenterImpl.this.h != 1) { ALog.a("ConnectPresenterImpl").d("connect fail!but not on STATE_CONNECTING, so not show fail view"); return; } ConnectPresenterImpl.this.h = 3; ConnectPresenterImpl.this.l = null; ConnectPresenterImpl.this.a.b0(); ConnectPresenterImpl.this.r(); } @Override // com.ubtrobot.jimu.bluetooth.ConnectCallback public void onSuccess() { ALog.a("ConnectPresenterImpl").d("connect successful!"); if (ConnectPresenterImpl.this.h != 1) { ALog.a("ConnectPresenterImpl").d("connect successful, but not on STATE_CONNECTING, so not show successful view, and disconnect"); ConnectPresenterImpl.this.a(); return; } ConnectPresenterImpl.this.h = 2; ConnectPresenterImpl.this.l = bluetoothDevice; Cache.getInstance().setLastConnectedDevice(bluetoothDevice); ConnectPresenterImpl.this.a.J(); if (ConnectPresenterImpl.this.j != 1) { ConnectPresenterImpl.this.f(); } else { ALog.a("ConnectPresenterImpl").d("Modify Id"); ConnectPresenterImpl.this.h(); } } }); } /* JADX INFO: Access modifiers changed from: private */ public void b() { ALog.a("ConnectPresenterImpl").d("start scan"); this.h = 0; this.d.c(); q(); this.a.U(); this.c = this.b.a(this.n, BuglyStrategy.a.MAX_USERDATA_VALUE_LENGTH); } /* JADX INFO: Access modifiers changed from: private */ public void a(ModelMatchModel modelMatchModel, BoardInfo boardInfo) { modelMatchModel.b(boardInfo, this.i, this.k); BluetoothDevice bluetoothDevice = this.l; if (bluetoothDevice != null && bluetoothDevice.getAddress() != null) { c(this.l.getAddress(), this.i); } a(this.e, boardInfo, this.i, this.k); List a = new ReadAllServoModel(this.d).a(this.i, this.k); if (a != null && a.size() != 0) { this.a.a(a); } else { this.a.c(-1); } } @Override // com.ubt.jimu.connect.contract.ConnectContract$Presenter public void a(final List list) { this.m.execute(new Runnable() { // from class: com.ubt.jimu.connect.presenter.ConnectPresenterImpl.3 @Override // java.lang.Runnable public void run() { new ReadAllServoModel(ConnectPresenterImpl.this.d).a(list); } }); } @Override // com.ubt.jimu.connect.contract.ConnectContract$Presenter public void a() { ALog.a("ConnectPresenterImpl").d("disconnect"); this.h = 3; this.d.c(); this.l = null; } @Override // com.ubt.jimu.connect.contract.ConnectContract$Presenter public void a(final Context context, final BoardInfo boardInfo, final String str, final boolean z) { this.m.execute(new Runnable() { // from class: com.ubt.jimu.connect.presenter.ConnectPresenterImpl.5 @Override // java.lang.Runnable public void run() { String address; JimuManager f = JimuApplication.l().f(); f.c(str); ConnectPresenterImpl.this.a(boardInfo); try { String j = f.j(); String m = f.m(); ConnectPresenterImpl.this.a(context, j, m); ConnectPresenterImpl.this.a(true, j, boardInfo.d()); int i = z ? 2 : 1; BluetoothDevice l = ConnectPresenterImpl.this.l(); if (l == null) { Log.e("ConnectPresenterImpl", "Connected device is null"); address = ""; } else { address = l.getAddress(); } ConnectPresenterImpl.this.a(str, i, m, j, address); } catch (JimuException e) { XLog.a("ConnectPresenterImpl", "Read McuId or SN fail!", e); } } }); } public void a(Context context, String str, final String str2) { XLog.a("ConnectPresenterImpl", "recordActivationRobot mcuId:%s sn:%s", str, str2); PackageRobotService packageRobotService = (PackageRobotService) ApiClient.getService(PackageRobotService.class); Map basicParams = ApiConstants.getBasicParams(); basicParams.put("activeAreaCode", ""); basicParams.put("equipmentUid", str); basicParams.put("activeArea", ""); User user = Cache.getInstance().getUser(); basicParams.put("userId", user != null ? String.valueOf(user.getUserId()) : ""); basicParams.put("serialNumber", str2); packageRobotService.activeRobot(basicParams).compose(RxSchedulers.a()).subscribe(new Observer>(this) { // from class: com.ubt.jimu.connect.presenter.ConnectPresenterImpl.6 private int a; private int b; private String c; @Override // io.reactivex.Observer /* renamed from: a, reason: merged with bridge method [inline-methods] */ public void onNext(ApiResult apiResult) { List models = apiResult.getModels(); if (models == null || models.size() <= 0) { this.c = str2; } else { this.c = models.get(0).serialNumber; } if ("2001".equals(apiResult.getInfo())) { this.a = 2001; this.b = 0; } else if ("2002".equals(apiResult.getInfo())) { this.a = 2002; this.b = 0; } else if ("0000".equals(apiResult.getInfo())) { this.a = 0; this.b = 1; } else { this.a = 3000; this.b = 0; } } @Override // io.reactivex.Observer public void onComplete() { JSONObject jSONObject = new JSONObject(); JSONObject jSONObject2 = new JSONObject(); try { jSONObject.put("funcName", "RegisterRobotResult"); jSONObject2.put("reason", this.a); jSONObject2.put("isSuccess", this.b); jSONObject2.put("sn", this.c); jSONObject.put("arg", jSONObject2); } catch (Exception e) { e.printStackTrace(); } XLog.a("ConnectPresenterImpl", "active result %s", jSONObject); } @Override // io.reactivex.Observer public void onError(Throwable th) { th.printStackTrace(); this.a = 3000; this.b = 0; } @Override // io.reactivex.Observer public void onSubscribe(Disposable disposable) { } }); } public void a(boolean z, String str, String str2) { XLog.a("ConnectPresenterImpl", "recordConnectResult isConnectSuccess:%b mcuId:%s controlboxVersion:%s", Boolean.valueOf(z), str, str2); if (str == null) { str = ""; } if (str2 == null) { str2 = ""; } HashMap hashMap = new HashMap(); if (z) { hashMap.put("resultCode", "1-1"); } else { hashMap.put("resultCode", "1-2"); } hashMap.put("controlboxId", str); hashMap.put("contorlboxVersion", str2); JimuAnalytics.b().a("app_bluetooth_connect", hashMap); } public void a(String str, int i, String str2, String str3, String str4) { if (str3 == null) { str3 = ""; } if (str2 == null) { str2 = ""; } if (str4 == null) { str4 = ""; } HashMap hashMap = new HashMap(); hashMap.put("modelId", str); hashMap.put(UnityActivity.pModelType, String.valueOf(i)); hashMap.put("serialNumber", str2); hashMap.put("mcuId", str3); hashMap.put("bluetoothMac", str4); JimuAnalytics.b().a("app_model_connect", hashMap); } public static boolean a(Context context) { LocationManager locationManager = (LocationManager) context.getApplicationContext().getSystemService("location"); if (locationManager != null) { if (Build.VERSION.SDK_INT >= 28) { return locationManager.isLocationEnabled(); } try { return Settings.Secure.getInt(context.getContentResolver(), "location_mode") != 0; } catch (Settings.SettingNotFoundException e) { e.printStackTrace(); } } return false; } public void a(BoardInfo boardInfo) { BoardInfo.SensorBoardInfo sensorBoardInfo; List b; try { this.d.a(true); } catch (JimuException e) { XLog.a("ConnectPresenterImpl", "enable self check fail", e); } HashMap h = boardInfo.h(); if (h == null || h.size() <= 0) { return; } for (Integer num : h.keySet()) { if (10 != num.intValue() && (sensorBoardInfo = h.get(num)) != null && (b = sensorBoardInfo.b()) != null && b.size() != 0) { try { this.d.a(num.intValue(), b, true); } catch (JimuException e2) { Log.e("ConnectPresenterImpl", "enableSensor fail!", e2); } } } } }