DropBoxUtil

package com.android.demo.lileidemo.utils;

import android.content.Context;
import android.os.DropBoxManager;

import com.android.demo.lileidemo.MyApplication;
import com.android.demo.lileidemo.constant.AppConstants;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* date: 04/08/2020.
* author: lilei.
*/
public class DropBoxUtil {
private static final String TAG = AppConstants.APP_TAG + "DropBoxUtil ";
private static final boolean DEBUG = false;
private static final int MAX_BYTES = 8192 * 100;
private static final String SAVE_LOG_DIR_PATH = AppConstants.TSP_DIR + "/dropBox/";

/**
* Get the first entry after the specified time point, and specify the tag.
*/
public static String getDropBoxPackageName(String tag, long time) {
LogUtil.d(TAG + "getDropBoxPackageName() tag:" + tag + " time:" + time);
String packageName = null;
//Android.permission.read'logs permission needs to be added to Android manifest.
DropBoxManager dropBoxManager = (DropBoxManager) MyApplication.getAppContext()
.getSystemService(Context.DROPBOX_SERVICE);
DropBoxManager.Entry entry;
if ((entry = dropBoxManager.getNextEntry(tag, time)) != null) {
String text = entry.getText(MAX_BYTES);
LogUtil.d(TAG + "getDropBoxPackageName() text:" + text);
Pattern pattern = Pattern.compile("^Process:");
Matcher matcher = pattern.matcher(text);
if (matcher.find()) {
packageName = matcher.replaceAll("").trim();
if (null != entry) {
entry.close();
}
return packageName;
}
}
if (null != entry) {
entry.close();
}
return null;
}

/**
* Get the first entry after the specified time point, and specify the tag.
*
* @param tag dropBox tag.
* @param time dropBpx time.
* @return save full file path.
*/
public static String getDropBoxLogToFile(String tag, long time) {
//Android.permission.read'logs permission needs to be added to Android manifest.
DropBoxManager dropBoxManager = (DropBoxManager) MyApplication.getAppContext()
.getSystemService(Context.DROPBOX_SERVICE);
DropBoxManager.Entry entry;

File dirFile = new File(SAVE_LOG_DIR_PATH);
if (!dirFile.exists()) {
dirFile.mkdirs();
}
String fullLogPath = SAVE_LOG_DIR_PATH + tag + "@" + time + ".txt";
LogUtil.d(TAG + "getDropBoxLogToFile() * tag:" + tag + " time:" + time
+ " fullLogPath:" + fullLogPath);
BufferedWriter bufferedWriter = null;
try {
bufferedWriter = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(fullLogPath), "UTF-8"));
if ((entry = dropBoxManager.getNextEntry(tag, time - 1)) != null) {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(
entry.getInputStream()));
String str = null;
while ((str = bufferedReader.readLine()) != null) {
//Start reading the log, one line at a time.
if (DEBUG) {
LogUtil.d(TAG + "getRetrieveLogToFile() str:" + str);
}
bufferedWriter.write(str + " ");
}

}
if (null != entry) {
entry.close();
}
} catch (IOException e1) {
LogUtil.d(TAG + "getDropBoxLogToFile() error:" + e1);
e1.printStackTrace();
} catch (Exception e) {
LogUtil.d(TAG + "getDropBoxLogToFile() error:" + e);
e.printStackTrace();
} finally {
if (null != bufferedWriter) {
try {
bufferedWriter.close();
bufferedWriter = null;
} catch (IOException e) {
LogUtil.e(TAG + "getDropBoxLogToFile() error:" + e);
}
}
}
return fullLogPath;
}
}
原文地址:https://www.cnblogs.com/adamli/p/13139603.html