Skip to content
This repository has been archived by the owner on Nov 1, 2022. It is now read-only.

Commit

Permalink
Closes #4038: Don't assume that ContentProviderClient is AutoCloseable
Browse files Browse the repository at this point in the history
  • Loading branch information
Grisha Kruglov authored and grigoryk committed Aug 6, 2019
1 parent 32d73b8 commit 98cb47d
Showing 1 changed file with 17 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
}
}
}

Expand Down

0 comments on commit 98cb47d

Please sign in to comment.