Commit 9b1dbef3 authored by feihuaduo's avatar feihuaduo

加入本地崩溃日志,本地崩溃日志路径"ygocore/log"

parent 2edadf4b
......@@ -5,6 +5,8 @@ import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatDelegate;
import cn.garymb.ygomobile.utils.CrashHandler;
public class App extends GameApplication {
@Override
......@@ -12,6 +14,9 @@ public class App extends GameApplication {
super.onCreate();
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
AppsSettings.init(this);
//初始化异常工具类
CrashHandler crashHandler = CrashHandler.getInstance();
crashHandler.init(getApplicationContext());
if (AppsSettings.get().isSoundEffect()) {
initSoundEffectPool();
setInitSoundEffectPool(true);
......
......@@ -119,7 +119,8 @@ public class AppsSettings {
mScreenHeight = context.getResources().getDisplayMetrics().heightPixels;
mScreenWidth = context.getResources().getDisplayMetrics().widthPixels;
if (isImmerSiveMode() && context instanceof Activity) {
if (context instanceof Activity) {
DisplayMetrics dm = SystemUtils.getHasVirtualDisplayMetrics((Activity) context);
if (dm != null) {
int height = Math.max(dm.widthPixels, dm.heightPixels);
......@@ -131,29 +132,28 @@ public class AppsSettings {
} catch (IOException e) {
e.printStackTrace();
}
Log.e("YGOMobile","原始长"+mScreenHeight);
Log.e("YGOMobile","原始宽"+mScreenWidth);
Log.e("YGOMobile","界面长"+dm.heightPixels);
Log.e("YGOMobile","界面宽"+dm.widthPixels);
// if (mScreenHeight> mScreenWidth) {
// mScreenHeight = height;
// } else {
// mScreenWidth = height;
// }
if(isImmerSiveMode())
return;
ScreenUtil.findNotchInformation(((Activity) context), new ScreenUtil.FindNotchInformation() {
@Override
public void onNotchInformation(boolean isNotch, int notchHeight, int phoneType) {
int height = Math.max(dm.widthPixels, dm.heightPixels);
try {
FileLogUtil.writeAndTime("是否有刘海: "+isNotch);
FileLogUtil.writeAndTime("刘海高"+notchHeight);
FileLogUtil.writeAndTime("height值: "+height);
} catch (IOException e) {
e.printStackTrace();
}
if (isNotch) {
try {
FileLogUtil.writeAndTime("刘海高"+notchHeight);
} catch (IOException e) {
e.printStackTrace();
}
height-=notchHeight;
}
try {
FileLogUtil.writeAndTime("处理后height值: "+height);
} catch (IOException e) {
e.printStackTrace();
}
if (mScreenHeight> mScreenWidth) {
mScreenHeight = height;
......@@ -395,6 +395,13 @@ public class AppsSettings {
return new File(getResourcePath(), Constants.CORE_IMAGE_PATH).getAbsolutePath();
}
/***
* log文件夹
*/
public String getMobileLogPath() {
return new File(getResourcePath(), Constants.MOBILE_LOG).getAbsolutePath();
}
/***
* 当前数据库文件夹
*/
......
......@@ -65,6 +65,7 @@ public interface Constants {
String CORE_EXPANSIONS = "expansions";
String CORE_SINGLE_PATH = "single";
String CORE_IMAGE_PATH = "pics";
String MOBILE_LOG="log";
String CORE_EXPANSIONS_IMAGE_PATH = "expansions/pics";
String CORE_IMAGE_FIELD_PATH = "field";
String CORE_SCRIPT_PATH = "script";
......
......@@ -529,23 +529,23 @@ public abstract class HomeActivity extends BaseActivity implements NavigationVie
public void AnimationShake() {
Animation shake = AnimationUtils.loadAnimation(this, R.anim.shake);//加载动画资源文件
ImageView iv=findViewById(R.id.cube);
iv.startAnimation(shake); //给组件播放动画效果
iv.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
startActivity(new Intent(HomeActivity.this,FileLogActivity.class));
return true;
}
});
findViewById(R.id.cube).startAnimation(shake); //给组件播放动画效果
}
public void StartMycard() {
$(R.id.btn_mycard).setOnClickListener((v) -> {
ImageView iv_mc = $(R.id.btn_mycard);
iv_mc.setOnClickListener((v) -> {
if (Constants.SHOW_MYCARD) {
startActivity(new Intent(this, MyCardActivity.class));
}
});
iv_mc.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
startActivity(new Intent(HomeActivity.this, FileLogActivity.class));
return true;
}
});
}
public boolean joinQQGroup(String key) {
......
......@@ -274,7 +274,8 @@ public class ResCheckTask extends AsyncTask<Void, Integer, Integer> {
Constants.CORE_DECK_PATH,
Constants.CORE_REPLAY_PATH,
Constants.FONT_DIRECTORY,
Constants.CORE_IMAGE_PATH
Constants.CORE_IMAGE_PATH,
Constants.MOBILE_LOG
};
File dirFile = null;
for (String dir : dirs) {
......
package cn.garymb.ygomobile.utils;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Environment;
import android.os.Looper;
import android.util.Log;
import android.widget.Toast;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.lang.reflect.Field;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import cn.garymb.ygomobile.AppsSettings;
public class CrashHandler implements Thread.UncaughtExceptionHandler {
public static final String TAG = "YGOMobile-Exception";
public static final CrashHandler INSTANCE = new CrashHandler();
private Map<String, String> infos = new HashMap<String, String>();
private DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH.mm.ss");
private Context context;
private Thread.UncaughtExceptionHandler defaultHandler;
private CrashHandler() {
}
public static CrashHandler getInstance() {
return INSTANCE;
}
public void init(Context context) {
this.context = context;
defaultHandler = Thread.getDefaultUncaughtExceptionHandler();
Thread.setDefaultUncaughtExceptionHandler(this);
}
@Override
public void uncaughtException(Thread thread, Throwable ex) {
if (!handleException(ex) && defaultHandler != null) {
defaultHandler.uncaughtException(thread, ex);
} else {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
Log.e(TAG, "error : ", e);
}
System.exit(1);
android.os.Process.killProcess(android.os.Process.myPid());
}
}
private boolean handleException(Throwable ex) {
if (ex == null) {
return false;
}
//使用Toast来显示异常信息
new Thread() {
@Override
public void run() {
Looper.prepare();
Toast.makeText(context, "软件错误退出", Toast.LENGTH_LONG).show();
Looper.loop();
}
}.start();
collectDeviceInfo(context);
saveCrashInfo2File(ex);
return true;
}
public void collectDeviceInfo(Context ctx) {
try {
PackageManager pm = ctx.getPackageManager();
PackageInfo pi = pm.getPackageInfo(ctx.getPackageName(), PackageManager.GET_ACTIVITIES);
if (pi != null) {
String versionName = pi.versionName == null ? "null" : pi.versionName;
String versionCode = pi.versionCode + "";
infos.put("versionName", versionName);
infos.put("versionCode", versionCode);
}
} catch (PackageManager.NameNotFoundException e) {
Log.e(TAG, "an error occured when collect package info", e);
}
Field[] fields = Build.class.getDeclaredFields();
for (Field field : fields) {
try {
field.setAccessible(true);
infos.put(field.getName(), field.get(null).toString());
Log.d(TAG, field.getName() + " : " + field.get(null));
} catch (Exception e) {
Log.e(TAG, "an error occured when collect crash info", e);
}
}
}
private String saveCrashInfo2File(Throwable ex) {
StringBuffer sb = new StringBuffer();
for (Map.Entry<String, String> entry : infos.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
sb.append(key + "=" + value + "\n");
}
Writer writer = new StringWriter();
PrintWriter printWriter = new PrintWriter(writer);
ex.printStackTrace(printWriter);
Throwable cause = ex.getCause();
while (cause != null) {
cause.printStackTrace(printWriter);
cause = cause.getCause();
}
printWriter.close();
String result = writer.toString();
sb.append(result);
Log.e(TAG,sb.toString());
try {
// long timestamp = System.currentTimeMillis();
String time = formatter.format(new Date());
String fileName ="【Demo】" + time+/* timestamp +*/ ".log";
String path = AppsSettings.get().getMobileLogPath() + "/";
FileOutputStream fos = new FileOutputStream(path + fileName);
fos.write(sb.toString().getBytes());
fos.close();
return fileName;
} catch (Exception e) {
Log.e(TAG, "an error occured while writing file...", e);
}
return null;
}
}
......@@ -6,6 +6,9 @@
<include
layout="@layout/content_toolbar"/>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:textStyle="bold"
android:id="@+id/tv_log"
......@@ -15,4 +18,8 @@
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.v4.widget.NestedScrollView>
</LinearLayout>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment