package com.facetec.sdk; import android.content.Context; import android.hardware.Sensor; import android.hardware.SensorEvent; import android.hardware.SensorEventListener; import android.hardware.SensorManager; import android.os.Handler; import com.facetec.sdk.cd; import com.google.android.gms.maps.model.BitmapDescriptorFactory; import com.google.common.primitives.UnsignedBytes; import java.lang.ref.WeakReference; import java.util.Timer; /* JADX INFO: Access modifiers changed from: package-private */ /* loaded from: classes.dex */ public final class bs implements SensorEventListener { private Timer a; private Context b; private Timer d; private WeakReference c = new WeakReference<>(null); private WeakReference j = new WeakReference<>(null); private boolean g = false; private boolean i = false; private int h = 0; private Sensor e = a().getDefaultSensor(5); /* JADX INFO: Access modifiers changed from: package-private */ @FunctionalInterface /* loaded from: classes.dex */ public interface d { void onDarkLightDetected(); } @Override // android.hardware.SensorEventListener public final void onAccuracyChanged(Sensor sensor, int i) { } private SensorManager a() { return (SensorManager) this.b.getSystemService("sensor"); } /* JADX INFO: Access modifiers changed from: package-private */ public bs(Context context) { this.b = context.getApplicationContext(); } /* JADX INFO: Access modifiers changed from: package-private */ public final void b(d dVar, cd cdVar) { synchronized (this) { this.c = new WeakReference<>(dVar); this.j = new WeakReference<>(cdVar); if (this.e != null) { new Handler().postDelayed(new Runnable(this, this) { // from class: com.facetec.sdk.bs$$ExternalSyntheticLambda2 public final bs f$0; public final bs f$1; @Override // java.lang.Runnable public final void run() { this.f$0.b(this.f$1); } { this.f$0 = this; this.f$1 = this; } }, 50L); this.i = true; } da daVar = new da(new Runnable(this) { // from class: com.facetec.sdk.bs$$ExternalSyntheticLambda3 public final bs f$0; @Override // java.lang.Runnable public final void run() { this.f$0.h(); } { this.f$0 = this; } }); Timer timer = new Timer(); this.d = timer; try { timer.scheduleAtFixedRate(daVar, 500L, 1000L); } catch (IllegalStateException unused) { } } } /* JADX INFO: Access modifiers changed from: private */ public /* synthetic */ void b(bs bsVar) { a().registerListener(bsVar, this.e, 0); } private void c() { synchronized (this) { if (this.a == null) { Timer timer = new Timer(); this.a = timer; try { timer.schedule(new da(new Runnable(this) { // from class: com.facetec.sdk.bs$$ExternalSyntheticLambda0 public final bs f$0; @Override // java.lang.Runnable public final void run() { this.f$0.e(); } { this.f$0 = this; } }), 200L); } catch (IllegalStateException unused) { } } } } /* JADX INFO: Access modifiers changed from: private */ public /* synthetic */ void e() { d dVar = this.c.get(); if (dVar != null) { dVar.onDarkLightDetected(); d(); } } private void b() { Timer timer = this.a; if (timer != null) { timer.cancel(); this.a = null; } } /* JADX INFO: Access modifiers changed from: package-private */ public final void d() { this.g = true; final Sensor sensor = this.e; if (sensor != null) { this.e = null; cy.b(new Runnable(this, sensor) { // from class: com.facetec.sdk.bs$$ExternalSyntheticLambda1 public final bs f$0; public final Sensor f$1; @Override // java.lang.Runnable public final void run() { this.f$0.d(this.f$1); } { this.f$0 = this; this.f$1 = sensor; } }); } b(); Timer timer = this.d; if (timer != null) { timer.cancel(); this.d = null; } this.c.clear(); } /* JADX INFO: Access modifiers changed from: private */ public /* synthetic */ void d(Sensor sensor) { a().unregisterListener(this, sensor); } @Override // android.hardware.SensorEventListener public final void onSensorChanged(SensorEvent sensorEvent) { if (this.g) { return; } this.h = 0; if (sensorEvent.values[0] < 3.0f) { c(); } else { b(); } } /* JADX INFO: Access modifiers changed from: private */ public /* synthetic */ void h() { cd.d j; this.h++; cd cdVar = this.j.get(); if (cdVar != null) { if ((this.i && this.h <= 1) || (j = cdVar.j()) == null || j.a == null) { return; } byte[] bArr = j.a; int i = j.b * j.c; int i2 = i / 4; float f = BitmapDescriptorFactory.HUE_RED; int i3 = 0; for (int i4 = 1; i4 <= i; i4 += 4) { i3 += bArr[i4 - 1] & UnsignedBytes.MAX_VALUE; if (i4 % 8421504 == 0) { f += i3 / i2; i3 = 0; } } if (((int) (f + (i3 / i2))) < 75.0f) { c(); } else { b(); } } } }