Commit 98cb47db authored by Grisha Kruglov's avatar Grisha Kruglov Committed by Grisha Kruglov
Browse files

Closes #4038: Don't assume that ContentProviderClient is AutoCloseable

parent 32d73b88
Loading
Loading
Loading
Loading
+17 −3
Original line number Diff line number Diff line
@@ -76,16 +76,22 @@ object AccountSharing {
        }
    }

    @Suppress("Recycle")
    @Suppress("Recycle", "ComplexMethod")
    private fun queryForAccount(context: Context, packageName: String): ShareableAccount? {
        // assuming a certain formatting for all authorities from all sources
        val authority = "$packageName.fxa.auth"

        val client = context.contentResolver.acquireContentProviderClient(authority) ?: return null
        return client.use {

        // We don't use `.use` below because in older versions of Android (5, 6), ContentProviderClient
        // does not implement an AutoCloseable interface. This won't be caught at compile time, but
        // at runtime we'll get hit `IncompatibleClassChangeError` exception.
        // See https://github.com/mozilla-mobile/android-components/issues/4038.
        return try {
            val authAuthorityUri = Uri.parse("content://$authority")
            val authStateUri = Uri.withAppendedPath(authAuthorityUri, "state")

            it.query(
            client.query(
                authStateUri,
                arrayOf(KEY_EMAIL, KEY_SESSION_TOKEN, KEY_KSYNC, KEY_KXSCS),
                null, null, null
@@ -109,6 +115,14 @@ object AccountSharing {
                    null
                }
            }
        } finally {
            // 'close' was added in API24. For earlier API versions, we need to use 'release'.
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                client.close()
            } else {
                @Suppress("deprecation")
                client.release()
            }
        }
    }