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

Unverified Commit 61e9d60b authored by Arnau Mora's avatar Arnau Mora Committed by GitHub
Browse files

Correctly handle `SecurityException` for `acquireContentProvider` (#1622)



* Handle `SecurityException` for `acquireContentProvider`

* Added optional `throwOnMissingPermissions` arg to `acquireContentProvider`

Signed-off-by: default avatarArnau Mora <arnyminerz@proton.me>

* Set `throwOnMissingPermissions` to `true`

Signed-off-by: default avatarArnau Mora <arnyminerz@proton.me>

* Adapt comments, remove now unnecessary try/catch in AccountSettingsMigration20

---------

Signed-off-by: default avatarArnau Mora <arnyminerz@proton.me>
Co-authored-by: default avatarRicki Hirner <hirner@bitfire.at>
parent dc9fb7b6
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -189,7 +189,7 @@ class SyncerTest {
        override val authority: String
            get() = throw NotImplementedError()

        override fun acquireContentProvider(): ContentProviderClient? {
        override fun acquireContentProvider(throwOnMissingPermissions: Boolean): ContentProviderClient? {
            throw NotImplementedError()
        }

+7 −1
Original line number Diff line number Diff line
@@ -78,8 +78,14 @@ class LocalAddressBookStore @Inject constructor(
        return sb.toString()
    }

    override fun acquireContentProvider() =
    override fun acquireContentProvider(throwOnMissingPermissions: Boolean) = try {
        context.contentResolver.acquireContentProviderClient(authority)
    } catch (e: SecurityException) {
        if (throwOnMissingPermissions)
            throw e
        else
            /* return */ null
    }

    override fun create(provider: ContentProviderClient, fromCollection: Collection): LocalAddressBook? {
        val service = serviceRepository.getBlocking(fromCollection.serviceId) ?: throw IllegalArgumentException("Couldn't fetch DB service from collection")
+7 −1
Original line number Diff line number Diff line
@@ -39,8 +39,14 @@ class LocalCalendarStore @Inject constructor(
    override val authority: String
        get() = CalendarContract.AUTHORITY

    override fun acquireContentProvider() =
    override fun acquireContentProvider(throwOnMissingPermissions: Boolean) = try {
        context.contentResolver.acquireContentProviderClient(authority)
    } catch (e: SecurityException) {
        if (throwOnMissingPermissions)
            throw e
        else
            /* return */ null
    }

    override fun create(client: ContentProviderClient, fromCollection: Collection): LocalCalendar? {
        val service = serviceRepository.getBlocking(fromCollection.serviceId) ?: throw IllegalArgumentException("Couldn't fetch DB service from collection")
+8 −5
Original line number Diff line number Diff line
@@ -25,16 +25,19 @@ interface LocalDataStore<T: LocalCollection<*>> {
     *
     * **The caller is responsible for closing the content provider client!**
     *
     * @return the content provider client, or `null` if the content provider could not be acquired
     * @param throwOnMissingPermissions If `true`, the function will throw [SecurityException] if permissions are not granted.
     *
     * @return the content provider client, or `null` if the content provider could not be acquired (or permissions are not
     * granted and [throwOnMissingPermissions] is `false`)
     *
     * @throws SecurityException on missing permissions
     */
    fun acquireContentProvider(): ContentProviderClient?
    fun acquireContentProvider(throwOnMissingPermissions: Boolean = false): ContentProviderClient?

    /**
     * Creates a new local collection from the given (remote) collection info.
     *
     * @param provider       the content provider client
     * @param client        the content provider client
     * @param fromCollection collection info
     *
     * @return the new local collection, or `null` if creation failed
@@ -46,7 +49,7 @@ interface LocalDataStore<T: LocalCollection<*>> {
     * [Collection] entry.
     *
     * @param account  the account that the data store is associated with
     * @param provider the content provider client
     * @param client   the content provider client
     *
     * @return a list of all local collections
     */
@@ -55,7 +58,7 @@ interface LocalDataStore<T: LocalCollection<*>> {
    /**
     * Updates the local collection with the data from the given (remote) collection info.
     *
     * @param provider        the content provider client
     * @param client          the content provider client
     * @param localCollection the local collection to update
     * @param fromCollection  collection info
     */
+8 −2
Original line number Diff line number Diff line
@@ -37,8 +37,14 @@ class LocalJtxCollectionStore @Inject constructor(
    override val authority: String
        get() = JtxContract.AUTHORITY

    override fun acquireContentProvider() =
        context.contentResolver.acquireContentProviderClient(JtxContract.AUTHORITY)
    override fun acquireContentProvider(throwOnMissingPermissions: Boolean) = try {
        context.contentResolver.acquireContentProviderClient(authority)
    } catch (e: SecurityException) {
        if (throwOnMissingPermissions)
            throw e
        else
            /* return */ null
    }

    override fun create(provider: ContentProviderClient, fromCollection: Collection): LocalJtxCollection? {
        val service = serviceDao.get(fromCollection.serviceId) ?: throw IllegalArgumentException("Couldn't fetch DB service from collection")
Loading