package com.jqyd.son; import java.io.File; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.Map; import org.json.JSONException; import org.json.JSONObject; 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.camera.PhotoUtil; import com.jqyd.camera.UIUtil; import com.jqyd.manager.R; import com.jqyd.manager.Xxfk; import com.jqyd.model.LocationModule; import com.jqyd.model.RecordModel; import com.jqyd.shareInterface.Baidu_location; import com.jqyd.shareInterface.Optdb_interfce; import com.jqyd.shareInterface.Optsharepre_interface; import com.jqyd.shareInterface.SystemInfo; import com.jqyd.shareInterface.UpdataToServer; import android.annotation.SuppressLint; import android.app.Activity; import android.app.AlertDialog; 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.media.MediaScannerConnection; 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.text.Html; 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.TextView; import android.widget.Toast; public class Scjc extends Activity implements OnClickListener { private TextView khmc; private TextView lxr; private TextView lxdh; private TextView lxdz; private EditText zhpf; private EditText jcnr; private Button up; private Button pz; private String jclb; private Bundle bundle = null; 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 MyApp myApp; private Optdb_interfce db = null; private Optsharepre_interface share_obj = null; private String jhid = "-1"; private static String imagePath; private static String imagePath_new; private int num = 1;// 默认第一张 private ArrayList<String> paths = new ArrayList<String>(); private ArrayList<String> picTimes = new ArrayList<String>(); int param = 2; private ScjcThread scjcThread = null; private String log = "";//记录日志 private ShareMethod shareMethod = null; private final String TAG = "SCJC"; private Baidu_location baidu=null; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.scjc); share_obj = new Optsharepre_interface(Scjc.this); if(share_obj.getDataFromPres("istakephoto").equals("1")){ share_obj.editPres("istakephoto", "0"); UIUtil.showMsg(Scjc.this, "手机内存不足,拍照失败"); finish(); } khmc = (TextView) this.findViewById(R.id.khmc); lxr = (TextView) this.findViewById(R.id.lxr); lxdh = (TextView) this.findViewById(R.id.lxdh); lxdz = (TextView) this.findViewById(R.id.lxdz); zhpf = (EditText) this.findViewById(R.id.zhpf); jcnr = (EditText) this.findViewById(R.id.jcnr); up = (Button) this.findViewById(R.id.up); pz = (Button) this.findViewById(R.id.pz); up.setOnClickListener(this); pz.setOnClickListener(this); myApp = (MyApp) this.getApplication(); jclb = myApp.getSort(); Intent intent = this.getIntent(); jhid = intent.getStringExtra("spid"); Bundle bundle = intent.getExtras(); khmc.setText(bundle.getString("cname")); lxr.setText(bundle.getString("linkman")); lxdh.setText(bundle.getString("link_sim")); lxdz.setText(bundle.getString("address")); custId = bundle.getString("cid"); // 暂时性的容错处理 String lonStr = bundle.getString("lon"); String latStr = bundle.getString("lat"); String clonStr = bundle.getString("clon"); String clatStr = bundle.getString("clat"); dislimit = Integer.parseInt(share_obj .getDataFromPres("DISLIMIT")); 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(Scjc.this); //注册百度 baidu = new Baidu_location(Scjc.this); } @Override public void onClick(View v) { // TODO Auto-generated method stub if (v == pz) { share_obj.editPres("istakephoto", "1"); takePhotos(); } else if (v == up) { scjcThread = new ScjcThread(); scjcThread.start(); } } /** * 非空警告提示 */ public void warnTell() { new AlertDialog.Builder(Scjc.this).setTitle("警告") .setMessage("请将信息填写完整!").setPositiveButton("确定", null).show(); } /** * 市场检查线程 * * @author Administrator * */ class ScjcThread extends Thread { @Override public void run() { // TODO Auto-generated method stub super.run(); Looper.prepare(); String upResult = ""; Message message = new Message(); message.what = 1; myHander.sendMessage(message); Message message2 = new Message(); param = 2; Bundle bundle = new Bundle(); // 获取基站相关信息 LocationModule loc = new LocationUtils(Scjc.this).takeCellInfos(); LocationUtils locationUtils=new LocationUtils(Scjc.this); boolean b=locationUtils.getLocation(loc, "All",custId,baidu); 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 = uploadScjcInfo(loc); } }else{ //位置获取失败 upResult = "-999"; } if (upResult.equals("-999")) { upResult= "位置获取失败,请稍后重新操作!"; } else if (upResult.equals("-888")) { upResult= "超出管理员设置的误差范围" + dislimit + "米,请走近客户进行操作!"; } bundle.putString("msg", upResult); message2.setData(bundle); message2.what = param; myHander.sendMessage(message2); Looper.loop(); } } /** * 上传市场检查信息 * * @param loc * @param dw_type * @return */ public String uploadScjcInfo(LocationModule loc) { String result = "-1"; db = new Optdb_interfce(Scjc.this); // 获得登录者姓名 String userName = db.searchEmpName(share_obj.getDataFromPres("GUID")); db.close_SqlDb(); JSONObject object = new JSONObject(); Log.i(TAG, "检查类别----------"+jclb); try { object.put("cosim", share_obj.getDataFromPres("COSIM")); object.put("regsim", share_obj.getDataFromPres("REGSIM")); 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("loc_method", loc.getDw_type()); object.put("radius", loc.getRadius()); object.put("yys", loc.getYys()); object.put("gguid", share_obj.getDataFromPres("GGUID")); object.put("guid", share_obj.getDataFromPres("GUID")); object.put("cname", khmc.getText().toString()); object.put("zdmc", userName); object.put("content", jcnr.getText().toString()); object.put("zhpf", zhpf.getText().toString()); object.put("jclb", jclb); object.put("jhid", jhid); } catch (JSONException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } UpdataToServer server = new UpdataToServer(Scjc.this); // 请求服务器 result = server.dataToServer("SCJC", object); //记录日志 log = "《市场检查》-----"+"定位方式:"+loc.getDw_type()+",经度:"+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); result = obj.getString("detail"); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } } else { param = 3; failToSave(object); } return result; } /** * 上传失败后保存 */ public void failToSave(final JSONObject object) { new AlertDialog.Builder(Scjc.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 { String time = ""; if (object.getLong("time") != 0) { time = object.getLong("time") + ""; } else { time = new Date().getTime() + ""; } record.setAdd_time(time); record.setCcode(object.getString("country_code")); record.setCid(object.getString("cell_id")); record.setContent(object.getString("content")); record.setCustid(object.getString("cid")); record.setGid("7"); record.setJclb(object.getString("jclb")); 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.setWzbs(object.getString("loc_method"));// 保存位置获取方式 record.setXhqd(object.getString("signal_strength")); record.setXxcjlb(object.getString("jhid"));// 保存计划id record.setResult(object.getString("yys"));// 保存运营商 record.setRemark2(object.getString("cname"));// 保存客户名称 record.setRemark1(object.getString("zhpf"));// 综合评分 } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } list.add(record); db = new Optdb_interfce(Scjc.this); db.updateToDb("T_RECORDS", list); db.close_SqlDb(); Toast.makeText(Scjc.this, "保存成功", Toast.LENGTH_SHORT) .show(); } }).setNegativeButton("取消", null).show(); } /** * 误差距离判断 * * @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; } 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 bundle = msg.getData(); showToast(bundle.getString("msg")); break; case 3: removeDialog(1); 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); Uri photoUri = getContentResolver().insert( MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values); // 限制最多拍五张照片 if (num <= 5) { num += 1; if(photoUri!=null){ imagePath = getRealPathFromURI(photoUri, getContentResolver()); }else{ imagePath = ""; } if(!new File(imagePath).exists()){ 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(); } //paths.add(imagePath); System.out.println("imagePath:" + imagePath); String picTime = new Date().getTime() + ""; picTimes.add(picTime); 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); } else { new AlertDialog.Builder(Scjc.this) .setTitle("提示") .setMessage(Html.fromHtml("对不起最多只能拍摄五张照片!")) .setPositiveButton("确定", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { jumpActivity(); } }).show(); } } /** * 拍照返回结果 */ @SuppressLint("NewApi") @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); BitmapUtils bitmapUtils=new BitmapUtils(Scjc.this); //压缩图片之后保存起来 Bitmap bitmap = bitmapUtils.decodeBitmap(imagePath); if(bitmap!=null){ Bitmap bitmap_new = bitmapUtils.zoomBitmap(bitmap); try { imagePath_new=bitmapUtils.saveBitmap(bitmap_new); } catch (IOException e) { e.printStackTrace(); } paths.add(imagePath_new.equals("")?imagePath:imagePath_new); if(!imagePath_new.equals("")){ new Thread(new Runnable() { @Override public void run() { Looper.prepare(); File originImage=new File(imagePath); originImage.delete(); Looper.loop(); } }).start(); } new AlertDialog.Builder(Scjc.this) .setTitle("提示") .setMessage(Html.fromHtml("是否继续拍照?")) .setPositiveButton("继续拍", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { takePhotos(); } }) .setNegativeButton("不拍了", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { for(String path : paths){ shareMethod.recordLog("日志上报---地址++"+Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getPath()+"/"+path); MediaScannerConnection.scanFile(Scjc.this, new String[]{path}, null, null); } jumpActivity(); } }).show(); }else{ 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 jumpActivity() { db = new Optdb_interfce(Scjc.this); // 获得登录者姓名 String userName = db.searchEmpName(share_obj.getDataFromPres("GUID")); db.close_SqlDb(); bundle = new Bundle(); Intent intent = new Intent(); //bundle.putString("imagePath", imagePath); share_obj = new Optsharepre_interface(Scjc.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("content", jcnr.getText().toString()); bundle.putString("zhpf", zhpf.getText().toString()); bundle.putString("cname", khmc.getText().toString()); bundle.putString("cid", custId); bundle.putString("zdmc", userName); bundle.putString("cuLon", cuLon + ""); bundle.putString("cuLat", cuLat + ""); bundle.putString("cuClon", cuClon + ""); bundle.putString("cuClat", cuClat + ""); Log.i(TAG, "拍照传递检查类别----------"+jclb); bundle.putString("jclb", jclb); bundle.putString("jhid", jhid); intent.putExtras(bundle); intent.putExtra("pic_sort", "scjc"); intent.putStringArrayListExtra("imagePaths", paths);// 存放多图片路径 intent.setClass(Scjc.this, PicMoreDetail.class); startActivity(intent); finish(); } /** * 登录结果显示 * * @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(this); dialog.setIndeterminate(true); dialog.setCancelable(false); switch (id) { case 1: dialog.setMessage("正在上传,请稍候……"); break; } return dialog; } @Override public void finish() { // TODO Auto-generated method stub super.finish(); // 终止上报线程 if (scjcThread != null && scjcThread.isAlive()) { System.out.println("终止上报线程"); try { ScjcThread.currentThread().interrupt(); } catch (Exception e) { // TODO Auto-generated catch block System.out.println("终止线程出现异常"); e.printStackTrace(); } } freeBdListener(); } // 销毁百度定位监听器 public void freeBdListener() { if (baidu != null) { baidu.stopListener(); baidu = null; } } @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); } }