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

Commit af950c5f authored by Guillaume Jacquart's avatar Guillaume Jacquart
Browse files

5217 add blocked trackers to leaks count stats api

parent 938e9b23
Loading
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -33,7 +33,7 @@ android {

dependencies {
    implementation project(':privacymoduletrackers')
    implementation 'foundation.e:privacymodule.api:0.5.0-dev'
    implementation 'foundation.e:privacymodule.api:1.0.0'

    implementation 'com.google.code.gson:gson:2.9.0'

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

        versionCode 1
        versionName "0.5.1"
        versionName "0.6.0"
    }

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

publishing {
+7 −6
Original line number Diff line number Diff line
@@ -35,6 +35,7 @@ import foundation.e.privacymodules.trackers.ITrackTrackersPrivacyModule;
import foundation.e.privacymodules.trackers.Tracker;
import foundation.e.privacymodules.trackers.data.StatsRepository;
import foundation.e.privacymodules.trackers.data.TrackersRepository;
import kotlin.Pair;

public class TrackTrackersPrivacyModule implements ITrackTrackersPrivacyModule {

@@ -69,18 +70,18 @@ public class TrackTrackersPrivacyModule implements ITrackTrackersPrivacyModule {

    @NonNull
    @Override
    public List<Integer> getPastDayTrackersCalls() {
        return statsRepository.getTrackersLeaksOnPeriod(24, ChronoUnit.HOURS);
    public List<Pair<Integer, Integer>> getPastDayTrackersCalls() {
        return statsRepository.getTrackersCallsOnPeriod(24, ChronoUnit.HOURS);
    }

    @Override
    public List<Integer> getPastMonthTrackersCalls() {
        return statsRepository.getTrackersLeaksOnPeriod(30, ChronoUnit.DAYS);
    public List<Pair<Integer, Integer>> getPastMonthTrackersCalls() {
        return statsRepository.getTrackersCallsOnPeriod(30, ChronoUnit.DAYS);
    }

    @Override
    public List<Integer> getPastYearTrackersCalls() {
        return statsRepository.getTrackersLeaksOnPeriod(12, ChronoUnit.MONTHS);
    public List<Pair<Integer, Integer>> getPastYearTrackersCalls() {
        return statsRepository.getTrackersCallsOnPeriod(12, ChronoUnit.MONTHS);
    }

    @Override
+14 −15
Original line number Diff line number Diff line
@@ -26,7 +26,6 @@ import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.provider.BaseColumns;
import android.util.Log;

import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
@@ -39,7 +38,7 @@ import java.util.Map;
import java.util.concurrent.TimeUnit;

import foundation.e.privacymodules.trackers.Tracker;
import foundation.e.privacymodules.trackers.data.TrackersRepository;
import kotlin.Pair;

public class StatsDatabase extends SQLiteOpenHelper {
    public static final int DATABASE_VERSION = 1;
@@ -88,7 +87,7 @@ public class StatsDatabase extends SQLiteOpenHelper {
                    AppTrackerEntry.COLUMN_NAME_NUMBER_BLOCKED + " INTEGER)";


    private HashMap<String, Integer> getLeaksByPeriod(
    private HashMap<String, Pair<Integer, Integer>> getCallsByPeriod(
        int periodsCount,
        TemporalUnit periodUnit,
        String sqlitePeriodFormat
@@ -115,26 +114,26 @@ public class StatsDatabase extends SQLiteOpenHelper {
                    " ORDER BY " + AppTrackerEntry.COLUMN_NAME_TIMESTAMP + " DESC" +
                    " LIMIT " + periodsCount, selectionArg);

            HashMap<String, Integer> leaksByPeriod = new HashMap<>();
            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));

                leaksByPeriod.put(
                callsByPeriod.put(
                        cursor.getString(cursor.getColumnIndex(projectionNamePeriod)),
                        contacted - blocked
                        new Pair(blocked, contacted - blocked)
                );
            }

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

            return leaksByPeriod;
            return callsByPeriod;
        }
    }

    private List<Integer> leaksByPeriodToPeriodsList(
        Map<String, Integer> leaksByPeriod,
    private List<Pair<Integer, Integer>> callsByPeriodToPeriodsList(
        Map<String, Pair<Integer, Integer>> callsByPeriod,
        int periodsCount,
        TemporalUnit periodUnit,
        String javaPeriodFormat
@@ -142,17 +141,17 @@ public class StatsDatabase extends SQLiteOpenHelper {
        ZonedDateTime currentDate = ZonedDateTime.now().minus(periodsCount, periodUnit);
        DateTimeFormatter formater = DateTimeFormatter.ofPattern(javaPeriodFormat);

        List<Integer> leaks = new ArrayList(periodsCount);
        List<Pair<Integer, Integer>> calls = new ArrayList(periodsCount);
        for (int i = 0; i < periodsCount; i++) {
            currentDate = currentDate.plus(1, periodUnit);

            String currentPeriod = formater.format(currentDate);
            leaks.add(leaksByPeriod.getOrDefault(currentPeriod, 0));
            calls.add(callsByPeriod.getOrDefault(currentPeriod, new Pair(0, 0)));
        }
        return leaks;
        return calls;
    }

    public List<Integer> getTrackersLeaksOnPeriod(int periodsCount, TemporalUnit periodUnit) {
    public List<Pair<Integer, Integer>> getTrackersCallsOnPeriod(int periodsCount, TemporalUnit periodUnit) {
        String sqlitePeriodFormat = "%Y%m";
        String javaPeriodFormat = "yyyyMM";

@@ -167,8 +166,8 @@ public class StatsDatabase extends SQLiteOpenHelper {
            javaPeriodFormat = "yyyyMMddHH";
        }

        Map<String, Integer> leaksByPeriod = getLeaksByPeriod(periodsCount, periodUnit, sqlitePeriodFormat);
        return leaksByPeriodToPeriodsList(leaksByPeriod, periodsCount, periodUnit, javaPeriodFormat);
        Map<String, Pair<Integer, Integer>> callsByPeriod = getCallsByPeriod(periodsCount, periodUnit, sqlitePeriodFormat);
        return callsByPeriodToPeriodsList(callsByPeriod, periodsCount, periodUnit, javaPeriodFormat);
    }


+3 −2
Original line number Diff line number Diff line
@@ -8,6 +8,7 @@ import java.util.Map;
import java.util.function.Consumer;

import foundation.e.privacymodules.trackers.Tracker;
import kotlin.Pair;

public class StatsRepository {
    private static StatsRepository instance;
@@ -38,8 +39,8 @@ public class StatsRepository {
        }
    }

    public List<Integer> getTrackersLeaksOnPeriod(int periodsCount, TemporalUnit periodUnit) {
        return database.getTrackersLeaksOnPeriod(periodsCount, periodUnit);
    public List<Pair<Integer, Integer>> getTrackersCallsOnPeriod(int periodsCount, TemporalUnit periodUnit) {
        return database.getTrackersCallsOnPeriod(periodsCount, periodUnit);
    }

    public int getActiveTrackersByPeriod(int periodsCount, TemporalUnit periodUnit) {