package com.jqyd.manager;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import org.json.JSONException;
import org.json.JSONObject;
import com.jqyd.app.BitmapUtils;
import com.jqyd.app.LocationUtils;
import com.jqyd.app.MyApp;
import com.jqyd.app.ShareMethod;
import com.jqyd.camera.PhotoUtil;
import com.jqyd.camera.UIUtil;
import com.jqyd.model.LocationModule;
import com.jqyd.model.RecordModel;
import com.jqyd.shareInterface.Baidu_location;
import com.jqyd.shareInterface.CheckState_interface;
import com.jqyd.shareInterface.GpsService;
import com.jqyd.shareInterface.Optdb_interfce;
import com.jqyd.shareInterface.Optsharepre_interface;
import com.jqyd.shareInterface.SystemInfo;
import com.jqyd.shareInterface.UpLocationService;
import com.jqyd.shareInterface.UpdataToServer;
import com.jqyd.son.Ddxq;
import com.jqyd.son.PicDetail;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.provider.MediaStore;
import android.provider.MediaStore.Audio.Media;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;
public class Khcj extends Activity implements OnClickListener {
private EditText khjc;
private EditText sjhm;
private EditText lxr;
private EditText lxdh;
private EditText qydz;
private EditText bz;
private EditText bz1;
private EditText bz2;
private EditText bfnr;
private Button pz;
private Button up;
private Button back;
private MyApp myApp;
private LinearLayout bz_layout;
private LinearLayout bz1_layout;
private LinearLayout bz2_layout;
private LinearLayout bfnr_layout;
private static String imagePath;
private static String imagePath_new;
private Bundle bundle = null;
private Optsharepre_interface share_obj = null;
private Optdb_interfce db = null;
private String custId = "";
private String module = "";
private String jhid = "-1";
private KhcjThread khcjThread = null;
private String log = "";// 记录日志
private ShareMethod shareMethod = null;
private final String TAG = "KHCJ";
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.khcj);
khjc = (EditText) this.findViewById(R.id.khjc);
sjhm = (EditText) this.findViewById(R.id.sjhm);
lxr = (EditText) this.findViewById(R.id.lxr);
lxdh = (EditText) this.findViewById(R.id.lxdh);
qydz = (EditText) this.findViewById(R.id.qydz);
bz = (EditText) this.findViewById(R.id.bz);
bz1 = (EditText) this.findViewById(R.id.bz1);
bz2 = (EditText) this.findViewById(R.id.bz2);
bfnr = (EditText) this.findViewById(R.id.bfnr);
pz = (Button) this.findViewById(R.id.pz);
back = (Button) this.findViewById(R.id.back);
up = (Button) this.findViewById(R.id.up);
up.setOnClickListener(this);
pz.setOnClickListener(this);
back.setOnClickListener(this);
bz_layout = (LinearLayout) this.findViewById(R.id.bz_layout);
bz1_layout = (LinearLayout) this.findViewById(R.id.bz1_layout);
bz2_layout = (LinearLayout) this.findViewById(R.id.bz2_layout);
bfnr_layout = (LinearLayout) this.findViewById(R.id.bfnr_layout);
myApp = (MyApp) this.getApplication();
bundle = new Bundle();
shareMethod = new ShareMethod(Khcj.this);
share_obj = new Optsharepre_interface(Khcj.this);
if (share_obj.getDataFromPres("istakephoto").equals("1")) {
module = share_obj.getDataFromPres("temp");
share_obj.editPres("istakephoto", "0");
UIUtil.showMsg(Khcj.this, "手机内存不足,拍照失败");
} else {
module = myApp.getModuleName();
share_obj.editPres("temp", module);
}
if (module.equals("khcj")) {
shareMethod.recordLog("客户采集");
Intent intent = this.getIntent();
Bundle bundle = intent.getExtras();
bz_layout.setVisibility(LinearLayout.VISIBLE);
bz1_layout.setVisibility(LinearLayout.VISIBLE);
bz2_layout.setVisibility(LinearLayout.VISIBLE);
bfnr_layout.setVisibility(LinearLayout.GONE);
this.setTitle("客户采集");
if (bundle != null) {
custId = bundle.getString("cid");
khjc.setText(bundle.getString("cname"));
lxr.setText(bundle.getString("linkman"));
lxdh.setText(bundle.getString("sjhm"));
qydz.setText(bundle.getString("address"));
sjhm.setText(bundle.getString("link_sim"));
}
} else if (module.equals("msbf") || module.equals("jh_msbf")) {
shareMethod.recordLog("陌生拜访");
bz_layout.setVisibility(LinearLayout.GONE);
bz1_layout.setVisibility(LinearLayout.GONE);
bz2_layout.setVisibility(LinearLayout.GONE);
bz1.setVisibility(LinearLayout.GONE);
bz2.setVisibility(LinearLayout.GONE);
bfnr_layout.setVisibility(LinearLayout.VISIBLE);
this.setTitle("陌生拜访");
if (module.equals("jh_msbf")) {
jhid = this.getIntent().getStringExtra("spid");
}
}
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v == up) {
String khjcStr = khjc.getText().toString();
String sjhmStr = sjhm.getText().toString();
String lxrStr = lxr.getText().toString();
if (khjcStr.equals("")) {
Toast.makeText(Khcj.this, "请填写客户简称", Toast.LENGTH_SHORT).show();
} else if (sjhmStr.equals("")) {
Toast.makeText(Khcj.this, "请填写手机号码", Toast.LENGTH_SHORT).show();
} else if (lxrStr.equals("")) {
Toast.makeText(Khcj.this, "请填写联系人", Toast.LENGTH_SHORT).show();
} else {
bundle.putString("gguid", share_obj.getDataFromPres("GGUID"));
bundle.putString("guid", share_obj.getDataFromPres("GUID"));
bundle.putString("cosim", share_obj.getDataFromPres("COSIM"));
bundle.putString("regsim", share_obj.getDataFromPres("REGSIM"));
bundle.putString("cname", khjc.getText().toString());
bundle.putString("link_sim", sjhm.getText().toString());
bundle.putString("linkman", lxr.getText().toString());
bundle.putString("sjhm", lxdh.getText().toString());
bundle.putString("address", qydz.getText().toString());
khcjThread = new KhcjThread(false);
khcjThread.start();
}
} else if (v == pz) {
String khjcStr = khjc.getText().toString();
String sjhmStr = sjhm.getText().toString();
String lxrStr = lxr.getText().toString();
if (khjcStr.equals("")) {
Toast.makeText(Khcj.this, "请填写客户简称", Toast.LENGTH_SHORT).show();
} else if (sjhmStr.equals("")) {
Toast.makeText(Khcj.this, "请填写手机号码", Toast.LENGTH_SHORT).show();
} else if (lxrStr.equals("")) {
Toast.makeText(Khcj.this, "请填写联系人", Toast.LENGTH_SHORT).show();
} else {
share_obj.editPres("istakephoto", "1");
takePhotos();
}
} else if (v == back) {
finish();
}
}
/**
* 客户采集线程
*
* @author Administrator
*
*/
class KhcjThread extends Thread {
boolean goOn = false;
public KhcjThread(boolean goOn){
this.goOn = goOn;
}
@Override
public void run() {
// TODO Auto-generated method stub
super.run();
Looper.prepare();
Message message = new Message();
message.what = 4;
myHander.sendMessage(message);
Message message2 = new Message();
Bundle bundle2 = new Bundle();
int param = 5;
// 获取基站相关信息
LocationModule loc = new LocationUtils(Khcj.this).takeCellInfos();
LocationUtils locationUtils = new LocationUtils(Khcj.this);
Baidu_location baidu = null;// 因为只用GPS定位
boolean b = false;
// if(goOn){
// b=true;
// }else{
b = locationUtils.getLocation(loc, "onlyGps", "", baidu);
// }
if (b) {// 位置获取成功(仅仅卫星)
param = UploadData(bundle2, loc);
} else {
// if(loc!=null&&loc.getState()==2){
// //经纬度错误
// myHander.sendMessage(Message.obtain(myHander, 7));
// return;
// }
bundle2.putString("msg", "位置获取失败,请稍后重新操作!");
// 记录日志
log = "通过卫星获取位置信息失败!";
shareMethod.recordLog(log);
}
message2.setData(bundle2);
message2.what = param;
myHander.sendMessage(message2);
Looper.loop();
}
}
private int UploadData(Bundle bundle2, LocationModule loc) {
int param;
Message message3 = new Message();
message3.what = 6;
myHander.sendMessage(message3);
param = 2;
JSONObject object = new JSONObject();
try {
object.put("gguid", bundle.getString("gguid"));
object.put("guid", bundle.getString("guid"));
object.put("cosim", bundle.getString("cosim"));
object.put("regsim", bundle.getString("regsim"));
object.put("cname", bundle.getString("cname"));
object.put("sjhm", bundle.getString("sjhm"));
object.put("linkman", bundle.getString("linkman"));
object.put("link_sim", bundle.getString("link_sim"));
object.put("address", bundle.getString("address"));
object.put("cid", custId);
object.put("time", loc.getTime());
object.put("lon", loc.getLon());
object.put("lat", loc.getLat());
object.put("cell_id", loc.getCell_id());
object.put("lac_code", loc.getLac());
object.put("country_code", loc.getCcode());
object.put("ncode", loc.getNcode());
object.put("signal_strength", loc.getXhqd());
object.put("yys", loc.getYys());
object.put("radius", loc.getRadius());
object.put("jhid", jhid);
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
String result = "500";
// 请求服务器
if (module.equals("")) {
module = share_obj.getDataFromPres("cjlx");
}
if (module.equals("khcj")) {
try {
object.put("bz", bz.getText().toString() + "#"
+ bz1.getText().toString() + "#"
+ bz2.getText().toString());
object.put("kh_belong", "1");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (module != null) {
shareMethod.recordLog("khbelong" + 1 + module);
} else {
shareMethod.recordLog("khbelong" + 1);
}
result = new UpdataToServer(Khcj.this).dataToServer("KHCJ",
object);
} else {
try {
object.put("content", bfnr.getText().toString());
object.put("kh_belong", "2");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (module != null) {
shareMethod.recordLog("khbelong" + 2 + module);
} else {
shareMethod.recordLog("khbelong" + 2);
}
Log.e("khbelong", "2");
result = new UpdataToServer(Khcj.this).dataToServer("MSBF",
object);
}
// 记录日志
log = "《客户采集》-----" + ",经度:" + loc.getLon() + ",纬度:"
+ loc.getLat() + ",上传结果:" + result;
Log.i(TAG, log);
shareMethod.recordLog(log);
if (result != null && !result.equals("-1")
&& !result.equals("500")) {
try {
JSONObject obj = new JSONObject(result);
String res = obj.getString("result");
if (res.equals("0")) {
bundle2.putString("msg", "上传成功");
} else {
String errMsg = obj.getString("detail");
bundle2.putString("msg", errMsg);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
param = 3;
failToSave(object);
}
return param;
}
/**
* 上传失败后保存
*/
public void failToSave(final JSONObject object) {
new AlertDialog.Builder(Khcj.this).setTitle("提示")
.setMessage("上传失败,是否保存?")
.setPositiveButton("保存", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
ArrayList<Object> list = new ArrayList<Object>();
RecordModel record = new RecordModel();
try {
record.setAdd_time(object.getLong("time") + "");
record.setCid(object.getString("cell_id"));
record.setCustid(object.getString("cid"));
record.setJclb(object.getString("yys"));// 保存运营商字段值
record.setCcode(object.getString("country_code"));
record.setLac(object.getString("lac_code"));
record.setLat(object.getDouble("lat") + "");
record.setLon(object.getDouble("lon") + "");
record.setNcode(object.getString("ncode"));
record.setRaduis(object.getString("radius"));
record.setXhqd(object.getString("signal_strength"));
record.setRemark1(object.getString("cname"));// 保存客户名称
record.setRemark2(object.getString("linkman"));// 保存联系人
record.setRemark3(object.getString("sjhm"));// 保存手机号码
record.setRemark4(object.getString("link_sim"));// 保存联系人手机号码
record.setContent(object.getString("address"));// 保存客户地址
record.setXxcjlb(object.getString("jhid"));// 保存计划id
record.setResult(object.getString("kh_belong"));// 1、客户采集2、陌生拜访
if (module.equals("")) {
module = share_obj.getDataFromPres("cjlx");
}
if (module.equals("khcj")) {
record.setGid("8");
record.setBz(bz.getText().toString() + "#"
+ bz1.getText().toString() + "#"
+ bz2.getText().toString());
} else {
record.setGid("5");
record.setBz(object.getString("content"));
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
list.add(record);
db = new Optdb_interfce(Khcj.this);
db.updateToDb("T_RECORDS", list);
db.close_SqlDb();
Toast.makeText(Khcj.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);
break;
case 2:
removeDialog(1);
Bundle data = msg.getData();
String message = data.getString("msg");
showToast(message);
break;
case 3:
removeDialog(1);
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);
showDialog(1);
break;
case 7:
removeDialog(2);
AlertDialog.Builder builder = new Builder(getApplicationContext());
builder.setMessage("获取经纬度错误是否继续上传").setNeutralButton("取消上传", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
}).setNegativeButton("继续上传", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
khcjThread = new KhcjThread(true);
khcjThread.start();
}
}).create().show();
break;
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("显示或取消运行框时出现异常");
}
}
};
/**
* 拍照按钮
*/
public void takePhotos() {
if (!Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
Toast.makeText(this, "SD卡不可用", Toast.LENGTH_SHORT).show();
return;
}
SimpleDateFormat timeStampFormat = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
String filename = timeStampFormat.format(new Date());
ContentValues values = new ContentValues();
values.put(Media.TITLE, filename);
try{
Uri photoUri = getContentResolver().insert(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
if(photoUri!=null){
imagePath = getRealPathFromURI(photoUri, getContentResolver());
}else{
imagePath="";
}
}catch(Exception e){
e.printStackTrace();
imagePath="";
}
if(new File(imagePath).exists()){
Log.e("xiao","true");
}else{
Log.e("xiao", "false");
//如果为FALSE说明无法创建文件
// 更换目录
File dir = new File(PhotoUtil.SDCARD_ROOT_PATH+ PhotoUtil.SAVE_PATH_IN_SDCARD);
if(!dir.exists()){
dir.mkdirs();
}
imagePath=PhotoUtil.SDCARD_ROOT_PATH+ PhotoUtil.SAVE_PATH_IN_SDCARD+PhotoUtil.createPhotoName();
}
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
intent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(new File(imagePath)));
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 0);
startActivityForResult(intent, 0);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode != 0) {
return;
} else if (resultCode == Activity.RESULT_OK) {
System.out.println("GetCamera+sendPath:" + imagePath);
shareMethod.recordLog("客户采集图片地址:"+imagePath);
BitmapUtils bitmapUtils = new BitmapUtils(Khcj.this);
// 压缩图片之后保存起来
Bitmap bitmap = bitmapUtils.decodeBitmap(imagePath);
shareMethod.recordLog("客户采集图片:"+bitmap);
if (bitmap != null) {
Bitmap bitmap_new = bitmapUtils.zoomBitmap(bitmap);
try {
imagePath_new = bitmapUtils.saveBitmap(bitmap_new);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(!imagePath_new.equals("")){
new Thread(new Runnable() {
@Override
public void run() {
Looper.prepare();
File originImage = new File(imagePath);
originImage.delete();
Looper.loop();
}
}).start();
}
Intent intent = new Intent();
bundle = new Bundle();
share_obj = new Optsharepre_interface(Khcj.this);
bundle.putString("gguid", share_obj.getDataFromPres("GGUID"));
bundle.putString("guid", share_obj.getDataFromPres("GUID"));
bundle.putString("cosim", share_obj.getDataFromPres("COSIM"));
bundle.putString("regsim", share_obj.getDataFromPres("REGSIM"));
bundle.putString("cname", khjc.getText().toString());
bundle.putString("sjhm", lxdh.getText().toString());
bundle.putString("linkman", lxr.getText().toString());
bundle.putString("link_sim", sjhm.getText().toString());
bundle.putString("address", qydz.getText().toString());
if (module.equals("")) {
module = share_obj.getDataFromPres("cjlx");
}
if (module.equals("khcj")) {
bundle.putString("bz", bz.getText().toString() + "#"
+ bz1.getText().toString() + "#"
+ bz2.getText().toString());
bundle.putString("kh_belong", "1");
bundle.putString("cid", custId);
intent.putExtra("pic_sort", "khcj");
} else {
bundle.putString("content", bfnr.getText().toString());
bundle.putString("kh_belong", "2");
intent.putExtra("pic_sort", "msbf");
bundle.putString("jhid", jhid);
}
// if (module != null) {
shareMethod.recordLog("拍照khbelong" + module);
bundle.putString("imagePath",
imagePath_new.equals("") ? imagePath
: imagePath_new);
intent.putExtras(bundle);
intent.setClass(Khcj.this, PicDetail.class);
startActivity(intent);
finish();
// } else {
shareMethod.recordLog("拍照module____" + module);
// Toast.makeText(this, "手机内存不足,拍照失败", Toast.LENGTH_SHORT)
// .show();
// }
} else {
Toast.makeText(this, "未找到图片。", Toast.LENGTH_SHORT).show();
}
}
}
// 获得图片保存的路径
public static String getRealPathFromURI(Uri uri, ContentResolver resolver) {
String[] proj = { MediaStore.Images.Media.DATA };
Cursor cursor = resolver.query(uri, proj, null, null, null);
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
String str = cursor.getString(column_index);
cursor.close();
return str;
}
/**
* 清空历史数据
*/
public void cheanHisdatas() {
khjc.setText("");
sjhm.setText("");
lxr.setText("");
lxdh.setText("");
qydz.setText("");
bz.setText("");
bfnr.setText("");
}
/**
* 结果显示
*
* @param message
*/
public void showToast(final String message) {
new AlertDialog.Builder(Khcj.this).setTitle("提示").setMessage(message)
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
if (message.contains("成功")) {
cheanHisdatas();
}
}
}).show();
}
@Override
protected Dialog onCreateDialog(int id) {
// TODO Auto-generated method stub
ProgressDialog dialog = new ProgressDialog(Khcj.this);
dialog.setIndeterminate(true);
dialog.setCancelable(false);
switch (id) {
case 1:
dialog.setMessage("正在上传,请稍候……");
break;
case 2:
dialog.setMessage("正在搜索GPS位置,请耐心等待……");
break;
}
return dialog;
}
@Override
public void finish() {
// TODO Auto-generated method stub
// 终止上报线程
if (khcjThread != null && khcjThread.isAlive()) {
System.out.println("终止上报线程");
try {
khcjThread.interrupt();
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println("终止线程出现异常");
e.printStackTrace();
}
}
super.finish();
}
}