Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

first checkin of miui compass's source code

commit 077fdb627bb7511f52cde8eddfdc24c2409e4190 1 parent b9ca237
Gary Hua authored
Showing with 698 additions and 0 deletions.
  1. +1 −0  .gitignore
  2. +48 −0 AndroidManifest.xml
  3. BIN  res/drawable-hdpi/app_icon.png
  4. BIN  res/drawable-hdpi/background_bottom.9.png
  5. BIN  res/drawable-hdpi/background_compass.png
  6. BIN  res/drawable-hdpi/background_light.png
  7. BIN  res/drawable-hdpi/background_texture.png
  8. BIN  res/drawable-hdpi/compass.png
  9. BIN  res/drawable-hdpi/compass_cn.png
  10. BIN  res/drawable-hdpi/degree.png
  11. BIN  res/drawable-hdpi/e.png
  12. BIN  res/drawable-hdpi/e_cn.png
  13. BIN  res/drawable-hdpi/miui_cover.png
  14. BIN  res/drawable-hdpi/n.png
  15. BIN  res/drawable-hdpi/n_cn.png
  16. BIN  res/drawable-hdpi/number_0.png
  17. BIN  res/drawable-hdpi/number_1.png
  18. BIN  res/drawable-hdpi/number_2.png
  19. BIN  res/drawable-hdpi/number_3.png
  20. BIN  res/drawable-hdpi/number_4.png
  21. BIN  res/drawable-hdpi/number_5.png
  22. BIN  res/drawable-hdpi/number_6.png
  23. BIN  res/drawable-hdpi/number_7.png
  24. BIN  res/drawable-hdpi/number_8.png
  25. BIN  res/drawable-hdpi/number_9.png
  26. BIN  res/drawable-hdpi/prompt.png
  27. BIN  res/drawable-hdpi/s.png
  28. BIN  res/drawable-hdpi/s_cn.png
  29. BIN  res/drawable-hdpi/w.png
  30. BIN  res/drawable-hdpi/w_cn.png
  31. +21 −0 res/drawable/background.xml
  32. +143 −0 res/layout/main.xml
  33. +28 −0 res/values-zh-rCN/strings.xml
  34. +28 −0 res/values/strings.xml
  35. +364 −0 src/net/micode/compass/CompassActivity.java
  36. +65 −0 src/net/micode/compass/CompassView.java
View
1  .gitignore
@@ -3,6 +3,7 @@ bin/
gen/
# Local configuration file (sdk path, etc)
+proguard.cfg
project.properties
.settings/
.classpath
View
48 AndroidManifest.xml
@@ -0,0 +1,48 @@
+<?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.
+-->
+
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+ package="net.micode.compass"
+ android:versionCode="1"
+ android:versionName="0.1" >
+
+ <uses-sdk android:minSdkVersion="4" />
+
+ <uses-permission android:name="android.permission.VIBRATE" />
+ <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
+
+ <application
+ android:icon="@drawable/app_icon"
+ android:label="@string/app_label"
+ android:taskAffinity="net.micode.compass" >
+ <activity
+ android:label="@string/app_label"
+ android:name=".CompassActivity"
+ android:screenOrientation="portrait"
+ android:theme="@android:style/Theme.NoTitleBar" >
+ <intent-filter >
+ <action android:name="android.intent.action.MAIN" />
+ <category android:name="android.intent.category.DEFAULT" />
+ </intent-filter>
+ <intent-filter >
+ <action android:name="android.intent.action.MAIN" />
+ <category android:name="android.intent.category.LAUNCHER" />
+ </intent-filter>
+ </activity>
+ </application>
+
+</manifest>
View
BIN  res/drawable-hdpi/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-hdpi/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-hdpi/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-hdpi/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-hdpi/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-hdpi/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-hdpi/compass_cn.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/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-hdpi/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-hdpi/e_cn.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/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-hdpi/n.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/n_cn.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/number_0.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/number_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/number_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/number_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/number_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/number_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/number_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/number_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/number_8.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/number_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-hdpi/prompt.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/s.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/s_cn.png
Diff not rendered
View
BIN  res/drawable-hdpi/w.png
Diff not rendered
View
BIN  res/drawable-hdpi/w_cn.png
Diff not rendered
View
21 res/drawable/background.xml
@@ -0,0 +1,21 @@
+<?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.
+-->
+
+<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
+ android:dither="true"
+ android:src="@drawable/background_texture"
+ android:tileMode="repeat" />
View
143 res/layout/main.xml
@@ -0,0 +1,143 @@
+<?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.
+-->
+
+<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
+ android:layout_width="fill_parent"
+ android:layout_height="fill_parent" >
+
+ <FrameLayout
+ android:layout_width="fill_parent"
+ android:layout_height="fill_parent"
+ android:background="@drawable/background" >
+
+ <LinearLayout
+ android:id="@+id/view_compass"
+ android:layout_width="fill_parent"
+ android:layout_height="fill_parent"
+ android:background="@drawable/background_light"
+ android:orientation="vertical" >
+
+ <LinearLayout
+ android:layout_width="fill_parent"
+ android:layout_height="0dip"
+ android:layout_weight="1"
+ android:orientation="vertical" >
+
+ <FrameLayout
+ android:layout_width="fill_parent"
+ android:layout_height="wrap_content"
+ android:background="@drawable/prompt" >
+
+ <LinearLayout
+ android:layout_width="fill_parent"
+ android:layout_height="wrap_content"
+ android:layout_gravity="center_horizontal"
+ android:layout_marginTop="70dip"
+ android:orientation="horizontal" >
+
+ <LinearLayout
+ android:id="@+id/layout_direction"
+ android:layout_width="0dip"
+ android:layout_height="wrap_content"
+ android:layout_weight="1"
+ android:gravity="right"
+ android:orientation="horizontal" >
+ </LinearLayout>
+
+ <ImageView
+ android:layout_width="20dip"
+ android:layout_height="fill_parent" >
+ </ImageView>
+
+ <LinearLayout
+ android:id="@+id/layout_angle"
+ android:layout_width="0dip"
+ android:layout_height="wrap_content"
+ android:layout_weight="1"
+ android:gravity="left"
+ android:orientation="horizontal" >
+ </LinearLayout>
+ </LinearLayout>
+ </FrameLayout>
+
+ <LinearLayout
+ android:layout_width="fill_parent"
+ android:layout_height="0dip"
+ android:layout_weight="1"
+ android:orientation="vertical" >
+
+ <FrameLayout
+ android:layout_width="fill_parent"
+ android:layout_height="wrap_content"
+ android:layout_gravity="center" >
+
+ <ImageView
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:layout_gravity="center"
+ android:src="@drawable/background_compass" />
+
+ <net.micode.compass.CompassView
+ android:id="@+id/compass_pointer"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:layout_gravity="center"
+ android:src="@drawable/compass" />
+
+ <ImageView
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:layout_gravity="center"
+ android:src="@drawable/miui_cover" />
+ </FrameLayout>
+ </LinearLayout>
+ </LinearLayout>
+
+ <FrameLayout
+ android:id="@+id/location_layout"
+ android:layout_width="fill_parent"
+ android:layout_height="wrap_content" >
+
+ <LinearLayout
+ android:layout_width="fill_parent"
+ android:layout_height="wrap_content"
+ android:background="@drawable/background_bottom"
+ android:orientation="vertical" >
+ </LinearLayout>
+
+ <TextView
+ android:id="@+id/textview_location"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:layout_gravity="center"
+ android:text="@string/getting_location"
+ android:textAppearance="?android:attr/textAppearanceMedium"
+ android:textColor="#7FFFFFFF" />
+ </FrameLayout>
+ </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>
View
28 res/values-zh-rCN/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">正在获取经纬度...</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>
+
+</resources>
View
28 res/values/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">Compass</string>
+ <string name="getting_location">Getting location...</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>
+
+</resources>
View
364 src/net/micode/compass/CompassActivity.java
@@ -0,0 +1,364 @@
+/*
+ * 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.
+ */
+
+package net.micode.compass;
+
+import android.app.Activity;
+import android.content.Context;
+import android.hardware.Sensor;
+import android.hardware.SensorEvent;
+import android.hardware.SensorEventListener;
+import android.hardware.SensorManager;
+import android.location.Criteria;
+import android.location.Location;
+import android.location.LocationListener;
+import android.location.LocationManager;
+import android.location.LocationProvider;
+import android.os.Bundle;
+import android.os.Handler;
+import android.text.TextUtils;
+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 java.util.Locale;
+
+public class CompassActivity extends Activity {
+
+ private final float MAX_ROATE_DEGREE = 1.0f;
+ private SensorManager mSensorManager;
+ private Sensor mOrientationSensor;
+ private LocationManager mLocationManager;
+ private String mLocationProvider;
+ private float mDirection;
+ private float mTargetDirection;
+ private AccelerateInterpolator mInterpolator;
+ protected final Handler mHandler = new Handler();
+ private boolean mStopDrawing;
+ private boolean mChinease;
+
+ View mCompassView;
+ CompassView mPointer;
+ TextView mLocationTextView;
+ LinearLayout mDirectionLayout;
+ LinearLayout mAngleLayout;
+
+ protected Runnable mCompassViewUpdater = new Runnable() {
+ @Override
+ public void run() {
+ if (mPointer != null && !mStopDrawing) {
+ if (mDirection != mTargetDirection) {
+
+ // calculate the short routine
+ float to = mTargetDirection;
+ if (to - mDirection > 180) {
+ to -= 360;
+ } else if (to - mDirection < -180) {
+ to += 360;
+ }
+
+ // limit the max speed to MAX_ROTATE_DEGREE
+ float distance = to - mDirection;
+ if (Math.abs(distance) > MAX_ROATE_DEGREE) {
+ distance = distance > 0 ? MAX_ROATE_DEGREE : (-1.0f * MAX_ROATE_DEGREE);
+ }
+
+ // need to slow down if the distance is short
+ mDirection = normalizeDegree(mDirection
+ + ((to - mDirection) * mInterpolator.getInterpolation(Math
+ .abs(distance) > MAX_ROATE_DEGREE ? 0.4f : 0.3f)));
+ mPointer.updateDirection(mDirection);
+ }
+
+ updateDirection();
+
+ mHandler.postDelayed(mCompassViewUpdater, 20);
+ }
+ }
+ };
+
+ @Override
+ protected void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ setContentView(R.layout.main);
+ initResources();
+ initServices();
+ }
+
+ @Override
+ protected void onResume() {
+ super.onResume();
+ if (mLocationProvider != null) {
+ updateLocation(mLocationManager.getLastKnownLocation(mLocationProvider));
+ mLocationManager.requestLocationUpdates(mLocationProvider, 2000, 10, mLocationListener);
+ } else {
+ mLocationTextView.setText(R.string.cannot_get_location);
+ }
+ if (mOrientationSensor != null) {
+ mSensorManager.registerListener(mOrientationSensorEventListener, mOrientationSensor,
+ SensorManager.SENSOR_DELAY_GAME);
+ }
+ mStopDrawing = false;
+ mHandler.postDelayed(mCompassViewUpdater, 20);
+ }
+
+ @Override
+ protected void onPause() {
+ super.onPause();
+ mStopDrawing = true;
+ if (mOrientationSensor != null) {
+ mSensorManager.unregisterListener(mOrientationSensorEventListener);
+ }
+ if (mLocationProvider != null) {
+ mLocationManager.removeUpdates(mLocationListener);
+ }
+ }
+
+ private void initResources() {
+ mDirection = 0.0f;
+ 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);
+ mLocationTextView = (TextView) findViewById(R.id.textview_location);
+ mDirectionLayout = (LinearLayout) findViewById(R.id.layout_direction);
+ mAngleLayout = (LinearLayout) findViewById(R.id.layout_angle);
+
+ mPointer.setImageResource(mChinease ? R.drawable.compass_cn : R.drawable.compass);
+ }
+
+ private void initServices() {
+ // sensor manager
+ mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
+ mOrientationSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
+
+ // location manager
+ mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
+ Criteria criteria = new Criteria();
+ criteria.setAccuracy(Criteria.ACCURACY_FINE);
+ criteria.setAltitudeRequired(false);
+ criteria.setBearingRequired(false);
+ criteria.setCostAllowed(true);
+ criteria.setPowerRequirement(Criteria.POWER_LOW);
+ mLocationProvider = mLocationManager.getBestProvider(criteria, true);
+
+ }
+
+ private void updateDirection() {
+ LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
+
+ mDirectionLayout.removeAllViews();
+ mAngleLayout.removeAllViews();
+
+ ImageView east = null;
+ ImageView west = null;
+ ImageView south = null;
+ ImageView north = null;
+ float direction = normalizeDegree(mTargetDirection * -1.0f);
+ if (direction > 22.5f && direction < 157.5f) {
+ // east
+ east = new ImageView(this);
+ east.setImageResource(mChinease ? R.drawable.e_cn : 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.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.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.setLayoutParams(lp);
+ }
+
+ if (mChinease) {
+ // east/west should be before north/south
+ if (east != null) {
+ mDirectionLayout.addView(east);
+ }
+ if (west != null) {
+ mDirectionLayout.addView(west);
+ }
+ if (south != null) {
+ mDirectionLayout.addView(south);
+ }
+ if (north != null) {
+ mDirectionLayout.addView(north);
+ }
+ } else {
+ // north/south should be before east/west
+ if (south != null) {
+ mDirectionLayout.addView(south);
+ }
+ if (north != null) {
+ mDirectionLayout.addView(north);
+ }
+ if (east != null) {
+ mDirectionLayout.addView(east);
+ }
+ if (west != null) {
+ mDirectionLayout.addView(west);
+ }
+ }
+
+ int direction2 = (int) direction;
+ boolean show = false;
+ if (direction2 >= 100) {
+ mAngleLayout.addView(getNumberImage(direction2 / 100));
+ direction2 %= 100;
+ show = true;
+ }
+ if (direction2 >= 10 || show) {
+ mAngleLayout.addView(getNumberImage(direction2 / 10));
+ direction2 %= 10;
+ }
+ mAngleLayout.addView(getNumberImage(direction2));
+
+ ImageView degreeImageView = new ImageView(this);
+ degreeImageView.setImageResource(R.drawable.degree);
+ degreeImageView.setLayoutParams(lp);
+ mAngleLayout.addView(degreeImageView);
+ }
+
+ private ImageView getNumberImage(int number) {
+ ImageView image = new ImageView(this);
+ LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
+ switch (number) {
+ case 0:
+ image.setImageResource(R.drawable.number_0);
+ break;
+ case 1:
+ image.setImageResource(R.drawable.number_1);
+ break;
+ case 2:
+ image.setImageResource(R.drawable.number_2);
+ break;
+ case 3:
+ image.setImageResource(R.drawable.number_3);
+ break;
+ case 4:
+ image.setImageResource(R.drawable.number_4);
+ break;
+ case 5:
+ image.setImageResource(R.drawable.number_5);
+ break;
+ case 6:
+ image.setImageResource(R.drawable.number_6);
+ break;
+ case 7:
+ image.setImageResource(R.drawable.number_7);
+ break;
+ case 8:
+ image.setImageResource(R.drawable.number_8);
+ break;
+ case 9:
+ image.setImageResource(R.drawable.number_9);
+ break;
+ }
+ image.setLayoutParams(lp);
+ return image;
+ }
+
+ private void updateLocation(Location location) {
+ if (location == null) {
+ mLocationTextView.setText(R.string.getting_location);
+ } else {
+ StringBuilder sb = new StringBuilder();
+ double latitude = location.getLatitude();
+ double longitude = location.getLongitude();
+
+ if (latitude >= 0.0f) {
+ sb.append(getString(R.string.location_north, getLocationString(latitude)));
+ } else {
+ sb.append(getString(R.string.location_south, getLocationString(-1.0 * latitude)));
+ }
+
+ sb.append(" ");
+
+ if (longitude >= 0.0f) {
+ sb.append(getString(R.string.location_east, getLocationString(longitude)));
+ } else {
+ sb.append(getString(R.string.location_west, getLocationString(-1.0 * longitude)));
+ }
+
+ mLocationTextView.setText(sb.toString());
+ }
+ }
+
+ private String getLocationString(double input) {
+ int du = (int) input;
+ int fen = (((int) ((input - du) * 3600))) / 60;
+ int miao = (((int) ((input - du) * 3600))) % 60;
+ return String.valueOf(du) + "°" + String.valueOf(fen) + "'" + String.valueOf(miao) + "\"";
+ }
+
+ 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 float normalizeDegree(float degree) {
+ return (degree + 720) % 360;
+ }
+
+ LocationListener mLocationListener = new LocationListener() {
+
+ @Override
+ public void onStatusChanged(String provider, int status, Bundle extras) {
+ if (status != LocationProvider.OUT_OF_SERVICE) {
+ updateLocation(mLocationManager.getLastKnownLocation(mLocationProvider));
+ } else {
+ mLocationTextView.setText(R.string.cannot_get_location);
+ }
+ }
+
+ @Override
+ public void onProviderEnabled(String provider) {
+ }
+
+ @Override
+ public void onProviderDisabled(String provider) {
+ }
+
+ @Override
+ public void onLocationChanged(Location location) {
+ updateLocation(location);
+ }
+
+ };
+}
View
65 src/net/micode/compass/CompassView.java
@@ -0,0 +1,65 @@
+/*
+ * 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.
+ */
+
+package net.micode.compass;
+
+import android.content.Context;
+import android.graphics.Canvas;
+import android.graphics.drawable.Drawable;
+import android.util.AttributeSet;
+import android.widget.ImageView;
+
+public class CompassView extends ImageView {
+ private float mDirection;
+ private Drawable compass;
+
+ public CompassView(Context context) {
+ super(context);
+ mDirection = 0.0f;
+ compass = null;
+ }
+
+ public CompassView(Context context, AttributeSet attrs) {
+ super(context, attrs);
+ mDirection = 0.0f;
+ compass = null;
+ }
+
+ public CompassView(Context context, AttributeSet attrs, int defStyle) {
+ super(context, attrs, defStyle);
+ mDirection = 0.0f;
+ compass = null;
+ }
+
+ @Override
+ protected void onDraw(Canvas canvas) {
+ if (compass == null) {
+ compass = getDrawable();
+ compass.setBounds(0, 0, getWidth(), getHeight());
+ }
+
+ canvas.save();
+ canvas.rotate(mDirection, getWidth() / 2, getHeight() / 2);
+ compass.draw(canvas);
+ canvas.restore();
+ }
+
+ public void updateDirection(float direction) {
+ mDirection = direction;
+ invalidate();
+ }
+
+}

0 comments on commit 077fdb6

Please sign in to comment.
Something went wrong with that request. Please try again.