parent
8e50864765
commit
e552c6d3fc
11 changed files with 289 additions and 277 deletions
@ -0,0 +1,62 @@ |
||||
package com.bingce.controlnetwork.activity; |
||||
|
||||
import android.annotation.SuppressLint; |
||||
import android.content.Context; |
||||
import android.content.Intent; |
||||
import android.content.pm.ActivityInfo; |
||||
import android.os.Bundle; |
||||
import android.text.TextUtils; |
||||
import android.view.MenuItem; |
||||
import android.view.WindowManager; |
||||
import android.widget.TextView; |
||||
|
||||
import androidx.annotation.Nullable; |
||||
import androidx.appcompat.widget.Toolbar; |
||||
|
||||
import com.bingce.controlnetwork.R; |
||||
import com.bingce.controlnetwork.util.CrashCollectUtils; |
||||
import com.bingce.utils.ThreadPoolUtil; |
||||
|
||||
import org.polaric.colorful.ColorfulActivity; |
||||
|
||||
public class ViewRecentCrashActivity extends ColorfulActivity { |
||||
|
||||
@SuppressLint("SourceLockedOrientationActivity") |
||||
@Override |
||||
protected void onCreate(@Nullable Bundle savedInstanceState) { |
||||
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); |
||||
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); |
||||
|
||||
super.onCreate(savedInstanceState); |
||||
setContentView(R.layout.activity_view_recent_crash_log); |
||||
Toolbar mToolbar = findViewById(R.id.toolbar); |
||||
setSupportActionBar(mToolbar); |
||||
if (getSupportActionBar() != null) |
||||
getSupportActionBar().setDisplayHomeAsUpEnabled(true); |
||||
TextView textView = findViewById(R.id.tv_content); |
||||
if (textView != null) { |
||||
ThreadPoolUtil.execute(() -> { |
||||
CrashCollectUtils.collectCrashLogInWorkerThread((cloudTable, appLabelKey, fileNameKey, contentKey, uuidKey, crashContent) -> { |
||||
if (!TextUtils.isEmpty(crashContent)) { |
||||
runOnUiThread(() -> textView.setText(crashContent)); |
||||
} |
||||
}); |
||||
}); |
||||
} |
||||
} |
||||
|
||||
@Override |
||||
public boolean onOptionsItemSelected(MenuItem item) { |
||||
switch (item.getItemId()) { |
||||
case android.R.id.home: |
||||
finish(); |
||||
break; |
||||
} |
||||
return super.onOptionsItemSelected(item); |
||||
} |
||||
|
||||
public static void start(Context context) { |
||||
Intent intent = new Intent(context, ViewRecentCrashActivity.class); |
||||
context.startActivity(intent); |
||||
} |
||||
} |
@ -0,0 +1,101 @@ |
||||
package com.bingce.controlnetwork.util; |
||||
|
||||
import android.content.Context; |
||||
|
||||
import com.bingce.utils.FileUtil; |
||||
|
||||
import java.io.File; |
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
|
||||
import blankj.utilcode.util.FileUtils; |
||||
import blankj.utilcode.util.Utils; |
||||
|
||||
public class CrashCollectUtils { |
||||
public static File crashLogFolder(Context context) { |
||||
File cacheFolder = context.getExternalCacheDir(); |
||||
if (!cacheFolder.exists() || cacheFolder.isFile()) { |
||||
cacheFolder.mkdir(); |
||||
} |
||||
File dir = new File(cacheFolder, "crash"); |
||||
if (!dir.exists() || dir.isFile()) { |
||||
dir.mkdir(); |
||||
} |
||||
return dir; |
||||
} |
||||
|
||||
//收集崩溃日志,在处理完成后根据结果删除
|
||||
public static void collectCrashLogInWorkerThread(ICrashLogCollectListener listener) { |
||||
File dir = crashLogFolder(Utils.getApp().getApplicationContext()); |
||||
//读取其下面的数据
|
||||
if (!dir.exists() || dir.isFile()) { |
||||
return; |
||||
} |
||||
|
||||
StringBuilder sb = new StringBuilder(); |
||||
|
||||
File[] files = dir.listFiles(); |
||||
|
||||
if (files != null) { |
||||
List<File> needMoveFileList = new ArrayList<>(); |
||||
File lastFile = null; |
||||
for (File file : files) { |
||||
if (file == null || !file.isFile()) { |
||||
continue; |
||||
} |
||||
|
||||
if (lastFile == null) { |
||||
lastFile = file; |
||||
} else { |
||||
if (file.lastModified() > lastFile.lastModified()) { |
||||
lastFile = file; |
||||
} |
||||
} |
||||
|
||||
needMoveFileList.add(file); |
||||
} |
||||
needMoveFileList.remove(lastFile); |
||||
|
||||
for (File file : needMoveFileList) { |
||||
moveToBackedUpFolder(dir, file); |
||||
} |
||||
|
||||
if (lastFile != null) { |
||||
FileUtil.ReadFileResult readFileResult = FileUtil.readStringFrom(lastFile); |
||||
if (readFileResult.success) { |
||||
sb.append(readFileResult.string).append("\n"); |
||||
} |
||||
} |
||||
} |
||||
|
||||
String CRASH_LOG_TABLE = "CloudCrashLog"; |
||||
final String contentKey = "crash_content"; |
||||
final String fileNameKey = "file_name"; |
||||
final String appLabelKey = "app"; |
||||
final String uuidKey = "UUID"; |
||||
listener.onCollected(CRASH_LOG_TABLE, appLabelKey, fileNameKey, contentKey, uuidKey, sb.toString()); |
||||
} |
||||
|
||||
private static void moveToBackedUpFolder(File dir, File targetLogFile) { |
||||
File repeatedFolder = new File(dir, "BACKED_UP"); |
||||
if (!repeatedFolder.exists()) { |
||||
repeatedFolder.mkdirs(); |
||||
} |
||||
FileUtils.move(targetLogFile, new File(repeatedFolder, targetLogFile.getName())); |
||||
} |
||||
|
||||
public interface ICrashLogCollectListener { |
||||
void onCollected(String cloudTable, String appLabelKey, String fileNameKey, String contentKey, String uuidKey, |
||||
String content); |
||||
} |
||||
|
||||
public static class CrashItem { |
||||
public final File file; |
||||
public final String content; |
||||
|
||||
CrashItem(File file, String content) { |
||||
this.file = file; |
||||
this.content = content; |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,21 @@ |
||||
<?xml version="1.0" encoding="utf-8"?> |
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="match_parent" |
||||
android:orientation="vertical"> |
||||
|
||||
<include layout="@layout/toolbar" /> |
||||
|
||||
<ScrollView |
||||
android:layout_width="match_parent" |
||||
android:layout_height="match_parent" |
||||
android:layout_marginStart="16dp" |
||||
android:layout_marginEnd="16dp"> |
||||
|
||||
<TextView |
||||
android:id="@+id/tv_content" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="wrap_content" |
||||
android:textSize="16sp" /> |
||||
</ScrollView> |
||||
</LinearLayout> |
After Width: | Height: | Size: 1.7 KiB |
Loading…
Reference in new issue