diff --git a/src/org/kde/kdeconnect/Helpers/LifecycleHelper.java b/src/org/kde/kdeconnect/Helpers/LifecycleHelper.java deleted file mode 100644 index fe768932..00000000 --- a/src/org/kde/kdeconnect/Helpers/LifecycleHelper.java +++ /dev/null @@ -1,43 +0,0 @@ -/* - * SPDX-FileCopyrightText: 2020 Erik Duisters - * - * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL -*/ - -package org.kde.kdeconnect.Helpers; - -import androidx.annotation.NonNull; -import androidx.lifecycle.DefaultLifecycleObserver; -import androidx.lifecycle.LifecycleOwner; -import androidx.lifecycle.ProcessLifecycleOwner; - -public class LifecycleHelper { - - private static class LifecycleObserver implements DefaultLifecycleObserver { - private boolean inForeground = false; - - @Override - public void onStart(@NonNull LifecycleOwner owner) { - inForeground = true; - } - - @Override - public void onStop(@NonNull LifecycleOwner owner) { - inForeground = false; - } - - boolean isInForeground() { - return inForeground; - } - } - - private final static LifecycleObserver foregroundTracker = new LifecycleObserver(); - - public static boolean isInForeground() { - return foregroundTracker.isInForeground(); - } - - public static void initializeObserver() { - ProcessLifecycleOwner.get().getLifecycle().addObserver(foregroundTracker); - } -} diff --git a/src/org/kde/kdeconnect/Helpers/LifecycleHelper.kt b/src/org/kde/kdeconnect/Helpers/LifecycleHelper.kt new file mode 100644 index 00000000..0091453c --- /dev/null +++ b/src/org/kde/kdeconnect/Helpers/LifecycleHelper.kt @@ -0,0 +1,33 @@ +/* + * SPDX-FileCopyrightText: 2020 Erik Duisters + * + * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL +*/ +package org.kde.kdeconnect.Helpers + +import androidx.lifecycle.DefaultLifecycleObserver +import androidx.lifecycle.LifecycleOwner +import androidx.lifecycle.ProcessLifecycleOwner + +object LifecycleHelper { + fun initializeObserver() { + ProcessLifecycleOwner.get().lifecycle.addObserver(foregroundTracker) + } + + @JvmStatic + val isInForeground: Boolean + get() = foregroundTracker.isInForeground + + private val foregroundTracker = object : DefaultLifecycleObserver { + var isInForeground: Boolean = false + private set + + override fun onStart(owner: LifecycleOwner) { + this.isInForeground = true + } + + override fun onStop(owner: LifecycleOwner) { + this.isInForeground = false + } + } +}