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 Diff line number Diff line
@@ -133,8 +133,7 @@ val appModule = module {
    single {
        WeeklyReportLocalRepository(
            context = androidContext(),
            trackersRepository = get(),
            appListRepository = get()
            json = get()
        )
    }

+3 −17
Original line number 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.stringPreferencesKey
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.removeKey
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.trackers.data.TrackersRepository
import foundation.e.advancedprivacy.trackers.domain.entities.Tracker
import kotlinx.coroutines.flow.first
import kotlinx.serialization.json.Json
import java.lang.reflect.Type
import timber.log.Timber

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

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


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

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

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

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

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

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

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

    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_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.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_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_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
    }

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

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

        return null // buildLabel(getLast())
    }

//    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_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.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_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_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
//    }
//
//    private fun buildLabel(weeklyReport: WeeklyReport?): String? {
//        if (weeklyReport == null) return null
//
//        return resourcesRepository.getString(
//            weeklyReport.labelId.toStringId(),
//            *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>()
        val start = System.currentTimeMillis()

@@ -116,9 +119,12 @@ class WeeklyReportUseCase(
            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}")
        history.mapNotNull {
            it.toDisplayableReport()
        }
    }

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

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

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