package com.jqyd.son;
import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.Map;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
import com.jqyd.app.BitmapUtils;
import com.jqyd.app.GetDistance;
import com.jqyd.app.LocationUtils;
import com.jqyd.app.MyApp;
import com.jqyd.app.PublicDeal;
import com.jqyd.app.ShareMethod;
import com.jqyd.app.UpMyFile;
import com.jqyd.manager.R;
import com.jqyd.model.LocationModule;
import com.jqyd.model.RecordModel;
import com.jqyd.pub.MyProgressDialog;
import com.jqyd.shareInterface.Baidu_location;
import com.jqyd.shareInterface.Optdb_interfce;
import com.jqyd.shareInterface.Optsharepre_interface;
import com.jqyd.shareInterface.SystemInfo;
public class PicVist extends Activity implements OnClickListener {
private EditText pic_detail;
private ImageView imageView;
private Button ksbf;
private Button jsbf;
private Button give;
private Bundle bundle = null;
private Bitmap bm = null;
private MyApp myApp;
private Optdb_interfce db = null;
private Baidu_location baidu = null;
private int bf_point = 1;// 1、开始拜访2、结束拜访
private String custId = "";
private double cuLon = 0.0;// 客户真实经度
private double cuLat = 0.0;// 客户真实纬度
private double cuClon = 0.0;// 客户粗经度
private double cuClat = 0.0;// 客户粗纬度
private int dislimit = 0;
private Optsharepre_interface share_obj = null;
int param = 2;
private PicDealThread picThread = null;
private String log = "";//记录日志
private ShareMethod shareMethod = null;
private final String TAG = "KHBF";
String content;//拜访内容
private MyProgressDialog pBar;
private MyBroadcastReciver receiveBroadCast;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.picvist);
pic_detail = (EditText) this.findViewById(R.id.pic_detail);
imageView = (ImageView) this.findViewById(R.id.imageView);
ksbf = (Button) this.findViewById(R.id.ksbf);
jsbf = (Button) this.findViewById(R.id.jsbf);
give = (Button) this.findViewById(R.id.give);
ksbf.setOnClickListener(this);
jsbf.setOnClickListener(this);
give.setOnClickListener(this);
Intent intent = this.getIntent();
bundle = intent.getExtras();
//bm = BitmapFactory.decodeFile(bundle.getString("imagePath"));
bm= new BitmapUtils(PicVist.this).decodeBitmap(bundle.getString("imagePath"));
this.imageView.setImageBitmap(bm);
myApp = (MyApp) this.getApplication();
custId = bundle.getString("cid");
// 判断是否已经开始拜访
db = new Optdb_interfce(PicVist.this);
content = db.selectBfRecord(custId);
if (!"".equals(content)&&content!=null) {
jsbf.setVisibility(Button.VISIBLE);
} else {
jsbf.setVisibility(Button.GONE);
}
//获取拜访内容
content=bundle.getString("content");
share_obj = new Optsharepre_interface(PicVist.this);
dislimit = Integer.parseInt(share_obj
.getDataFromPres("DISLIMIT"));
// 暂时性的容错处理
String lonStr = bundle.getString("cuLon");
String latStr = bundle.getString("cuLat");
String clonStr = bundle.getString("cuClon");
String clatStr = bundle.getString("cuClat");
if (lonStr == null || lonStr.equals("")) {
lonStr = "0.0";
}
if (latStr == null || latStr.equals("")) {
latStr = "0.0";
}
if (clonStr == null || clonStr.equals("")) {
clonStr = "0.0";
}
if (clatStr == null || clatStr.equals("")) {
clatStr = "0.0";
}
cuLon = Double.parseDouble(lonStr);
cuLat = Double.parseDouble(latStr);
cuClon = Double.parseDouble(clonStr);
cuClat = Double.parseDouble(clatStr);
shareMethod = new ShareMethod(PicVist.this);
//注册百度
baidu=new Baidu_location(PicVist.this);
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v == ksbf) {
bf_point = 1;
registerRec();
picThread = new PicDealThread();
picThread.start();
} else if (v == jsbf) {
bf_point = 2;
registerRec();
picThread = new PicDealThread();
picThread.start();
} else if (v == give) {
finish();
}
}
private class MyBroadcastReciver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String action = intent.getAction();
if(action.equals("upfile")) {
int max= intent.getIntExtra("max", 0);
Log.e("xiao","max_______"+max);;
if(max!=0){
if(pBar!=null){
pBar.setMax(max/1000);
}
}
int length= intent.getIntExtra("length", 0);
Log.e("xiao","length_______"+length);;
if(pBar!=null){
pBar.setProgress(length/1024);
}
}
}
}
public void registerRec(){
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("upfile");
receiveBroadCast = new MyBroadcastReciver();
this.registerReceiver(receiveBroadCast, intentFilter);
}
/**
* 图片上传线程
*
* @author Administrator
*
*/
class PicDealThread extends Thread {
@Override
public void run() {
// TODO Auto-generated method stub
super.run();
Looper.prepare();
Message message = new Message();
message.what = 1;
myHander.sendMessage(message);
File uploadFile = new File(bundle.getString("imagePath"));
int uplength = (int) uploadFile.length();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Message message2 = new Message();
param = 2;
Bundle bundle = new Bundle();
String result = "1";
// 请求服务器
result = khbfUpToServer();
shareMethod.recordLog("客户拜访-----服务器返回的数据为++"+result);
db = new Optdb_interfce(PicVist.this);
Log.i(TAG, "上传结果:"+result);
if (result != null && result.equals("0")) {
bundle.putString("msg", "上传成功");
// 判断是初次拜访还是结束拜访
if (bf_point == 1) {// 开始拜访
db.updateBfRecord("add", custId,content);
} else if (bf_point == 2) {// 结束拜访
db.updateBfRecord("delete", custId,"");
}
db.close_SqlDb();
} else if (result.equals("-999")) {
bundle.putString("msg", "位置获取失败,请稍后重新操作!");
} else if (result.equals("-888")) {
bundle.putString("msg", "超出管理员设置的误差范围" + dislimit
+ "米,请走近客户进行操作!");
} else if (result.equals("2")) {
bundle.putString("msg", "找不到图片的路径,无法上传!");
} else {
failToSave();
}
Log.i(TAG, "param-----------"+param);
message2.setData(bundle);
message2.what=param;
myHander.sendMessage(message2);
Looper.loop();
}
}
/**
* 上传失败后保存
*/
public void failToSave() {
new AlertDialog.Builder(PicVist.this).setTitle("提示")
.setMessage("上传失败,是否保存?")
.setPositiveButton("保存", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
ArrayList<Object> list = new ArrayList<Object>();
RecordModel record = new RecordModel();
record.setPic_url(bundle.getString("imagePath"));// 存放图片地址
record.setPic_detail(bundle.getString("pic_detail"));// 存放图片描述
String time = "";
if (bundle.getString("time") != null
&& !bundle.getString("time").equals("0")) {
time = bundle.getString("time");
} else {
time = new Date().getTime() + "";
}
record.setAdd_time(time);
record.setCid(bundle.getString("cell_id"));
record.setCustid(bundle.getString("cid"));
record.setJclb(bundle.getString("yys"));// 保存运营商字段值
record.setCcode(bundle.getString("country_code"));
record.setLac(bundle.getString("lac_code"));
record.setLat(bundle.getString("lat"));
record.setLon(bundle.getString("lon"));
record.setNcode(bundle.getString("ncode"));
record.setRaduis(bundle.getString("radius"));
record.setXhqd(bundle.getString("signal_strength"));
record.setRemark1(bundle.getString("cname"));// 保存客户名称
record.setContent(bundle.getString("content"));// 保存拜访内容
if (bundle.getString("type").equals("1")) {// 1、客户走访2、二次回访
record.setGid("4");
} else {
record.setGid("6");
}
record.setResult(bundle.getString("isfirst"));// 1、开始拜访2、结束拜访
record.setXxcjlb(bundle.getString("jhid"));// 保存计划id
list.add(record);
db = new Optdb_interfce(PicVist.this);
db.updateToDb("T_RECORDS", list);
db.close_SqlDb();
Toast.makeText(PicVist.this, "保存成功", Toast.LENGTH_SHORT)
.show();
}
}).setNegativeButton("取消", null).show();
}
Handler myHander = new Handler() {
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
try {
switch (msg.what) {
case 1:
//showDialog(1);
pBar = new MyProgressDialog(PicVist.this);
pBar.setMessage("正在上传,请稍候……");
pBar.setCancelable(false);
pBar.show();
break;
case 2:
//removeDialog(1);
pBar.dismiss();
Bundle data = msg.getData();
String message = data.getString("msg");
showToast(message);
break;
case 3:
removeDialog(1);
break;
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("显示或取消运行框时出现异常");
}
}
};
/**
* 客户拜访上传图片
*
* @return
*/
public String khbfUpToServer() {
String upResult = "";
double distance = 0.0;
if(pic_detail.getText().toString().trim().equals("")){
bundle.putString("pic_detail", "无");
}else{
bundle.putString("pic_detail", pic_detail.getText().toString());
}
ShareMethod share_loc = new ShareMethod(PicVist.this);
// 获取基站相关信息
LocationModule loc = new LocationUtils(PicVist.this).takeCellInfos();
LocationUtils locationUtils=new LocationUtils(PicVist.this);
boolean b=locationUtils.getLocation(loc, "All",bundle.getString("cid"),baidu);
// b=true;//测试用
if(b){
//位置获取成功
int state=loc.getState();
boolean up=true;//默认上传
if(state==2){
// 百度获取位置成功
if (!flagDistance(loc.getLon(), loc.getLat(), 3)) {// 百度获得的位置超出误差范围,获取卫星位置
System.out
.println("******************百度获取到位置超出距离,请求卫星******************");
boolean b2=locationUtils.getLocation(loc, "onlyGps","",baidu);
if(b2){
if (!flagDistance(loc.getLon(), loc.getLat(),
1)) {// 卫星位置判断误差
System.out
.println("******************卫星获取到位置超出距离******************");
upResult = "-888";
up=false;
}
}else{
int s=loc.getState();
if(s==-1){
upResult = "-888";
//记录日志
log = "GPS模块未打开,获取位置失败!";
shareMethod.recordLog(log);
up=false;
}else if(s==0){
System.out
.println("******************通过卫星获取位置信息失败******************");
upResult = "-888";
//记录日志
log = "通过卫星获取位置信息失败!";
shareMethod.recordLog(log);
up=false;
}
}
}
}else if(state==1){
//卫星获取位置成功
if (!flagDistance(loc.getLon(), loc.getLat(), 1)) {// 卫星位置判断误差
System.out
.println("******************卫星获取到位置超出距离******************");
upResult = "-888";
up=false;
}
}
// 上报位置
if(up){
upResult = upFile(loc);
}
}else{
//位置获取失败
upResult = "-999";
}
return upResult;
}
/**
* 上传文件
*/
public String upFile(LocationModule loc) {
String result = "";
bundle.putString("country_code", loc.getCcode());
bundle.putString("cell_id", loc.getCell_id());
bundle.putString("lac_code", loc.getLac());
bundle.putString("lon", loc.getLon() + "");
bundle.putString("lat", loc.getLat() + "");
bundle.putString("time", loc.getTime() + "");
bundle.putString("ncode", loc.getNcode());
bundle.putString("radius", loc.getRadius());
bundle.putString("signal_strength", loc.getXhqd());
bundle.putString("yys", loc.getYys());
bundle.putString("loc_method", loc.getDw_type() + "");
bundle.putString("isfirst", bf_point + "");
UpMyFile file = new UpMyFile(PicVist.this);
result = file.upToServer(bundle, "khbf");
//记录日志
log = "《客户走访/二次回访拍照》-----"+"定位方式:"+loc.getDw_type()+",经度:"+loc.getLon()+",纬度:"+loc.getLat()+",上传结果:"+result;
Log.i(TAG, log);
shareMethod.recordLog(log);
if(!result.equals("0") && !result.equals("2")){
param=3;
}
return result;
}
/**
* 误差距离判断
*
* @param lon
* @param lat
*/
public boolean flagDistance(double lon, double lat, int dwType) {
log = "《客户走访/二次回访拍照》-----"+"定位方式:"+dwType+",经度:"+lon+",纬度:"+lat;
boolean b=true;//默认在误差范围内
double distance = 0.0;
switch (dwType) {
case 1:// 卫星位置判断
// 如果客户的真实经纬度为空,则需要加对客户加密过后的经纬度加密比较
if (cuLon == 0.0 || cuLat == 0.0) {
PublicDeal pd = new PublicDeal();
Map map = pd.jp(lon, lat);
double clon = Double.parseDouble((String) map.get("x"));
double clat = Double.parseDouble((String) map.get("y"));
distance = GetDistance.getDistance(clon, clat, cuClon, cuClat);
log+=",客户端对客户加过密后的经度:"+clon+",纬度:"+clat+",误差结果:"+distance;
} else {
distance = GetDistance.getDistance(lon, lat, cuLon, cuLat);
log+=",客户真实经度:"+cuLon+",真实纬度:"+cuLat+",误差结果:"+distance;
}
break;
case 3:// 百度位置判断
distance = GetDistance.getDistance(lon, lat, cuClon, cuClat);
log+=",客户加过密后的经度:"+cuClon+",纬度:"+cuClat+",误差结果:"+distance;
break;
}
//记录日志
Log.i(TAG, log);
shareMethod.recordLog(log);
if (distance > dislimit){
b=false;
}
return b;
}
// 销毁百度定位监听器
public void freeBdListener() {
if (baidu != null) {
baidu.stopListener();
baidu = null;
}
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
if(receiveBroadCast!=null){
PicVist.this.unregisterReceiver(receiveBroadCast);
}
super.onDestroy();
}
@Override
public void finish() {
// TODO Auto-generated method stub
super.finish();
System.out
.println("××××××××××××××××××××××××清空图片所占内存××××××××××××××××××××××××");
// 清空内存
if (bm != null && !bm.isRecycled()) {
bm.recycle();
bm = null;
}
// 终止上报线程
if (picThread != null && picThread.isAlive()) {
System.out.println("终止上报线程");
try {
PicDealThread.currentThread().interrupt();
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println("终止线程出现异常");
e.printStackTrace();
}
}
// 销毁百度定位监听器
freeBdListener();
//System.gc();
}
// 返回就清空图片内存
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
finish();
}
return super.onKeyDown(keyCode, event);
}
/**
* 结果显示
*
* @param message
*/
public void showToast(final String message) {
new AlertDialog.Builder(PicVist.this).setTitle("提示").setMessage(message)
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
if(message.contains("成功")){
PicVist.this.finish();
}
}
}).show();
}
@Override
protected Dialog onCreateDialog(int id) {
// TODO Auto-generated method stub
ProgressDialog dialog = new ProgressDialog(PicVist.this);
dialog.setIndeterminate(true);
dialog.setCancelable(false);
switch (id) {
case 1:
dialog.setMessage("正在上传,请稍候……");
break;
}
return dialog;
}
}