Skip to content

Loading…

new feature add calibration support #2

Open
wants to merge 4 commits into from

1 participant

@shaobin0604
  1. [new] feature: calibration.
  2. [new] support xhdpi devices.
  3. [fix] use magnetic sensor and accelerometer sensor to replace orientation sensor.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 14, 2013
  1. @shaobin0604

    deprecate orientation_sensor

    shaobin0604 committed
Commits on Jan 15, 2013
  1. @shaobin0604

    add calibration support

    shaobin0604 committed
  2. @shaobin0604

    adjust accurate value

    shaobin0604 committed
Commits on Jan 16, 2013
  1. @shaobin0604

    * add calibration supprt

    shaobin0604 committed
    * add zh-rTW support
This page is out of date. Refresh to see the latest.
Showing with 279 additions and 36 deletions.
  1. BIN res/drawable-hdpi/calibrate_1.png
  2. BIN res/drawable-hdpi/calibrate_2.png
  3. BIN res/drawable-hdpi/calibrate_3.png
  4. BIN res/drawable-hdpi/calibrate_4.png
  5. BIN res/drawable-hdpi/calibrate_5.png
  6. BIN res/drawable-hdpi/calibrate_6.png
  7. BIN res/drawable-hdpi/calibrate_7.png
  8. BIN res/drawable-hdpi/guide.png
  9. BIN res/drawable-xhdpi/app_icon.png
  10. BIN res/drawable-xhdpi/background_bottom.9.png
  11. BIN res/drawable-xhdpi/background_compass.png
  12. BIN res/drawable-xhdpi/background_light.png
  13. BIN res/drawable-xhdpi/background_texture.png
  14. BIN res/drawable-xhdpi/calibrate_1.png
  15. BIN res/drawable-xhdpi/calibrate_2.png
  16. BIN res/drawable-xhdpi/calibrate_3.png
  17. BIN res/drawable-xhdpi/calibrate_4.png
  18. BIN res/drawable-xhdpi/calibrate_5.png
  19. BIN res/drawable-xhdpi/calibrate_6.png
  20. BIN res/drawable-xhdpi/calibrate_7.png
  21. BIN res/drawable-xhdpi/compass.png
  22. BIN res/drawable-xhdpi/degree.png
  23. BIN res/drawable-xhdpi/e.png
  24. BIN res/drawable-xhdpi/guide.png
  25. BIN res/drawable-xhdpi/miui_cover.png
  26. BIN res/drawable-xhdpi/n.png
  27. BIN res/drawable-xhdpi/number_0.png
  28. BIN res/drawable-xhdpi/number_1.png
  29. BIN res/drawable-xhdpi/number_2.png
  30. BIN res/drawable-xhdpi/number_3.png
  31. BIN res/drawable-xhdpi/number_4.png
  32. BIN res/drawable-xhdpi/number_5.png
  33. BIN res/drawable-xhdpi/number_6.png
  34. BIN res/drawable-xhdpi/number_7.png
  35. BIN res/drawable-xhdpi/number_8.png
  36. BIN res/drawable-xhdpi/number_9.png
  37. BIN res/drawable-xhdpi/prompt.png
  38. BIN res/drawable-xhdpi/s.png
  39. BIN res/drawable-xhdpi/w.png
  40. 0 res/{drawable-hdpi/compass_cn.png → drawable-zh-rCN-hdpi/compass.png}
  41. 0 res/{drawable-hdpi/e_cn.png → drawable-zh-rCN-hdpi/e.png}
  42. BIN res/drawable-zh-rCN-hdpi/guide.png
  43. 0 res/{drawable-hdpi/n_cn.png → drawable-zh-rCN-hdpi/n.png}
  44. 0 res/{drawable-hdpi/s_cn.png → drawable-zh-rCN-hdpi/s.png}
  45. 0 res/{drawable-hdpi/w_cn.png → drawable-zh-rCN-hdpi/w.png}
  46. BIN res/drawable-zh-rCN-xhdpi/compass.png
  47. BIN res/drawable-zh-rCN-xhdpi/e.png
  48. BIN res/drawable-zh-rCN-xhdpi/guide.png
  49. BIN res/drawable-zh-rCN-xhdpi/n.png
  50. BIN res/drawable-zh-rCN-xhdpi/s.png
  51. BIN res/drawable-zh-rCN-xhdpi/w.png
  52. BIN res/drawable-zh-rTW-hdpi/compass.png
  53. BIN res/drawable-zh-rTW-hdpi/e.png
  54. BIN res/drawable-zh-rTW-hdpi/guide.png
  55. BIN res/drawable-zh-rTW-hdpi/n.png
  56. BIN res/drawable-zh-rTW-hdpi/s.png
  57. BIN res/drawable-zh-rTW-hdpi/w.png
  58. BIN res/drawable-zh-rTW-xhdpi/compass.png
  59. BIN res/drawable-zh-rTW-xhdpi/e.png
  60. BIN res/drawable-zh-rTW-xhdpi/guide.png
  61. BIN res/drawable-zh-rTW-xhdpi/n.png
  62. BIN res/drawable-zh-rTW-xhdpi/s.png
  63. BIN res/drawable-zh-rTW-xhdpi/w.png
  64. +11 −0 res/drawable/calibrate_animation.xml
  65. +23 −10 res/layout/main.xml
  66. +2 −2 res/values-zh-rCN/strings.xml
  67. +28 −0 res/values-zh-rTW/strings.xml
  68. +2 −2 res/values/strings.xml
  69. +213 −22 src/net/micode/compass/CompassActivity.java
View
BIN res/drawable-hdpi/calibrate_1.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN res/drawable-hdpi/calibrate_2.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN res/drawable-hdpi/calibrate_3.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN res/drawable-hdpi/calibrate_4.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN res/drawable-hdpi/calibrate_5.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN res/drawable-hdpi/calibrate_6.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN res/drawable-hdpi/calibrate_7.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN res/drawable-hdpi/guide.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN res/drawable-xhdpi/app_icon.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN res/drawable-xhdpi/background_bottom.9.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN res/drawable-xhdpi/background_compass.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN res/drawable-xhdpi/background_light.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN res/drawable-xhdpi/background_texture.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN res/drawable-xhdpi/calibrate_1.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN res/drawable-xhdpi/calibrate_2.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN res/drawable-xhdpi/calibrate_3.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN res/drawable-xhdpi/calibrate_4.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN res/drawable-xhdpi/calibrate_5.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN res/drawable-xhdpi/calibrate_6.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN res/drawable-xhdpi/calibrate_7.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN res/drawable-xhdpi/compass.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN res/drawable-xhdpi/degree.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN res/drawable-xhdpi/e.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN res/drawable-xhdpi/guide.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN res/drawable-xhdpi/miui_cover.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN res/drawable-xhdpi/n.png
Diff not rendered.
View
BIN res/drawable-xhdpi/number_0.png
Diff not rendered.
View
BIN res/drawable-xhdpi/number_1.png
Diff not rendered.
View
BIN res/drawable-xhdpi/number_2.png
Diff not rendered.
View
BIN res/drawable-xhdpi/number_3.png
Diff not rendered.
View
BIN res/drawable-xhdpi/number_4.png
Diff not rendered.
View
BIN res/drawable-xhdpi/number_5.png
Diff not rendered.
View
BIN res/drawable-xhdpi/number_6.png
Diff not rendered.
View
BIN res/drawable-xhdpi/number_7.png
Diff not rendered.
View
BIN res/drawable-xhdpi/number_8.png
Diff not rendered.
View
BIN res/drawable-xhdpi/number_9.png
Diff not rendered.
View
BIN res/drawable-xhdpi/prompt.png
Diff not rendered.
View
BIN res/drawable-xhdpi/s.png
Diff not rendered.
View
BIN res/drawable-xhdpi/w.png
Diff not rendered.
View
0 res/drawable-hdpi/compass_cn.png → res/drawable-zh-rCN-hdpi/compass.png
Diff not rendered.
View
0 res/drawable-hdpi/e_cn.png → res/drawable-zh-rCN-hdpi/e.png
Diff not rendered.
View
BIN res/drawable-zh-rCN-hdpi/guide.png
Diff not rendered.
View
0 res/drawable-hdpi/n_cn.png → res/drawable-zh-rCN-hdpi/n.png
Diff not rendered.
View
0 res/drawable-hdpi/s_cn.png → res/drawable-zh-rCN-hdpi/s.png
Diff not rendered.
View
0 res/drawable-hdpi/w_cn.png → res/drawable-zh-rCN-hdpi/w.png
Diff not rendered.
View
BIN res/drawable-zh-rCN-xhdpi/compass.png
Diff not rendered.
View
BIN res/drawable-zh-rCN-xhdpi/e.png
Diff not rendered.
View
BIN res/drawable-zh-rCN-xhdpi/guide.png
Diff not rendered.
View
BIN res/drawable-zh-rCN-xhdpi/n.png
Diff not rendered.
View
BIN res/drawable-zh-rCN-xhdpi/s.png
Diff not rendered.
View
BIN res/drawable-zh-rCN-xhdpi/w.png
Diff not rendered.
View
BIN res/drawable-zh-rTW-hdpi/compass.png
Diff not rendered.
View
BIN res/drawable-zh-rTW-hdpi/e.png
Diff not rendered.
View
BIN res/drawable-zh-rTW-hdpi/guide.png
Diff not rendered.
View
BIN res/drawable-zh-rTW-hdpi/n.png
Diff not rendered.
View
BIN res/drawable-zh-rTW-hdpi/s.png
Diff not rendered.
View
BIN res/drawable-zh-rTW-hdpi/w.png
Diff not rendered.
View
BIN res/drawable-zh-rTW-xhdpi/compass.png
Diff not rendered.
View
BIN res/drawable-zh-rTW-xhdpi/e.png
Diff not rendered.
View
BIN res/drawable-zh-rTW-xhdpi/guide.png
Diff not rendered.
View
BIN res/drawable-zh-rTW-xhdpi/n.png
Diff not rendered.
View
BIN res/drawable-zh-rTW-xhdpi/s.png
Diff not rendered.
View
BIN res/drawable-zh-rTW-xhdpi/w.png
Diff not rendered.
View
11 res/drawable/calibrate_animation.xml
@@ -0,0 +1,11 @@
+<?xml version="1.0" encoding="utf-8"?>
+<animation-list android:oneshot="false"
+ xmlns:android="http://schemas.android.com/apk/res/android">
+ <item android:duration="250" android:drawable="@drawable/calibrate_1" />
+ <item android:duration="250" android:drawable="@drawable/calibrate_2" />
+ <item android:duration="250" android:drawable="@drawable/calibrate_3" />
+ <item android:duration="250" android:drawable="@drawable/calibrate_4" />
+ <item android:duration="250" android:drawable="@drawable/calibrate_5" />
+ <item android:duration="250" android:drawable="@drawable/calibrate_6" />
+ <item android:duration="250" android:drawable="@drawable/calibrate_7" />
+</animation-list>
View
33 res/layout/main.xml
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
-
-<!-- Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net)
+<!--
+ Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
@@ -131,13 +131,26 @@
</LinearLayout>
</FrameLayout>
- <include
- android:id="@+id/low_accuracy_view"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="bottom|center_horizontal"
- android:layout_marginBottom="35dip"
- layout="@*android:layout/transient_notification"
- android:visibility="gone" />
+ <FrameLayout
+ android:id="@+id/view_guide"
+ android:layout_width="fill_parent"
+ android:layout_height="fill_parent"
+ android:visibility="gone" >
+
+ <ImageView
+ android:id="@+id/guide_description"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:layout_gravity="center"
+ android:background="@drawable/guide" />
+
+ <ImageView
+ android:id="@+id/guide_animation"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:layout_gravity="top|center"
+ android:layout_marginTop="195.0dip"
+ android:src="@drawable/calibrate_animation" />
+ </FrameLayout>
</FrameLayout>
View
4 res/values-zh-rCN/strings.xml
@@ -18,11 +18,11 @@
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<string name="app_label">指南针</string>
- <string name="getting_location">正在获取经纬度...</string>
+ <string name="getting_location">正在获取经纬度&#8230;</string>
<string name="cannot_get_location">无法获取经纬度</string>
<string name="location_north">北纬 %s</string>
<string name="location_south">南纬 %sS</string>
<string name="location_east">东经 %s</string>
<string name="location_west">西经 %s</string>
-
+ <string name="calibrate_success">校准成功</string>
</resources>
View
28 res/values-zh-rTW/strings.xml
@@ -0,0 +1,28 @@
+<?xml version="1.0" encoding="utf-8"?>
+
+<!-- Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net)
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+-->
+
+<resources xmlns:android="http://schemas.android.com/apk/res/android">
+
+ <string name="app_label">指南针</string>
+ <string name="getting_location">正在讀取經緯度&#8230;</string>
+ <string name="cannot_get_location">無法讀取經緯度</string>
+ <string name="location_north">北 %s</string>
+ <string name="location_south">南 %sS</string>
+ <string name="location_east">东 %s</string>
+ <string name="location_west">西 %s</string>
+ <string name="calibrate_success">校正成功</string>
+</resources>
View
4 res/values/strings.xml
@@ -18,11 +18,11 @@
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<string name="app_label">Compass</string>
- <string name="getting_location">Getting location...</string>
+ <string name="getting_location">Getting location&#8230;</string>
<string name="cannot_get_location">Cannot get your location</string>
<string name="location_north">%s N</string>
<string name="location_south">%s S</string>
<string name="location_east">%s E</string>
<string name="location_west">%s W</string>
-
+ <string name="calibrate_success">Calibration complete</string>
</resources>
View
235 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 @@
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
+
}
};
Something went wrong with that request. Please try again.