Skip to content

Commit

Permalink
Refactored permission properties back to functions for better Java su…
Browse files Browse the repository at this point in the history
…pport
  • Loading branch information
vestrel00 committed Nov 26, 2021
1 parent 1f48bd1 commit 2e7910d
Show file tree
Hide file tree
Showing 33 changed files with 80 additions and 82 deletions.
2 changes: 1 addition & 1 deletion core/src/main/java/contacts/core/BroadQuery.kt
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ private class BroadQueryImpl(
override fun find(): List<Contact> = find { false }

override fun find(cancel: () -> Boolean): List<Contact> {
if (!permissions.canQuery) {
if (!permissions.canQuery()) {
return emptyList()
}

Expand Down
17 changes: 8 additions & 9 deletions core/src/main/java/contacts/core/ContactsPermissions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@ interface ContactsPermissions {
/**
* Returns true if [READ_PERMISSION] is granted.
*/
val canQuery: Boolean
fun canQuery(): Boolean

/**
* Returns true if [WRITE_PERMISSION] and [GET_ACCOUNTS_PERMISSION] are granted.
*/
val canInsert: Boolean
fun canInsert(): Boolean

/**
* Returns true if [WRITE_PERMISSION] is granted.
*/
val canUpdateDelete: Boolean
fun canUpdateDelete(): Boolean

companion object {
const val READ_PERMISSION: String = Manifest.permission.READ_CONTACTS
Expand All @@ -42,15 +42,14 @@ private class ContactsPermissionsImpl(
private val applicationContext: Context
) : ContactsPermissions {

override val canQuery: Boolean
get() = applicationContext.isPermissionGrantedFor(READ_PERMISSION)
override fun canQuery(): Boolean = applicationContext.isPermissionGrantedFor(READ_PERMISSION)

override val canInsert: Boolean
get() = applicationContext.isPermissionGrantedFor(WRITE_PERMISSION)
override fun canInsert(): Boolean =
applicationContext.isPermissionGrantedFor(WRITE_PERMISSION)
&& applicationContext.isPermissionGrantedFor(GET_ACCOUNTS_PERMISSION)

override val canUpdateDelete: Boolean
get() = applicationContext.isPermissionGrantedFor(WRITE_PERMISSION)
override fun canUpdateDelete(): Boolean =
applicationContext.isPermissionGrantedFor(WRITE_PERMISSION)
}

// [ANDROID X] Same as ContextCompat.checkSelfPermission, which we are not using to avoid having
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/java/contacts/core/Delete.kt
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ private class DeleteImpl(
}

override fun commit(): Delete.Result {
if ((contactIds.isEmpty() && rawContactIds.isEmpty()) || !permissions.canUpdateDelete) {
if ((contactIds.isEmpty() && rawContactIds.isEmpty()) || !permissions.canUpdateDelete()) {
return DeleteFailed()
}

Expand Down Expand Up @@ -218,7 +218,7 @@ private class DeleteImpl(
}

override fun commitInOneTransaction(): Boolean {
if ((rawContactIds.isEmpty() && contactIds.isEmpty()) || !permissions.canUpdateDelete) {
if ((rawContactIds.isEmpty() && contactIds.isEmpty()) || !permissions.canUpdateDelete()) {
return false
}

Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/contacts/core/Insert.kt
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ private class InsertImpl(
override fun commit(): Insert.Result = commit { false }

override fun commit(cancel: () -> Boolean): Insert.Result {
if (rawContacts.isEmpty() || !contacts.permissions.canInsert || cancel()) {
if (rawContacts.isEmpty() || !contacts.permissions.canInsert() || cancel()) {
return InsertFailed()
}

Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/contacts/core/Query.kt
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ private class QueryImpl(
override fun find(): List<Contact> = find { false }

override fun find(cancel: () -> Boolean): List<Contact> {
if (!permissions.canQuery || cancel()) {
if (!permissions.canQuery() || cancel()) {
return emptyList()
}

Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/contacts/core/Update.kt
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ private class UpdateImpl(
override fun commit(): Update.Result = commit { false }

override fun commit(cancel: () -> Boolean): Update.Result {
if (rawContacts.isEmpty() || !contacts.permissions.canUpdateDelete || cancel()) {
if (rawContacts.isEmpty() || !contacts.permissions.canUpdateDelete() || cancel()) {
return UpdateFailed()
}

Expand Down
18 changes: 9 additions & 9 deletions core/src/main/java/contacts/core/accounts/AccountsPermissions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@ interface AccountsPermissions {
/**
* Returns true if [GET_ACCOUNTS_PERMISSION] and [READ_PERMISSION] are granted.
*/
val canQueryAccounts: Boolean
fun canQueryAccounts(): Boolean

/**
* Returns true if [READ_PERMISSION] is granted.
*/
val canQueryRawContacts: Boolean
fun canQueryRawContacts(): Boolean

/**
* Returns true if [GET_ACCOUNTS_PERMISSION] and [WRITE_PERMISSION] are granted.
*/
val canUpdateRawContactsAssociations: Boolean
fun canUpdateRawContactsAssociations(): Boolean

companion object {
const val GET_ACCOUNTS_PERMISSION: String = Manifest.permission.GET_ACCOUNTS
Expand All @@ -39,14 +39,14 @@ internal fun AccountsPermissions(context: Context): AccountsPermissions =
private class AccountsPermissionsImpl(private val applicationContext: Context) :
AccountsPermissions {

override val canQueryAccounts: Boolean
get() = applicationContext.isPermissionGrantedFor(GET_ACCOUNTS_PERMISSION)
override fun canQueryAccounts(): Boolean =
applicationContext.isPermissionGrantedFor(GET_ACCOUNTS_PERMISSION)
&& applicationContext.isPermissionGrantedFor(READ_PERMISSION)

override val canQueryRawContacts: Boolean
get() = applicationContext.isPermissionGrantedFor(READ_PERMISSION)
override fun canQueryRawContacts(): Boolean =
applicationContext.isPermissionGrantedFor(READ_PERMISSION)

override val canUpdateRawContactsAssociations: Boolean
get() = applicationContext.isPermissionGrantedFor(GET_ACCOUNTS_PERMISSION)
override fun canUpdateRawContactsAssociations(): Boolean =
applicationContext.isPermissionGrantedFor(GET_ACCOUNTS_PERMISSION)
&& applicationContext.isPermissionGrantedFor(WRITE_PERMISSION)
}
8 changes: 4 additions & 4 deletions core/src/main/java/contacts/core/accounts/AccountsQuery.kt
Original file line number Diff line number Diff line change
Expand Up @@ -185,21 +185,21 @@ private class AccountsQueryImpl(
}
""".trimIndent()

override fun allAccounts(): List<Account> = if (!permissions.canQueryAccounts) {
override fun allAccounts(): List<Account> = if (!permissions.canQueryAccounts()) {
emptyList()
} else {
accountManager.accounts.asList()
}

override fun accountsWithType(type: String): List<Account> =
if (!permissions.canQueryAccounts) {
if (!permissions.canQueryAccounts()) {
emptyList()
} else {
accountManager.getAccountsByType(type).asList()
}

override fun accountFor(rawContact: RawContactEntity): Account? =
if (!permissions.canQueryAccounts || rawContact.isProfile != isProfile) {
if (!permissions.canQueryAccounts() || rawContact.isProfile != isProfile) {
// Intentionally fail the operation to ensure that this is only used for intended
// profile or non-profile operations. Otherwise, operation can succeed. This is only
// done to enforce API design.
Expand Down Expand Up @@ -228,7 +228,7 @@ private class AccountsQueryImpl(
override fun accountsFor(rawContacts: Sequence<RawContactEntity>, cancel: () -> Boolean):
AccountsQuery.AccountsList {

if (!permissions.canQueryAccounts) {
if (!permissions.canQueryAccounts()) {
return AccountsListImpl(emptyMap())
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ private class AccountsRawContactsAssociationsUpdateImpl(
override fun associateAccountWithLocalRawContacts(
account: Account, rawContacts: Sequence<RawContactEntity>
): Boolean {
if (!accounts.permissions.canUpdateRawContactsAssociations ||
if (!accounts.permissions.canUpdateRawContactsAssociations() ||
account.isNotInSystem(accounts)
) {
return false
Expand Down Expand Up @@ -429,7 +429,7 @@ private class AccountsRawContactsAssociationsUpdateImpl(
}

override fun associateAccountWithAllLocalRawContacts(account: Account): Boolean {
if (!accounts.permissions.canUpdateRawContactsAssociations ||
if (!accounts.permissions.canUpdateRawContactsAssociations() ||
account.isNotInSystem(accounts)
) {
return false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ private class AccountsRawContactsQueryImpl(
override fun find(): AccountsRawContactsQuery.BlankRawContactsList = find { false }

override fun find(cancel: () -> Boolean): AccountsRawContactsQuery.BlankRawContactsList =
if (!permissions.canQueryRawContacts) {
if (!permissions.canQueryRawContacts()) {
BlankRawContactsListImpl(emptyMap())
} else {
contentResolver.resolve(
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/java/contacts/core/data/DataDelete.kt
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ private class DataDeleteImpl(
}

override fun commit(): DataDelete.Result {
if (dataIds.isEmpty() || !permissions.canUpdateDelete) {
if (dataIds.isEmpty() || !permissions.canUpdateDelete()) {
return DataDeleteFailed()
}

Expand All @@ -160,7 +160,7 @@ private class DataDeleteImpl(
}

override fun commitInOneTransaction(): Boolean {
if (dataIds.isEmpty() || !permissions.canUpdateDelete) {
if (dataIds.isEmpty() || !permissions.canUpdateDelete()) {
return false
}

Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/contacts/core/data/DataQuery.kt
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ private class CommonDataQueryImpl<F : CommonDataField, E : CommonDataEntity>(
override fun find(): List<E> = find { false }

override fun find(cancel: () -> Boolean): List<E> {
if (!contacts.permissions.canQuery) {
if (!contacts.permissions.canQuery()) {
return emptyList()
}

Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/contacts/core/data/DataUpdate.kt
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ private class DataUpdateImpl(
override fun commit(): DataUpdate.Result = commit { false }

override fun commit(cancel: () -> Boolean): DataUpdate.Result {
if (data.isEmpty() || !permissions.canUpdateDelete || cancel()) {
if (data.isEmpty() || !permissions.canUpdateDelete() || cancel()) {
return DataUpdateFailed()
}

Expand Down
4 changes: 2 additions & 2 deletions core/src/main/java/contacts/core/groups/GroupsDelete.kt
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ private class GroupsDeleteImpl(
}

override fun commit(): GroupsDelete.Result {
if (groupIds.isEmpty() || !permissions.canUpdateDelete) {
if (groupIds.isEmpty() || !permissions.canUpdateDelete()) {
return GroupsDeleteFailed()
}

Expand All @@ -149,7 +149,7 @@ private class GroupsDeleteImpl(
return GroupsDeleteResult(results)
}

override fun commitInOneTransaction(): Boolean = permissions.canUpdateDelete
override fun commitInOneTransaction(): Boolean = permissions.canUpdateDelete()
&& groupIds.isNotEmpty()
&& !groupIds.contains(INVALID_ID)
&& contentResolver.applyBatch(GroupsOperation().delete(groupIds)) != null
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/contacts/core/groups/GroupsInsert.kt
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ private class GroupsInsertImpl(
override fun commit(): GroupsInsert.Result = commit { false }

override fun commit(cancel: () -> Boolean): GroupsInsert.Result {
if (groups.isEmpty() || !permissions.canInsert || cancel()) {
if (groups.isEmpty() || !permissions.canInsert() || cancel()) {
return GroupsInsertFailed()
}

Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/contacts/core/groups/GroupsQuery.kt
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ private class GroupsQueryImpl(
override fun find(): GroupsQuery.GroupsList = find { false }

override fun find(cancel: () -> Boolean): GroupsQuery.GroupsList =
if (!permissions.canQuery) {
if (!permissions.canQuery()) {
GroupsListImpl()
} else {
contentResolver.resolve(
Expand Down
3 changes: 1 addition & 2 deletions core/src/main/java/contacts/core/groups/GroupsUpdate.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import android.accounts.Account
import android.content.ContentResolver
import contacts.core.Contacts
import contacts.core.ContactsPermissions
import contacts.core.entities.Group
import contacts.core.entities.MutableGroup
import contacts.core.entities.operation.GroupsOperation
import contacts.core.util.applyBatch
Expand Down Expand Up @@ -194,7 +193,7 @@ private class GroupsUpdateImpl(
override fun commit(): GroupsUpdate.Result = commit { false }

override fun commit(cancel: () -> Boolean): GroupsUpdate.Result {
if (groups.isEmpty() || !permissions.canUpdateDelete || cancel()) {
if (groups.isEmpty() || !permissions.canUpdateDelete() || cancel()) {
return GroupsUpdateFailed()
}

Expand Down
4 changes: 2 additions & 2 deletions core/src/main/java/contacts/core/profile/ProfileDelete.kt
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ private class ProfileDeleteImpl(
}

override fun commit(): ProfileDelete.Result {
if ((rawContactIds.isEmpty() && !deleteProfileContact) || !permissions.canUpdateDelete) {
if ((rawContactIds.isEmpty() && !deleteProfileContact) || !permissions.canUpdateDelete()) {
return ProfileDeleteFailed()
}

Expand Down Expand Up @@ -212,7 +212,7 @@ private class ProfileDeleteImpl(
}

override fun commitInOneTransaction(): Boolean {
if ((rawContactIds.isEmpty() && !deleteProfileContact) || !permissions.canUpdateDelete) {
if ((rawContactIds.isEmpty() && !deleteProfileContact) || !permissions.canUpdateDelete()) {
return false
}

Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/contacts/core/profile/ProfileInsert.kt
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ private class ProfileInsertImpl(

if (rawContact == null
|| (!allowBlanks && rawContact.isBlank)
|| !contacts.permissions.canInsert
|| !contacts.permissions.canInsert()
|| cancel()
) {
return ProfileInsertFailed()
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/contacts/core/profile/ProfileQuery.kt
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ private class ProfileQueryImpl(
override fun find(): Contact? = find { false }

override fun find(cancel: () -> Boolean): Contact? {
if (!permissions.canQuery) {
if (!permissions.canQuery()) {
return null
}

Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/contacts/core/profile/ProfileUpdate.kt
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ private class ProfileUpdateImpl(
override fun commit(): ProfileUpdate.Result = commit { false }

override fun commit(cancel: () -> Boolean): ProfileUpdate.Result {
if (rawContacts.isEmpty() || !contacts.permissions.canUpdateDelete || cancel()) {
if (rawContacts.isEmpty() || !contacts.permissions.canUpdateDelete() || cancel()) {
return ProfileUpdateFailed()
}

Expand Down
4 changes: 2 additions & 2 deletions core/src/main/java/contacts/core/util/ContactLinks.kt
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ fun ContactEntity.link(
): ContactLinkResult {
val mainContactId = id

if (!contactsApi.permissions.canUpdateDelete ||
if (!contactsApi.permissions.canUpdateDelete() ||
mainContactId == null ||
mainContactId.isProfileId ||
contacts.find { it.isProfile } != null
Expand Down Expand Up @@ -257,7 +257,7 @@ private class ContactLinkFailed : ContactLinkResult {
fun ContactEntity.unlink(contactsApi: Contacts): ContactUnlinkResult {
val contactId = id

if (!contactsApi.permissions.canUpdateDelete ||
if (!contactsApi.permissions.canUpdateDelete() ||
contactId == null ||
contactId.isProfileId
) {
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/java/contacts/core/util/ContactOptions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import contacts.core.entities.table.Table
fun ContactEntity.options(contacts: Contacts): Options {
val contactId = id

if (!contacts.permissions.canQuery || contactId == null) {
if (!contacts.permissions.canQuery() || contactId == null) {
return Options()
}

Expand Down Expand Up @@ -91,7 +91,7 @@ fun ContactEntity.options(contacts: Contacts): Options {
fun ContactEntity.setOptions(contacts: Contacts, options: MutableOptions): Boolean {
val contactId = id

if (!contacts.permissions.canUpdateDelete || contactId == null) {
if (!contacts.permissions.canUpdateDelete() || contactId == null) {
return false
}

Expand Down
Loading

0 comments on commit 2e7910d

Please sign in to comment.