aboutsummaryrefslogtreecommitdiffstats
path: root/api/src/main/java/foundation/e/privacymodules/permissions
diff options
context:
space:
mode:
authorGuillaume Jacquart <guillaume.jacquart@hoodbrains.com>2022-08-17 08:49:03 +0000
committerGuillaume Jacquart <guillaume.jacquart@hoodbrains.com>2022-08-17 08:49:03 +0000
commitc8d88ec3364218802bc48257b7766ad8f19a6e45 (patch)
treea767b29c62cb88ec39c3475ee579439a25141474 /api/src/main/java/foundation/e/privacymodules/permissions
parent12510a55c9c2b1d21c6e1f45d0058778ddfc9eaa (diff)
downloadadvanced-privacy-c8d88ec3364218802bc48257b7766ad8f19a6e45.tar.gz
2-Simplify sources modules tree
Diffstat (limited to 'api/src/main/java/foundation/e/privacymodules/permissions')
-rw-r--r--api/src/main/java/foundation/e/privacymodules/permissions/APermissionsPrivacyModule.kt158
-rw-r--r--api/src/main/java/foundation/e/privacymodules/permissions/IPermissionsPrivacyModule.kt127
-rw-r--r--api/src/main/java/foundation/e/privacymodules/permissions/data/AppOpModes.kt43
-rw-r--r--api/src/main/java/foundation/e/privacymodules/permissions/data/ApplicationDescription.kt30
-rw-r--r--api/src/main/java/foundation/e/privacymodules/permissions/data/PermissionDescription.kt26
5 files changed, 384 insertions, 0 deletions
diff --git a/api/src/main/java/foundation/e/privacymodules/permissions/APermissionsPrivacyModule.kt b/api/src/main/java/foundation/e/privacymodules/permissions/APermissionsPrivacyModule.kt
new file mode 100644
index 0000000..68f7ee1
--- /dev/null
+++ b/api/src/main/java/foundation/e/privacymodules/permissions/APermissionsPrivacyModule.kt
@@ -0,0 +1,158 @@
+/*
+ * 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.privacymodules.permissions
+
+import android.app.AppOpsManager
+import android.content.Context
+import android.content.pm.ApplicationInfo
+import android.content.pm.PackageManager
+import android.content.pm.PermissionInfo
+import android.content.pm.PermissionInfo.PROTECTION_DANGEROUS
+import android.graphics.drawable.Drawable
+import android.os.Build
+import android.util.Log
+import foundation.e.privacymodules.permissions.data.AppOpModes
+import foundation.e.privacymodules.permissions.data.ApplicationDescription
+import foundation.e.privacymodules.permissions.data.PermissionDescription
+
+/**
+ * Implementation of the commons functionality between privileged and standard
+ * versions of the module.
+ * @param context an Android context, to retrieve packageManager for example.
+ */
+abstract class APermissionsPrivacyModule(protected val context: Context): IPermissionsPrivacyModule {
+
+ companion object {
+ private const val TAG = "PermissionsModule"
+ }
+ /**
+ * @see IPermissionsPrivacyModule.getAllApplications
+ */
+ override fun getAllApplications(): List<ApplicationDescription> {
+ val appInfos = context.packageManager.getInstalledApplications(0)
+ return appInfos.map { buildApplicationDescription(it, false) }
+ }
+
+ /**
+ * @see IPermissionsPrivacyModule.getInstalledApplications
+ */
+ override fun getInstalledApplications(): List<ApplicationDescription> {
+ return context.packageManager.getInstalledApplications(0)
+ .filter { it.flags and ApplicationInfo.FLAG_SYSTEM == 0 }
+ .map { buildApplicationDescription(it, false) }
+ }
+
+ /**
+ * @see IPermissionsPrivacyModule.getInstalledApplications
+ */
+ override fun getApplicationDescription(packageName: String): ApplicationDescription {
+ val appDesc = buildApplicationDescription(context.packageManager.getApplicationInfo(packageName, 0), false)
+ appDesc.icon = getApplicationIcon(appDesc.packageName)
+ return appDesc
+ }
+
+ /**
+ * * @see IPermissionsPrivacyModule.getPermissions
+ */
+ override fun getPermissions(packageName: String): List<String> {
+ val packageInfo = context.packageManager.getPackageInfo(packageName, PackageManager.GET_PERMISSIONS)
+ return packageInfo.requestedPermissions?.asList() ?: emptyList()
+ }
+
+ override fun getPermissionDescription(permissionName: String): PermissionDescription {
+ val info = context.packageManager.getPermissionInfo(permissionName, 0)
+ return PermissionDescription(
+ name = permissionName,
+ isDangerous = isPermissionsDangerous(info),
+ group = null,
+ label = info.loadLabel(context.packageManager),
+ description = info.loadDescription(context.packageManager)
+ )
+ }
+
+ /**
+ * @see IPermissionsPrivacyModule.isDangerousPermissionGranted
+ */
+ override fun isDangerousPermissionGranted(packageName: String, permissionName: String): Boolean {
+ return context.packageManager
+ .checkPermission(permissionName, packageName) == PackageManager.PERMISSION_GRANTED
+ }
+
+ // on google version, work only for the current package.
+ @Suppress("DEPRECATION")
+ override fun getAppOpMode(
+ appDesc: ApplicationDescription,
+ appOpPermissionName: String
+ ): AppOpModes {
+
+ val appOps = context.getSystemService(Context.APP_OPS_SERVICE) as AppOpsManager
+
+ val mode = if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
+ appOps.checkOpNoThrow(appOpPermissionName,
+
+ appDesc.uid, appDesc.packageName)
+ } else {
+ appOps.unsafeCheckOpNoThrow(
+ appOpPermissionName,
+ appDesc.uid, appDesc.packageName)
+ }
+
+ return AppOpModes.getByModeValue(mode)
+ }
+
+ override fun isPermissionsDangerous(permissionName: String): Boolean {
+ try {
+ val permissionInfo = context.packageManager.getPermissionInfo(permissionName, 0)
+ return isPermissionsDangerous(permissionInfo)
+ } catch (e: Exception) {
+ Log.w(TAG, "exception in isPermissionsDangerous(String)", e)
+ return false
+ }
+ }
+
+ @Suppress("DEPRECATION")
+ private fun isPermissionsDangerous(permissionInfo: PermissionInfo): Boolean {
+ try {
+ return if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P) {
+ permissionInfo.protectionLevel and PROTECTION_DANGEROUS == 1
+ } else {
+ permissionInfo.protection == PROTECTION_DANGEROUS
+ }
+ } catch (e: Exception) {
+ Log.w(TAG, "exception in isPermissionsDangerous(PermissionInfo)", e)
+ return false
+ }
+ }
+
+ override fun buildApplicationDescription(appInfo: ApplicationInfo, withIcon: Boolean)
+ : ApplicationDescription {
+ return ApplicationDescription(
+ packageName = appInfo.packageName,
+ uid = appInfo.uid,
+ label = getAppLabel(appInfo),
+ icon = if (withIcon) getApplicationIcon(appInfo.packageName) else null
+ )
+ }
+
+ private fun getAppLabel(appInfo: ApplicationInfo): CharSequence {
+ return context.packageManager.getApplicationLabel(appInfo)
+ }
+
+ override fun getApplicationIcon(packageName: String): Drawable? {
+ return context.packageManager.getApplicationIcon(packageName)
+ }
+}
diff --git a/api/src/main/java/foundation/e/privacymodules/permissions/IPermissionsPrivacyModule.kt b/api/src/main/java/foundation/e/privacymodules/permissions/IPermissionsPrivacyModule.kt
new file mode 100644
index 0000000..ba85f13
--- /dev/null
+++ b/api/src/main/java/foundation/e/privacymodules/permissions/IPermissionsPrivacyModule.kt
@@ -0,0 +1,127 @@
+/*
+ * 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.privacymodules.permissions
+
+import android.content.pm.ApplicationInfo
+import android.graphics.drawable.Drawable
+import foundation.e.privacymodules.permissions.data.AppOpModes
+import foundation.e.privacymodules.permissions.data.ApplicationDescription
+import foundation.e.privacymodules.permissions.data.PermissionDescription
+
+/**
+ * List applications and manage theirs permissions.
+ */
+interface IPermissionsPrivacyModule {
+
+ fun buildApplicationDescription(appInfo: ApplicationInfo, withIcon: Boolean = true): ApplicationDescription
+
+ /**
+ * List the installed application on the device which have not the FLAGS_SYSTEM.
+ * @return list of filled up [ApplicationDescription]
+ */
+ fun getInstalledApplications(): List<ApplicationDescription>
+
+ /**
+ * List all the installed application on the device.
+ * @return list of filled up [ApplicationDescription]
+ */
+ fun getAllApplications(): List<ApplicationDescription>
+
+ /**
+ * List of permissions names used by an app, specified by its [packageName].
+ * @param packageName the appId of the app
+ * @return the list off permission, in the "android.permission.PERMISSION" format.
+ */
+ fun getPermissions(packageName: String): List<String>
+
+ fun getPermissionDescription(permissionName: String): PermissionDescription
+
+
+ /**
+ * Get the filled up [ApplicationDescription] for the app specified by its [packageName]
+ * @param packageName the appId of the app
+ * @return the informations about the app.
+ */
+ fun getApplicationDescription(packageName: String): ApplicationDescription
+
+ /**
+ * Check if the current runtime permission is granted for the specified app.
+ *
+ * @param packageName the packageName of the app
+ * @param permissionName the name of the permission in "android.permission.PERMISSION" format.
+ * @return the current status for this permission.
+ */
+ fun isDangerousPermissionGranted(packageName: String, permissionName: String): Boolean
+
+
+ /**
+ * Get the appOps mode for the specified [appOpPermissionName] of the specified application.
+ *
+ * @param appDesc the application
+ * @param appOpPermissionName the AppOps permission name.
+ * @return mode, as a [AppOpModes]
+ */
+ fun getAppOpMode(appDesc: ApplicationDescription, appOpPermissionName: String): AppOpModes
+
+ /**
+ * Grant or revoke the specified permission for the specigfied app.
+ * If their is not enough privileges to get the permission, return the false
+ *
+ * @param appDesc the application
+ * @param permissionName the name of the permission in "android.permission.PERMISSION" format.
+ * @param grant true grant the permission, false revoke it.
+ * @return true if the permission is or has just been granted, false if
+ * user has to do it himself.
+ */
+ fun toggleDangerousPermission(
+ appDesc: ApplicationDescription,
+ permissionName: String,
+ grant: Boolean
+ ): Boolean
+
+
+ /**
+ * Change the appOp Mode for the specified appOpPermission and application.
+ * @param appDesc the application
+ * @param appOpPermissionName the AppOps permission name.
+ * @return true if the mode has been changed, false if
+ * user has to do it himself.
+ */
+ fun setAppOpMode(
+ appDesc: ApplicationDescription,
+ appOpPermissionName: String,
+ status: AppOpModes
+ ): Boolean
+
+ /**
+ * Return true if the application is flagged Dangerous.
+ */
+ fun isPermissionsDangerous(permissionName: String): Boolean
+
+ /**
+ * Get the application icon.
+ */
+ fun getApplicationIcon(packageName: String): Drawable?
+
+ /**
+ * Authorize the specified package to be used as Vpn.
+ * @return true if authorization has been set, false if an error has occurred.
+ */
+ fun setVpnPackageAuthorization(packageName: String): Boolean
+
+} \ No newline at end of file
diff --git a/api/src/main/java/foundation/e/privacymodules/permissions/data/AppOpModes.kt b/api/src/main/java/foundation/e/privacymodules/permissions/data/AppOpModes.kt
new file mode 100644
index 0000000..367645d
--- /dev/null
+++ b/api/src/main/java/foundation/e/privacymodules/permissions/data/AppOpModes.kt
@@ -0,0 +1,43 @@
+/*
+ * 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.privacymodules.permissions.data
+
+import android.app.AppOpsManager.*
+import android.os.Build
+
+enum class AppOpModes(val modeValue: Int) {
+ ALLOWED(MODE_ALLOWED),
+ IGNORED(MODE_IGNORED),
+ ERRORED(MODE_ERRORED),
+ DEFAULT(MODE_DEFAULT),
+ FOREGROUND(if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) MODE_ALLOWED else MODE_FOREGROUND);
+
+ companion object {
+ private val byMode = mapOf(
+ FOREGROUND.modeValue to FOREGROUND,
+ ALLOWED.modeValue to ALLOWED,
+ IGNORED.modeValue to IGNORED,
+ ERRORED.modeValue to ERRORED,
+ DEFAULT.modeValue to DEFAULT,
+ )
+
+ fun getByModeValue(modeValue: Int): AppOpModes {
+ return byMode.get(modeValue) ?: DEFAULT
+ }
+ }
+}
diff --git a/api/src/main/java/foundation/e/privacymodules/permissions/data/ApplicationDescription.kt b/api/src/main/java/foundation/e/privacymodules/permissions/data/ApplicationDescription.kt
new file mode 100644
index 0000000..cafe256
--- /dev/null
+++ b/api/src/main/java/foundation/e/privacymodules/permissions/data/ApplicationDescription.kt
@@ -0,0 +1,30 @@
+/*
+ * 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.privacymodules.permissions.data
+
+import android.graphics.drawable.Drawable
+
+/**
+ * Useful informations to identify and describe an application.
+ */
+data class ApplicationDescription(
+ val packageName: String,
+ val uid: Int,
+ var label: CharSequence?,
+ var icon: Drawable?
+)
diff --git a/api/src/main/java/foundation/e/privacymodules/permissions/data/PermissionDescription.kt b/api/src/main/java/foundation/e/privacymodules/permissions/data/PermissionDescription.kt
new file mode 100644
index 0000000..9ed297d
--- /dev/null
+++ b/api/src/main/java/foundation/e/privacymodules/permissions/data/PermissionDescription.kt
@@ -0,0 +1,26 @@
+/*
+ * 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.privacymodules.permissions.data
+
+data class PermissionDescription(
+ val name: String,
+ var isDangerous: Boolean,
+ val group: String?,
+ var label: CharSequence?,
+ var description: CharSequence?
+) \ No newline at end of file