package com.jqyd.son;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
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.graphics.BitmapFactory.Options;
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.LocationUtils;
import com.jqyd.app.MyApp;
import com.jqyd.app.ShareMethod;
import com.jqyd.app.UpMyFile;
import com.jqyd.manager.R;
import com.jqyd.manager.Xxfk;
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.SystemInfo;
public class PicDetail extends Activity implements OnClickListener {
private EditText pic_detail;
private ImageView imageView;
private Button up;
private Button save;
private Button giveup;
private Bundle bundle = null;
private Bitmap bm = null;
private String pic_sort = "";
private MyApp myApp;
private Optdb_interfce db = null;
private int dislimit = 0;
private PicDealThread picThread = null;
private SaveThread saveThread = null;
int param = 2;
private String log = "";// 记录日志
private ShareMethod shareMethod = null;
private final String TAG = "KHCJ";
private Baidu_location baidu = null;
private MyProgressDialog pBar;
private MyBroadcastReciver receiveBroadCast;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.picinfo);
pic_detail = (EditText) this.findViewById(R.id.pic_detail);
imageView = (ImageView) this.findViewById(R.id.imageView);
up = (Button) this.findViewById(R.id.up);
save = (Button) this.findViewById(R.id.save);
giveup = (Button) this.findViewById(R.id.giveup);
up.setOnClickListener(this);
save.setOnClickListener(this);
giveup.setOnClickListener(this);
Intent intent = this.getIntent();
bundle = intent.getExtras();
pic_sort = intent.getStringExtra("pic_sort");
save.setVisibility(View.GONE);
if (pic_sort.equals("xxfk")) {
save.setVisibility(View.VISIBLE);
}
String filePath = bundle.getString("imagePath");
// bm = zoomBitmap(filePath);
// bm = BitmapFactory.decodeFile(bundle.getString("imagePath"));
bm = new BitmapUtils(PicDetail.this).decodeBitmap(filePath);
this.imageView.setImageBitmap(bm);
myApp = (MyApp) this.getApplication();
shareMethod = new ShareMethod(PicDetail.this);
// 注册百度
baidu = new Baidu_location(PicDetail.this);
}
/**
* 图片处理
*
* @param filePath
* @return
*/
private Bitmap zoomBitmap(String filePath) {
try {
Options options = new Options();
options.inPreferredConfig = Bitmap.Config.RGB_565;
options.inPurgeable = true;
options.inInputShareable = true;
InputStream is = null;
is = new FileInputStream(new File(filePath));
return BitmapFactory.decodeStream(is, null, options);
} catch (Exception e) {
e.printStackTrace();
System.out.println("找不到文件,可能是文件路径不对!");
}
return null;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v == up) {
registerRec();
picThread = new PicDealThread();
picThread.start();
} else if (v == save) {
saveThread = new SaveThread();
saveThread.start();
} else if (v == giveup) {
finish();
}
}
/**
* 图片上传线程
*
* @author Administrator
*
*/
class PicDealThread extends Thread {
@Override
public void run() {
// TODO Auto-generated method stub
super.run();
Looper.prepare();
String result = "1";
File uploadFile = new File(bundle.getString("imagePath"));
int uplength = (int) uploadFile.length();
// 请求服务器
if (pic_sort.equals("xxfk")) {// 信息反馈
Message message = new Message();
message.what = 1;
myHander.sendMessage(message);
result = xxfkUpToServer();
} else if (pic_sort.equals("khcj") || pic_sort.equals("msbf")) {// 客户采集陌生拜访
Message message = new Message();
message.what = 7;
myHander.sendMessage(message);
result = khcjUpToServer();
}
Message message2 = new Message();
Bundle bundle = new Bundle();
if (result != null && result.equals("0")) {
bundle.putString("msg", "上传成功");
} else if (result.equals("-777")) {
bundle.putString("msg", "位置获取失败,请稍后重新操作!");
} else if (result.equals("2")) {
bundle.putString("msg", "找不到图片的路径,无法上传!");
} else {
param = 10;
if (pic_sort.equals("xxfk")) {
failToSave();
} else if (pic_sort.equals("khcj") || pic_sort.equals("msbf")) {
failToSave();
}
}
message2.what = param;
message2.setData(bundle);
myHander.sendMessage(message2);
Looper.loop();
}
}
public void registerRec() {
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("upfile");
receiveBroadCast = new MyBroadcastReciver();
this.registerReceiver(receiveBroadCast, intentFilter);
}
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);
shareMethod.recordLog("PicDetail-----max=="+max);
if (max != 0) {
if (pBar != null) {
pBar.setMax(max / 1000);
}
}
int length = intent.getIntExtra("length", 0);
shareMethod.recordLog("PicDetail-----length=="+length);
if (pBar != null) {
pBar.setProgress(length / 1024);
}
}
}
}
/**
* 上传失败后保存
*/
public void failToSave() {
new AlertDialog.Builder(PicDetail.this).setTitle("提示")
.setMessage("上传失败,是否保存?")
.setPositiveButton("保存", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
ArrayList<Object> list = new ArrayList<Object>();
RecordModel record = new RecordModel();
if (pic_sort.equals("xxfk")) {
new SaveThread().start();//启动保存线程
} else if (pic_sort.equals("khcj")
|| pic_sort.equals("msbf")) {
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.setPic_detail(bundle.getString("pic_detail"));
record.setPic_url(bundle.getString("imagePath"));
record.setRemark1(bundle.getString("cname"));// 保存客户名称
record.setRemark2(bundle.getString("linkman"));// 保存联系人
record.setRemark3(bundle.getString("sjhm"));// 保存手机号码
record.setRemark4(bundle.getString("link_sim"));// 保存联系人手机号码
record.setContent(bundle.getString("address"));// 保存客户地址
record.setResult(bundle.getString("kh_belong"));// 1、客户采集2、陌生拜访
if (pic_sort.equals("khcj")) {
record.setGid("8");
record.setBz(bundle.getString("bz"));
} else {
record.setGid("5");
record.setXxcjlb(bundle.getString("jhid"));// 保存计划id
record.setBz(bundle.getString("content"));
}
}
if(!pic_sort.equals("xxfk")){
list.add(record);
db = new Optdb_interfce(PicDetail.this);
db.updateToDb("T_RECORDS", list);
db.close_SqlDb();
Toast.makeText(PicDetail.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(PicDetail.this);
pBar.setMessage("正在上传,请稍候……");
pBar.setCancelable(false);
pBar.show();
break;
case 2:
if (pBar != null) {
pBar.dismiss();
}
// removeDialog(1);
Bundle data = msg.getData();
String message = data.getString("msg");
showToast(message);
break;
case 3:
if (pBar != null) {
pBar.dismiss();
}
// removeDialog(1);
showDialog(2);
break;
case 4:
showDialog(2);
break;
case 5:
removeDialog(2);
Bundle data2 = msg.getData();
String message2 = data2.getString("msg");
showToast(message2);
break;
case 6:
removeDialog(2);
break;
case 7:
showDialog(3);
break;
case 8:
removeDialog(3);
// showDialog(1);
pBar = new MyProgressDialog(PicDetail.this);
pBar.setMessage("正在上传,请稍候……");
pBar.setCancelable(false);
pBar.show();
break;
case 9:
removeDialog(3);
Bundle data3 = msg.getData();
String message3 = data3.getString("msg");
showToast(message3);
break;
case 10:
// removeDialog(1);
if (pBar != null) {
pBar.dismiss();
}
break;
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
/**
* 客户采集上传图片
*
* @return
*/
public String khcjUpToServer() {
String result = "";
bundle.putString("pic_detail", pic_detail.getText().toString());
// 获取基站相关信息
LocationModule loc = new LocationUtils(PicDetail.this).takeCellInfos();
LocationUtils locationUtils = new LocationUtils(PicDetail.this);
boolean b = locationUtils.getLocation(loc, "onlyGps", "", baidu);
if (b) {
Message message3 = new Message();
message3.what = 8;
myHander.sendMessage(message3);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
File uploadFile = new File(bundle.getString("imagePath"));
int uplength = (int) uploadFile.length();
if (pBar != null) {
pBar.setMax(uplength / 1024);
}
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());
UpMyFile file = new UpMyFile(PicDetail.this);
result = file.upToServer(bundle, pic_sort);
// 记录日志
log = "《客户采集拍照》-----" + ",经度:" + loc.getLon() + ",纬度:"
+ loc.getLat() + ",上传结果:" + result;
Log.i(TAG, log);
shareMethod.recordLog(log);
} else {
int state = loc.getState();
if (state == -1) {
Toast.makeText(PicDetail.this, "GPS模块不可用,请打开GPS功能!",
Toast.LENGTH_LONG).show();
result = "-777";
// 记录日志
log = "GPS模块未打开,获取位置失败!";
shareMethod.recordLog(log);
} else if (state == 0) {
result = "-777";
param = 9;
// 记录日志
log = "通过卫星获取位置信息失败!";
shareMethod.recordLog(log);
}
}
return result;
}
/**
* 客户采集图片保存
*
* @return
*/
public String khcjSave() {
String result = "";
bundle.putString("pic_detail", pic_detail.getText().toString());
// 获取基站相关信息
LocationModule loc = new LocationUtils(PicDetail.this).takeCellInfos();
LocationUtils locationUtils = new LocationUtils(PicDetail.this);
Baidu_location baidu = null;
boolean b = locationUtils.getLocation(loc, "", "", baidu);
if (b) {
Message message3 = new Message();
message3.what = 3;
myHander.sendMessage(message3);
ArrayList<Object> list = new ArrayList<Object>();
RecordModel record = new RecordModel();
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());
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.setPic_url(bundle.getString("imagePath"));// 存放图片地址
record.setPic_detail(bundle.getString("pic_detail"));// 存放图片描述
record.setRemark1(bundle.getString("cname"));// 保存客户名称
record.setRemark2(bundle.getString("linkman"));// 保存联系人
record.setRemark3(bundle.getString("sjhm"));// 保存手机号码
record.setRemark4(bundle.getString("link_sim"));// 保存联系人手机号码
record.setContent(bundle.getString("address"));// 保存客户地址
record.setResult(bundle.getString("kh_belong"));// 1、客户采集2、陌生拜访
if (pic_sort.equals("khcj")) {
record.setGid("8");
record.setBz(bundle.getString("bz"));
} else {
record.setGid("5");
record.setXxcjlb(bundle.getString("jhid"));// 保存计划id
record.setBz(bundle.getString("content"));
}
list.add(record);
db = new Optdb_interfce(PicDetail.this);
db.updateToDb("T_RECORDS", list);
db.close_SqlDb();
result = "0";
} else {
int state = loc.getState();
if (state == -1) {
Toast.makeText(PicDetail.this, "GPS模块不可用,请打开GPS功能!",
Toast.LENGTH_LONG).show();
result = "-777";
// 记录日志
log = "GPS模块未打开,获取位置失败!";
shareMethod.recordLog(log);
} else if (state == 0) {
result = "-777";
// 记录日志
log = "通过卫星获取位置信息失败!";
shareMethod.recordLog(log);
}
}
return result;
}
/**
* 信息反馈上传图片
*
* @return
*/
public String xxfkUpToServer() {
param = 2;// 成功
bundle.putString("pic_detail", pic_detail.getText().toString());
// 获取基站相关信息
LocationModule loc = new LocationUtils(PicDetail.this).takeCellInfos();
LocationUtils locationUtils = new LocationUtils(PicDetail.this);
boolean b = locationUtils.getLocation(loc, "All", "", baidu);
bundle.putString("country_code", loc.getCcode());
bundle.putString("cell_id", loc.getCell_id());
bundle.putString("lac_code", loc.getLac());
bundle.putString("time", String.valueOf(loc.getTime()));
bundle.putString("ncode", loc.getNcode());
if (loc.getXhqd().trim().equals("")) {
bundle.putString("signal_strength", "0");
} else {
bundle.putString("signal_strength", loc.getXhqd());
}
bundle.putString("yys", loc.getYys());
if (b) {
bundle.putString("lon", String.valueOf(loc.getLon()));
bundle.putString("lat", String.valueOf(loc.getLat()));
bundle.putString("radius", loc.getRadius());
bundle.putString("loc_method", String.valueOf(loc.getDw_type()));
} else {
bundle.putString("lon", "0.0");
bundle.putString("lat", "0.0");
bundle.putString("radius", "0");
bundle.putString("loc_method", "-1");
}
UpMyFile file = new UpMyFile(PicDetail.this);
String result = file.upToServer(bundle, pic_sort);
return result;
}
/**
* 信息反馈图片保存
*
* @return
*/
public String xxfkSave() {
bundle.putString("pic_detail", pic_detail.getText().toString());
LocationModule loc = new LocationUtils(PicDetail.this).takeCellInfos();
LocationUtils locationUtils = new LocationUtils(PicDetail.this);
boolean b = locationUtils.getLocation(loc, "All", "", baidu);
Optdb_interfce db = new Optdb_interfce(PicDetail.this);
ArrayList<Object> list = new ArrayList<Object>();
RecordModel record = new RecordModel();
record.setCcode(loc.getCcode());
record.setNcode(loc.getNcode());
record.setXhqd(loc.getXhqd());
record.setBz(loc.getYys());
record.setCid(loc.getCell_id());
record.setLac(loc.getLac());
record.setGid("3");
record.setXxcjlb(bundle.getString("xxcjlb"));
record.setContent(bundle.getString("xxnr"));// 存放信息内容
record.setRemark2(bundle.getString("xxbt"));// 备注存放信息标题
record.setAdd_time(new Date().getTime() + "");// 保存时间
record.setPic_url(bundle.getString("imagePath"));// 存放图片地址
record.setPic_detail(bundle.getString("pic_detail"));// 存放图片描述
if (b) {//定位成功
record.setLon(String.valueOf(loc.getLon()));
record.setLat(String.valueOf(loc.getLat()));
record.setRaduis(loc.getRadius());
record.setWzbs(String.valueOf(loc.getDw_type()));
} else {
record.setLon("0.0");
record.setLat("0.0");
record.setRaduis("0");
record.setWzbs("-1");
}
list.add(record);
db = new Optdb_interfce(PicDetail.this);
db.updateToDb("T_RECORDS", list);//写入数据库
db.close_SqlDb();
return "0";
}
/**
* 保存线程
*/
class SaveThread extends Thread {
@Override
public void run() {
// TODO Auto-generated method stub
super.run();
Looper.prepare();
String result = "1";
if (pic_sort.equals("xxfk")) {
Message message = new Message();
message.what = 4;
myHander.sendMessage(message);
result = xxfkSave();
} else if (pic_sort.equals("khcj") || pic_sort.equals("msbf")) {
Message message = new Message();
message.what = 7;
myHander.sendMessage(message);
result = khcjSave();
}
Message message2 = new Message();
message2.what = 5;
Bundle bundle = new Bundle();
if (result != null && result.equals("0")) {
bundle.putString("msg", "保存成功");
} else if (result.equals("-777")) {
message2.what = 2;
bundle.putString("msg", "位置获取失败,请稍后重新操作!");
} else if (result.equals("2")) {
bundle.putString("msg", "找不到图片的路径,无法保存!");
} else {
bundle.putString("msg", "保存失败!");
}
message2.setData(bundle);
myHander.sendMessage(message2);
Looper.loop();
}
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
if (receiveBroadCast != null) {
PicDetail.this.unregisterReceiver(receiveBroadCast);
}
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();
}
}
// 终止上报线程
if (saveThread != null && saveThread.isAlive()) {
System.out.println("终止上报线程");
try {
SaveThread.currentThread().interrupt();
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println("终止线程出现异常");
e.printStackTrace();
}
}
System.gc();
super.onDestroy();
}
// 返回就清空图片内存
@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(this).setTitle("提示").setMessage(message)
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
if (message.contains("成功")) {
finish();
}
}
}).show();
}
@Override
protected Dialog onCreateDialog(int id) {
// TODO Auto-generated method stub
ProgressDialog dialog = new ProgressDialog(PicDetail.this);
dialog.setIndeterminate(true);
dialog.setCancelable(false);
switch (id) {
case 1:
dialog.setMessage("正在上传,请稍候……");
break;
case 2:
dialog.setMessage("正在保存,请稍候……");
break;
case 3:
dialog.setMessage("正在搜索GPS位置,请耐心等待……");
break;
}
return dialog;
}
@Override
public void finish() {
// TODO Auto-generated method stub
super.finish();
freeBdListener();
}
// 销毁百度定位监听器
public void freeBdListener() {
if (baidu != null) {
baidu.stopListener();
baidu = null;
}
}
}