package com.epson.cameracopy.device;

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;

public class AptSensorAdapter implements SensorEventListener {
    static final /* synthetic */ boolean $assertionsDisabled = false;
    private float m2BaseValue = ((float) Math.pow(0.4d, 2.0d));
    private int mDisplayRotation;
    private boolean mIsValidValue;
    private long mLastOverTime;
    private long mLastOverTime2;
    private float mPosX;
    private float mPosY;
    private float mPreD0;
    private float mPreD1;
    private float mPreD2;
    private double mReferenceAccelaration;
    private final int mSensorDelay = 2;
    private SensorManager mSensorManager;

    public void onAccuracyChanged(Sensor sensor, int i) {
    }

    public static boolean hasAptSensor(SensorManager sensorManager) {
        return (sensorManager.getDefaultSensor(1) == null || sensorManager.getDefaultSensor(10) == null) ? false : true;
    }

    public static boolean hasAptSensor(Context context) {
        return hasAptSensor((SensorManager) context.getSystemService("sensor"));
    }

    public void setSensorManager(SensorManager sensorManager) {
        this.mSensorManager = sensorManager;
    }

    public void start() {
        Sensor defaultSensor = this.mSensorManager.getDefaultSensor(1);
        if (defaultSensor != null) {
            this.mSensorManager.registerListener(this, defaultSensor, 2);
        }
        Sensor defaultSensor2 = this.mSensorManager.getDefaultSensor(10);
        if (defaultSensor2 != null) {
            this.mSensorManager.registerListener(this, defaultSensor2, 2);
        }
        this.mLastOverTime = 0;
        this.mLastOverTime2 = 0;
    }

    public void stop() {
        this.mSensorManager.unregisterListener(this);
    }

    public static double getRangeCompValue(int i) {
        return Math.pow(Math.sin((((double) i) * 3.141592653589793d) / 180.0d) * 9.806650161743164d, 2.0d);
    }

    public void setDisplayRotation(int i) {
        this.mDisplayRotation = i;
    }

    public void setReferenceAccelaration(double d) {
        this.mReferenceAccelaration = d;
        this.mLastOverTime = 0;
    }

    public void resetTime() {
        this.mLastOverTime = 0;
    }

    public boolean isTerminalAngleInRange(double d) {
        if (!this.mIsValidValue) {
            return false;
        }
        float f = this.mPosX;
        float f2 = this.mPosY;
        if (d >= ((double) ((f * f) + (f2 * f2)))) {
            return true;
        }
        return false;
    }

    public float getPosX() {
        return this.mPosX;
    }

    public float getPosY() {
        return this.mPosY;
    }

    public void onSensorChanged(SensorEvent sensorEvent) {
        int type = sensorEvent.sensor.getType();
        if (type == 1) {
            updateAccValue(sensorEvent);
        } else if (type == 10) {
            updateLinearAccelValue(sensorEvent);
        }
    }

    private void updateLinearAccelValue(SensorEvent sensorEvent) {
        int i = 0;
        while (true) {
            if (i >= 3) {
                break;
            } else if (((double) Math.abs(sensorEvent.values[i])) > this.mReferenceAccelaration) {
                this.mLastOverTime = System.currentTimeMillis();
                break;
            } else {
                i++;
            }
        }
        if (this.mLastOverTime == 0) {
            this.mLastOverTime = System.currentTimeMillis();
        }
    }

    private void updateAccValue(SensorEvent sensorEvent) {
        this.mIsValidValue = true;
        switch (this.mDisplayRotation) {
            case 0:
                this.mPosX = sensorEvent.values[0];
                this.mPosY = sensorEvent.values[1];
                break;
            case 1:
                this.mPosX = -sensorEvent.values[1];
                this.mPosY = sensorEvent.values[0];
                break;
            case 2:
                this.mPosX = -sensorEvent.values[0];
                this.mPosY = -sensorEvent.values[1];
                break;
            case 3:
                this.mPosX = sensorEvent.values[1];
                this.mPosY = -sensorEvent.values[0];
                break;
        }
        float f = sensorEvent.values[0];
        float f2 = sensorEvent.values[1];
        float f3 = sensorEvent.values[2];
        this.mPreD0 = f;
        this.mPreD1 = f2;
        this.mPreD2 = f3;
        if (((float) (Math.pow((double) (this.mPreD0 - f), 2.0d) + Math.pow((double) (this.mPreD1 - f2), 2.0d) + Math.pow((double) (this.mPreD2 - f3), 2.0d))) > this.m2BaseValue) {
            this.mLastOverTime2 = System.currentTimeMillis();
        }
    }

    public long getLastOverTime2() {
        return this.mLastOverTime2;
    }

    public long getLastAccelOverTime() {
        return this.mLastOverTime;
    }
}
