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

Commit 34038194 authored by Fahim Salam Chowdhury's avatar Fahim Salam Chowdhury 👽
Browse files

Enable periodicSync after bootComplete

parent 40c8d8c4
Loading
Loading
Loading
Loading
Loading
+9 −0
Original line number Diff line number Diff line
@@ -679,6 +679,15 @@

        </activity>


        <receiver
            android:name=".BootCompletedReceiver"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>

        <!-- provider to share debug info/logs -->
        <provider
            android:name="androidx.core.content.FileProvider"
+33 −0
Original line number Diff line number Diff line
/***************************************************************************************************
 * Copyright © All Contributors. See LICENSE and AUTHORS in the root directory for details.
 **************************************************************************************************/

package at.bitfire.davdroid

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import at.bitfire.davdroid.log.Logger
import at.bitfire.davdroid.settings.AccountSettings
import at.bitfire.davdroid.syncadapter.AccountUtils

/**
 * There are circumstances when Android drops automatic sync of accounts and resets them
 * to manual synchronization, for instance when the device is booted into safe mode.
 *
 * This receiver causes the app to be started when the device is rebooted. When the app
 * is started, it checks (and repairs, if necessary) the sync intervals in [App.onCreate].
 */
class BootCompletedReceiver: BroadcastReceiver() {

    override fun onReceive(context: Context, intent: Intent) {
        Logger.log.info("Device has been rebooted; checking sync intervals etc.")
        // sync intervals are checked in App.onCreate()
        AccountUtils.getMainAccounts(context)
            .forEach {
                val accountSettings = AccountSettings(context, it)
                accountSettings.initSync()
            }
    }

}
 No newline at end of file
+31 −0
Original line number Diff line number Diff line
@@ -9,6 +9,7 @@ import android.content.*
import android.os.Bundle
import android.provider.CalendarContract
import androidx.annotation.WorkerThread
import at.bitfire.davdroid.Constants
import at.bitfire.davdroid.InvalidAccountException
import at.bitfire.davdroid.R
import at.bitfire.davdroid.db.AppDatabase
@@ -542,4 +543,34 @@ class AccountSettings(
        }
    }

    fun initSync() {
        val authorities = listOf(
            context.getString(R.string.address_books_authority),
            CalendarContract.AUTHORITY,
            context.getString(R.string.task_authority),
            TaskProvider.ProviderName.JtxBoard.authority,
            TaskProvider.ProviderName.OpenTasks.authority,
            TaskProvider.ProviderName.TasksOrg.authority
        )

        authorities.forEach {
            enablePeriodicWorkManager(it)
        }
    }

    fun enablePeriodicWorkManager(authority: String) {
        if (ContentResolver.getIsSyncable(account, authority) <= 0) {
            return
        }

        val interval = getSyncInterval(authority) ?: getDefaultSyncInterval(authority)
        setSyncInterval(authority, interval)
    }

    private fun getDefaultSyncInterval(authority: String) =
        when (authority) {
            context.getString(R.string.address_books_authority) ->
                Constants.DEFAULT_CONTACTS_SYNC_INTERVAL
            else -> Constants.DEFAULT_CALENDAR_SYNC_INTERVAL
        }
}
+2 −0
Original line number Diff line number Diff line
@@ -84,6 +84,8 @@ abstract class SyncAdapterService: Service() {
                }
            }

            accountSettings.enablePeriodicWorkManager(authority)

            Logger.log.log(Level.INFO,"Returning to sync framework")
        }

+4 −0
Original line number Diff line number Diff line
@@ -301,6 +301,10 @@ class SyncWorker @AssistedInject constructor(
        val authority = inputData.getString(ARG_AUTHORITY) ?: throw IllegalArgumentException("$ARG_AUTHORITY required")
        Logger.log.info("Running sync worker: account=$account, authority=$authority")

        if (ContentResolver.getIsSyncable(account, authority) <= 0) {
            return Result.success()
        }

        // What are we going to sync? Select syncer based on authority
        val syncer: Syncer = when (authority) {
            applicationContext.getString(R.string.address_books_authority) ->