Skip to content

Instantly share code, notes, and snippets.

ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
List<ApplicationExitInfo> applicationExitInfos = am.getHistoricalProcessExitReasons(null, 0, 0);
long lastExitTimestamp = getPreferences(0).getLong(PREV_DETECT_TIME_KEY, 0);
List<ApplicationExitInfo> unprocessedExitInfos = new ArrayList<>();
for (ApplicationExitInfo exitInfo: applicationExitInfos) {
if (exitInfo.getTimestamp() > lastExitTimestamp) {
unprocessedExitInfos.add(exitInfo);
} else {
break;
}
}
for (ApplicationExitInfo exitInfo: unprocessedExitInfos) {
Log.e(TAG, String.format("Exit reason: %d, description: %s", exitInfo.getReason(), exitInfo.getDescription()));
}
for (ApplicationExitInfo exitInfo: unprocessedExitInfos) {
if (exitInfo.getReason() == ApplicationExitInfo.REASON_ANR) {
UUID uuid = UUID.randomUUID();
String fileName = "anr_info_" + uuid.toString() + ".trace";
File outFile = new File(getFilesDir().getAbsolutePath() + "/" + fileName);
try (InputStream inputStream = exitInfo.getTraceInputStream()) {
copyStreamToFile(inputStream, outFile);
} catch (IOException e) {
Log.e(TAG, "copyStreamToFile: ", e);
@yangweigbh
yangweigbh / reverseindex.main.kts
Created August 17, 2020 15:19
simple reverse index application that parse wikipedia abstract https://dumps.wikimedia.org/enwiki/latest/enwiki-latest-abstract1.xml.gz
@file:DependsOn("com.github.ajalt:clikt:2.8.0")
@file:DependsOn("org.apache.opennlp:opennlp-tools:1.9.3")
import com.github.ajalt.clikt.core.CliktCommand
import com.github.ajalt.clikt.parameters.options.option
import com.github.ajalt.clikt.parameters.options.prompt
import opennlp.tools.stemmer.PorterStemmer
import org.xml.sax.Attributes
import org.xml.sax.helpers.DefaultHandler
import java.io.File