aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/de/thedevstack/android/logcat/tasks/ReadLogCatAsyncTask.java
blob: b9fa450b884f503b716cb97fd95be98013f0ca22 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package de.thedevstack.android.logcat.tasks;

import android.os.AsyncTask;
import de.thedevstack.conversationsplus.utils.Logging;
import android.widget.ArrayAdapter;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

import de.thedevstack.conversationsplus.utils.StreamUtil;

/**
 * Created by tzur on 20.11.2015.
 */
public class ReadLogCatAsyncTask extends AsyncTask<Void, Void, String[]> {
    private final ArrayAdapter<String> arrayAdapter;
    private static final String[] LOG_CAT_CMD = { "logcat", "-d", "-v", "time"};
    private static final String[] WHITE_LIST = { "thedevstack.", "E/", "W/" };
    private static final String[] BLACK_LIST = { "D/TextLayoutCache" };

    public ReadLogCatAsyncTask(ArrayAdapter<String> arrayAdapter) {
        this.arrayAdapter = arrayAdapter;
    }
    @Override
    protected String[] doInBackground(Void... params) {
        ArrayList<String> logCatOutput = new ArrayList<>();
        BufferedReader bufferedReader = null;
        try {
            Process process = Runtime.getRuntime().exec(ReadLogCatAsyncTask.LOG_CAT_CMD);
            bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line = "";
            while ((line = bufferedReader.readLine()) != null) {
                logCatOutput.add(line);
            }
            bufferedReader.close();
        } catch (IOException e) {
            Logging.e("ReadLogCat", "error while retrieving information from logcat: " + e.getMessage(), e);
        } finally {
            StreamUtil.close(bufferedReader);
        }
        logCatOutput.trimToSize();
        return logCatOutput.toArray(new String[0]);
    }

    @Override
    protected void onPostExecute(String[] items) {
        this.arrayAdapter.clear();
        if (null != items && items.length > 0) {
            for (String item : items) {
                if (!filter(item)) {
                    this.arrayAdapter.add(item);
                }
            }
        }
    }

    protected boolean filter(String item) {
        for (String whiteListed : ReadLogCatAsyncTask.WHITE_LIST) {
            if (item.contains(whiteListed)) {
                return false;
            }
        }
        return true;
    }
}