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

Commit c690dfd1 authored by Ricki Hirner's avatar Ricki Hirner
Browse files

Support read-only flag in raw contacts/data rows for read-only address books

parent 50775f26
Loading
Loading
Loading
Loading
+4 −4
Original line number Diff line number Diff line
@@ -9,7 +9,6 @@ package at.bitfire.davdroid.resource

import android.accounts.Account
import android.accounts.AccountManager
import android.annotation.TargetApi
import android.content.*
import android.os.Build
import android.os.Bundle
@@ -58,6 +57,8 @@ class LocalAddressBook(
            val addressBook = LocalAddressBook(context, account, provider)
            ContentResolver.setSyncAutomatically(account, ContactsContract.AUTHORITY, true)

            addressBook.readOnly = !info.privWriteContent || info.forceReadOnly

            // initialize Contacts Provider Settings
            val values = ContentValues(2)
            values.put(ContactsContract.Settings.SHOULD_SYNC, 1)
@@ -147,7 +148,7 @@ class LocalAddressBook(
                ?: throw IllegalStateException("Address book has no URL")
        set(url) = AccountManager.get(context).setUserData(account, USER_DATA_URL, url)

    var readOnly: Boolean
    override var readOnly: Boolean
        get() = AccountManager.get(context).getUserData(account, USER_DATA_READ_ONLY) != null
        set(readOnly) = AccountManager.get(context).setUserData(account, USER_DATA_READ_ONLY, if (readOnly) "1" else null)

@@ -188,7 +189,6 @@ class LocalAddressBook(
    fun update(info: CollectionInfo) {
        val newAccountName = accountName(mainAccount, info)

        @TargetApi(Build.VERSION_CODES.LOLLIPOP)
        if (account.name != newAccountName && Build.VERSION.SDK_INT >= 21) {
            // no need to re-assign contacts to new account, because they will be deleted by contacts provider in any case
            val accountManager = AccountManager.get(context)
@@ -196,8 +196,8 @@ class LocalAddressBook(
            account = future.result
        }

        Constants.log.info("Address book write permission? = ${info.privWriteContent}")
        readOnly = !info.privWriteContent || info.forceReadOnly
        Constants.log.info("Address book read-only = $readOnly")

        // make sure it will still be synchronized when contacts are updated
        ContentResolver.setSyncAutomatically(account, ContactsContract.AUTHORITY, true)
+0 −26
Original line number Diff line number Diff line
@@ -13,7 +13,6 @@ import android.content.*
import android.os.Build
import android.os.Bundle
import android.provider.ContactsContract.Groups
import androidx.core.app.NotificationCompat
import at.bitfire.dav4jvm.DavAddressBook
import at.bitfire.dav4jvm.DavResource
import at.bitfire.dav4jvm.DavResponseCallback
@@ -27,7 +26,6 @@ import at.bitfire.davdroid.log.Logger
import at.bitfire.davdroid.model.SyncState
import at.bitfire.davdroid.resource.*
import at.bitfire.davdroid.settings.AccountSettings
import at.bitfire.davdroid.ui.NotificationUtils
import at.bitfire.vcard4android.BatchOperation
import at.bitfire.vcard4android.Contact
import at.bitfire.vcard4android.GroupMethod
@@ -90,7 +88,6 @@ class ContactsSyncManager(
    }

    private val readOnly = localAddressBook.readOnly
    private var numDiscarded = 0

    private var hasVCard4 = false
    private val groupMethod = accountSettings.getGroupMethod()
@@ -158,17 +155,13 @@ class ContactsSyncManager(
            for (group in localCollection.findDeletedGroups()) {
                Logger.log.warning("Restoring locally deleted group (read-only address book!)")
                useLocal(group) { it.resetDeleted() }
                numDiscarded++
            }

            for (contact in localCollection.findDeletedContacts()) {
                Logger.log.warning("Restoring locally deleted contact (read-only address book!)")
                useLocal(contact) { it.resetDeleted() }
                numDiscarded++
            }

            if (numDiscarded > 0)
                notifyDiscardedChange()
            return false
        } else
            // mirror deletions to remote collection (DELETE)
@@ -180,18 +173,13 @@ class ContactsSyncManager(
            for (group in localCollection.findDirtyGroups()) {
                Logger.log.warning("Resetting locally modified group to ETag=null (read-only address book!)")
                useLocal(group) { it.clearDirty(null) }
                numDiscarded++
            }

            for (contact in localCollection.findDirtyContacts()) {
                Logger.log.warning("Resetting locally modified contact to ETag=null (read-only address book!)")
                useLocal(contact) { it.clearDirty(null) }
                numDiscarded++
            }

            if (numDiscarded > 0)
                notifyDiscardedChange()

        } else {
            if (groupMethod == GroupMethod.CATEGORIES) {
                /* groups memberships are represented as contact CATEGORIES */
@@ -240,20 +228,6 @@ class ContactsSyncManager(
        return super.uploadDirty()
    }

    private fun notifyDiscardedChange() {
        val notification = NotificationUtils.newBuilder(context, NotificationUtils.CHANNEL_SYNC_STATUS)
                .setSmallIcon(R.drawable.ic_delete_notification)
                .setContentTitle(context.getString(R.string.sync_contacts_read_only_address_book))
                .setContentText(context.resources.getQuantityString(R.plurals.sync_contacts_local_contact_changes_discarded, numDiscarded, numDiscarded))
                .setNumber(numDiscarded)
                .setSubText(account.name)
                .setCategory(NotificationCompat.CATEGORY_STATUS)
                .setPriority(NotificationCompat.PRIORITY_MIN)
                .setLocalOnly(true)
                .build()
        notificationManager.notify("discarded_${account.name}", 0, notification)
    }

    override fun prepareUpload(resource: LocalAddress): RequestBody = useLocal(resource) {
        val contact: Contact
        if (resource is LocalContact) {
+0 −4
Original line number Diff line number Diff line
@@ -282,10 +282,6 @@
    <string name="authority_debug_provider" translatable="false">at.bitfire.davdroid.debug</string>
    <string name="debug_info_title">Debug info</string>
    <string name="sync_contacts_read_only_address_book">Read-only address book</string>
    <plurals name="sync_contacts_local_contact_changes_discarded">
        <item quantity="one">Local contact change discarded</item>
        <item quantity="other">%d local contact changes discarded</item>
    </plurals>
    <string name="sync_error_permissions">DAVx⁵ permissions</string>
    <string name="sync_error_permissions_text">Additional permissions required</string>
    <string name="sync_error_opentasks_too_old">OpenTasks too old</string>
Compare c9f44bb8 to 4e5acd17
Original line number Diff line number Diff line
Subproject commit c9f44bb8bfeacbee4f04c67c208858e372c5bd3d
Subproject commit 4e5acd17221491006014affb33cb5f41b8b84ed9