diff --git a/res/drawable-hdpi/calibrate_1.png b/res/drawable-hdpi/calibrate_1.png new file mode 100644 index 0000000..2b53ff4 Binary files /dev/null and b/res/drawable-hdpi/calibrate_1.png differ diff --git a/res/drawable-hdpi/calibrate_2.png b/res/drawable-hdpi/calibrate_2.png new file mode 100644 index 0000000..e22bb19 Binary files /dev/null and b/res/drawable-hdpi/calibrate_2.png differ diff --git a/res/drawable-hdpi/calibrate_3.png b/res/drawable-hdpi/calibrate_3.png new file mode 100644 index 0000000..1a11897 Binary files /dev/null and b/res/drawable-hdpi/calibrate_3.png differ diff --git a/res/drawable-hdpi/calibrate_4.png b/res/drawable-hdpi/calibrate_4.png new file mode 100644 index 0000000..0c2f27f Binary files /dev/null and b/res/drawable-hdpi/calibrate_4.png differ diff --git a/res/drawable-hdpi/calibrate_5.png b/res/drawable-hdpi/calibrate_5.png new file mode 100644 index 0000000..2c1d8a3 Binary files /dev/null and b/res/drawable-hdpi/calibrate_5.png differ diff --git a/res/drawable-hdpi/calibrate_6.png b/res/drawable-hdpi/calibrate_6.png new file mode 100644 index 0000000..ff839d6 Binary files /dev/null and b/res/drawable-hdpi/calibrate_6.png differ diff --git a/res/drawable-hdpi/calibrate_7.png b/res/drawable-hdpi/calibrate_7.png new file mode 100644 index 0000000..5671160 Binary files /dev/null and b/res/drawable-hdpi/calibrate_7.png differ diff --git a/res/drawable-hdpi/guide.png b/res/drawable-hdpi/guide.png new file mode 100644 index 0000000..9ee54c9 Binary files /dev/null and b/res/drawable-hdpi/guide.png differ diff --git a/res/drawable-xhdpi/app_icon.png b/res/drawable-xhdpi/app_icon.png new file mode 100644 index 0000000..59b9e1f Binary files /dev/null and b/res/drawable-xhdpi/app_icon.png differ diff --git a/res/drawable-xhdpi/background_bottom.9.png b/res/drawable-xhdpi/background_bottom.9.png new file mode 100644 index 0000000..96e2489 Binary files /dev/null and b/res/drawable-xhdpi/background_bottom.9.png differ diff --git a/res/drawable-xhdpi/background_compass.png b/res/drawable-xhdpi/background_compass.png new file mode 100644 index 0000000..b4fb8ee Binary files /dev/null and b/res/drawable-xhdpi/background_compass.png differ diff --git a/res/drawable-xhdpi/background_light.png b/res/drawable-xhdpi/background_light.png new file mode 100644 index 0000000..755b4cf Binary files /dev/null and b/res/drawable-xhdpi/background_light.png differ diff --git a/res/drawable-xhdpi/background_texture.png b/res/drawable-xhdpi/background_texture.png new file mode 100644 index 0000000..d5ce3c1 Binary files /dev/null and b/res/drawable-xhdpi/background_texture.png differ diff --git a/res/drawable-xhdpi/calibrate_1.png b/res/drawable-xhdpi/calibrate_1.png new file mode 100644 index 0000000..d08339a Binary files /dev/null and b/res/drawable-xhdpi/calibrate_1.png differ diff --git a/res/drawable-xhdpi/calibrate_2.png b/res/drawable-xhdpi/calibrate_2.png new file mode 100644 index 0000000..f38e6fa Binary files /dev/null and b/res/drawable-xhdpi/calibrate_2.png differ diff --git a/res/drawable-xhdpi/calibrate_3.png b/res/drawable-xhdpi/calibrate_3.png new file mode 100644 index 0000000..d275a01 Binary files /dev/null and b/res/drawable-xhdpi/calibrate_3.png differ diff --git a/res/drawable-xhdpi/calibrate_4.png b/res/drawable-xhdpi/calibrate_4.png new file mode 100644 index 0000000..40119de Binary files /dev/null and b/res/drawable-xhdpi/calibrate_4.png differ diff --git a/res/drawable-xhdpi/calibrate_5.png b/res/drawable-xhdpi/calibrate_5.png new file mode 100644 index 0000000..01e5b87 Binary files /dev/null and b/res/drawable-xhdpi/calibrate_5.png differ diff --git a/res/drawable-xhdpi/calibrate_6.png b/res/drawable-xhdpi/calibrate_6.png new file mode 100644 index 0000000..0ae4e60 Binary files /dev/null and b/res/drawable-xhdpi/calibrate_6.png differ diff --git a/res/drawable-xhdpi/calibrate_7.png b/res/drawable-xhdpi/calibrate_7.png new file mode 100644 index 0000000..7c091ad Binary files /dev/null and b/res/drawable-xhdpi/calibrate_7.png differ diff --git a/res/drawable-xhdpi/compass.png b/res/drawable-xhdpi/compass.png new file mode 100644 index 0000000..b437db9 Binary files /dev/null and b/res/drawable-xhdpi/compass.png differ diff --git a/res/drawable-xhdpi/degree.png b/res/drawable-xhdpi/degree.png new file mode 100644 index 0000000..92fb52c Binary files /dev/null and b/res/drawable-xhdpi/degree.png differ diff --git a/res/drawable-xhdpi/e.png b/res/drawable-xhdpi/e.png new file mode 100644 index 0000000..b889292 Binary files /dev/null and b/res/drawable-xhdpi/e.png differ diff --git a/res/drawable-xhdpi/guide.png b/res/drawable-xhdpi/guide.png new file mode 100644 index 0000000..1e32b6c Binary files /dev/null and b/res/drawable-xhdpi/guide.png differ diff --git a/res/drawable-xhdpi/miui_cover.png b/res/drawable-xhdpi/miui_cover.png new file mode 100644 index 0000000..fed54f4 Binary files /dev/null and b/res/drawable-xhdpi/miui_cover.png differ diff --git a/res/drawable-xhdpi/n.png b/res/drawable-xhdpi/n.png new file mode 100644 index 0000000..fd1e983 Binary files /dev/null and b/res/drawable-xhdpi/n.png differ diff --git a/res/drawable-xhdpi/number_0.png b/res/drawable-xhdpi/number_0.png new file mode 100644 index 0000000..865c85b Binary files /dev/null and b/res/drawable-xhdpi/number_0.png differ diff --git a/res/drawable-xhdpi/number_1.png b/res/drawable-xhdpi/number_1.png new file mode 100644 index 0000000..450dde1 Binary files /dev/null and b/res/drawable-xhdpi/number_1.png differ diff --git a/res/drawable-xhdpi/number_2.png b/res/drawable-xhdpi/number_2.png new file mode 100644 index 0000000..ff8df92 Binary files /dev/null and b/res/drawable-xhdpi/number_2.png differ diff --git a/res/drawable-xhdpi/number_3.png b/res/drawable-xhdpi/number_3.png new file mode 100644 index 0000000..305cc1c Binary files /dev/null and b/res/drawable-xhdpi/number_3.png differ diff --git a/res/drawable-xhdpi/number_4.png b/res/drawable-xhdpi/number_4.png new file mode 100644 index 0000000..aae80e7 Binary files /dev/null and b/res/drawable-xhdpi/number_4.png differ diff --git a/res/drawable-xhdpi/number_5.png b/res/drawable-xhdpi/number_5.png new file mode 100644 index 0000000..1c63d40 Binary files /dev/null and b/res/drawable-xhdpi/number_5.png differ diff --git a/res/drawable-xhdpi/number_6.png b/res/drawable-xhdpi/number_6.png new file mode 100644 index 0000000..c1323d9 Binary files /dev/null and b/res/drawable-xhdpi/number_6.png differ diff --git a/res/drawable-xhdpi/number_7.png b/res/drawable-xhdpi/number_7.png new file mode 100644 index 0000000..db2d71b Binary files /dev/null and b/res/drawable-xhdpi/number_7.png differ diff --git a/res/drawable-xhdpi/number_8.png b/res/drawable-xhdpi/number_8.png new file mode 100644 index 0000000..72403d7 Binary files /dev/null and b/res/drawable-xhdpi/number_8.png differ diff --git a/res/drawable-xhdpi/number_9.png b/res/drawable-xhdpi/number_9.png new file mode 100644 index 0000000..0cc0541 Binary files /dev/null and b/res/drawable-xhdpi/number_9.png differ diff --git a/res/drawable-xhdpi/prompt.png b/res/drawable-xhdpi/prompt.png new file mode 100644 index 0000000..4893c0a Binary files /dev/null and b/res/drawable-xhdpi/prompt.png differ diff --git a/res/drawable-xhdpi/s.png b/res/drawable-xhdpi/s.png new file mode 100644 index 0000000..93d1a69 Binary files /dev/null and b/res/drawable-xhdpi/s.png differ diff --git a/res/drawable-xhdpi/w.png b/res/drawable-xhdpi/w.png new file mode 100644 index 0000000..bffa007 Binary files /dev/null and b/res/drawable-xhdpi/w.png differ diff --git a/res/drawable-hdpi/compass_cn.png b/res/drawable-zh-rCN-hdpi/compass.png similarity index 100% rename from res/drawable-hdpi/compass_cn.png rename to res/drawable-zh-rCN-hdpi/compass.png diff --git a/res/drawable-hdpi/e_cn.png b/res/drawable-zh-rCN-hdpi/e.png similarity index 100% rename from res/drawable-hdpi/e_cn.png rename to res/drawable-zh-rCN-hdpi/e.png diff --git a/res/drawable-zh-rCN-hdpi/guide.png b/res/drawable-zh-rCN-hdpi/guide.png new file mode 100644 index 0000000..f098025 Binary files /dev/null and b/res/drawable-zh-rCN-hdpi/guide.png differ diff --git a/res/drawable-hdpi/n_cn.png b/res/drawable-zh-rCN-hdpi/n.png similarity index 100% rename from res/drawable-hdpi/n_cn.png rename to res/drawable-zh-rCN-hdpi/n.png diff --git a/res/drawable-hdpi/s_cn.png b/res/drawable-zh-rCN-hdpi/s.png similarity index 100% rename from res/drawable-hdpi/s_cn.png rename to res/drawable-zh-rCN-hdpi/s.png diff --git a/res/drawable-hdpi/w_cn.png b/res/drawable-zh-rCN-hdpi/w.png similarity index 100% rename from res/drawable-hdpi/w_cn.png rename to res/drawable-zh-rCN-hdpi/w.png diff --git a/res/drawable-zh-rCN-xhdpi/compass.png b/res/drawable-zh-rCN-xhdpi/compass.png new file mode 100644 index 0000000..ab25824 Binary files /dev/null and b/res/drawable-zh-rCN-xhdpi/compass.png differ diff --git a/res/drawable-zh-rCN-xhdpi/e.png b/res/drawable-zh-rCN-xhdpi/e.png new file mode 100644 index 0000000..5851b96 Binary files /dev/null and b/res/drawable-zh-rCN-xhdpi/e.png differ diff --git a/res/drawable-zh-rCN-xhdpi/guide.png b/res/drawable-zh-rCN-xhdpi/guide.png new file mode 100644 index 0000000..8560c58 Binary files /dev/null and b/res/drawable-zh-rCN-xhdpi/guide.png differ diff --git a/res/drawable-zh-rCN-xhdpi/n.png b/res/drawable-zh-rCN-xhdpi/n.png new file mode 100644 index 0000000..58b4b65 Binary files /dev/null and b/res/drawable-zh-rCN-xhdpi/n.png differ diff --git a/res/drawable-zh-rCN-xhdpi/s.png b/res/drawable-zh-rCN-xhdpi/s.png new file mode 100644 index 0000000..aec083a Binary files /dev/null and b/res/drawable-zh-rCN-xhdpi/s.png differ diff --git a/res/drawable-zh-rCN-xhdpi/w.png b/res/drawable-zh-rCN-xhdpi/w.png new file mode 100644 index 0000000..3d415a7 Binary files /dev/null and b/res/drawable-zh-rCN-xhdpi/w.png differ diff --git a/res/drawable-zh-rTW-hdpi/compass.png b/res/drawable-zh-rTW-hdpi/compass.png new file mode 100644 index 0000000..ece4748 Binary files /dev/null and b/res/drawable-zh-rTW-hdpi/compass.png differ diff --git a/res/drawable-zh-rTW-hdpi/e.png b/res/drawable-zh-rTW-hdpi/e.png new file mode 100644 index 0000000..f20f43e Binary files /dev/null and b/res/drawable-zh-rTW-hdpi/e.png differ diff --git a/res/drawable-zh-rTW-hdpi/guide.png b/res/drawable-zh-rTW-hdpi/guide.png new file mode 100644 index 0000000..ee7ca0e Binary files /dev/null and b/res/drawable-zh-rTW-hdpi/guide.png differ diff --git a/res/drawable-zh-rTW-hdpi/n.png b/res/drawable-zh-rTW-hdpi/n.png new file mode 100644 index 0000000..530623d Binary files /dev/null and b/res/drawable-zh-rTW-hdpi/n.png differ diff --git a/res/drawable-zh-rTW-hdpi/s.png b/res/drawable-zh-rTW-hdpi/s.png new file mode 100644 index 0000000..babd662 Binary files /dev/null and b/res/drawable-zh-rTW-hdpi/s.png differ diff --git a/res/drawable-zh-rTW-hdpi/w.png b/res/drawable-zh-rTW-hdpi/w.png new file mode 100644 index 0000000..615fb5f Binary files /dev/null and b/res/drawable-zh-rTW-hdpi/w.png differ diff --git a/res/drawable-zh-rTW-xhdpi/compass.png b/res/drawable-zh-rTW-xhdpi/compass.png new file mode 100644 index 0000000..5b65aca Binary files /dev/null and b/res/drawable-zh-rTW-xhdpi/compass.png differ diff --git a/res/drawable-zh-rTW-xhdpi/e.png b/res/drawable-zh-rTW-xhdpi/e.png new file mode 100644 index 0000000..3c19051 Binary files /dev/null and b/res/drawable-zh-rTW-xhdpi/e.png differ diff --git a/res/drawable-zh-rTW-xhdpi/guide.png b/res/drawable-zh-rTW-xhdpi/guide.png new file mode 100644 index 0000000..02f7970 Binary files /dev/null and b/res/drawable-zh-rTW-xhdpi/guide.png differ diff --git a/res/drawable-zh-rTW-xhdpi/n.png b/res/drawable-zh-rTW-xhdpi/n.png new file mode 100644 index 0000000..58b4b65 Binary files /dev/null and b/res/drawable-zh-rTW-xhdpi/n.png differ diff --git a/res/drawable-zh-rTW-xhdpi/s.png b/res/drawable-zh-rTW-xhdpi/s.png new file mode 100644 index 0000000..aec083a Binary files /dev/null and b/res/drawable-zh-rTW-xhdpi/s.png differ diff --git a/res/drawable-zh-rTW-xhdpi/w.png b/res/drawable-zh-rTW-xhdpi/w.png new file mode 100644 index 0000000..3d415a7 Binary files /dev/null and b/res/drawable-zh-rTW-xhdpi/w.png differ diff --git a/res/drawable/calibrate_animation.xml b/res/drawable/calibrate_animation.xml new file mode 100644 index 0000000..7d2431f --- /dev/null +++ b/res/drawable/calibrate_animation.xml @@ -0,0 +1,11 @@ + + + + + + + + + + \ No newline at end of file diff --git a/res/layout/main.xml b/res/layout/main.xml index 3a7d887..09a5570 100644 --- a/res/layout/main.xml +++ b/res/layout/main.xml @@ -1,6 +1,6 @@ - - + + + + 指南针 + 正在讀取經緯度… + 無法讀取經緯度 + 北 %s + 南 %sS + 东 %s + 西 %s + 校正成功 + \ No newline at end of file diff --git a/res/values/strings.xml b/res/values/strings.xml index b464d1f..63dee04 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -18,11 +18,11 @@ Compass - Getting location... + Getting location… Cannot get your location %s N %s S %s E %s W - + Calibration complete \ No newline at end of file diff --git a/src/net/micode/compass/CompassActivity.java b/src/net/micode/compass/CompassActivity.java index 3f8c8eb..9fb3f89 100644 --- a/src/net/micode/compass/CompassActivity.java +++ b/src/net/micode/compass/CompassActivity.java @@ -17,7 +17,10 @@ package net.micode.compass; import android.app.Activity; +import android.app.AlertDialog; +import android.app.Dialog; import android.content.Context; +import android.graphics.drawable.AnimationDrawable; import android.hardware.Sensor; import android.hardware.SensorEvent; import android.hardware.SensorEventListener; @@ -29,21 +32,30 @@ import android.location.LocationProvider; import android.os.Bundle; import android.os.Handler; +import android.os.Vibrator; +import android.test.MoreAsserts; import android.text.TextUtils; +import android.util.Log; import android.view.View; import android.view.ViewGroup.LayoutParams; import android.view.animation.AccelerateInterpolator; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; +import android.widget.Toast; import java.util.Locale; public class CompassActivity extends Activity { + private static final int MATRIX_SIZE = 9; private final float MAX_ROATE_DEGREE = 1.0f; private SensorManager mSensorManager; - private Sensor mOrientationSensor; + // private Sensor mOrientationSensor; + + private Sensor mAccelerometerSensor; + private Sensor mMagneticFieldSensor; + private LocationManager mLocationManager; private String mLocationProvider; private float mDirection; @@ -51,18 +63,67 @@ public class CompassActivity extends Activity { private AccelerateInterpolator mInterpolator; protected final Handler mHandler = new Handler(); private boolean mStopDrawing; - private boolean mChinease; + + private boolean mChinese; + + private View mCompassView; + private CompassView mPointer; + private TextView mLocationTextView; + private LinearLayout mDirectionLayout; + private LinearLayout mAngleLayout; + private View mViewGuide; + private AnimationDrawable mGuideAnimation; + private Vibrator mVibrator; + - View mCompassView; - CompassView mPointer; - TextView mLocationTextView; - LinearLayout mDirectionLayout; - LinearLayout mAngleLayout; + private static final int MAX_ACCURATE_COUNT = 20; + private static final int MAX_INACCURATE_COUNT = 20; + + private volatile int mAccurateCount; + private volatile int mInaccurateCount; + + private volatile boolean mCalibration; + + private void resetAccurateCount() { + mAccurateCount = 0; + } + + private void increaseAccurateCount() { + mAccurateCount++; + } + + private void resetInaccurateCount() { + mInaccurateCount = 0; + } + + private void increaseInaccurateCount() { + mInaccurateCount++; + } + + + private void switchMode(boolean calibration) { + mCalibration = calibration; + if (calibration) { + mViewGuide.setVisibility(View.VISIBLE); + mGuideAnimation.start(); + + resetAccurateCount(); + } else { + mGuideAnimation.stop(); + mViewGuide.setVisibility(View.GONE); + Toast.makeText(this, R.string.calibrate_success, Toast.LENGTH_SHORT).show(); + mVibrator.vibrate(200); + resetInaccurateCount(); + } + } protected Runnable mCompassViewUpdater = new Runnable() { @Override public void run() { if (mPointer != null && !mStopDrawing) { + + calculateTargetDirection(); + if (mDirection != mTargetDirection) { // calculate the short routine @@ -110,10 +171,23 @@ protected void onResume() { } else { mLocationTextView.setText(R.string.cannot_get_location); } - if (mOrientationSensor != null) { - mSensorManager.registerListener(mOrientationSensorEventListener, mOrientationSensor, + + // if (mOrientationSensor != null) { + // mSensorManager.registerListener(mOrientationSensorEventListener, + // mOrientationSensor, + // SensorManager.SENSOR_DELAY_GAME); + // } + + if (mAccelerometerSensor != null) { + mSensorManager.registerListener(mAccelerometerSensorEventListener, mAccelerometerSensor, SensorManager.SENSOR_DELAY_GAME); } + + if (mMagneticFieldSensor != null) { + mSensorManager.registerListener(mMagnetFieldSensorEventListener, mMagneticFieldSensor, + SensorManager.SENSOR_DELAY_GAME); + } + mStopDrawing = false; mHandler.postDelayed(mCompassViewUpdater, 20); } @@ -122,9 +196,18 @@ protected void onResume() { protected void onPause() { super.onPause(); mStopDrawing = true; - if (mOrientationSensor != null) { - mSensorManager.unregisterListener(mOrientationSensorEventListener); + // if (mOrientationSensor != null) { + // mSensorManager.unregisterListener(mOrientationSensorEventListener); + // } + + if (mAccelerometerSensor != null) { + mSensorManager.unregisterListener(mAccelerometerSensorEventListener); + } + + if (mMagneticFieldSensor != null) { + mSensorManager.unregisterListener(mMagnetFieldSensorEventListener); } + if (mLocationProvider != null) { mLocationManager.removeUpdates(mLocationListener); } @@ -135,7 +218,6 @@ private void initResources() { mTargetDirection = 0.0f; mInterpolator = new AccelerateInterpolator(); mStopDrawing = true; - mChinease = TextUtils.equals(Locale.getDefault().getLanguage(), "zh"); mCompassView = findViewById(R.id.view_compass); mPointer = (CompassView) findViewById(R.id.compass_pointer); @@ -143,14 +225,28 @@ private void initResources() { mDirectionLayout = (LinearLayout) findViewById(R.id.layout_direction); mAngleLayout = (LinearLayout) findViewById(R.id.layout_angle); - mPointer.setImageResource(mChinease ? R.drawable.compass_cn : R.drawable.compass); + mPointer.setImageResource(R.drawable.compass); + + mViewGuide = findViewById(R.id.view_guide); + + ImageView animationImage = (ImageView) findViewById(R.id.guide_animation); + + mGuideAnimation = (AnimationDrawable) animationImage.getDrawable(); + + mChinese = TextUtils.equals(Locale.getDefault().getLanguage(), "zh"); } private void initServices() { // sensor manager mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); - mOrientationSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION); + // mOrientationSensor = + // mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION); + mAccelerometerSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); + mMagneticFieldSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD); + + mVibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE); + // location manager mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); Criteria criteria = new Criteria(); @@ -177,28 +273,28 @@ private void updateDirection() { if (direction > 22.5f && direction < 157.5f) { // east east = new ImageView(this); - east.setImageResource(mChinease ? R.drawable.e_cn : R.drawable.e); + east.setImageResource(R.drawable.e); east.setLayoutParams(lp); } else if (direction > 202.5f && direction < 337.5f) { // west west = new ImageView(this); - west.setImageResource(mChinease ? R.drawable.w_cn : R.drawable.w); + west.setImageResource(R.drawable.w); west.setLayoutParams(lp); } if (direction > 112.5f && direction < 247.5f) { // south south = new ImageView(this); - south.setImageResource(mChinease ? R.drawable.s_cn : R.drawable.s); + south.setImageResource(R.drawable.s); south.setLayoutParams(lp); } else if (direction < 67.5 || direction > 292.5f) { // north north = new ImageView(this); - north.setImageResource(mChinease ? R.drawable.n_cn : R.drawable.n); + north.setImageResource(R.drawable.n); north.setLayoutParams(lp); } - if (mChinease) { + if (mChinese) { // east/west should be before north/south if (east != null) { mDirectionLayout.addView(east); @@ -319,16 +415,111 @@ private String getLocationString(double input) { return String.valueOf(du) + "°" + String.valueOf(fen) + "'" + String.valueOf(miao) + "\""; } - private SensorEventListener mOrientationSensorEventListener = new SensorEventListener() { + // private SensorEventListener mOrientationSensorEventListener = new + // SensorEventListener() { + // + // @Override + // public void onSensorChanged(SensorEvent event) { + // float direction = event.values[0] * -1.0f; + // mTargetDirection = normalizeDegree(direction); + // } + // + // @Override + // public void onAccuracyChanged(Sensor sensor, int accuracy) { + // } + // }; + + private void calculateTargetDirection() { + synchronized (this) { + double data = Math.sqrt(Math.pow(mMagneticFieldValues[0], 2) + Math.pow(mMagneticFieldValues[1], 2) + + Math.pow(mMagneticFieldValues[2], 2)); + + Log.d("Compass", "data = " + data); + + if (mCalibration) { + if (mMagneticFieldAccuracy != SensorManager.SENSOR_STATUS_UNRELIABLE && (data >= 25 && data <= 65)) { + increaseAccurateCount(); + } else { + resetAccurateCount(); + } + + Log.d("Compass", "accurate count = " + mAccurateCount); + + if (mAccurateCount >= MAX_ACCURATE_COUNT) { + switchMode(false); + } + + } else { + if (mMagneticFieldAccuracy == SensorManager.SENSOR_STATUS_UNRELIABLE || (data < 25 || data > 65)) { + increaseInaccurateCount(); + } else { + resetInaccurateCount(); + } + + Log.d("Compass", "inaccurate count = " + mInaccurateCount); + + if (mInaccurateCount >= MAX_INACCURATE_COUNT) { + switchMode(true); + } + } + + if (mMagneticFieldValues != null && mAccelerometerValues != null) { + float[] R = new float[MATRIX_SIZE]; + if (SensorManager.getRotationMatrix(R, null, mAccelerometerValues, mMagneticFieldValues)) { + float[] orientation = new float[3]; + SensorManager.getOrientation(R, orientation); + float direction = (float) Math.toDegrees(orientation[0]) * -1.0f; + mTargetDirection = normalizeDegree(direction); + Log.d("Compass", "mTargetDirection = " + mTargetDirection); + } else { + Log.d("Compass", "Error: SensorManager.getRotationMatrix"); + } + } + } + } + + private int mMagneticFieldAccuracy = SensorManager.SENSOR_STATUS_UNRELIABLE; + private float[] mMagneticFieldValues = new float[3]; + private float[] mAccelerometerValues = new float[3]; + + private SensorEventListener mAccelerometerSensorEventListener = new SensorEventListener() { @Override public void onSensorChanged(SensorEvent event) { - float direction = event.values[0] * -1.0f; - mTargetDirection = normalizeDegree(direction); + // TODO Auto-generated method stub + + // if (event.accuracy == SensorManager.SENSOR_STATUS_UNRELIABLE) { + // return; + // } + + System.arraycopy(event.values, 0, mAccelerometerValues, 0, 3); } @Override public void onAccuracyChanged(Sensor sensor, int accuracy) { + // TODO Auto-generated method stub + + } + }; + + private SensorEventListener mMagnetFieldSensorEventListener = new SensorEventListener() { + + @Override + public void onSensorChanged(SensorEvent event) { + // TODO Auto-generated method stub + + // if (event.accuracy == SensorManager.SENSOR_STATUS_UNRELIABLE) { + // return; + // } + + System.arraycopy(event.values, 0, mMagneticFieldValues, 0, 3); + mMagneticFieldAccuracy = event.accuracy; + } + + @Override + public void onAccuracyChanged(Sensor sensor, int accuracy) { + // TODO Auto-generated method stub + } };