Skip to content

Commit bcc2650

Browse files
Merge branch 'master' into patch-1
2 parents 55cd8e0 + 0e64640 commit bcc2650

File tree

18 files changed

+3425
-24
lines changed

18 files changed

+3425
-24
lines changed

CommonUtil/src/main/java/com/jingewenku/abrahamcaijin/commonutil/AppNetworkMgr.java

Lines changed: 818 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
package com.jingewenku.abrahamcaijin.commonutil;
2+
3+
/**
4+
* @Description:主要功能:GPS坐标转换工具 百度坐标(BD09)、国测局坐标(火星坐标,GCJ02)、和WGS84坐标系之间的转换的工具
5+
* @Prject: CommonUtilLibrary
6+
* @Package: com.jingewenku.abrahamcaijin.commonutil
7+
* @author: AbrahamCaiJin
8+
* @date: 2017年06月02日 10:26
9+
* @Copyright: 个人版权所有
10+
* @Company:
11+
* @version: 1.0.0
12+
*/
13+
14+
public class CoordinateTransformUtil {
15+
16+
static double x_pi = 3.14159265358979324 * 3000.0 / 180.0;
17+
static double pi = 3.1415926535897932384626;
18+
static double a = 6378245.0;
19+
static double ee = 0.00669342162296594323;
20+
21+
/**
22+
* 百度坐标系(BD-09)转WGS坐标(百度坐标纬度,百度坐标经度),WGS84坐标数组
23+
* @param lng
24+
* @param lat
25+
* @return
26+
*/
27+
public static double[] bd09towgs84(double lng, double lat) {
28+
double[] gcj = bd09togcj02(lng, lat);
29+
double[] wgs84 = gcj02towgs84(gcj[0], gcj[1]);
30+
return wgs84;
31+
}
32+
33+
/**
34+
* WGS坐标转百度坐标系(BD-09)(WGS84坐标系的经度,WGS84坐标系的纬度),百度坐标数组
35+
* @param lng
36+
* @param lat
37+
* @return
38+
*/
39+
public static double[] wgs84tobd09(double lng, double lat) {
40+
double[] gcj = wgs84togcj02(lng, lat);
41+
double[] bd09 = gcj02tobd09(gcj[0], gcj[1]);
42+
return bd09;
43+
}
44+
45+
/**
46+
* 火星坐标系(GCJ-02)转百度坐标系(BD-09)(火星坐标经度,火星坐标纬度),百度坐标数组
47+
* @param lng
48+
* @param lat
49+
* @return
50+
*/
51+
public static double[] gcj02tobd09(double lng, double lat) {
52+
double z = Math.sqrt(lng * lng + lat * lat) + 0.00002 * Math.sin(lat * x_pi);
53+
double theta = Math.atan2(lat, lng) + 0.000003 * Math.cos(lng * x_pi);
54+
double bd_lng = z * Math.cos(theta) + 0.0065;
55+
double bd_lat = z * Math.sin(theta) + 0.006;
56+
return new double[]{bd_lng, bd_lat};
57+
}
58+
59+
/**
60+
* 百度坐标系(BD-09)转火星坐标系(GCJ-02)(百度坐标纬度,百度坐标经度),火星坐标数组
61+
* @param bd_lon
62+
* @param bd_lat
63+
* @return
64+
*/
65+
public static double[] bd09togcj02(double bd_lon, double bd_lat) {
66+
double x = bd_lon - 0.0065;
67+
double y = bd_lat - 0.006;
68+
double z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_pi);
69+
double theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi);
70+
double gg_lng = z * Math.cos(theta);
71+
double gg_lat = z * Math.sin(theta);
72+
return new double[]{gg_lng, gg_lat};
73+
}
74+
75+
/**
76+
* WGS84转GCJ02(火星坐标系)(WGS84坐标系的经度,WGS84坐标系的纬度),火星坐标数组
77+
* @param lng
78+
* @param lat
79+
* @return
80+
*/
81+
public static double[] wgs84togcj02(double lng, double lat) {
82+
if (out_of_china(lng, lat)) {
83+
return new double[]{lng, lat};
84+
}
85+
double dlat = transformlat(lng - 105.0, lat - 35.0);
86+
double dlng = transformlng(lng - 105.0, lat - 35.0);
87+
double radlat = lat / 180.0 * pi;
88+
double magic = Math.sin(radlat);
89+
magic = 1 - ee * magic * magic;
90+
double sqrtmagic = Math.sqrt(magic);
91+
dlat = (dlat * 180.0) / ((a * (1 - ee)) / (magic * sqrtmagic) * pi);
92+
dlng = (dlng * 180.0) / (a / sqrtmagic * Math.cos(radlat) * pi);
93+
double mglat = lat + dlat;
94+
double mglng = lng + dlng;
95+
return new double[]{mglng, mglat};
96+
}
97+
98+
/**
99+
* GCJ02(火星坐标系)转GPS84(火星坐标系的经度,火星坐标系纬度),WGS84坐标数组
100+
* @param lng
101+
* @param lat
102+
* @return
103+
*/
104+
public static double[] gcj02towgs84(double lng, double lat) {
105+
if (out_of_china(lng, lat)) {
106+
return new double[]{lng, lat};
107+
}
108+
double dlat = transformlat(lng - 105.0, lat - 35.0);
109+
double dlng = transformlng(lng - 105.0, lat - 35.0);
110+
double radlat = lat / 180.0 * pi;
111+
double magic = Math.sin(radlat);
112+
magic = 1 - ee * magic * magic;
113+
double sqrtmagic = Math.sqrt(magic);
114+
dlat = (dlat * 180.0) / ((a * (1 - ee)) / (magic * sqrtmagic) * pi);
115+
dlng = (dlng * 180.0) / (a / sqrtmagic * Math.cos(radlat) * pi);
116+
double mglat = lat + dlat;
117+
double mglng = lng + dlng;
118+
return new double[]{lng * 2 - mglng, lat * 2 - mglat};
119+
}
120+
121+
/**
122+
* 纬度转换
123+
* @param lng
124+
* @param lat
125+
* @return
126+
*/
127+
public static double transformlat(double lng, double lat) {
128+
double ret = -100.0 + 2.0 * lng + 3.0 * lat + 0.2 * lat * lat + 0.1 * lng * lat + 0.2 * Math.sqrt(Math.abs(lng));
129+
ret += (20.0 * Math.sin(6.0 * lng * pi) + 20.0 * Math.sin(2.0 * lng * pi)) * 2.0 / 3.0;
130+
ret += (20.0 * Math.sin(lat * pi) + 40.0 * Math.sin(lat / 3.0 * pi)) * 2.0 / 3.0;
131+
ret += (160.0 * Math.sin(lat / 12.0 * pi) + 320 * Math.sin(lat * pi / 30.0)) * 2.0 / 3.0;
132+
return ret;
133+
}
134+
135+
/**
136+
* 经度转换
137+
* @param lng
138+
* @param lat
139+
* @return
140+
*/
141+
public static double transformlng(double lng, double lat) {
142+
double ret = 300.0 + lng + 2.0 * lat + 0.1 * lng * lng + 0.1 * lng * lat + 0.1 * Math.sqrt(Math.abs(lng));
143+
ret += (20.0 * Math.sin(6.0 * lng * pi) + 20.0 * Math.sin(2.0 * lng * pi)) * 2.0 / 3.0;
144+
ret += (20.0 * Math.sin(lng * pi) + 40.0 * Math.sin(lng / 3.0 * pi)) * 2.0 / 3.0;
145+
ret += (150.0 * Math.sin(lng / 12.0 * pi) + 300.0 * Math.sin(lng / 30.0 * pi)) * 2.0 / 3.0;
146+
return ret;
147+
}
148+
149+
/**
150+
* 判断是否在国内,不在国内不做偏移
151+
* @param lng
152+
* @param lat
153+
* @return
154+
*/
155+
public static boolean out_of_china(double lng, double lat) {
156+
if (lng < 72.004 || lng > 137.8347) {
157+
return true;
158+
} else if (lat < 0.8293 || lat > 55.8271) {
159+
return true;
160+
}
161+
return false;
162+
}
163+
}
Lines changed: 226 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,226 @@
1+
package com.jingewenku.abrahamcaijin.commonutil;
2+
3+
import android.content.Context;
4+
import android.content.pm.PackageInfo;
5+
import android.content.pm.PackageManager;
6+
import android.os.Build;
7+
import android.os.Environment;
8+
import android.os.Looper;
9+
import android.util.Log;
10+
import android.widget.Toast;
11+
12+
import java.io.*;
13+
import java.lang.reflect.Field;
14+
import java.text.DateFormat;
15+
import java.text.SimpleDateFormat;
16+
import java.util.Date;
17+
import java.util.HashMap;
18+
import java.util.Locale;
19+
import java.util.Map;
20+
21+
22+
/**
23+
* @Description:主要功能:UncaughtException处理类,当程序发生Uncaught异常的时候,有该类来接管程序,并记录发送错误报告.
24+
* @Prject: CommonUtilLibrary
25+
* @Package: com.jingewenku.abrahamcaijin.commonutil
26+
* @author: AbrahamCaiJin
27+
* @date: 2017年06月01日 10:49
28+
* @Copyright: 个人版权所有
29+
* @Company:
30+
* @version: 1.0.0
31+
*/
32+
33+
/*
34+
* public class AndroidUtilsApplication extends Application {
35+
* public void onCreate() {
36+
* super.onCreate();
37+
* //崩溃处理
38+
* CrashHandlerUtil crashHandlerUtil = CrashHandlerUtil.getInstance();
39+
* crashHandlerUtil.init(this);
40+
* crashHandlerUtil.setCrashTip("很抱歉,程序出现异常,即将退出!");
41+
* }
42+
* }
43+
*/
44+
45+
public class CrashHandlerUtil implements Thread.UncaughtExceptionHandler {
46+
47+
public static final String TAG = "CrashHandlerUtil";
48+
49+
//系统默认的UncaughtException处理类
50+
private Thread.UncaughtExceptionHandler mDefaultHandler;
51+
//CrashHandler实例
52+
private static CrashHandlerUtil INSTANCE = new CrashHandlerUtil();
53+
//程序的Context对象
54+
private Context mContext;
55+
//用来存储设备信息和异常信息
56+
private Map<String, String> infos = new HashMap<>();
57+
58+
//用于格式化日期,作为日志文件名的一部分
59+
private DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss", Locale.CHINA);
60+
private String crashTip = "很抱歉,程序出现异常,即将退出!";
61+
62+
public String getCrashTip() {
63+
return crashTip;
64+
}
65+
66+
public void setCrashTip(String crashTip) {
67+
this.crashTip = crashTip;
68+
}
69+
70+
/**
71+
* 保证只有一个CrashHandler实例
72+
*/
73+
private CrashHandlerUtil() {
74+
}
75+
76+
/**
77+
* 获取CrashHandler实例 ,单例模式
78+
*
79+
* @return 单例
80+
*/
81+
public static CrashHandlerUtil getInstance() {
82+
return INSTANCE;
83+
}
84+
85+
/**
86+
* 初始化
87+
*
88+
* @param context 上下文
89+
*/
90+
public void init(Context context) {
91+
mContext = context;
92+
//获取系统默认的UncaughtException处理器
93+
mDefaultHandler = Thread.getDefaultUncaughtExceptionHandler();
94+
//设置该CrashHandler为程序的默认处理器
95+
Thread.setDefaultUncaughtExceptionHandler(this);
96+
}
97+
98+
/**
99+
* 当UncaughtException发生时会转入该函数来处理
100+
*
101+
* @param thread 线程
102+
* @param ex 异常
103+
*/
104+
@Override
105+
public void uncaughtException(Thread thread, Throwable ex) {
106+
if (!handleException(ex) && mDefaultHandler != null) {
107+
//如果用户没有处理则让系统默认的异常处理器来处理
108+
mDefaultHandler.uncaughtException(thread, ex);
109+
} else {
110+
try {
111+
Thread.sleep(3000);
112+
} catch (InterruptedException e) {
113+
Log.e(TAG, "error : ", e);
114+
e.printStackTrace();
115+
}
116+
//退出程序
117+
AppManager.getAppManager().exitApp();
118+
}
119+
}
120+
121+
/**
122+
* 自定义错误处理,收集错误信息 发送错误报告等操作均在此完成.
123+
*
124+
* @param throwable 异常
125+
* @return true:如果处理了该异常信息;否则返回false.
126+
*/
127+
private boolean handleException(final Throwable throwable) {
128+
if (throwable == null) {
129+
return false;
130+
}
131+
//使用Toast来显示异常信息
132+
new Thread() {
133+
@Override
134+
public void run() {
135+
Looper.prepare();
136+
throwable.printStackTrace();
137+
Toast.makeText(mContext, getCrashTip(), Toast.LENGTH_LONG).show();
138+
Looper.loop();
139+
}
140+
}.start();
141+
//收集设备参数信息
142+
collectDeviceInfo(mContext);
143+
//保存日志文件
144+
saveCrashInfo2File(throwable);
145+
return true;
146+
}
147+
148+
/**
149+
* 收集设备参数信息
150+
*
151+
* @param ctx 上下文
152+
*/
153+
public void collectDeviceInfo(Context ctx) {
154+
try {
155+
PackageManager pm = ctx.getPackageManager();
156+
PackageInfo pi = pm.getPackageInfo(ctx.getPackageName(), PackageManager.GET_ACTIVITIES);
157+
if (pi != null) {
158+
String versionName = pi.versionName == null ? "null" : pi.versionName;
159+
String versionCode = pi.versionCode + "";
160+
infos.put("versionName", versionName);
161+
infos.put("versionCode", versionCode);
162+
}
163+
} catch (PackageManager.NameNotFoundException e) {
164+
Log.e(TAG, "an error occured when collect package info", e);
165+
}
166+
Field[] fields = Build.class.getDeclaredFields();
167+
for (Field field : fields) {
168+
try {
169+
field.setAccessible(true);
170+
infos.put(field.getName(), field.get(null).toString());
171+
Log.d(TAG, field.getName() + " : " + field.get(null));
172+
} catch (Exception e) {
173+
Log.e(TAG, "an error occured when collect crash info", e);
174+
}
175+
}
176+
}
177+
178+
/**
179+
* 保存错误信息到文件中
180+
*
181+
* @param ex 异常
182+
* @return 返回文件名称, 便于将文件传送到服务器
183+
*/
184+
private String saveCrashInfo2File(Throwable ex) {
185+
186+
StringBuffer sb = new StringBuffer();
187+
for (Map.Entry<String, String> entry : infos.entrySet()) {
188+
String key = entry.getKey();
189+
String value = entry.getValue();
190+
sb.append(key + "=" + value + "\n");
191+
}
192+
193+
Writer writer = new StringWriter();
194+
PrintWriter printWriter = new PrintWriter(writer);
195+
ex.printStackTrace(printWriter);
196+
Throwable cause = ex.getCause();
197+
while (cause != null) {
198+
cause.printStackTrace(printWriter);
199+
cause = cause.getCause();
200+
}
201+
printWriter.close();
202+
String result = writer.toString();
203+
sb.append(result);
204+
try {
205+
long timestamp = System.currentTimeMillis();
206+
String time = formatter.format(new Date());
207+
String fileName = "crash-" + time + "-" + timestamp + ".log";
208+
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
209+
String path = Environment.getExternalStorageDirectory().getPath() + "/crash/";
210+
AppLogMessageMgr.d("path=" + path);
211+
File dir = new File(path);
212+
if (!dir.exists()) {
213+
dir.mkdirs();
214+
}
215+
FileOutputStream fos = new FileOutputStream(path + fileName);
216+
fos.write(sb.toString().getBytes());
217+
fos.close();
218+
}
219+
return fileName;
220+
} catch (Exception e) {
221+
Log.e(TAG, "an error occured while writing file...", e);
222+
}
223+
return null;
224+
}
225+
226+
}

0 commit comments

Comments
 (0)