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

Commit 954ec718 authored by Guillaume Jacquart's avatar Guillaume Jacquart
Browse files

219 add stats on leaks by apps

parent 9f5a214d
Loading
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -10,7 +10,7 @@ android {
        targetSdkVersion 30

        versionCode 1
        versionName "0.6.1"
        versionName "0.7.0"
    }

    buildTypes {
@@ -26,7 +26,7 @@ android {
    }
}
dependencies{
    implementation 'foundation.e:privacymodule.api:1.0.0'
    implementation 'foundation.e:privacymodule.api:1.1.0'
}

publishing {
+17 −0
Original line number Diff line number Diff line
@@ -115,6 +115,23 @@ public class TrackTrackersPrivacyModule implements ITrackTrackersPrivacyModule {
        return statsRepository.getActiveTrackersByPeriod(12, ChronoUnit.MONTHS);
    }

    @Override
    public int getPastDayMostLeakedApp() {
        return statsRepository.getMostLeakedApp(24, ChronoUnit.HOURS);
    }

    @NonNull
    @Override
    public Map<Integer, Pair<Integer, Integer>> getPastDayTrackersCallsByApps() {
        return statsRepository.getCallsByApps(24, ChronoUnit.HOURS);
    }

    @NonNull
    @Override
    public Pair<Integer, Integer> getPastDayTrackersCallsForApp(int appUid) {
        return statsRepository.getCalls(appUid, 24, ChronoUnit.HOURS);
    }

    @Override
    public void addListener(ITrackTrackersPrivacyModule.Listener listener) {
        mListeners.add(listener);
+114 −0
Original line number Diff line number Diff line
@@ -258,6 +258,120 @@ 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();

            String selection = AppTrackerEntry.COLUMN_NAME_TIMESTAMP + " >= ?";
            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;

            Cursor cursor = db.rawQuery(
            "SELECT " + projection + " FROM " + AppTrackerEntry.TABLE_NAME +
                " WHERE " + selection +
                " GROUP BY " + AppTrackerEntry.COLUMN_NAME_APP_UID,
                selectionArg);


            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));

                callsByApp.put(
                    cursor.getInt(cursor.getColumnIndex(AppTrackerEntry.COLUMN_NAME_APP_UID)),
                    new Pair(blocked, contacted - blocked)
                );
            }

            cursor.close();
            db.close();

            return callsByApp;
        }
    }

    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();

            String selection =
                    AppTrackerEntry.COLUMN_NAME_APP_UID + " = ? AND " +
                    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;

            Cursor cursor = db.rawQuery(
                "SELECT " + projection + " FROM " + AppTrackerEntry.TABLE_NAME +
                    " WHERE " + selection,
                    selectionArg);

            HashMap<Integer, Pair<Integer, Integer>> callsByApp = new HashMap<>();

            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));

                calls = new Pair(blocked, contacted - blocked);
            }

            cursor.close();
            db.close();

            return calls;
        }
    }

    public int getMostLeakedApp(int periodCount, TemporalUnit periodUnit) {
        synchronized (lock) {
            long minTimestamp = getPeriodStartTs(periodCount, periodUnit);
            String projectionNameLeakedSum = "leakedsum";

            SQLiteDatabase db = getReadableDatabase();

            String selection = AppTrackerEntry.COLUMN_NAME_TIMESTAMP + " >= ?";
            String[] selectionArg = new String[]{"" + minTimestamp};
            String projection =
                    AppTrackerEntry.COLUMN_NAME_APP_UID + ", " +
                            "SUM(" + AppTrackerEntry.COLUMN_NAME_NUMBER_CONTACTED +
                                " - " + AppTrackerEntry.COLUMN_NAME_NUMBER_BLOCKED +
                            ") " + projectionNameLeakedSum;

            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",
                selectionArg);


            int appUid = 0;
            if (cursor.moveToNext()) {
                appUid = cursor.getInt(cursor.getColumnIndex(AppTrackerEntry.COLUMN_NAME_APP_UID));
            }

            cursor.close();
            db.close();

            return appUid;
        }
    }

    private long getCurrentHourTs() {
        long hourInMs = TimeUnit.HOURS.toMillis(1L);
+15 −0
Original line number Diff line number Diff line
@@ -2,9 +2,11 @@ package foundation.e.privacymodules.trackers.data;

import android.content.Context;

import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalUnit;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;

import foundation.e.privacymodules.trackers.Tracker;
@@ -58,4 +60,17 @@ public class StatsRepository {
    public List<Tracker> getAllTrackersOfApp(int app_uid) {
        return database.getAllTrackersOfApp(app_uid);
    }

    public Map<Integer, Pair<Integer, Integer>> getCallsByApps(int periodCount, TemporalUnit periodUnit) {
        return database.getCallsByApps(periodCount, periodUnit);
    }

    public Pair<Integer, Integer> getCalls(int appUid, int periodCount, TemporalUnit periodUnit) {
        return database.getCalls(appUid, periodCount, periodUnit);
    }


        public int getMostLeakedApp(int periodCount, TemporalUnit periodUnit) {
        return database.getMostLeakedApp(periodCount, periodUnit);
    }
}
 No newline at end of file