diff options
| author | Guillaume Jacquart <guillaume.jacquart@hoodbrains.com> | 2022-03-01 08:06:11 +0000 |
|---|---|---|
| committer | Guillaume Jacquart <guillaume.jacquart@hoodbrains.com> | 2022-03-01 08:06:11 +0000 |
| commit | ed659e60de259fe51b811af96a589c6bb9fd7d35 (patch) | |
| tree | 11a6458d30450420b7397560170579cfc857cded /app/src/main/java/foundation/e/privacycentralapp/data | |
| parent | 13b6d0463fe7fa5cc5586b677e17d38a0fd5d0dd (diff) | |
| parent | 41480b04ee31e8e694d370184c15de8c4dce03d0 (diff) | |
| download | advanced-privacy-ed659e60de259fe51b811af96a589c6bb9fd7d35.tar.gz | |
Merge branch 'hidden_system_app' into 'main'
Hide systems app behind parameter app in lists.
See merge request e/privacy-central/privacycentralapp!19
Diffstat (limited to 'app/src/main/java/foundation/e/privacycentralapp/data')
| -rw-r--r-- | app/src/main/java/foundation/e/privacycentralapp/data/repositories/AppListsRepository.kt | 104 |
1 files changed, 104 insertions, 0 deletions
diff --git a/app/src/main/java/foundation/e/privacycentralapp/data/repositories/AppListsRepository.kt b/app/src/main/java/foundation/e/privacycentralapp/data/repositories/AppListsRepository.kt new file mode 100644 index 0000000..4718923 --- /dev/null +++ b/app/src/main/java/foundation/e/privacycentralapp/data/repositories/AppListsRepository.kt @@ -0,0 +1,104 @@ +/* + * Copyright (C) 2022 E FOUNDATION + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +package foundation.e.privacycentralapp.data.repositories + +import android.Manifest +import android.content.Context +import android.content.Intent +import android.content.pm.ApplicationInfo +import foundation.e.privacymodules.permissions.PermissionsPrivacyModule +import foundation.e.privacymodules.permissions.data.ApplicationDescription +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.flow.map +import kotlinx.coroutines.launch + +class AppListsRepository( + private val permissionsModule: PermissionsPrivacyModule, + private val context: Context, + private val coroutineScope: CoroutineScope +) { + val dummySystemApp = permissionsModule.getApplicationDescription("com.android.settings") + + fun getVisibleApps(): Flow<List<ApplicationDescription>> { + coroutineScope.launch { + val (visible, hidden) = splitVisibleToHidden(getAppsUsingInternet()) + appDescriptions.emit( + visible.map { it.toApplicationDescription(withIcon = true) } + to hidden.map { it.toApplicationDescription() } + ) + } + return appDescriptions.map { it.first.sortedBy { app -> app.label.toString().lowercase() } } + } + fun getHiddenSystemApps(): List<ApplicationDescription> { + return appDescriptions.value.second + } + + private val pm get() = context.packageManager + + private val appDescriptions = MutableStateFlow( + emptyList<ApplicationDescription>() to emptyList<ApplicationDescription>() + ) + + private fun getAppsUsingInternet(): List<ApplicationInfo> { + return pm.getInstalledApplications(0) + .filter { + permissionsModule.getPermissions(it.packageName) + .contains(Manifest.permission.INTERNET) + } + } + + private fun isNotHiddenSystemApp(app: ApplicationInfo, launcherApps: List<String>): Boolean { + if (app.hasFlag(ApplicationInfo.FLAG_UPDATED_SYSTEM_APP)) { + return true + } else if (!app.hasFlag(ApplicationInfo.FLAG_SYSTEM)) { + return true + } else if (launcherApps.contains(app.packageName)) { + return true + } + return false + } + + private fun ApplicationInfo.hasFlag(flag: Int) = (flags and flag) == 1 + + private fun splitVisibleToHidden(apps: List<ApplicationInfo>): Pair<List<ApplicationInfo>, List<ApplicationInfo>> { + val launcherPackageNames = pm.queryIntentActivities( + Intent(Intent.ACTION_MAIN, null).apply { addCategory(Intent.CATEGORY_LAUNCHER) }, + 0 + ).mapNotNull { it.activityInfo?.packageName } + return apps.fold( + mutableListOf<ApplicationInfo>() to mutableListOf<ApplicationInfo>() + ) { + acc, app -> + if (isNotHiddenSystemApp(app, launcherPackageNames)) { + acc.first.add(app) + } else { + acc.second.add(app) + } + acc + } + } + + private fun ApplicationInfo.toApplicationDescription(withIcon: Boolean = true) = ApplicationDescription( + packageName = packageName, + uid = uid, + label = pm.getApplicationLabel(this), + icon = if (withIcon) pm.getApplicationIcon(packageName) else null + ) +} |
