Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 85d4f2cc authored by Guillaume Jacquart's avatar Guillaume Jacquart
Browse files

Update PR

parent 954ec718
Loading
Loading
Loading
Loading
Loading
+26 −35
Original line number Diff line number Diff line
@@ -86,6 +86,11 @@ public class StatsDatabase extends SQLiteOpenHelper {
                    AppTrackerEntry.COLUMN_NAME_NUMBER_CONTACTED + " INTEGER," +
                    AppTrackerEntry.COLUMN_NAME_NUMBER_BLOCKED + " INTEGER)";

    private static final String PROJECTION_NAME_PERIOD = "period";
    private static final String PROJECTION_NAME_CONTACTED_SUM = "contactedsum";
    private static final String PROJECTION_NAME_BLOCKED_SUM = "blockedsum";
    private static final String PROJECTION_NAME_LEAKED_SUM = "leakedsum";
    private static final String PROJECTION_NAME_TRACKERS_COUNT = "trackerscount";

    private HashMap<String, Pair<Integer, Integer>> getCallsByPeriod(
        int periodsCount,
@@ -95,32 +100,28 @@ public class StatsDatabase extends SQLiteOpenHelper {
        synchronized (lock) {
            long minTimestamp = getPeriodStartTs(periodsCount, periodUnit);

            String projectionNamePeriod = "period";
            String projectionNameContactedSum = "contactedsum";
            String projectionNameBlockedSum = "blockedsum";

            SQLiteDatabase db = getReadableDatabase();

            String selection = AppTrackerEntry.COLUMN_NAME_TIMESTAMP + " >= ?";
            String[] selectionArg = new String[]{"" + minTimestamp};
            String projection =
                    AppTrackerEntry.COLUMN_NAME_TIMESTAMP + ", " +
                            "STRFTIME('" + sqlitePeriodFormat + "', DATETIME(" + AppTrackerEntry.COLUMN_NAME_TIMESTAMP + ", 'unixepoch', 'localtime')) " + projectionNamePeriod + "," +
                            "SUM(" + AppTrackerEntry.COLUMN_NAME_NUMBER_CONTACTED + ") " + projectionNameContactedSum + "," +
                            "SUM(" + AppTrackerEntry.COLUMN_NAME_NUMBER_BLOCKED + ") " + projectionNameBlockedSum;
                            "STRFTIME('" + sqlitePeriodFormat + "', DATETIME(" + AppTrackerEntry.COLUMN_NAME_TIMESTAMP + ", 'unixepoch', 'localtime')) " + PROJECTION_NAME_PERIOD + "," +
                            "SUM(" + AppTrackerEntry.COLUMN_NAME_NUMBER_CONTACTED + ") " + PROJECTION_NAME_CONTACTED_SUM + "," +
                            "SUM(" + AppTrackerEntry.COLUMN_NAME_NUMBER_BLOCKED + ") " + PROJECTION_NAME_BLOCKED_SUM;
            Cursor cursor = db.rawQuery("SELECT " + projection + " FROM " + AppTrackerEntry.TABLE_NAME
                    + " WHERE " + selection +
                    " GROUP BY " + projectionNamePeriod +
                    " GROUP BY " + PROJECTION_NAME_PERIOD +
                    " ORDER BY " + AppTrackerEntry.COLUMN_NAME_TIMESTAMP + " DESC" +
                    " LIMIT " + periodsCount, selectionArg);

            HashMap<String, Pair<Integer, Integer>> callsByPeriod = new HashMap<>();
            while (cursor.moveToNext()) {
                int contacted = cursor.getInt(cursor.getColumnIndex(projectionNameContactedSum));
                int blocked = cursor.getInt(cursor.getColumnIndex(projectionNameBlockedSum));
                int contacted = cursor.getInt(cursor.getColumnIndex(PROJECTION_NAME_CONTACTED_SUM));
                int blocked = cursor.getInt(cursor.getColumnIndex(PROJECTION_NAME_BLOCKED_SUM));

                callsByPeriod.put(
                        cursor.getString(cursor.getColumnIndex(projectionNamePeriod)),
                        cursor.getString(cursor.getColumnIndex(PROJECTION_NAME_PERIOD)),
                        new Pair(blocked, contacted - blocked)
                );
            }
@@ -176,7 +177,6 @@ public class StatsDatabase extends SQLiteOpenHelper {
        synchronized (lock) {
            long minTimestamp = getPeriodStartTs(periodsCount, periodUnit);

            String projectionNameTrackersCount = "trackerscount";

            SQLiteDatabase db = getWritableDatabase();

@@ -184,7 +184,7 @@ public class StatsDatabase extends SQLiteOpenHelper {
                    AppTrackerEntry.COLUMN_NAME_NUMBER_CONTACTED + " > " + AppTrackerEntry.COLUMN_NAME_NUMBER_BLOCKED;
            String[] selectionArg = new String[]{"" + minTimestamp};
            String projection =
                    "COUNT(DISTINCT " + AppTrackerEntry.COLUMN_NAME_TRACKER + ") " + projectionNameTrackersCount;
                    "COUNT(DISTINCT " + AppTrackerEntry.COLUMN_NAME_TRACKER + ") " + PROJECTION_NAME_TRACKERS_COUNT;
            Cursor cursor = db.rawQuery("SELECT " + projection + " FROM " + AppTrackerEntry.TABLE_NAME
                    + " WHERE " + selection, selectionArg);

@@ -204,11 +204,9 @@ public class StatsDatabase extends SQLiteOpenHelper {

    public int getContactedTrackersCount() {
        synchronized (lock) {
            String projectionNameTrackersCount = "trackerscount";

            SQLiteDatabase db = getReadableDatabase();
            String projection =
                    "COUNT(DISTINCT " + AppTrackerEntry.COLUMN_NAME_TRACKER + ") " + projectionNameTrackersCount;
                    "COUNT(DISTINCT " + AppTrackerEntry.COLUMN_NAME_TRACKER + ") " + PROJECTION_NAME_TRACKERS_COUNT;

            Cursor cursor = db.rawQuery(
                    "SELECT " + projection + " FROM " + AppTrackerEntry.TABLE_NAME,
@@ -230,12 +228,10 @@ public class StatsDatabase extends SQLiteOpenHelper {

    public Map<Integer, Integer> getContactedTrackersCountByApp() {
        synchronized (lock) {
            String projectionNameTrackersCount = "trackerscount";

            SQLiteDatabase db = getReadableDatabase();
            String projection =
                    AppTrackerEntry.COLUMN_NAME_APP_UID + ", " +
                    "COUNT(DISTINCT " + AppTrackerEntry.COLUMN_NAME_TRACKER + ") " + projectionNameTrackersCount;
                    "COUNT(DISTINCT " + AppTrackerEntry.COLUMN_NAME_TRACKER + ") " + PROJECTION_NAME_TRACKERS_COUNT;

            Cursor cursor = db.rawQuery(
                "SELECT " + projection + " FROM " + AppTrackerEntry.TABLE_NAME +
@@ -247,7 +243,7 @@ public class StatsDatabase extends SQLiteOpenHelper {
            while (cursor.moveToNext()) {
                countByApp.put(
                        cursor.getInt(cursor.getColumnIndex(AppTrackerEntry.COLUMN_NAME_APP_UID)),
                        cursor.getInt(cursor.getColumnIndex(projectionNameTrackersCount))
                        cursor.getInt(cursor.getColumnIndex(PROJECTION_NAME_TRACKERS_COUNT))
                );
            }

@@ -261,8 +257,6 @@ public class StatsDatabase extends SQLiteOpenHelper {
    public Map<Integer, Pair<Integer, Integer>> getCallsByApps(int periodCount, TemporalUnit periodUnit) {
        synchronized (lock) {
            long minTimestamp = getPeriodStartTs(periodCount, periodUnit);
            String projectionNameContactedSum = "contactedsum";
            String projectionNameBlockedSum = "blockedsum";

            SQLiteDatabase db = getReadableDatabase();

@@ -270,8 +264,8 @@ public class StatsDatabase extends SQLiteOpenHelper {
            String[] selectionArg = new String[]{"" + minTimestamp};
            String projection =
                    AppTrackerEntry.COLUMN_NAME_APP_UID + ", " +
                    "SUM(" + AppTrackerEntry.COLUMN_NAME_NUMBER_CONTACTED + ") " + projectionNameContactedSum + "," +
                            "SUM(" + AppTrackerEntry.COLUMN_NAME_NUMBER_BLOCKED + ") " + projectionNameBlockedSum;
                    "SUM(" + AppTrackerEntry.COLUMN_NAME_NUMBER_CONTACTED + ") " + PROJECTION_NAME_CONTACTED_SUM + "," +
                            "SUM(" + AppTrackerEntry.COLUMN_NAME_NUMBER_BLOCKED + ") " + PROJECTION_NAME_BLOCKED_SUM;

            Cursor cursor = db.rawQuery(
            "SELECT " + projection + " FROM " + AppTrackerEntry.TABLE_NAME +
@@ -283,8 +277,8 @@ public class StatsDatabase extends SQLiteOpenHelper {
            HashMap<Integer, Pair<Integer, Integer>> callsByApp = new HashMap<>();

            while (cursor.moveToNext()) {
                int contacted = cursor.getInt(cursor.getColumnIndex(projectionNameContactedSum));
                int blocked = cursor.getInt(cursor.getColumnIndex(projectionNameBlockedSum));
                int contacted = cursor.getInt(cursor.getColumnIndex(PROJECTION_NAME_CONTACTED_SUM));
                int blocked = cursor.getInt(cursor.getColumnIndex(PROJECTION_NAME_BLOCKED_SUM));

                callsByApp.put(
                    cursor.getInt(cursor.getColumnIndex(AppTrackerEntry.COLUMN_NAME_APP_UID)),
@@ -302,8 +296,6 @@ public class StatsDatabase extends SQLiteOpenHelper {
    public Pair<Integer, Integer> getCalls(int appUid, int periodCount, TemporalUnit periodUnit) {
        synchronized (lock) {
            long minTimestamp = getPeriodStartTs(periodCount, periodUnit);
            String projectionNameContactedSum = "contactedsum";
            String projectionNameBlockedSum = "blockedsum";

            SQLiteDatabase db = getReadableDatabase();

@@ -312,8 +304,8 @@ public class StatsDatabase extends SQLiteOpenHelper {
                    AppTrackerEntry.COLUMN_NAME_TIMESTAMP + " >= ?";
            String[] selectionArg = new String[]{ "" + appUid, "" + minTimestamp };
            String projection =
                    "SUM(" + AppTrackerEntry.COLUMN_NAME_NUMBER_CONTACTED + ") " + projectionNameContactedSum + "," +
                    "SUM(" + AppTrackerEntry.COLUMN_NAME_NUMBER_BLOCKED + ") " + projectionNameBlockedSum;
                    "SUM(" + AppTrackerEntry.COLUMN_NAME_NUMBER_CONTACTED + ") " + PROJECTION_NAME_CONTACTED_SUM + "," +
                    "SUM(" + AppTrackerEntry.COLUMN_NAME_NUMBER_BLOCKED + ") " + PROJECTION_NAME_BLOCKED_SUM;

            Cursor cursor = db.rawQuery(
                "SELECT " + projection + " FROM " + AppTrackerEntry.TABLE_NAME +
@@ -325,8 +317,8 @@ public class StatsDatabase extends SQLiteOpenHelper {
            Pair<Integer, Integer> calls = new Pair(0, 0);

            if (cursor.moveToNext()) {
                int contacted = cursor.getInt(cursor.getColumnIndex(projectionNameContactedSum));
                int blocked = cursor.getInt(cursor.getColumnIndex(projectionNameBlockedSum));
                int contacted = cursor.getInt(cursor.getColumnIndex(PROJECTION_NAME_CONTACTED_SUM));
                int blocked = cursor.getInt(cursor.getColumnIndex(PROJECTION_NAME_BLOCKED_SUM));

                calls = new Pair(blocked, contacted - blocked);
            }
@@ -341,7 +333,6 @@ public class StatsDatabase extends SQLiteOpenHelper {
    public int getMostLeakedApp(int periodCount, TemporalUnit periodUnit) {
        synchronized (lock) {
            long minTimestamp = getPeriodStartTs(periodCount, periodUnit);
            String projectionNameLeakedSum = "leakedsum";

            SQLiteDatabase db = getReadableDatabase();

@@ -351,13 +342,13 @@ public class StatsDatabase extends SQLiteOpenHelper {
                    AppTrackerEntry.COLUMN_NAME_APP_UID + ", " +
                            "SUM(" + AppTrackerEntry.COLUMN_NAME_NUMBER_CONTACTED +
                                " - " + AppTrackerEntry.COLUMN_NAME_NUMBER_BLOCKED +
                            ") " + projectionNameLeakedSum;
                            ") " + PROJECTION_NAME_LEAKED_SUM;

            Cursor cursor = db.rawQuery(
            "SELECT " + projection + " FROM " + AppTrackerEntry.TABLE_NAME +
                " WHERE " + selection +
                " GROUP BY " + AppTrackerEntry.COLUMN_NAME_APP_UID +
                " ORDER BY " + projectionNameLeakedSum + " DESC LIMIT 1",
                " ORDER BY " + PROJECTION_NAME_LEAKED_SUM + " DESC LIMIT 1",
                selectionArg);