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

Commit 09334e9c authored by Hasib Prince's avatar Hasib Prince
Browse files

App Lounge: Privacy score calculation

Privacy score computation is done from App Lounge now.
parent 28cd3f5e
Loading
Loading
Loading
Loading
+17 −12
Original line number Diff line number Diff line
@@ -256,18 +256,6 @@ class ApplicationFragment : Fragment(R.layout.fragment_application) {
                    ).show(childFragmentManager, TAG)
                }

                if (it.ratings.privacyScore != -1.0) {
                    val privacyScore = it.ratings.privacyScore.toInt().toString()
                    appPrivacyScore.text = getString(
                        R.string.privacy_rating_out_of,
                        privacyScore
                    )

                    appPrivacyScore.setCompoundDrawablesRelativeWithIntrinsicBounds(
                        null, null, getPrivacyDrawable(privacyScore), null
                    )
                    appPrivacyScore.compoundDrawablePadding = 15
                }
                appPrivacyScoreLayout.setOnClickListener {
                    ApplicationDialogFragment(
                        R.drawable.ic_lock,
@@ -343,11 +331,28 @@ class ApplicationFragment : Fragment(R.layout.fragment_application) {

    private fun fetchAppTracker() {
        applicationViewModel.fetchTrackerData().observe(viewLifecycleOwner) {
            updatePrivacyScore()
            binding.applicationLayout.visibility = View.VISIBLE
            binding.progressBar.visibility = View.GONE
        }
    }

    private fun updatePrivacyScore() {
        val privacyScore = applicationViewModel.getPrivacyScore()
        if (privacyScore != -1) {
            val appPrivacyScore = binding.ratingsInclude.appPrivacyScore
            appPrivacyScore.text = getString(
                R.string.privacy_rating_out_of,
                privacyScore.toString()
            )

            appPrivacyScore.setCompoundDrawablesRelativeWithIntrinsicBounds(
                null, null, getPrivacyDrawable(privacyScore.toString()), null
            )
            appPrivacyScore.compoundDrawablePadding = 15
        }
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
+26 −0
Original line number Diff line number Diff line
@@ -37,6 +37,8 @@ import foundation.e.apps.utils.enums.Status
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import javax.inject.Inject
import kotlin.math.ceil
import kotlin.math.round

@HiltViewModel
class ApplicationViewModel @Inject constructor(
@@ -118,4 +120,28 @@ class ApplicationViewModel @Inject constructor(
        }
        return ""
    }

    fun getPrivacyScore(): Int {
        fusedApp.value?.let {
            return calculatePrivacyScore(it)
        }
        return -1
    }

    private fun calculatePrivacyScore(fusedApp: FusedApp): Int {
        return calculateTrackersScore(fusedApp.trackers.size) + calculatePermissionsScore(
            countAndroidPermissions(fusedApp)
        )
    }

    private fun countAndroidPermissions(fusedApp: FusedApp) =
        fusedApp.perms.filter { it.contains("android.permission") }.size

    private fun calculateTrackersScore(numberOfTrackers: Int): Int {
        return if (numberOfTrackers > 5) 0 else 9 - numberOfTrackers
    }

    private fun calculatePermissionsScore(numberOfPermission: Int): Int {
        return if (numberOfPermission > 9) 0 else round(0.2 * ceil((10 - numberOfPermission) / 2.0)).toInt()
    }
}