package com.epson.cameracopy.device;

import android.app.Service;
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(Service.SENSOR_SERVICE));
    }

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

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

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

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

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

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

    public void resetTime() {
        mLastOverTime = 0;
    }

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

    public float getPosX() {
        return mPosX;
    }

    public float getPosY() {
        return 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 ((Math.abs(sensorEvent.values[i])) > mReferenceAccelaration) {
                mLastOverTime = System.currentTimeMillis();
                break;
            } else {
                i++;
            }
        }
        if (mLastOverTime == 0) {
            mLastOverTime = System.currentTimeMillis();
        }
    }

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

    public long getLastOverTime2() {
        return mLastOverTime2;
    }

    public long getLastAccelOverTime() {
        return mLastOverTime;
    }
}
