package com.jqyd.app;
import java.net.URL;
import java.net.URLConnection;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import org.json.JSONException;
import org.json.JSONObject;
import android.content.Context;
import android.content.Intent;
import android.os.SystemClock;
import android.telephony.TelephonyManager;
import android.telephony.cdma.CdmaCellLocation;
import android.telephony.gsm.GsmCellLocation;
import android.util.Log;
import com.jqyd.manager.UpLocationManager;
import com.jqyd.model.RecordModel;
import com.jqyd.shareInterface.Baidu_location;
import com.jqyd.shareInterface.ConnectStateService;
import com.jqyd.shareInterface.Optdb_interfce;
import com.jqyd.shareInterface.Optsharepre_interface;
import com.jqyd.shareInterface.ScreenService;
import com.jqyd.shareInterface.SystemInfo;
import com.jqyd.shareInterface.UpLocationService;
import com.jqyd.shareInterface.UpdataToServer;
import com.jqyd.son.Ddxq;
public class ShareMethod {
public static final double R = 6371004;
public Context context = null;
public Optdb_interfce db = null;
public Optsharepre_interface share_obj = null;
private TelephonyManager tm;
private WriteFile writeFile = null;
private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd/HH:mm:ss");
private String log = "";
public ShareMethod(Context context) {
this.context = context;
writeFile = new WriteFile(context, "wl_记录久其管家操作日志:"
+ new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
tm = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
}
// **********************************************判断误差**************************************************
/**
* 根据给定的两个经纬度计算两地之间的距离,单位km
*
* @param lon1
* 经度1
* @param lat1
* 纬度1
* @param lon2
* 经度2
* @param lat2
* 纬度2
* @return 两地距离
*/
public static double getDistance(double lon1, double lat1, double lon2,
double lat2) {
double x = changeToRad(lon1);
double y = changeToRad(lat1);
double a = changeToRad(lon2);
double b = changeToRad(lat2);
double rad = Math.acos(Math.cos(y) * Math.cos(b) * Math.cos(x - a)
+ Math.sin(y) * Math.sin(b));
if (rad > Math.PI)
rad = Math.PI * 2 - rad;
return R * rad;
}
/**
* 将角度转化为弧度
*
* @param angle
* 角度
* @return 弧度
*/
public static double changeToRad(double angle) {
return angle / 180 * Math.PI;
}
/**
* 获取当前网络CellId
*
* @return
*/
public int getCellId() {
int cid = 0;
int type = tm.getNetworkType();
// 在中国,联通的3G为UMTS或HSDPA,移动和联通的2G为GPRS或EGDE,电信的2G为CDMA,电信的3G为EVDO
// G网
try {
if (type == TelephonyManager.NETWORK_TYPE_UMTS
|| type == TelephonyManager.NETWORK_TYPE_HSDPA
|| type == TelephonyManager.NETWORK_TYPE_EDGE
|| type == TelephonyManager.NETWORK_TYPE_GPRS
|| type == TelephonyManager.NETWORK_TYPE_HSPA
|| type == TelephonyManager.NETWORK_TYPE_HSUPA) {
GsmCellLocation gcl = (GsmCellLocation) tm.getCellLocation();
cid = gcl.getCid();// 小区编号
// C网
} else if (type == TelephonyManager.NETWORK_TYPE_CDMA
|| type == TelephonyManager.NETWORK_TYPE_EVDO_0
|| type == TelephonyManager.NETWORK_TYPE_EVDO_A) {
// 参考地址:http://topic.csdn.net/u/20110110/23/c2f6524a-8746-4c1b-914e-57c7c9cced02.html
CdmaCellLocation cdl = (CdmaCellLocation) tm.getCellLocation();
cid = cdl.getBaseStationId();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return cid;
}
// ************************************************获取位置******************************************************
/**
* 获取位置共分为三个步骤 1、判断Cell id信息是否一致 2、判断是否获取到卫星 3、获取Baidu位置
* 返回参数:位置获取方式+经度+纬度+时间+误差半径+小区定位方式中的位置获取方式 已废弃
*
* @return
*/
public String takeLocation(int cellid, MyApp myApp, Baidu_location baidu,
int lac) {
db = new Optdb_interfce(context);
share_obj = new Optsharepre_interface(context);
HashMap<String, String> map = db.searchLacs(cellid, "", lac);
String locinfo = "";
long gpsTimeTemp = 0;
String bdTimeTemp = "";
int CellId = 0;
try {
gpsTimeTemp = Long.parseLong(share_obj
.getDataFromPres("gpsTimeTemp"));
bdTimeTemp = share_obj.getDataFromPres("bdTimeTemp");
CellId = Integer.valueOf(share_obj.getDataFromPres("cellId"))
.intValue();
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("获取历史时间出现异常");
}
log = "《获取位置--卫星定位》*******************" + "当前卫星时间:" + myApp.getTime()
+ ",上次卫星时间:" + gpsTimeTemp + ",上次百度时间:" + bdTimeTemp
+ ",获取位置信息:经度:" + myApp.getLon() + ",纬度:" + myApp.getLat();
if (map != null) {// 小区对比成功
locinfo = "0#" + map.get("lon") + "#" + map.get("lat") + "#" + "0"
+ "#" + map.get("radius") + "#" + map.get("loc_type");
// 记录日志
log = "《获取位置--小区定位》*******************获取位置信息:经度:" + map.get("lon")
+ ",纬度:" + map.get("lat") + ",定位方式:" + map.get("loc_type");
} else if (myApp.isActive() && myApp.getTime() != gpsTimeTemp) {// 卫星获取成功,且当前卫星时间和上次卫星时间不相同
locinfo = "1#" + myApp.getLon() + "#" + myApp.getLat() + "#"
+ myApp.getTime() + "#" + "0" + "#" + "-1";
// 记录gps的时间变量参数
share_obj.editPres("gpsTimeTemp", String.valueOf(myApp.getTime()));
} else {
if (baidu.getSb() != null) {// 请求百度获取位置成功
System.out.println("请求Baidu,获得结果:" + baidu.getSb());
String ret = baidu.getSb().toString();
String[] strs = ret.split(";");
String baiduCode = strs[3];
System.out.println("*****************百度返回代码-----------------"
+ baiduCode);
// 只有161为网络交互结果成功,其他为失败,暂时不调用百度的gps信息,且获取的位置为最新位置
int cellId = getCellId();// 当前网络CellId
// 记录日志
log = "《获取位置--百度定位》*******************百度返回代码:" + baiduCode
+ ",当前网络CellId:" + cellId + ",上次获取网络CellId:" + cellId
+ ",当前百度时间:" + strs[1] + ",上次百度时间:" + bdTimeTemp
+ ",获取位置信息:经度:" + strs[7] + ",纬度:" + strs[5];
if (baiduCode.equals("161")
&& ((strs[1].equals(bdTimeTemp) && cellId == CellId) || !strs[1]
.equals(bdTimeTemp))) {
locinfo = "3#" + strs[7] + "#" + strs[5] + "#" + strs[1]
+ "#" + strs[9] + "#" + "-1";
// 记录百度时间变量参数
share_obj.editPres("bdTimeTemp", strs[1]);
share_obj.editPres("cellId", String.valueOf(cellId));
} else {
System.out.println("************获取位置失败**************");
}
} else {
System.out.println("************获取位置失败**************");
}
}
System.out.println("获取位置log:" + log);
writeFile.writeToFile(sdf.format(new Date()) + " " + log + "\n");
return locinfo;
}
/**
* 获取基站信息,如果cellid为-1,说明当前基站信息是未知的,不用查询 param cellId
*/
public String getCellInfo(int cellid, String custId, int lac) {
db = new Optdb_interfce(context);
share_obj = new Optsharepre_interface(context);
String locinfo = "";
if (cellid != -1) {
try {
HashMap<String, String> map = db.searchLacs(cellid, custId, lac);
db.close_SqlDb();
if (map != null) {// 小区对比成功
locinfo = "0#" + map.get("lon") + "#" + map.get("lat")
+ "#" + "0" + "#" + map.get("radius") + "#"
+ map.get("loc_type");
// 记录日志
log = "《获取位置--小区定位》*******************获取位置信息:经度:"
+ map.get("lon") + ",纬度:" + map.get("lat")
+ ",定位方式:" + map.get("loc_type");
writeFile.writeToFile(sdf.format(new Date()) + " " + log
+ "\n");
} else {
System.out.println("************获取小区位置失败**************");
}
} catch (Exception e) {
e.printStackTrace();
}
}
return locinfo;
}
/**
* 获取百度位置 param baidu
*/
public String getBaiduInfo(MyApp myApp, Baidu_location baidu) {
share_obj = new Optsharepre_interface(context);
String locinfo = "";
if (baidu.getSb() != null) {// 请求百度获取位置成功
String bdTimeTemp = "";
int CellId = 0;
try {
bdTimeTemp = share_obj.getDataFromPres("bdTimeTemp");
CellId = Integer.valueOf(share_obj.getDataFromPres("cellId"))
.intValue();
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("获取历史时间出现异常");
}
System.out.println("请求Baidu,获得结果:" + baidu.getSb());
String ret = baidu.getSb().toString();
recordLog("请求Baidu,获得结果:" + ret);
String[] strs = ret.split(";");
String baiduCode = strs[3];
System.out.println("*****************百度返回代码-----------------"
+ baiduCode);
// 只有161为网络交互结果成功,其他为失败,暂时不调用百度的gps信息,且获取的位置为最新位置
int cellId = getCellId();// 当前网络CellId
// 记录日志
log = "《获取位置--百度定位》*******************百度返回代码:" + baiduCode
+ ",当前网络CellId:" + cellId + ",上次获取网络CellId:" + cellId
+ ",当前百度时间:" + strs[1] + ",上次百度时间:" + bdTimeTemp
+ ",获取位置信息:经度:" + strs[7] + ",纬度:" + strs[5];
recordLog(log);
if (baiduCode.equals("161")
&& ((strs[1].equals(bdTimeTemp) && cellId == CellId) || !strs[1]
.equals(bdTimeTemp))) {
locinfo = "3#" + strs[7] + "#" + strs[5] + "#" + strs[1] + "#"
+ strs[9] + "#" + "-1";
// 记录百度时间变量参数
share_obj.editPres("bdTimeTemp", strs[1]);
share_obj.editPres("cellId", String.valueOf(cellId));
} else {
System.out.println("************获取位置失败**************");
// 记录百度定位返回的错误代码
share_obj.editPres("errCode", baiduCode);
}
} else {
System.out.println("************获取位置失败**************");
// 记录百度定位返回的错误代码
share_obj.editPres("errCode", "-1");
}
return locinfo;
}
/**
* 仅获取卫星位置
*
* @param cellid
* @param myApp
* @return
*/
public String takeGpsLocation(MyApp myApp) {
share_obj = new Optsharepre_interface(context);
String locinfo = "";
long gpsTimeTemp = 0;
try {
gpsTimeTemp = Long.parseLong(share_obj
.getDataFromPres("gpsTimeTemp"));
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("获取历史时间出现异常");
}
log = "《获取位置--卫星定位》*******************" + "当前时间:" + myApp.getTime()
+ ",上次时间:" + gpsTimeTemp + ",获取位置信息:经度:" + myApp.getLon()
+ ",纬度:" + myApp.getLat() + " myApp.toString()"
+ myApp.toString();
writeFile
.writeToFile(sdf.format(new Date()) + " " + myApp.getTime()
+ "," + myApp.getLon() + "," + myApp.getLat()
+ ",历史时间:" + gpsTimeTemp + " myApp.isActive():"
+ myApp.isActive() + "\n");
System.out.println(myApp.getTime() + "," + myApp.getLon() + ","
+ myApp.getLat() + ",历史时间:" + gpsTimeTemp);
if (myApp.isActive() && (myApp.getTime() != gpsTimeTemp)) {// 卫星获取成功,且当前卫星时间和上次卫星时间不相同
locinfo = "0#" + myApp.getLon() + "#" + myApp.getLat() + "#"
+ myApp.getTime();
// 记录gps的时间变量参数
share_obj.editPres("gpsTimeTemp", String.valueOf(myApp.getTime()));
} else {
locinfo = "1";// 获取位置失败
}
System.out.println("获取位置log:" + log);
writeFile.writeToFile(sdf.format(new Date()) + " " + log + "\n");
return locinfo;
}
/**
* 获取Gps或Baidu位置
*
* @param myApp
* @param baidu
* @return 返回参数:位置获取方式+经度+纬度+时间+误差半径+小区定位方式中的位置获取方式
*/
public String takeBaiAndGpsLocation(MyApp myApp, Baidu_location baidu) {
share_obj = new Optsharepre_interface(context);
long gpsTimeTemp = 0;
String bdTimeTemp = "";
int CellId = 0;
try {
gpsTimeTemp = Long.parseLong(share_obj
.getDataFromPres("gpsTimeTemp"));
bdTimeTemp = share_obj.getDataFromPres("bdTimeTemp");
CellId = Integer.valueOf(share_obj.getDataFromPres("cellId"))
.intValue();
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("获取历史时间出现异常");
}
String locinfo = "";
log = "《获取位置--卫星定位》*******************" + "当前百度时间:" + myApp.getTime()
+ ",上次百度时间:" + bdTimeTemp + ",获取位置信息:经度:" + myApp.getLon()
+ ",纬度:" + myApp.getLat();
if (myApp.isActive() && myApp.getTime() != gpsTimeTemp) {// 卫星获取成功,且当前卫星时间和上次卫星时间不相同
locinfo = "1#" + myApp.getLon() + "#" + myApp.getLat() + "#"
+ myApp.getTime() + "#" + "0" + "#" + "-1";
// 记录gps的时间变量参数
share_obj.editPres("gpsTimeTemp", String.valueOf(myApp.getTime()));
} else {
System.out.println("-----------------请求百度-----------------"
+ baidu.getSb());
if (baidu.getSb() != null) {// 请求百度获取位置成功
System.out.println("请求Baidu,获得结果:" + baidu.getSb());
String ret = baidu.getSb().toString();
String[] strs = ret.split(";");
String baiduCode = strs[3];
System.out.println("*****************百度返回代码-----------------"
+ baiduCode);
// 只有161为网络交互结果成功,其他为失败,暂时不调用百度的gps信息,且获取的位置为最新位置
int cellId = getCellId();// 当前网络CellId
log = "《获取位置--百度定位》*******************百度返回代码:" + baiduCode
+ ",当前网络CellId:" + cellId + ",上次获取网络CellId:" + cellId
+ ",当前百度时间:" + strs[1] + ",上次百度时间:" + bdTimeTemp
+ ",获取位置信息:经度:" + strs[7] + ",纬度:" + strs[5];
if (baiduCode.equals("161")
&& ((strs[1].equals(bdTimeTemp) && cellId == CellId) || !strs[1]
.equals(bdTimeTemp))) {
locinfo = "3#" + strs[7] + "#" + strs[5] + "#" + strs[1]
+ "#" + strs[9] + "#" + "-1";
// 记录百度时间变量参数
share_obj.editPres("bdTimeTemp", strs[1]);
share_obj.editPres("cellId", String.valueOf(cellId));
} else {
System.out.println("************获取位置失败**************");
}
} else {
System.out.println("************获取位置失败**************");
}
}
System.out.println("获取位置log:" + log);
writeFile.writeToFile(sdf.format(new Date()) + " " + log + "\n");
return locinfo;
}
/**
* 获取服务器端时间
*
* @return
*/
public long getServerTime() {
long serverTime = 0;
SntpClient client = SntpClient.getInstance();
writeFile.writeToFile("------" + sdf.format(new Date()) + "开始获取网络时间\n");
JSONObject object = new JSONObject();
try {
object.put("getTime", "yes");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
UpdataToServer server = new UpdataToServer(context);
// 请求服务器
String result = server.dataToServer("GETIME", object);
if (result != null && !result.equals("-1") && !result.equals("500")) {
try {
JSONObject obj = new JSONObject(result);
serverTime = obj.getLong("timeSec");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
writeFile.writeToFile("------" + sdf.format(new Date())
+ "通过服务器 获取到的时间\n");
} else {
if (client.requestTime("cn.pool.ntp.org", 60000)) {
serverTime = client.getNtpTime()
+ SystemClock.elapsedRealtime()
- client.getNtpTimeReference();
writeFile.writeToFile("------" + sdf.format(new Date())
+ "通过ntp获取到的时间\n");
}
if (serverTime == 0) {
try {
URL url = new URL("http://www.bjtime.cn");// 取得资源对象
URLConnection uc = url.openConnection();// 生成连接对象
uc.connect(); // 发出连接
serverTime = uc.getDate(); // 取得网站日期时间
} catch (Exception e) {
e.printStackTrace();
}
}
writeFile.writeToFile("------" + sdf.format(new Date())
+ "通过北京时间 获取到的时间\n");
}
writeFile.writeToFile("------" + sdf.format(new Date()) + "获取网络时间结束\n");
Date current = new Date(serverTime);
// 获取网络时间
writeFile.writeToFile("------获取到的网络时间:" + current.toString()
+ " long值:" + serverTime + "\n-------");
return serverTime;
}
/**
* 检测连续上报服务、锁屏服务、错误日志记录服务是否启动
*/
public void checkService() {
String lxsbService = "com.jqyd.shareInterface.UpLocationService";
String screenService = "com.jqyd.shareInterface.ScreenService";
String connectStateServiceName = "com.jqyd.shareInterface.ConnectStateService";
SystemInfo systemInfo = new SystemInfo(context);
log = "";
if (!systemInfo.isServiceRunning(lxsbService)) {
Log.e("CHECK", "启动连续上报Service");
recordLog("启动连续上报Service");
Intent stateService = new Intent(context, UpLocationService.class);
context.startService(stateService);
} else {
Log.e("CHECK", "连续上报Service已经启动");
recordLog("连续上报Service已经启动");
}
if (!systemInfo.isServiceRunning(screenService)) {
log = "启动锁屏开屏Service";
Log.e("CHECK", log);
recordLog("启动锁屏开屏Service");
// 开启锁屏开屏服务
Intent intent = new Intent(context, ScreenService.class);
context.startService(intent);
} else {
log = "锁屏开屏Service已经启动";
Log.e("CHECK", log);
recordLog(log);
}
if (!systemInfo.isServiceRunning(connectStateServiceName)) {
ArrayList<Object> recordInfo = new Optdb_interfce(context).getRecordInfo(0);
log = ",启动网络监控Service前,判断数据库中是否有值:"+(recordInfo==null?"没有值":"有值");
Log.e("CHECK", log);
if(recordInfo!=null&&recordInfo.size()>0){
log = log+"启动网络监控Service";
Intent intent = new Intent(context, ConnectStateService.class);
context.startService(intent);
}
recordLog(log);
} else {
log = "网络监控Service已经启动";
Log.e("CHECK", log);
recordLog(log);
}
// recordLog(log);
}
/**
* 记录日志
*
* @param log
*/
public void recordLog(String log) {
writeFile.writeToFile(log);
}
/**
*
* Description: 删除日志 Title: deleteLog
*
* @param n
* 天数,删除多少天前 void
*/
public void deleteLog(int n) {
writeFile.deleteFile(n);
}
/**
* 记录连续上报日志
*
* @param log
*/
public void recordLxsbLog(String log) {
writeFile = new WriteFile(context, "wl_记录连续上报服务日志:"
+ new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
writeFile.writeToFile(sdf.format(new Date()) + log + "\n");
}
public void recordOptTime(String tContent) {
writeFile = new WriteFile(context, "wl_记录操作时间日志:"
+ new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
String con = tContent
+ new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
.format(new Date());
writeFile.recordOptTime(con + "\n");
}
public void recordNetState(String tContent) {
writeFile = new WriteFile(context, "wl_记录网络状况日志:"
+ new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
String con = tContent
+ new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
.format(new Date());
writeFile.recordOptTime(con + "\n");
}
/**
* 关闭数据库
*/
public void closeSqlite3Db() {
if (db != null) {
db.close_SqlDb();
}
}
}