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

Commit 8aba0c80 authored by Ricki Hirner's avatar Ricki Hirner
Browse files

New option: keep in foreground (may help when the device prevents automatic synchronization)

parent 652b0ba9
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -7,6 +7,7 @@
    <!-- normal permissions -->
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.READ_SYNC_SETTINGS"/>
    <uses-permission android:name="android.permission.READ_SYNC_STATS"/>
@@ -49,6 +50,7 @@
        tools:ignore="UnusedAttribute">

        <service android:name=".DavService"/>
        <service android:name=".ForegroundService"/>

        <activity android:name=".ui.intro.IntroActivity" android:theme="@style/AppTheme.NoActionBar" />
        <activity
+5 −4
Original line number Diff line number Diff line
@@ -20,10 +20,8 @@ import at.bitfire.davdroid.settings.AccountSettings
import at.bitfire.davdroid.ui.DebugInfoActivity
import at.bitfire.davdroid.ui.NotificationUtils
import at.bitfire.davdroid.ui.UiUtils
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.util.logging.Level
import kotlin.concurrent.thread
import kotlin.system.exitProcess

@Suppress("unused")
@@ -65,7 +63,7 @@ class App: Application(), Thread.UncaughtExceptionHandler {
        NotificationUtils.createChannels(this)

        // don't block UI for some background checks
        CoroutineScope(Dispatchers.Default).launch {
        thread {
            // create/update app shortcuts
            UiUtils.updateShortcuts(this@App)

@@ -77,6 +75,9 @@ class App: Application(), Thread.UncaughtExceptionHandler {

            // check/repair sync intervals
            AccountSettings.repairSyncIntervals(this@App)

            // foreground service (possible workaround for devices which prevent DAVx5 from being started)
            startService(Intent(ForegroundService.ACTION_FOREGROUND, null, this, ForegroundService::class.java))
        }
    }

+2 −1
Original line number Diff line number Diff line
@@ -40,6 +40,7 @@ import kotlin.collections.*
class DavService: IntentService("DavService") {

    companion object {

        const val ACTION_REFRESH_COLLECTIONS = "refreshCollections"
        const val EXTRA_DAV_SERVICE_ID = "davServiceID"

@@ -72,7 +73,6 @@ class DavService: IntentService("DavService") {
     */
    private val refreshingStatusListeners = Collections.synchronizedList(LinkedList<WeakReference<RefreshingStatusListener>>())


    @WorkerThread
    override fun onHandleIntent(intent: Intent?) {
        if (intent == null)
@@ -100,6 +100,7 @@ class DavService: IntentService("DavService") {
                )
                forceSync(authority, account)
            }

        }
    }

+48 −0
Original line number Diff line number Diff line
package at.bitfire.davdroid

import android.app.PendingIntent
import android.app.Service
import android.content.Intent
import androidx.core.app.NotificationCompat
import at.bitfire.davdroid.settings.Settings
import at.bitfire.davdroid.settings.SettingsManager
import at.bitfire.davdroid.ui.AppSettingsActivity
import at.bitfire.davdroid.ui.NotificationUtils

class ForegroundService : Service() {

    companion object {

        /**
         * Starts/stops a foreground service, according to the app setting [Settings.FOREGROUND_SERVICE].
         */
        const val ACTION_FOREGROUND = "foreground"

    }


    override fun onBind(intent: Intent?) = null

    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
        val settings = SettingsManager.getInstance(this)
        val foreground = settings.getBooleanOrNull(Settings.FOREGROUND_SERVICE) == true

        if (foreground) {
            val settingsIntent = Intent(this, AppSettingsActivity::class.java).apply {
                putExtra(AppSettingsActivity.EXTRA_SCROLL_TO, Settings.FOREGROUND_SERVICE)
            }
            val builder = NotificationCompat.Builder(this, NotificationUtils.CHANNEL_STATUS)
                    .setSmallIcon(R.drawable.ic_foreground_notify)
                    .setContentTitle(getString(R.string.foreground_service_notify_title))
                    .setContentText(getString(R.string.foreground_service_notify_text))
                    .setStyle(NotificationCompat.BigTextStyle())
                    .setContentIntent(PendingIntent.getActivity(this, 0, settingsIntent, PendingIntent.FLAG_UPDATE_CURRENT))
            startForeground(NotificationUtils.NOTIFY_FOREGROUND, builder.build())
            return START_STICKY
        } else {
            stopForeground(true)
            return START_NOT_STICKY
        }
    }

}
 No newline at end of file
+2 −0
Original line number Diff line number Diff line
@@ -2,6 +2,8 @@ package at.bitfire.davdroid.settings

object Settings {

    const val FOREGROUND_SERVICE = "foreground_service"

    const val DISTRUST_SYSTEM_CERTIFICATES = "distrust_system_certs"

    const val OVERRIDE_PROXY = "override_proxy"
Loading