package com.kofax.mobile.sdk.af; import android.hardware.Sensor; import android.hardware.SensorEvent; import android.hardware.SensorEventListener; import android.hardware.SensorManager; import android.os.Build; import android.os.Handler; import android.os.HandlerThread; import com.google.android.gms.maps.model.BitmapDescriptorFactory; import com.kofax.kmc.kui.uicontrols.Utility; import com.kofax.kmc.kut.utilities.error.ErrorInfo; import com.kofax.kmc.kut.utilities.error.KmcRuntimeException; import com.kofax.mobile.sdk._internal.IBus; import com.kofax.mobile.sdk._internal.impl.event.LevelChangedEvent; import com.kofax.mobile.sdk._internal.impl.event.SurfaceChangedEvent; import com.kofax.mobile.sdk._internal.impl.event.ay; import com.kofax.mobile.sdk._internal.impl.event.s; import com.kofax.mobile.sdk._internal.impl.event.t; import com.kofax.mobile.sdk._internal.impl.event.u; import com.kofax.mobile.sdk._internal.impl.event.v; import o.InterfaceC13004fNq; import o.InterfaceC13006fNs; import o.InterfaceC13391fZD; @com.kofax.mobile.sdk._internal.dagger.a /* loaded from: classes3.dex */ public class d implements SensorEventListener { private static final long Ue = 500; private static final float Uf = (float) Math.toRadians(180.0d); private static final float Ug = (float) Math.toRadians(90.0d); private static final float Uh = (float) Math.toRadians(75.0d); private static final float Ui = (float) Math.toRadians(60.0d); private static final float Uj = (float) Math.toRadians(30.0d); private static final float Uk = (float) Math.toRadians(15.0d); private float Cm; private float Cn; private float[] UA; private boolean UB; private Handler UC; private boolean UD; private boolean UE; private boolean UF; private final Runnable UG; private final a Ul; private float Um; private float Un; private final SensorManager Uo; private boolean Up; private long Uq; private long Ur; private float Us; private float Ut; private float Uu; private float Uv; private float Uw; private float[] Ux; private float[] Uy; private float[] Uz; private final IBus _bus; private final Object _lock = new Object(); private int jN; private boolean qZ() { return false; } @Override // android.hardware.SensorEventListener public void onAccuracyChanged(Sensor sensor, int i) { } @InterfaceC13391fZD public d(IBus iBus, SensorManager sensorManager) { a aVar = new a(); this.Ul = aVar; this.Uq = System.currentTimeMillis() - Ue; this.Ur = 0L; this.Us = BitmapDescriptorFactory.HUE_RED; this.Ut = BitmapDescriptorFactory.HUE_RED; this.Uu = 100.0f; this.Uv = BitmapDescriptorFactory.HUE_RED; this.Uw = BitmapDescriptorFactory.HUE_RED; this.Uz = new float[16]; this.UA = new float[16]; this.UB = false; this.UD = true; this.jN = 0; this.UE = false; this.UF = false; this.UG = new Runnable(this) { // from class: com.kofax.mobile.sdk.af.d.1 final d UH; { this.UH = this; } @Override // java.lang.Runnable public void run() { this.UH.rd(); if (this.UH.Up) { this.UH.UC.postDelayed(this.UH.UG, d.Ue); } } }; this._bus = iBus; iBus.register(aVar); this.Uo = sensorManager; } /* JADX INFO: Access modifiers changed from: private */ public void start() { if (this.Up) { return; } synchronized (this._lock) { if (this.Up) { return; } this.Up = true; HandlerThread handlerThread = new HandlerThread("Sensors"); handlerThread.start(); this.UC = new Handler(handlerThread.getLooper()); a(qW()); b(qX()); this.UC.postDelayed(this.UG, Ue); } } private Sensor qW() { Sensor defaultSensor = this.Uo.getDefaultSensor(11); if (defaultSensor != null && !qZ()) { return defaultSensor; } this.UB = true; return this.Uo.getDefaultSensor(1); } private Sensor qX() { if (this.UB) { return this.Uo.getDefaultSensor(2); } return null; } private void a(Sensor sensor) { this.UF = this.Uo.registerListener(this, sensor, qY(), this.UC); } private void b(Sensor sensor) { if (!this.UB || sensor == null) { return; } this.UE = this.Uo.registerListener(this, sensor, 2, this.UC); } private static int qY() { return Build.MODEL.equalsIgnoreCase(Utility.NEXUS_5) ? 1 : 0; } /* JADX INFO: Access modifiers changed from: private */ public void stop() { if (this.Up) { synchronized (this._lock) { if (this.Up) { this.Uo.unregisterListener(this); ju(); this.UC = null; this.Up = false; } } } } @Override // android.hardware.SensorEventListener public void onSensorChanged(SensorEvent sensorEvent) { if (this.UD) { a(sensorEvent.sensor, sensorEvent.values); } } protected void a(Sensor sensor, float[] fArr) { float[] b = b(sensor, fArr); if (b != null) { d(c(b)); rb(); rd(); rc(); } } private float[] b(Sensor sensor, float[] fArr) { float[] fArr2; if (this.UB && sensor.getType() != 1 && sensor.getType() != 2) { return null; } if (!this.UB && sensor.getType() != 11) { return null; } if (this.UB) { int type = sensor.getType(); if (type == 1) { this.Ux = (float[]) fArr.clone(); } else if (type == 2) { this.Uy = (float[]) fArr.clone(); } ra(); float[] fArr3 = this.Ux; if (fArr3 == null || (fArr2 = this.Uy) == null || !SensorManager.getRotationMatrix(this.Uz, null, fArr3, fArr2)) { return null; } } else { getRotationMatrixFromVector(this.Uz, fArr); } long currentTimeMillis = System.currentTimeMillis(); long j = currentTimeMillis - this.Uq; this.Ur = j; if (j < 100) { return null; } this.Uq = currentTimeMillis; return this.Uz; } private void ra() { float[] fArr; float[] fArr2; if (!this.UE && (fArr2 = this.Ux) != null) { this.Uy = e(fArr2); } else { if (this.UF || (fArr = this.Uy) == null) { return; } float[] fArr3 = {fArr[0], fArr[1], -fArr[2]}; this.Ux = fArr3; this.Uy = e(fArr3); } } private float[] c(float[] fArr) { int i = this.jN; int i2 = 2; int i3 = 129; if (i != 1) { if (i == 2) { i2 = 129; i3 = 130; } else if (i != 3) { i3 = 2; i2 = 1; } else { i3 = 1; i2 = 130; } } SensorManager.remapCoordinateSystem(fArr, i2, i3, this.UA); return this.UA; } private void d(float[] fArr) { float f; this.Cn = this.Us; this.Cm = this.Ut; float[] fArr2 = new float[9]; SensorManager.getOrientation(fArr, fArr2); float f2 = -fArr2[1]; this.Us = f2; this.Ut = fArr2[2]; float abs = Math.abs(f2); float abs2 = Math.abs(this.Ut); float f3 = Ug; if (abs2 > f3) { abs2 += f3 - abs2; } float f4 = Uh; boolean z = abs > f4; float f5 = Ui; boolean z2 = abs > f5; boolean z3 = abs < Uj; boolean z4 = abs2 > f4; if (z) { this.Ut = BitmapDescriptorFactory.HUE_RED; } else if (z2) { float f6 = 1.0f - ((abs - f5) / Uk); float f7 = this.Ut; if (f7 < (-f3)) { float f8 = Uf; f = ((f7 + f8) * f6) - f8; } else if (f7 > f3) { float f9 = Uf; f = ((f7 - f9) * f6) + f9; } else { f = f7 * f6; } this.Ut = f; } else if (z4 && z3) { this.Us = BitmapDescriptorFactory.HUE_RED; } this.Us = (float) Math.toDegrees(this.Us); this.Ut = (float) Math.toDegrees(this.Ut); } private void rb() { float f = ((float) this.Ur) / 1000.0f; this.Uv = (((float) Math.toRadians(this.Ut)) - ((float) Math.toRadians(this.Cm))) / f; this.Uw = (((float) Math.toRadians(this.Us)) - ((float) Math.toRadians(this.Cn))) / f; this.Uu = (float) Math.round(Math.max(Math.max((1.0f - (Math.abs(this.Uv) * 2.0f)) * 100.0f, (1.0f - (Math.abs(this.Uw) * 2.0f)) * 100.0f), 0.0d)); } private void rc() { Handler handler = this.UC; if (handler != null) { handler.removeCallbacks(this.UG); this.UC.postDelayed(this.UG, Ue); } } /* JADX INFO: Access modifiers changed from: private */ public void rd() { this.Ul.ri(); } private float[] e(float[] fArr) { char c = Math.abs(fArr[1]) < Math.abs(fArr[0]) ? (char) 1 : (char) 0; if (Math.abs(fArr[2]) < Math.abs(fArr[c])) { c = 2; } if (c == 0) { return new float[]{BitmapDescriptorFactory.HUE_RED, fArr[2], -fArr[1]}; } if (c == 1) { return new float[]{fArr[2], BitmapDescriptorFactory.HUE_RED, -fArr[0]}; } return new float[]{fArr[1], -fArr[0], BitmapDescriptorFactory.HUE_RED}; } private void ju() { re(); } private void re() { this.UC.getLooper().quitSafely(); } private void rf() { this.UC.getLooper().quit(); } /* JADX INFO: Access modifiers changed from: package-private */ /* loaded from: classes3.dex */ public class a { final d UH; private a(d dVar) { this.UH = dVar; } @InterfaceC13006fNs public void b(SurfaceChangedEvent surfaceChangedEvent) { if (surfaceChangedEvent.hasSurface) { this.UH.start(); } else { this.UH.stop(); } } @InterfaceC13006fNs public void c(v vVar) { this.UH.jN = vVar.rotation; } @InterfaceC13004fNq public LevelChangedEvent rg() { float f = this.UH.Us; float f2 = this.UH.Um; return new LevelChangedEvent(f - f2, this.UH.Ut - this.UH.Un, this.UH.Uw, this.UH.Uv); } @InterfaceC13004fNq public ay rh() { return new ay(Math.round(this.UH.Uu)); } @InterfaceC13006fNs public void a(s sVar) { int i = sVar.Iy; if (i >= -180 && i <= 180) { this.UH.Um = i; return; } throw new KmcRuntimeException(ErrorInfo.KMC_UI_INVALID_DECLINATION_ARGUMENT); } @InterfaceC13006fNs public void a(t tVar) { int i = tVar.Iz; if (i >= -180 && i <= 180) { this.UH.Un = i; return; } throw new KmcRuntimeException(ErrorInfo.KMC_UI_INVALID_DECLINATION_ARGUMENT); } @InterfaceC13006fNs public void a(u uVar) { this.UH.UD = uVar.enabled; } public void ri() { this.UH._bus.post(rg()); this.UH._bus.post(rh()); } } private static void getRotationMatrixFromVector(float[] fArr, float[] fArr2) { float sqrt; float f = fArr2[0]; float f2 = fArr2[1]; float f3 = fArr2[2]; if (fArr2.length == 4) { sqrt = fArr2[3]; } else { float f4 = ((1.0f - (f * f)) - (f2 * f2)) - (f3 * f3); sqrt = f4 > BitmapDescriptorFactory.HUE_RED ? (float) Math.sqrt(f4) : 0.0f; } float f5 = f * 2.0f; float f6 = f * f5; float f7 = f2 * 2.0f; float f8 = f7 * f2; float f9 = 2.0f * f3; float f10 = f9 * f3; float f11 = f2 * f5; float f12 = f9 * sqrt; float f13 = f5 * f3; float f14 = f7 * sqrt; float f15 = f7 * f3; float f16 = f5 * sqrt; if (fArr.length == 9) { fArr[0] = (1.0f - f8) - f10; fArr[1] = f11 - f12; fArr[2] = f13 + f14; fArr[3] = f11 + f12; float f17 = 1.0f - f6; fArr[4] = f17 - f10; fArr[5] = f15 - f16; fArr[6] = f13 - f14; fArr[7] = f15 + f16; fArr[8] = f17 - f8; return; } if (fArr.length == 16) { fArr[0] = (1.0f - f8) - f10; fArr[1] = f11 - f12; fArr[2] = f13 + f14; fArr[3] = 0.0f; fArr[4] = f11 + f12; float f18 = 1.0f - f6; fArr[5] = f18 - f10; fArr[6] = f15 - f16; fArr[7] = 0.0f; fArr[8] = f13 - f14; fArr[9] = f15 + f16; fArr[10] = f18 - f8; fArr[11] = 0.0f; fArr[14] = 0.0f; fArr[13] = 0.0f; fArr[12] = 0.0f; fArr[15] = 1.0f; } } }