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

Commit 46f17344 authored by Guillaume Jacquart's avatar Guillaume Jacquart
Browse files

WIP: POC on share and notifications in debug view

parent ed824732
Loading
Loading
Loading
Loading
+1 −2
Original line number Original line Diff line number Diff line
@@ -133,8 +133,7 @@ val appModule = module {
    single {
    single {
        WeeklyReportLocalRepository(
        WeeklyReportLocalRepository(
            context = androidContext(),
            context = androidContext(),
            trackersRepository = get(),
            json = get()
            appListRepository = get()
        )
        )
    }
    }


+3 −17
Original line number Original line Diff line number Diff line
@@ -22,30 +22,16 @@ import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.core.stringPreferencesKey
import androidx.datastore.preferences.core.stringPreferencesKey
import androidx.datastore.preferences.preferencesDataStore
import androidx.datastore.preferences.preferencesDataStore
import com.google.gson.Gson
import com.google.gson.GsonBuilder
import com.google.gson.JsonDeserializationContext
import com.google.gson.JsonDeserializer
import com.google.gson.JsonElement
import com.google.gson.JsonNull
import com.google.gson.JsonObject
import com.google.gson.JsonSerializationContext
import com.google.gson.JsonSerializer
import foundation.e.advancedprivacy.core.utils.getValue
import foundation.e.advancedprivacy.core.utils.getValue
import foundation.e.advancedprivacy.core.utils.removeKey
import foundation.e.advancedprivacy.core.utils.removeKey
import foundation.e.advancedprivacy.core.utils.setValue
import foundation.e.advancedprivacy.core.utils.setValue
import foundation.e.advancedprivacy.domain.entities.DisplayableApp
import foundation.e.advancedprivacy.domain.entities.WeeklyReport
import foundation.e.advancedprivacy.domain.entities.WeeklyReport
import foundation.e.advancedprivacy.trackers.data.TrackersRepository
import foundation.e.advancedprivacy.trackers.domain.entities.Tracker
import kotlinx.coroutines.flow.first
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.Json
import java.lang.reflect.Type
import timber.log.Timber
import timber.log.Timber


class WeeklyReportLocalRepository(
class WeeklyReportLocalRepository(
    context: Context,
    context: Context,
    private val json: Json,
    private val json: Json
) {
) {


    private val Context.dataStore: DataStore<Preferences> by preferencesDataStore(name = "weeklyreport_datastore")
    private val Context.dataStore: DataStore<Preferences> by preferencesDataStore(name = "weeklyreport_datastore")
+6 −5
Original line number Original line Diff line number Diff line
@@ -20,13 +20,12 @@ package foundation.e.advancedprivacy.domain.entities
import foundation.e.advancedprivacy.trackers.domain.entities.Tracker
import foundation.e.advancedprivacy.trackers.domain.entities.Tracker
import kotlinx.serialization.Serializable
import kotlinx.serialization.Serializable



@Serializable
@Serializable
data class WeeklyReport(
data class WeeklyReport(
    val statType: StatType,
    val statType: StatType,
    val labelId: LabelId,
    val labelId: LabelId,
    val primaryValue: String,
    val primaryValue: String,
    val secondaryValues: List<String>,
    val secondaryValues: List<String>
) {
) {
    @Serializable
    @Serializable
    enum class StatType {
    enum class StatType {
@@ -61,7 +60,9 @@ sealed class DisplayableReport {
        override val report: WeeklyReport,
        override val report: WeeklyReport,
        val app: DisplayableApp
        val app: DisplayableApp
    ) : DisplayableReport()
    ) : DisplayableReport()

    data class ReportWithTracker(
    data class ReportWithTracker(
        override val report: WeeklyReport,
        override val report: WeeklyReport,
        val tracker: Tracker
        val tracker: Tracker
    ): DisplayableReport
    ) : DisplayableReport()
}
+46 −39
Original line number Original line Diff line number Diff line
@@ -18,7 +18,6 @@
package foundation.e.advancedprivacy.domain.usecases
package foundation.e.advancedprivacy.domain.usecases


import android.util.Log
import android.util.Log
import foundation.e.advancedprivacy.R
import foundation.e.advancedprivacy.data.repositories.AppListRepository
import foundation.e.advancedprivacy.data.repositories.AppListRepository
import foundation.e.advancedprivacy.data.repositories.ResourcesRepository
import foundation.e.advancedprivacy.data.repositories.ResourcesRepository
import foundation.e.advancedprivacy.data.repositories.WeeklyReportLocalRepository
import foundation.e.advancedprivacy.data.repositories.WeeklyReportLocalRepository
@@ -51,16 +50,21 @@ class WeeklyReportUseCase(
    val showWeeklyReportNotification: SharedFlow<String> = _showWeeklyReportNotification.asSharedFlow()
    val showWeeklyReportNotification: SharedFlow<String> = _showWeeklyReportNotification.asSharedFlow()


    suspend fun updateWeeklyReport() = withContext(Dispatchers.IO) {
    suspend fun updateWeeklyReport() = withContext(Dispatchers.IO) {
        val history = weeklyReportRepository.getLast99Reports()
//        val history = weeklyReportRepository.getLast99Reports()
        val weeklyReport = chooseBestStatAndLabelToDisplay(computeStats(Instant.now()), history)
//        val weeklyReport = chooseBestStatAndLabelToDisplay(computeStats(Instant.now()), history)
        weeklyReportRepository.setLastWeeklyReport(weeklyReport)
//        weeklyReportRepository.setLastWeeklyReport(weeklyReport)
        buildLabel(weeklyReport)?.let {
//        buildLabel(weeklyReport)?.let {
            _showWeeklyReportNotification.emit(it)
//            _showWeeklyReportNotification.emit(it)
        }
//        }
    }
    }


    suspend fun getLast(): DisplayableReport? {
    suspend fun getLast(): DisplayableReport? {
        val weeklyReport = weeklyReportRepository.getLastWeeklyReport() ?: return null
        val weeklyReport = weeklyReportRepository.getLastWeeklyReport() ?: return null
        return weeklyReport.toDisplayableReport()
    }

    private fun WeeklyReport.toDisplayableReport(): DisplayableReport? {
        val weeklyReport = this
        return when (weeklyReport.labelId) {
        return when (weeklyReport.labelId) {
            WeeklyReport.LabelId.CALLS_PER_APP_1,
            WeeklyReport.LabelId.CALLS_PER_APP_1,
            WeeklyReport.LabelId.CALLS_PER_APP_2,
            WeeklyReport.LabelId.CALLS_PER_APP_2,
@@ -81,32 +85,31 @@ class WeeklyReportUseCase(
    }
    }


    suspend fun getLastLabel(): String? {
    suspend fun getLastLabel(): String? {
        return buildLabel(getLast())
        return null // buildLabel(getLast())
    }
    }


    private fun WeeklyReport.LabelId.toStringId(): Int = when (this) {
//    private fun WeeklyReport.LabelId.toStringId(): Int = when (this) {
        WeeklyReport.LabelId.CALLS_PER_APP_1 -> R.string.weeklyreport_label_calls_per_app_1
//        WeeklyReport.LabelId.CALLS_PER_APP_1 -> R.string.weeklyreport_label_calls_per_app_1
        WeeklyReport.LabelId.CALLS_PER_APP_2 -> R.string.weeklyreport_label_calls_per_app_2
//        WeeklyReport.LabelId.CALLS_PER_APP_2 -> R.string.weeklyreport_label_calls_per_app_2
        WeeklyReport.LabelId.CALLS_PER_APP_3 -> R.string.weeklyreport_label_calls_per_app_3
//        WeeklyReport.LabelId.CALLS_PER_APP_3 -> R.string.weeklyreport_label_calls_per_app_3
        WeeklyReport.LabelId.NEW_TRACKER_1 -> R.string.weeklyreport_label_new_tracker_1
//        WeeklyReport.LabelId.NEW_TRACKER_1 -> R.string.weeklyreport_label_new_tracker_1
        WeeklyReport.LabelId.NEW_TRACKER_2 -> R.string.weeklyreport_label_new_tracker_2
//        WeeklyReport.LabelId.NEW_TRACKER_2 -> R.string.weeklyreport_label_new_tracker_2
        WeeklyReport.LabelId.NEW_TRACKER_3 -> R.string.weeklyreport_label_new_tracker_3
//        WeeklyReport.LabelId.NEW_TRACKER_3 -> R.string.weeklyreport_label_new_tracker_3
        WeeklyReport.LabelId.CALLS_AND_LEAKS_1 -> R.string.weeklyreport_label_call_and_leaks_1
//        WeeklyReport.LabelId.CALLS_AND_LEAKS_1 -> R.string.weeklyreport_label_call_and_leaks_1
        WeeklyReport.LabelId.CALLS_AND_LEAKS_2 -> R.string.weeklyreport_label_call_and_leaks_2
//        WeeklyReport.LabelId.CALLS_AND_LEAKS_2 -> R.string.weeklyreport_label_call_and_leaks_2
        WeeklyReport.LabelId.TRACKER_WITH_MOST_APPS_1 -> R.string.weeklyreport_label_tracker_with_most_apps_1
//        WeeklyReport.LabelId.TRACKER_WITH_MOST_APPS_1 -> R.string.weeklyreport_label_tracker_with_most_apps_1
    }
//    }

//
    private fun buildLabel(weeklyReport: WeeklyReport?): String? {
//    private fun buildLabel(weeklyReport: WeeklyReport?): String? {
        if (weeklyReport == null) return null
//        if (weeklyReport == null) return null

//
        return resourcesRepository.getString(
//        return resourcesRepository.getString(
            weeklyReport.labelId.toStringId(),
//            weeklyReport.labelId.toStringId(),
            *weeklyReport.value.toTypedArray()
//            *weeklyReport.value.toTypedArray()
        )
//        )
    }
//    }



    suspend fun debugGenerateReporsSinceWeeksAgo(weeksAgo: Int) = withContext(Dispatchers.IO) {
    suspend fun debugGenerateReporsSinceWeeksAgo(weeksAgo: Int): List<DisplayableReport> = withContext(Dispatchers.IO) {
        var history = mutableListOf<WeeklyReport>()
        var history = mutableListOf<WeeklyReport>()
        val start = System.currentTimeMillis()
        val start = System.currentTimeMillis()


@@ -116,9 +119,12 @@ class WeeklyReportUseCase(
            history.add(weeklyReport)
            history.add(weeklyReport)
        }
        }


        Log.d("Debug-weekly", history.mapIndexed { index, report -> "week $index : ${buildLabel(report)}" }.joinToString("\n"))
//        Log.d("Debug-weekly", history.mapIndexed { index, report -> "week $index : ${buildLabel(report)}" }.joinToString("\n"))


        Log.d("Debug-weekly", "computed reports in : ${System.currentTimeMillis() - start}")
        Log.d("Debug-weekly", "computed reports in : ${System.currentTimeMillis() - start}")
        history.mapNotNull {
            it.toDisplayableReport()
        }
    }
    }


    private data class Stats(
    private data class Stats(
@@ -362,11 +368,12 @@ class WeeklyReportUseCase(
        }
        }


        // Sort candidates, regarding history.
        // Sort candidates, regarding history.
        return candidates.map { computeScore(it, history) }.sortedBy { it.score }.let {
        return candidates.map { computeScore(it, history) }.sortedBy { it.score }
            val scores = it.map { "${it.score} for: ${buildLabel(it.weeklyReport) }" }
//            .let {
            Log.d("Debug-weekly", "Computed scores : \n${scores.joinToString("\n")}")
//            val scores = it.map { "${it.score} for: ${buildLabel(it.weeklyReport) }" }
            it
//            Log.d("Debug-weekly", "Computed scores : \n${scores.joinToString("\n")}")
        }
//            it
//        }
            .first().weeklyReport
            .first().weeklyReport
    }
    }


+1 −1
Original line number Original line Diff line number Diff line
@@ -49,7 +49,7 @@ class ScreenshotsProvider : FileProvider() {
                action = Intent.ACTION_SEND
                action = Intent.ACTION_SEND
                flags = Intent.FLAG_GRANT_READ_URI_PERMISSION
                flags = Intent.FLAG_GRANT_READ_URI_PERMISSION
                setDataAndType(contentUri, "image/png")
                setDataAndType(contentUri, "image/png")
                putExtra(Intent.EXTRA_TEXT, message)
                // putExtra(Intent.EXTRA_TEXT, message)
                putExtra(Intent.EXTRA_STREAM, contentUri)
                putExtra(Intent.EXTRA_STREAM, contentUri)
            }
            }
        }
        }
Loading