2
0
mirror of https://github.com/KDE/kdeconnect-android synced 2025-08-30 13:47:41 +00:00

Migrate LifecycleHelper to Kotlin

This commit is contained in:
TPJ Schikhof
2024-10-03 03:14:21 +00:00
committed by Albert Vaca Cintora
parent 8cb9a1809d
commit b856bdbb0b
2 changed files with 33 additions and 43 deletions

View File

@@ -1,43 +0,0 @@
/*
* SPDX-FileCopyrightText: 2020 Erik Duisters <e.duisters1@gmail.com>
*
* 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);
}
}

View File

@@ -0,0 +1,33 @@
/*
* SPDX-FileCopyrightText: 2020 Erik Duisters <e.duisters1@gmail.com>
*
* 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
}
}
}