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:
parent
8cb9a1809d
commit
b856bdbb0b
@ -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);
|
||||
}
|
||||
}
|
33
src/org/kde/kdeconnect/Helpers/LifecycleHelper.kt
Normal file
33
src/org/kde/kdeconnect/Helpers/LifecycleHelper.kt
Normal 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
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user