From e73ab3e91b125f9f868c9ffe3ed8042d4f338673 Mon Sep 17 00:00:00 2001 From: Guillaume Jacquart Date: Fri, 30 Sep 2022 06:23:47 +0000 Subject: 5907: Hide deprecated trackers from trackers counts. --- .../trackers/api/ITrackTrackersPrivacyModule.kt | 6 +++ .../trackers/api/TrackTrackersPrivacyModule.kt | 6 ++- .../privacymodules/trackers/data/StatsDatabase.kt | 43 ++++++++++------------ .../trackers/data/StatsRepository.kt | 4 +- 4 files changed, 33 insertions(+), 26 deletions(-) (limited to 'trackers/src') diff --git a/trackers/src/main/java/foundation/e/privacymodules/trackers/api/ITrackTrackersPrivacyModule.kt b/trackers/src/main/java/foundation/e/privacymodules/trackers/api/ITrackTrackersPrivacyModule.kt index f7beebd..5f1fa92 100644 --- a/trackers/src/main/java/foundation/e/privacymodules/trackers/api/ITrackTrackersPrivacyModule.kt +++ b/trackers/src/main/java/foundation/e/privacymodules/trackers/api/ITrackTrackersPrivacyModule.kt @@ -29,6 +29,12 @@ interface ITrackTrackersPrivacyModule { */ fun getTrackersForApp(appUid: Int): List + /** + * List all the trackers encountere trackers since "ever", for the given [appUids], + * or all apps if [appUids] is null + */ + fun getTrackers(appUids: List? = null): List + /** * Return the number of encountered trackers since "ever", for the given [appUids], * or all apps if [appUids] is null diff --git a/trackers/src/main/java/foundation/e/privacymodules/trackers/api/TrackTrackersPrivacyModule.kt b/trackers/src/main/java/foundation/e/privacymodules/trackers/api/TrackTrackersPrivacyModule.kt index e0672cc..d8f75aa 100644 --- a/trackers/src/main/java/foundation/e/privacymodules/trackers/api/TrackTrackersPrivacyModule.kt +++ b/trackers/src/main/java/foundation/e/privacymodules/trackers/api/TrackTrackersPrivacyModule.kt @@ -72,7 +72,11 @@ class TrackTrackersPrivacyModule(private val context: Context) : ITrackTrackersP } override fun getTrackersForApp(appUid: Int): List { - return statsRepository.getAllTrackersOfApp(appUid) + return statsRepository.getTrackers(listOf(appUid)) + } + + override fun getTrackers(appUids: List?): List { + return statsRepository.getTrackers(appUids) } override fun getPastDayTrackersCount(): Int { diff --git a/trackers/src/main/java/foundation/e/privacymodules/trackers/data/StatsDatabase.kt b/trackers/src/main/java/foundation/e/privacymodules/trackers/data/StatsDatabase.kt index 86208ad..c93fe90 100644 --- a/trackers/src/main/java/foundation/e/privacymodules/trackers/data/StatsDatabase.kt +++ b/trackers/src/main/java/foundation/e/privacymodules/trackers/data/StatsDatabase.kt @@ -184,10 +184,7 @@ class StatsDatabase(context: Context) : fun getContactedTrackersCount(appUids: List?): Int { synchronized(lock) { val db = readableDatabase - val projection = - "COUNT(DISTINCT $COLUMN_NAME_TRACKER) $PROJECTION_NAME_TRACKERS_COUNT" - - var query = "SELECT $projection FROM $TABLE_NAME" + var query = "SELECT DISTINCT $COLUMN_NAME_TRACKER FROM $TABLE_NAME" appUids?.let { query += " WHERE $COLUMN_NAME_APP_UID IN (${it.joinToString(", ")})" @@ -195,8 +192,10 @@ class StatsDatabase(context: Context) : val cursor = db.rawQuery(query, arrayOf()) var count = 0 - if (cursor.moveToNext()) { - count = cursor.getInt(0) + while (cursor.moveToNext()) { + trackersRepository.getTracker(cursor.getString(COLUMN_NAME_TRACKER))?.let { + count++ + } } cursor.close() db.close() @@ -207,17 +206,17 @@ class StatsDatabase(context: Context) : fun getContactedTrackersCountByApp(): Map { synchronized(lock) { val db = readableDatabase - val projection = "$COLUMN_NAME_APP_UID, " + - "COUNT(DISTINCT $COLUMN_NAME_TRACKER) $PROJECTION_NAME_TRACKERS_COUNT" + val projection = "$COLUMN_NAME_APP_UID, $COLUMN_NAME_TRACKER" val cursor = db.rawQuery( - "SELECT $projection FROM $TABLE_NAME" + - " GROUP BY $COLUMN_NAME_APP_UID", + "SELECT DISTINCT $projection FROM $TABLE_NAME", //+ arrayOf() ) val countByApp = mutableMapOf() while (cursor.moveToNext()) { - countByApp[cursor.getInt(COLUMN_NAME_APP_UID)] = - cursor.getInt(PROJECTION_NAME_TRACKERS_COUNT) + trackersRepository.getTracker(cursor.getString(COLUMN_NAME_TRACKER))?.let { + val appUid = cursor.getInt(COLUMN_NAME_APP_UID) + countByApp[appUid] = countByApp.getOrDefault(appUid, 0) + 1 + } } cursor.close() db.close() @@ -364,16 +363,18 @@ class StatsDatabase(context: Context) : return entry } - fun getAllTrackersOfApp(appUid: Int): List { + + fun getTrackers(appUids: List?): List { synchronized(lock) { - val columns = - arrayOf(COLUMN_NAME_TRACKER, COLUMN_NAME_APP_UID) + val columns = arrayOf(COLUMN_NAME_TRACKER, COLUMN_NAME_APP_UID) var selection: String? = null + var selectionArg: Array? = null - if (appUid >= 0) { - selection = "$COLUMN_NAME_APP_UID = ?" - selectionArg = arrayOf("" + appUid) + appUids?.let { + selection = "$COLUMN_NAME_APP_UID IN (${it.joinToString(", ")})" + selectionArg = arrayOf() } + val db = readableDatabase val cursor = db.query( true, @@ -388,8 +389,7 @@ class StatsDatabase(context: Context) : ) val trackers: MutableList = ArrayList() while (cursor.moveToNext()) { - val trackerId = - cursor.getString(COLUMN_NAME_TRACKER) + val trackerId = cursor.getString(COLUMN_NAME_TRACKER) val tracker = trackersRepository.getTracker(trackerId) if (tracker != null) { trackers.add(tracker) @@ -401,9 +401,6 @@ class StatsDatabase(context: Context) : } } - val allTrackers: List - get() = getAllTrackersOfApp(-1) - class StatEntry { var app_uid = 0 var sum_contacted = 0 diff --git a/trackers/src/main/java/foundation/e/privacymodules/trackers/data/StatsRepository.kt b/trackers/src/main/java/foundation/e/privacymodules/trackers/data/StatsRepository.kt index 0e88102..f5d217a 100644 --- a/trackers/src/main/java/foundation/e/privacymodules/trackers/data/StatsRepository.kt +++ b/trackers/src/main/java/foundation/e/privacymodules/trackers/data/StatsRepository.kt @@ -65,8 +65,8 @@ class StatsRepository private constructor(context: Context) { return database.getContactedTrackersCount(appUids) } - fun getAllTrackersOfApp(app_uid: Int): List { - return database.getAllTrackersOfApp(app_uid) + fun getTrackers(appUids: List?): List { + return database.getTrackers(appUids) } fun getCallsByApps(periodCount: Int, periodUnit: TemporalUnit): Map> { -- cgit v1.2.1