379 lines
10 KiB
Java
379 lines
10 KiB
Java
package org.thinkname.ap.main;
|
||
|
||
import org.thinkname.ap.R;
|
||
import org.thinkname.ap.contact.ContactLocation;
|
||
import org.thinkname.ap.contact.ContactManager;
|
||
|
||
import android.app.Activity;
|
||
import android.content.Context;
|
||
import android.hardware.SensorListener;
|
||
import android.hardware.SensorManager;
|
||
import android.os.Bundle;
|
||
import android.os.Handler;
|
||
import android.os.Message;
|
||
import android.util.Log;
|
||
import android.view.View;
|
||
import android.view.animation.AlphaAnimation;
|
||
import android.view.animation.Animation;
|
||
import android.view.animation.RotateAnimation;
|
||
import android.widget.ImageView;
|
||
import android.widget.TextView;
|
||
import android.widget.Toast;
|
||
|
||
|
||
|
||
|
||
public class CompassActivity extends Activity implements SensorListener{
|
||
|
||
/**
|
||
* UI-Compass
|
||
*/
|
||
private ImageView ivCompass = null;
|
||
private ImageView ImgCompass = null;
|
||
private ImageView imgGps=null;
|
||
private TextView tvMyAddress=null;
|
||
private TextView tvMyLatLng=null;
|
||
private TextView OrientText = null;
|
||
private TextView OrientValue = null;
|
||
private ImageView imgFullScreen=null;
|
||
private boolean FullScreenState=false;
|
||
|
||
private SensorManager sm = null;
|
||
//private SensorManagerSimulator mSensorManager; //Test!
|
||
|
||
private static boolean openCompassState=false;
|
||
private static final String TAG = "Compass";
|
||
private int RotateSpeed = 200;
|
||
|
||
private AlphaAnimation myAnimation_Alpha = null;
|
||
private RotateAnimation myAni = null;
|
||
private float DegressQuondam = 0.0f;
|
||
|
||
|
||
|
||
@Override
|
||
protected void onCreate(Bundle savedInstanceState) {
|
||
super.onCreate(savedInstanceState);
|
||
|
||
|
||
initUI();
|
||
getMyLocInfo();
|
||
|
||
//传感器测试方法
|
||
//testSensor();
|
||
}
|
||
|
||
|
||
|
||
@Override
|
||
public void onStart() {
|
||
super.onStart();
|
||
//初始化感应器
|
||
sm = (SensorManager) getSystemService(SENSOR_SERVICE);
|
||
//下面的是模拟传感器
|
||
// mSensorManager = SensorManagerSimulator.getSystemService(this,SENSOR_SERVICE);
|
||
// mSensorManager.connectSimulator();
|
||
|
||
}
|
||
|
||
@Override
|
||
protected void onStop() {
|
||
sm.unregisterListener(this);
|
||
//测试
|
||
//mSensorManager.unregisterListener(this);
|
||
|
||
super.onStop();
|
||
}
|
||
|
||
@Override
|
||
public void onPause() {
|
||
super.onPause();
|
||
}
|
||
|
||
private void initUI() {
|
||
setContentView(R.layout.compass);
|
||
ivCompass = (ImageView) findViewById(R.id.ivCompass);
|
||
myAnimation_Alpha=new AlphaAnimation(1.0f, 0.1f);
|
||
myAnimation_Alpha.setDuration(3000);
|
||
myAnimation_Alpha.setAnimationListener(new Animation.AnimationListener() {
|
||
@Override
|
||
public void onAnimationStart(Animation animation) {
|
||
|
||
}
|
||
@Override
|
||
public void onAnimationRepeat(Animation animation) {
|
||
|
||
}
|
||
@Override
|
||
public void onAnimationEnd(Animation animation) {
|
||
OrientText = (TextView) findViewById(R.id.OrientText);
|
||
ImgCompass = (ImageView) findViewById(R.id.ivCompass);
|
||
OrientValue = (TextView) findViewById(R.id.OrientValue);
|
||
}
|
||
});
|
||
|
||
tvMyAddress=(TextView)findViewById(R.id.tvMyaddress);
|
||
tvMyLatLng=(TextView)findViewById(R.id.tvMyLATLNG);
|
||
// imgGps=(ImageView)findViewById(R.id.icongps);
|
||
// imgGps.setOnClickListener(new View.OnClickListener() {
|
||
// @Override
|
||
// public void onClick(View v) {
|
||
// startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS));
|
||
//
|
||
// }
|
||
// });
|
||
|
||
// imgFullScreen=(ImageView) findViewById(R.id.iconFullScreen);
|
||
// imgFullScreen.setOnClickListener(new View.OnClickListener() {
|
||
// public void onClick(View v) {
|
||
//
|
||
// if(!FullScreenState)
|
||
// {
|
||
// mainTabHost.getTabWidget().setVisibility(View.GONE);
|
||
// FullScreenState=true;
|
||
// }
|
||
// else
|
||
// {
|
||
// mainTabHost.getTabWidget().setVisibility(View.VISIBLE);
|
||
// FullScreenState=false;
|
||
// }
|
||
// }
|
||
// });
|
||
|
||
//设置 指北针可用
|
||
//widget.getChildTabViewAt(2).setEnabled(true);
|
||
//设置指北针上方标题信息
|
||
tvMyAddress=(TextView)findViewById(R.id.tvMyaddress);
|
||
tvMyAddress.setText(R.string.compassview_myaddress);
|
||
//点击时候显示完整信息
|
||
tvMyAddress.setOnClickListener(new View.OnClickListener() {
|
||
@Override
|
||
public void onClick(View v) {
|
||
Toast.makeText(getApplicationContext(), tvMyAddress.getText(),
|
||
Toast.LENGTH_SHORT).show();
|
||
}
|
||
});
|
||
|
||
//ImgCompass=(ImageView)findViewById(R.id.ivCompass);
|
||
ivCompass.setOnClickListener(new View.OnClickListener() {
|
||
@Override
|
||
public void onClick(View v) {
|
||
if(openCompassState)
|
||
{
|
||
sm.unregisterListener(CompassActivity.this);
|
||
//测试变量
|
||
// mSensorManager.unregisterListener(MainActivity.this);
|
||
Toast(getApplicationContext(),getString(R.string.toast_compass_stop),0);
|
||
Log.i("debug","he Compass has Stop!");
|
||
openCompassState=false;//reset the state
|
||
|
||
}
|
||
else
|
||
{
|
||
sm.registerListener(CompassActivity.this, SensorManager.SENSOR_ORIENTATION
|
||
| SensorManager.SENSOR_ACCELEROMETER, SensorManager.SENSOR_DELAY_NORMAL);
|
||
//测试变量
|
||
// mSensorManager.registerListener(MainActivity.this, SensorManager.SENSOR_ACCELEROMETER
|
||
// | SensorManager.SENSOR_MAGNETIC_FIELD
|
||
// | SensorManager.SENSOR_ORIENTATION,
|
||
// SensorManager.SENSOR_DELAY_FASTEST);
|
||
ivCompass.startAnimation(myAnimation_Alpha);//动画效果
|
||
Toast(getApplicationContext(),getString(R.string.toast_compass_start),0);
|
||
Log.i("debug","he Compass has Start!");
|
||
openCompassState=true;
|
||
}
|
||
}
|
||
});
|
||
|
||
}
|
||
|
||
private void getMyLocInfo() {
|
||
Thread t=new Thread(new Runnable() {
|
||
@Override
|
||
public void run() {
|
||
while(true)
|
||
{
|
||
ContactLocation myCloc = ContactManager.getInstance().getMyContactLocation();
|
||
|
||
double lat=myCloc.getLatitude();
|
||
double lng=myCloc.getLongitude();
|
||
if (lat!=Double.POSITIVE_INFINITY&&lng!=Double.POSITIVE_INFINITY)
|
||
{
|
||
//地点可获得
|
||
locHandel.sendEmptyMessage(1);
|
||
}else
|
||
{
|
||
locHandel.sendEmptyMessage(0);
|
||
}
|
||
try {
|
||
Thread.sleep(5000);
|
||
} catch (InterruptedException e) {
|
||
// TODO Auto-generated catch block
|
||
Log.e("getMyLocInfo", "Thread.sleep Error");
|
||
e.printStackTrace();
|
||
}
|
||
}
|
||
}
|
||
});
|
||
t.start();
|
||
}
|
||
|
||
//Handle the msg update the UI
|
||
Handler locHandel=new Handler(){
|
||
|
||
@Override
|
||
public void handleMessage(Message msg) {
|
||
super.handleMessage(msg);
|
||
switch(msg.what)
|
||
{
|
||
case 1:
|
||
ContactLocation myCloc = ContactManager.getInstance().getMyContactLocation();
|
||
String address=Location.getAddressByLatLng(myCloc.getLatitude(), myCloc.getLongitude());
|
||
tvMyAddress.setText(address);
|
||
tvMyLatLng.setText("经度:"+myCloc.getLatitude()+" 纬度:"+myCloc.getLongitude());
|
||
|
||
break;
|
||
case 0:
|
||
tvMyAddress.setText(R.string.compassview_requestaddress);
|
||
tvMyLatLng.setText(R.string.compassview_requestloc);
|
||
break;
|
||
}
|
||
|
||
}
|
||
};
|
||
|
||
|
||
|
||
private void AniRotateImage(float fDegress) {
|
||
if (Math.abs(fDegress - DegressQuondam) < 1) {
|
||
Log.e(TAG, "Degress: " + DegressQuondam + ", " + fDegress);
|
||
return;
|
||
}
|
||
Log.d(TAG, "Degress: " + DegressQuondam + ", " + fDegress);
|
||
|
||
myAni = new RotateAnimation(DegressQuondam, fDegress,
|
||
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
|
||
0.5f);
|
||
myAni.setDuration(RotateSpeed);
|
||
myAni.setFillAfter(true);
|
||
|
||
ImgCompass.startAnimation(myAni);
|
||
DegressQuondam = fDegress;
|
||
}
|
||
@Override
|
||
public void onSensorChanged(int sensor, float[] values) {
|
||
synchronized (this) {
|
||
if (sensor == SensorManager.SENSOR_ORIENTATION) {
|
||
if (null == OrientText || null == OrientValue || null == ImgCompass)
|
||
return;
|
||
|
||
switch ((int) values[0]) {
|
||
case 0: // North 北
|
||
OrientText.setText("正北");
|
||
break;
|
||
case 90: // East 东
|
||
OrientText.setText("正东");
|
||
break;
|
||
case 180: // South 南
|
||
OrientText.setText("正南");
|
||
break;
|
||
case 270: // West 西
|
||
OrientText.setText("正西");
|
||
break;
|
||
default: {
|
||
int v = (int) values[0];
|
||
if (v > 0 && v < 90) {
|
||
OrientText.setText("北偏东" + v);
|
||
}
|
||
|
||
if (v > 90 && v < 180) {
|
||
v = 180 - v;
|
||
OrientText.setText("南偏东" + v);
|
||
}
|
||
|
||
if (v > 180 && v < 270) {
|
||
v = v - 180;
|
||
OrientText.setText("南偏西" + v);
|
||
}
|
||
if (v > 270 && v < 360) {
|
||
v = 360 - v;
|
||
OrientText.setText("北偏西" + v);
|
||
}
|
||
}
|
||
}
|
||
|
||
OrientValue.setText(String.valueOf(values[0]));
|
||
|
||
if (DegressQuondam != -values[0])
|
||
AniRotateImage(-values[0]);
|
||
}
|
||
|
||
// if (sensor == SensorManager.SENSOR_ACCELEROMETER) { // //}
|
||
|
||
}
|
||
}
|
||
@Override
|
||
public void onAccuracyChanged(int sensor, int accuracy) {
|
||
Log.d(TAG, "onAccuracyChanged: " + sensor + ", accuracy: " + accuracy);
|
||
}
|
||
|
||
// //传感器测试方法
|
||
// private void testSensor()
|
||
// {
|
||
// ////////////////////////////////////////////////////////////////
|
||
// // INSTRUCTIONS
|
||
// // ============
|
||
//
|
||
// // 1) Use the separate application SensorSimulatorSettings
|
||
// // to enter the correct IP address of the SensorSimulator.
|
||
// // This should work before you proceed, because the same
|
||
// // settings are used for your custom sensor application.
|
||
//
|
||
// // 2) Include sensorsimulator-lib.jar in your project.
|
||
// // Put that file into the 'lib' folder.
|
||
// // In Eclipse, right-click on your project in the
|
||
// // Package Explorer, select
|
||
// // Properties > Java Build Path > (tab) Libraries
|
||
// // then click Add JARs to add this jar.
|
||
//
|
||
// // 3) You need the permission
|
||
// // <uses-permission android:name="android.permission.INTERNET"/>
|
||
// // in your Manifest file!
|
||
//
|
||
// // 4) Instead of calling the system service to obtain the Sensor manager,
|
||
// // you should obtain it from the SensorManagerSimulator:
|
||
//
|
||
// //mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
|
||
// mSensorManager = SensorManagerSimulator.getSystemService(this, SENSOR_SERVICE);
|
||
//
|
||
// // 5) Connect to the sensor simulator, using the settings
|
||
// // that have been set previously with SensorSimulatorSettings
|
||
// mSensorManager.connectSimulator();
|
||
//
|
||
// // The rest of your application can stay unmodified.
|
||
// ////////////////////////////////////////////////////////////////
|
||
// }
|
||
|
||
public void Toast(Context context,String msg,int timeID){
|
||
switch(timeID)
|
||
{
|
||
case 0:
|
||
Toast.makeText(context,
|
||
msg,
|
||
Toast.LENGTH_SHORT)
|
||
.show();
|
||
break;
|
||
case 1:
|
||
Toast.makeText(context,
|
||
msg,
|
||
Toast.LENGTH_LONG)
|
||
.show();
|
||
break;
|
||
}
|
||
}
|
||
|
||
|
||
|
||
|
||
}
|