AptSensorAdapter.java 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. package com.epson.cameracopy.device;
  2. import android.content.Context;
  3. import android.hardware.Sensor;
  4. import android.hardware.SensorEvent;
  5. import android.hardware.SensorEventListener;
  6. import android.hardware.SensorManager;
  7. public class AptSensorAdapter implements SensorEventListener {
  8. static final /* synthetic */ boolean $assertionsDisabled = false;
  9. private float m2BaseValue = ((float) Math.pow(0.4d, 2.0d));
  10. private int mDisplayRotation;
  11. private boolean mIsValidValue;
  12. private long mLastOverTime;
  13. private long mLastOverTime2;
  14. private float mPosX;
  15. private float mPosY;
  16. private float mPreD0;
  17. private float mPreD1;
  18. private float mPreD2;
  19. private double mReferenceAccelaration;
  20. private final int mSensorDelay = 2;
  21. private SensorManager mSensorManager;
  22. public void onAccuracyChanged(Sensor sensor, int i) {
  23. }
  24. public static boolean hasAptSensor(SensorManager sensorManager) {
  25. return (sensorManager.getDefaultSensor(1) == null || sensorManager.getDefaultSensor(10) == null) ? false : true;
  26. }
  27. public static boolean hasAptSensor(Context context) {
  28. return hasAptSensor((SensorManager) context.getSystemService("sensor"));
  29. }
  30. public void setSensorManager(SensorManager sensorManager) {
  31. this.mSensorManager = sensorManager;
  32. }
  33. public void start() {
  34. Sensor defaultSensor = this.mSensorManager.getDefaultSensor(1);
  35. if (defaultSensor != null) {
  36. this.mSensorManager.registerListener(this, defaultSensor, 2);
  37. }
  38. Sensor defaultSensor2 = this.mSensorManager.getDefaultSensor(10);
  39. if (defaultSensor2 != null) {
  40. this.mSensorManager.registerListener(this, defaultSensor2, 2);
  41. }
  42. this.mLastOverTime = 0;
  43. this.mLastOverTime2 = 0;
  44. }
  45. public void stop() {
  46. this.mSensorManager.unregisterListener(this);
  47. }
  48. public static double getRangeCompValue(int i) {
  49. return Math.pow(Math.sin((((double) i) * 3.141592653589793d) / 180.0d) * 9.806650161743164d, 2.0d);
  50. }
  51. public void setDisplayRotation(int i) {
  52. this.mDisplayRotation = i;
  53. }
  54. public void setReferenceAccelaration(double d) {
  55. this.mReferenceAccelaration = d;
  56. this.mLastOverTime = 0;
  57. }
  58. public void resetTime() {
  59. this.mLastOverTime = 0;
  60. }
  61. public boolean isTerminalAngleInRange(double d) {
  62. if (!this.mIsValidValue) {
  63. return false;
  64. }
  65. float f = this.mPosX;
  66. float f2 = this.mPosY;
  67. if (d >= ((double) ((f * f) + (f2 * f2)))) {
  68. return true;
  69. }
  70. return false;
  71. }
  72. public float getPosX() {
  73. return this.mPosX;
  74. }
  75. public float getPosY() {
  76. return this.mPosY;
  77. }
  78. public void onSensorChanged(SensorEvent sensorEvent) {
  79. int type = sensorEvent.sensor.getType();
  80. if (type == 1) {
  81. updateAccValue(sensorEvent);
  82. } else if (type == 10) {
  83. updateLinearAccelValue(sensorEvent);
  84. }
  85. }
  86. private void updateLinearAccelValue(SensorEvent sensorEvent) {
  87. int i = 0;
  88. while (true) {
  89. if (i >= 3) {
  90. break;
  91. } else if (((double) Math.abs(sensorEvent.values[i])) > this.mReferenceAccelaration) {
  92. this.mLastOverTime = System.currentTimeMillis();
  93. break;
  94. } else {
  95. i++;
  96. }
  97. }
  98. if (this.mLastOverTime == 0) {
  99. this.mLastOverTime = System.currentTimeMillis();
  100. }
  101. }
  102. private void updateAccValue(SensorEvent sensorEvent) {
  103. this.mIsValidValue = true;
  104. switch (this.mDisplayRotation) {
  105. case 0:
  106. this.mPosX = sensorEvent.values[0];
  107. this.mPosY = sensorEvent.values[1];
  108. break;
  109. case 1:
  110. this.mPosX = -sensorEvent.values[1];
  111. this.mPosY = sensorEvent.values[0];
  112. break;
  113. case 2:
  114. this.mPosX = -sensorEvent.values[0];
  115. this.mPosY = -sensorEvent.values[1];
  116. break;
  117. case 3:
  118. this.mPosX = sensorEvent.values[1];
  119. this.mPosY = -sensorEvent.values[0];
  120. break;
  121. }
  122. float f = sensorEvent.values[0];
  123. float f2 = sensorEvent.values[1];
  124. float f3 = sensorEvent.values[2];
  125. this.mPreD0 = f;
  126. this.mPreD1 = f2;
  127. this.mPreD2 = f3;
  128. 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) {
  129. this.mLastOverTime2 = System.currentTimeMillis();
  130. }
  131. }
  132. public long getLastOverTime2() {
  133. return this.mLastOverTime2;
  134. }
  135. public long getLastAccelOverTime() {
  136. return this.mLastOverTime;
  137. }
  138. }