Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "Improve Sync Performance" #488

Merged
merged 2 commits into from
Jul 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import android.app.PendingIntent
import android.app.TaskStackBuilder
import android.content.Context
import android.content.Intent
import android.content.pm.PackageInfo
import android.content.pm.PackageManager
import android.os.Binder
import android.os.Build
Expand All @@ -17,17 +18,20 @@ import androidx.core.app.NotificationChannelCompat
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import androidx.lifecycle.LifecycleService
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.lifecycleScope
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.collectIndexed
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.launch
import org.eu.exodus_privacy.exodusprivacy.manager.database.ExodusDatabaseRepository
import org.eu.exodus_privacy.exodusprivacy.manager.database.app.ExodusApplication
import org.eu.exodus_privacy.exodusprivacy.manager.database.tracker.TrackerData
import org.eu.exodus_privacy.exodusprivacy.manager.network.ExodusAPIRepository
import org.eu.exodus_privacy.exodusprivacy.manager.network.NetworkManager
import org.eu.exodus_privacy.exodusprivacy.manager.network.data.AppDetails
import org.eu.exodus_privacy.exodusprivacy.manager.packageinfo.ExodusPackageRepository
import org.eu.exodus_privacy.exodusprivacy.manager.sync.SyncManager
import org.eu.exodus_privacy.exodusprivacy.objects.Application
import javax.inject.Inject

@AndroidEntryPoint
Expand All @@ -43,16 +47,25 @@ class ExodusUpdateService : LifecycleService() {

private val TAG = ExodusUpdateService::class.java.simpleName

private val job = SupervisorJob()
private val serviceScope = CoroutineScope(job)

// Tracker and Apps
private val currentSize: MutableStateFlow<Int> = MutableStateFlow(1)
private val trackersList = mutableListOf<TrackerData>()
private val appList = mutableListOf<ExodusApplication>()
private val currentSize: MutableLiveData<Int> = MutableLiveData(1)

private var networkConnected: Boolean = false
private var totalNumberOfAppsHavingTrackers = 0
private var validPackages = listOf<PackageInfo>()
private var notificationPermGranted = false

@Inject
lateinit var networkManager: NetworkManager
// Inject required modules
private var applicationList = mutableListOf<Application>()
private var applicationListAfterUninstall = mutableListOf<Application>()

@Inject
lateinit var syncManager: SyncManager
lateinit var networkManager: NetworkManager

@Inject
lateinit var exodusPackageRepository: ExodusPackageRepository
Expand Down Expand Up @@ -122,40 +135,47 @@ class ExodusUpdateService : LifecycleService() {
private fun launchFetch(firstTime: Boolean) {
// create list of installed packages, that are system apps or launchable

val numberOfInstalledPackages = exodusPackageRepository.getValidPackageList().size
validPackages = exodusPackageRepository.getValidPackageList()
val numberOfInstalledPackages = validPackages.size

if (networkConnected) {
IS_SERVICE_RUNNING = true
val notificationPermGranted = ActivityCompat.checkSelfPermission(
this,
Manifest.permission.POST_NOTIFICATIONS,
) == PackageManager.PERMISSION_GRANTED

if (notificationPermGranted) {
if (ActivityCompat.checkSelfPermission(
this,
Manifest.permission.POST_NOTIFICATIONS,
) == PackageManager.PERMISSION_GRANTED
) {
Log.d(TAG, "Permission to post notification was granted.")
notificationPermGranted = true

// Create notification channels on post-nougat devices
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
notificationManager.createNotificationChannel(notificationChannel)
}

lifecycleScope.launch {
currentSize.collectIndexed { index, current ->
notificationManager.notify(
SERVICE_ID,
createNotification(
currentSize = current,
totalSize = numberOfInstalledPackages,
cancellable = !firstTime && index == 0,
context = this@ExodusUpdateService,
),
)
}
}
notificationManager.notify(
SERVICE_ID,
createNotification(
currentSize.value!!,
numberOfInstalledPackages,
!firstTime,
this,
),
)
}

// Update all database
updateAllDatabase()
updateAllDatabase(firstTime)

if (notificationPermGranted) {
currentSize.observe(this) { current ->
notificationManager.notify(
SERVICE_ID,
createNotification(current, numberOfInstalledPackages, false, this),
)
}
}
}
}

Expand Down Expand Up @@ -212,22 +232,155 @@ class ExodusUpdateService : LifecycleService() {
return builder
}

private fun updateAllDatabase() {
private fun updateAllDatabase(firstTime: Boolean) {
Toast.makeText(
this,
getString(R.string.fetching_apps),
Toast.LENGTH_SHORT,
).show()
lifecycleScope.launch {
syncManager.sync(
onTrackerSyncDone = {
// Show a different notification if possible
},
onAppSync = {
currentSize.update { it + 1 }
},
)
stopService()
serviceScope.launch {
if (!firstTime) removeUninstalledApps()
Log.d(TAG, "Refreshing trackers database.")
fetchTrackers()
}.invokeOnCompletion { trackerThrow ->
if (trackerThrow == null) {
serviceScope.launch {
Log.d(TAG, "Refreshing applications database.")
fetchApps()
}.invokeOnCompletion { appsThrow ->
if (appsThrow == null) {
serviceScope.launch {
// All data is fetched, save it
trackersList.forEach {
exodusDatabaseRepository.saveTrackerData(it)
}
Log.d(TAG, "Done saving tracker data.")
appList.forEach {
exodusDatabaseRepository.saveApp(it)
}
Log.d(TAG, "Done saving app details.")
// We are done, gracefully exit!
stopService()
}
} else {
Log.e(TAG, appsThrow.stackTrace.toString())
}
}
} else {
Log.e(TAG, trackerThrow.stackTrace.toString())
}
}
}

private suspend fun fetchTrackers() {
try {
val list = exodusAPIRepository.getAllTrackers()
list.trackers.forEach { (key, value) ->
val trackerData = TrackerData(
key.toInt(),
value.categories,
value.code_signature,
value.creation_date,
value.description,
value.name,
value.network_signature,
value.website,
)
trackersList.add(trackerData)
}
} catch (e: Exception) {
Log.e(TAG, "Unable to fetch trackers.", e)
}
}

private suspend fun fetchApps() {
try {
applicationList = exodusPackageRepository.getApplicationList(validPackages)
applicationList.forEach { app ->
val appDetailList =
exodusAPIRepository.getAppDetails(app.packageName).toMutableList()

val remoteVersionCodes: ArrayList<String> = arrayListOf()
val localVersionCode = app.versionCode

appDetailList.forEach { remoteVersionCodes.add(it.version_code) }
Log.d(TAG, "List of remote version codes for ${app.name}\n$remoteVersionCodes")
Log.d(TAG, "Local version code for ${app.name}\n$localVersionCode")

// Look for current installed version in the list, otherwise pick the latest one
val currentApp =
appDetailList.filter { it.version_code.toLongOrZero() == app.versionCode }

// if a matching version code was found, use this as our exodus app
val latestExodusApp = if (currentApp.isNotEmpty()) {
currentApp[0]
} else { // otherwise use highest number of version codes found
appDetailList.maxByOrNull { it.version_code.toLongOrZero() } ?: AppDetails()
}

// Create and save app data with proper tracker info
val exodusApp = ExodusApplication(
app.packageName,
app.name,
app.icon,
app.versionName,
app.versionCode,
app.permissions,
latestExodusApp.version_name,
latestExodusApp.version_code.toLongOrZero(),
latestExodusApp.trackers,
app.source,
latestExodusApp.report,
latestExodusApp.created,
latestExodusApp.updated,
)

appList.add(exodusApp)
// Update tracker data regarding this app
latestExodusApp.trackers.forEach { id ->
trackersList.find { it.id == id }?.let {
it.presentOnDevice = true
it.exodusApplications.add(exodusApp.packageName)
}
}
currentSize.postValue(currentSize.value!! + 1)
}

totalNumberOfAppsHavingTrackers = countAppsHavingTrackers(appList)
trackersList.forEach {
it.totalNumberOfAppsHavingTrackers = totalNumberOfAppsHavingTrackers
}
} catch (e: Exception) {
Log.e(TAG, "Unable to fetch apps.", e)
}
}

private suspend fun removeUninstalledApps() {
try {
applicationListAfterUninstall =
exodusPackageRepository.getApplicationList(validPackages)
val packageNameListAfterUninstall = mutableListOf<String>()
applicationListAfterUninstall.forEach { packageNameListAfterUninstall.add(it.packageName) }
val packageNameList = exodusDatabaseRepository.getAllPackageNames().toMutableList()
val listOfPackageNameToBeRemove = mutableListOf<String>()
if (packageNameList.size > packageNameListAfterUninstall.size) {
packageNameList.forEach {
if (!packageNameListAfterUninstall.contains(it)) {
listOfPackageNameToBeRemove.add(it)
}
}
exodusDatabaseRepository.deleteApps(listOfPackageNameToBeRemove)
}
} catch (e: Exception) {
Log.e(TAG, "Unable to remove apps.", e)
}
}

fun countAppsHavingTrackers(
appList: MutableList<ExodusApplication>,
): Int {
return appList.count {
it.exodusTrackers.isNotEmpty()
}
}

Expand All @@ -238,6 +391,15 @@ class ExodusUpdateService : LifecycleService() {
private fun stopService() {
IS_SERVICE_RUNNING = false
notificationManager.cancel(SERVICE_ID)
job.cancel()
stopSelf()
}

private fun String.toLongOrZero(): Long {
return if (this.isNotBlank()) {
this.toLong()
} else {
0L
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ class TrackersViewModel @Inject constructor(
exodusDatabaseRepository: ExodusDatabaseRepository,
) : ViewModel() {

val trackersList: LiveData<List<TrackerData>> = exodusDatabaseRepository.getActiveTrackers()
val trackersList: LiveData<List<TrackerData>> = exodusDatabaseRepository.getAllTrackers()
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,6 @@ class ExodusDatabaseRepository @Inject constructor(
return trackerDataDao.queryAllTrackers()
}

fun getActiveTrackers(): LiveData<List<TrackerData>> {
Log.d(TAG, "Querying all active trackers as live data.")
return trackerDataDao.queryActiveTrackers()
}

suspend fun getTrackers(listOfID: List<Int>): List<TrackerData> {
Log.d(TAG, "Querying trackers by list of ids: $listOfID.")
return trackerDataDao.queryTrackersByIdList(listOfID)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ data class TrackerData(
val network_signature: String = String(),
val website: String = String(),
var presentOnDevice: Boolean = false,
val exodusApplications: List<String> = mutableListOf(),
val exodusApplications: MutableList<String> = mutableListOf(),
@ColumnInfo(defaultValue = 0.toString()) var totalNumberOfAppsHavingTrackers: Int = 0,
)
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,8 @@ interface TrackerDataDao {
@Query("SELECT * FROM trackerdata WHERE id=:id")
suspend fun queryTrackerById(id: Int): List<TrackerData>

@Query("SELECT * FROM trackerdata")
fun queryAllTrackers(): LiveData<List<TrackerData>>

@Query("SELECT * FROM trackerdata WHERE presentOnDevice = 1")
fun queryActiveTrackers(): LiveData<List<TrackerData>>
fun queryAllTrackers(): LiveData<List<TrackerData>>

@Query("SELECT * FROM trackerdata WHERE id IN (:listOfID) ORDER BY name COLLATE NOCASE")
suspend fun queryTrackersByIdList(listOfID: List<Int>): List<TrackerData>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class ExodusPackageRepository @Inject constructor(

suspend fun getApplicationList(
validPackages: List<PackageInfo>,
): List<Application> {
): MutableList<Application> {
val permissionsMap = generatePermissionsMap(validPackages, packageManager)
val applicationList = mutableListOf<Application>()
validPackages.forEach { packageInfo ->
Expand All @@ -52,7 +52,7 @@ class ExodusPackageRepository @Inject constructor(
return applicationList
}

fun getValidPackageList(): List<PackageInfo> {
fun getValidPackageList(): MutableList<PackageInfo> {
val packageList = packageManager.getInstalledPackagesList(PackageManager.GET_PERMISSIONS)
val validPackages = mutableListOf<PackageInfo>()
packageList.forEach { pkgInfo ->
Expand Down
Loading