package com.unity3d.player;

import android.content.Context;
import android.hardware.GeomagneticField;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Build;
import android.os.Bundle;
import android.view.WindowManager;
import com.prime31.util.IabHelper;
import com.tapjoy.TapjoyConstants;
import com.tapjoy.TapjoyVideoObject;
import java.util.List;

/* JADX INFO: Access modifiers changed from: package-private */
/* loaded from: /storage/emulated/0/Documents/jadec/sources/com.UBI.A90.WW/dex-files/2.dex */
public final class r implements SensorEventListener, LocationListener {
    private static final boolean Q;
    private static final int[] e = {1, 1, 0, 1, -1, 1, 1, 0, -1, -1, 0, 1, 1, -1, 1, 0};
    private static final float[][] f = {new float[]{0.0f, 0.0f, 0.0f, 1.0f}, new float[]{0.0f, 0.0f, ((float) Math.sqrt(2.0d)) * 0.5f, (-((float) Math.sqrt(2.0d))) * 0.5f}, new float[]{0.0f, 0.0f, 1.0f, 0.0f}, new float[]{0.0f, 0.0f, (-((float) Math.sqrt(2.0d))) * 0.5f, (-((float) Math.sqrt(2.0d))) * 0.5f}};
    private double G;
    private int K;
    private Location M;
    private final Context b;
    private final UnityPlayer c;
    private final WindowManager d;
    private float i;
    private float j;
    private float k;
    private long l;
    private float m;
    private float n;
    private float o;
    private long p;
    private float q;
    private float r;
    private float s;
    private long t;
    private float u;
    private float v;
    private float w;
    private long x;
    private float[] g = new float[3];
    private float[] h = new float[3];
    private float[] y = new float[4];
    private float[] z = new float[4];
    private Runnable A = new Runnable() { // from class: com.unity3d.player.r.1
        @Override // java.lang.Runnable
        public final void run() {
            r.this.c.nativeSensor(r.this.i, r.this.j, r.this.k, r.this.l);
        }
    };
    private Runnable B = new Runnable() { // from class: com.unity3d.player.r.2
        @Override // java.lang.Runnable
        public final void run() {
            r.this.c.nativeGyro(r.this.m, r.this.n, r.this.o, r.this.p);
        }
    };
    private Runnable C = new Runnable() { // from class: com.unity3d.player.r.3
        @Override // java.lang.Runnable
        public final void run() {
            r.this.c.nativeGravity(r.this.q, r.this.r, r.this.s, r.this.t);
        }
    };
    private Runnable D = new Runnable() { // from class: com.unity3d.player.r.4
        @Override // java.lang.Runnable
        public final void run() {
            r.this.c.nativeLinearAcc(r.this.u, r.this.v, r.this.w, r.this.x);
        }
    };
    private Runnable E = new Runnable() { // from class: com.unity3d.player.r.5
        @Override // java.lang.Runnable
        public final void run() {
            UnityPlayer unityPlayer = r.this.c;
            float f2 = r.this.y[0];
            float f3 = r.this.y[1];
            float f4 = r.this.y[2];
            float f5 = r.this.y[3];
            r rVar = r.this;
            unityPlayer.nativeAttitude(f2, f3, f4, f5, r.f());
        }
    };
    private float[] F = new float[5];
    private Runnable H = new Runnable() { // from class: com.unity3d.player.r.6
        @Override // java.lang.Runnable
        public final void run() {
            r.this.c.nativeCompass(r.this.F[0], r.this.F[1], r.this.F[2], r.this.F[3], r.this.F[4], r.this.G);
        }
    };
    private float[] I = new float[9];
    private float[] J = new float[3];
    private Runnable L = new Runnable() { // from class: com.unity3d.player.r.7
        @Override // java.lang.Runnable
        public final void run() {
            r.this.c.nativeDeviceOrientation(r.this.K);
        }
    };
    private float N = 0.0f;
    private boolean O = false;
    private int P = 0;
    protected boolean a = true;
    private boolean R = false;
    private int S = 0;

    static {
        boolean z = false;
        if (Build.MANUFACTURER.equalsIgnoreCase("Amazon") && (Build.MODEL.equalsIgnoreCase("KFTT") || Build.MODEL.equalsIgnoreCase("KFJWI") || Build.MODEL.equalsIgnoreCase("KFJWA"))) {
            z = true;
        }
        Q = z;
    }

    /* JADX INFO: Access modifiers changed from: protected */
    public r(Context context, UnityPlayer unityPlayer) {
        this.b = context;
        this.c = unityPlayer;
        this.d = (WindowManager) this.b.getSystemService("window");
    }

    public static int a(SensorEvent sensorEvent, int i, int i2) {
        int i3;
        float f2;
        int i4;
        float f3;
        float f4 = sensorEvent.values[0];
        float f5 = sensorEvent.values[1];
        float f6 = sensorEvent.values[2];
        float sqrt = 1.0f / ((float) Math.sqrt(((f4 * f4) + (f5 * f5)) + (f6 * f6)));
        float f7 = f4 * sqrt;
        float f8 = f5 * sqrt;
        float f9 = f6 * sqrt;
        int i5 = (i - (i2 == 1 ? 0 : i2 == 0 ? 1 : i2 == 9 ? 2 : i2 == 8 ? 3 : 0)) & 3;
        if (i5 != 1) {
            if (i5 == 3) {
                f7 = -f7;
            } else {
                f8 = f7;
                f7 = f8;
            }
        }
        float f10 = Q ? -f8 : f8;
        if (-1.0f < f7) {
            i3 = 1;
            f2 = f7;
        } else {
            i3 = 0;
            f2 = -1.0f;
        }
        if (f2 < (-f7)) {
            f3 = -f7;
            i4 = 2;
        } else {
            i4 = i3;
            f3 = f2;
        }
        if (f3 < f10) {
            i4 = 3;
            f3 = f10;
        }
        if (f3 < (-f10)) {
            f3 = -f10;
            i4 = 4;
        }
        if (f3 < f9) {
            i4 = 5;
            f3 = f9;
        }
        if (f3 < (-f9)) {
            f3 = -f9;
            i4 = 6;
        }
        if (f3 < 0.5d * Math.sqrt(3.0d)) {
            return 0;
        }
        return i4;
    }

    private void a(int i) {
        this.S = i;
        this.c.nativeSetLocationStatus(i);
    }

    private void a(SensorEvent sensorEvent) {
        int orientation = this.a ? this.d.getDefaultDisplay().getOrientation() : 0;
        int orientation2 = this.c.getOrientation();
        int i = e[(orientation * 4) + 2];
        int i2 = e[(orientation * 4) + 3];
        this.i = e[(orientation * 4) + 0] * (-0.10197162f) * sensorEvent.values[i];
        this.j = sensorEvent.values[i2] * e[(orientation * 4) + 1] * (-0.10197162f);
        this.k = sensorEvent.values[2] * (-0.10197162f);
        this.l = sensorEvent.timestamp;
        this.c.queueEvent(this.A);
        this.K = a(sensorEvent, this.d.getDefaultDisplay().getOrientation(), orientation2);
        this.c.queueEvent(this.L);
    }

    private void a(Location location) {
        if (location != null && a(location, this.M)) {
            this.M = location;
            this.c.nativeSetLocation((float) location.getLatitude(), (float) location.getLongitude(), (float) location.getAltitude(), location.getAccuracy(), location.getTime() / 1000.0d);
        }
    }

    private static void a(float[] fArr, float[] fArr2, float[] fArr3) {
        fArr[0] = (((fArr2[3] * fArr3[0]) + (fArr2[0] * fArr3[3])) + (fArr2[1] * fArr3[2])) - (fArr2[2] * fArr3[1]);
        fArr[1] = (((fArr2[3] * fArr3[1]) + (fArr2[1] * fArr3[3])) + (fArr2[2] * fArr3[0])) - (fArr2[0] * fArr3[2]);
        fArr[2] = (((fArr2[3] * fArr3[2]) + (fArr2[2] * fArr3[3])) + (fArr2[0] * fArr3[1])) - (fArr2[1] * fArr3[0]);
        fArr[3] = (((fArr2[3] * fArr3[3]) - (fArr2[0] * fArr3[0])) - (fArr2[1] * fArr3[1])) - (fArr2[2] * fArr3[2]);
    }

    private static boolean a(Location location, Location location2) {
        if (location2 == null) {
            return true;
        }
        long time = location.getTime() - location2.getTime();
        boolean z = time > 120000;
        boolean z2 = time < -120000;
        boolean z3 = time > 0;
        if (z) {
            return true;
        }
        if (z2) {
            return false;
        }
        int accuracy = (int) (location.getAccuracy() - location2.getAccuracy());
        boolean z4 = accuracy > 0;
        boolean z5 = accuracy < 0;
        boolean z6 = (accuracy > 200) | (location.getAccuracy() == 0.0f);
        boolean a = a(location.getProvider(), location2.getProvider());
        if (z5) {
            return true;
        }
        if (!z3 || z4) {
            return z3 && !z6 && a;
        }
        return true;
    }

    private static boolean a(String str, String str2) {
        return str == null ? str2 == null : str.equals(str2);
    }

    static /* synthetic */ long f() {
        return 0L;
    }

    public final void a(float f2) {
        this.N = f2;
    }

    public final boolean a() {
        return !((LocationManager) this.b.getSystemService("location")).getProviders(new Criteria(), true).isEmpty();
    }

    public final void b() {
        LocationProvider locationProvider;
        this.R = false;
        if (this.O) {
            m.Log(5, "Location_StartUpdatingLocation already started!");
        } else if (!a()) {
            a(3);
        } else {
            LocationManager locationManager = (LocationManager) this.b.getSystemService("location");
            a(1);
            List<String> providers = locationManager.getProviders(true);
            if (providers.isEmpty()) {
                a(3);
                return;
            }
            if (this.P == 2) {
                for (String str : providers) {
                    LocationProvider provider = locationManager.getProvider(str);
                    if (provider.getAccuracy() == 2) {
                        locationProvider = provider;
                        break;
                    }
                }
            }
            locationProvider = null;
            for (String str2 : providers) {
                if (locationProvider == null || locationManager.getProvider(str2).getAccuracy() != 1) {
                    a(locationManager.getLastKnownLocation(str2));
                    locationManager.requestLocationUpdates(str2, 0L, this.N, this, this.b.getMainLooper());
                    this.O = true;
                }
            }
        }
    }

    public final void b(float f2) {
        if (f2 < 100.0f) {
            this.P = 1;
        } else if (f2 < 500.0f) {
            this.P = 1;
        } else {
            this.P = 2;
        }
    }

    public final void c() {
        ((LocationManager) this.b.getSystemService("location")).removeUpdates(this);
        this.O = false;
        this.M = null;
        a(0);
    }

    public final void d() {
        if (this.S == 1 || this.S == 2) {
            this.R = true;
            c();
        }
    }

    public final void e() {
        if (this.R) {
            b();
        }
    }

    @Override // android.hardware.SensorEventListener
    public final void onAccuracyChanged(Sensor sensor, int i) {
    }

    @Override // android.location.LocationListener
    public final void onLocationChanged(Location location) {
        a(2);
        a(location);
    }

    @Override // android.location.LocationListener
    public final void onProviderDisabled(String str) {
        this.M = null;
    }

    @Override // android.location.LocationListener
    public final void onProviderEnabled(String str) {
    }

    @Override // android.hardware.SensorEventListener
    public final void onSensorChanged(SensorEvent sensorEvent) {
        float[] fArr;
        char c;
        float sqrt;
        int orientation = this.a ? this.d.getDefaultDisplay().getOrientation() : 0;
        float f2 = e[(orientation * 4) + 0];
        float f3 = e[(orientation * 4) + 1];
        int i = e[(orientation * 4) + 2];
        int i2 = e[(orientation * 4) + 3];
        switch (sensorEvent.sensor.getType()) {
            case 1:
                this.g = (float[]) sensorEvent.values.clone();
                a(sensorEvent);
                return;
            case 2:
                this.h = (float[]) sensorEvent.values.clone();
                this.G = System.currentTimeMillis() / 1000.0d;
                if (!SensorManager.getRotationMatrix(this.I, null, this.g, this.h)) {
                    return;
                }
                SensorManager.getOrientation(this.I, this.J);
                float degrees = (float) Math.toDegrees(this.J[0]);
                switch (orientation) {
                    case 1:
                        degrees += 90.0f;
                        break;
                    case 2:
                        degrees += 180.0f;
                        break;
                    case 3:
                        degrees += 270.0f;
                        break;
                }
                while (degrees >= 360.0f) {
                    degrees -= 360.0f;
                }
                float f4 = degrees < 0.0f ? degrees + 360.0f : degrees;
                this.F[0] = this.h[0];
                this.F[1] = this.h[1];
                this.F[2] = this.h[2];
                this.F[3] = f4;
                if (this.M != null && System.currentTimeMillis() - this.M.getTime() < TapjoyConstants.RESUME_TOTAL_TIME) {
                    f4 += new GeomagneticField((float) this.M.getLatitude(), (float) this.M.getLongitude(), (float) this.M.getAltitude(), this.M.getTime()).getDeclination();
                }
                while (true) {
                    float f5 = f4;
                    if (f5 < 360.0f) {
                        if (f5 < 0.0f) {
                            f5 += 360.0f;
                        }
                        this.F[4] = f5;
                        this.c.queueEvent(this.H);
                        return;
                    }
                    f4 = f5 - 360.0f;
                }
                break;
            case 3:
            case 5:
            case 6:
            case IabHelper.BILLING_RESPONSE_RESULT_ITEM_ALREADY_OWNED /* 7 */:
            case 8:
            default:
                return;
            case 4:
                this.m = f2 * sensorEvent.values[i];
                this.n = sensorEvent.values[i2] * f3;
                this.o = sensorEvent.values[2];
                this.p = sensorEvent.timestamp;
                this.c.queueEvent(this.B);
                return;
            case 9:
                this.q = f2 * (-0.10197162f) * sensorEvent.values[i];
                this.r = f3 * (-0.10197162f) * sensorEvent.values[i2];
                this.s = sensorEvent.values[2] * (-0.10197162f);
                this.t = sensorEvent.timestamp;
                this.c.queueEvent(this.C);
                return;
            case TapjoyVideoObject.BUTTON_MAX /* 10 */:
                this.u = f2 * (-0.10197162f) * sensorEvent.values[i];
                this.v = f3 * (-0.10197162f) * sensorEvent.values[i2];
                this.w = sensorEvent.values[2] * (-0.10197162f);
                this.x = sensorEvent.timestamp;
                this.c.queueEvent(this.D);
                return;
            case 11:
                this.z[0] = sensorEvent.values[0];
                this.z[1] = sensorEvent.values[1];
                this.z[2] = sensorEvent.values[2];
                if (sensorEvent.values.length == 4) {
                    fArr = this.z;
                    c = 3;
                    sqrt = sensorEvent.values[3];
                } else {
                    float f6 = (this.z[0] * this.z[0]) + (this.z[1] * this.z[1]) + (this.z[2] * this.z[2]);
                    fArr = this.z;
                    c = 3;
                    sqrt = f6 < 1.0f ? (float) Math.sqrt(1.0f - f6) : 0.0f;
                }
                fArr[c] = sqrt;
                a(this.y, this.z, f[orientation]);
                this.c.queueEvent(this.E);
                return;
        }
    }

    @Override // android.location.LocationListener
    public final void onStatusChanged(String str, int i, Bundle bundle) {
    }
}
