mirror of
https://github.com/signalapp/Signal-Android.git
synced 2026-04-21 09:20:19 +01:00
Initialize AppDependencies if needed in AvatarProvider.
This commit is contained in:
committed by
Cody Henthorne
parent
9261c34213
commit
6112ee9bd3
@@ -1,92 +0,0 @@
|
||||
package org.thoughtcrime.securesms.util;
|
||||
|
||||
import androidx.annotation.AnyThread;
|
||||
import androidx.annotation.MainThread;
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.lifecycle.DefaultLifecycleObserver;
|
||||
import androidx.lifecycle.LifecycleOwner;
|
||||
import androidx.lifecycle.ProcessLifecycleOwner;
|
||||
|
||||
import org.signal.core.util.ThreadUtil;
|
||||
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.CopyOnWriteArraySet;
|
||||
|
||||
/**
|
||||
* A wrapper around {@link ProcessLifecycleOwner} that allows for safely adding/removing observers
|
||||
* on multiple threads.
|
||||
*/
|
||||
public class AppForegroundObserver {
|
||||
|
||||
private final Set<Listener> listeners = new CopyOnWriteArraySet<>();
|
||||
|
||||
private volatile Boolean isForegrounded = null;
|
||||
|
||||
@MainThread
|
||||
public void begin() {
|
||||
ThreadUtil.assertMainThread();
|
||||
|
||||
ProcessLifecycleOwner.get().getLifecycle().addObserver(new DefaultLifecycleObserver() {
|
||||
@Override
|
||||
public void onStart(@NonNull LifecycleOwner owner) {
|
||||
onForeground();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStop(@NonNull LifecycleOwner owner) {
|
||||
onBackground();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a listener to be notified of when the app moves between the background and the foreground.
|
||||
* To mimic the behavior of subscribing to {@link ProcessLifecycleOwner}, this listener will be
|
||||
* immediately notified of the foreground state if we've experienced a foreground/background event
|
||||
* already.
|
||||
*/
|
||||
@AnyThread
|
||||
public void addListener(@NonNull Listener listener) {
|
||||
listeners.add(listener);
|
||||
|
||||
if (isForegrounded != null) {
|
||||
if (isForegrounded) {
|
||||
listener.onForeground();
|
||||
} else {
|
||||
listener.onBackground();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@AnyThread
|
||||
public void removeListener(@NonNull Listener listener) {
|
||||
listeners.remove(listener);
|
||||
}
|
||||
|
||||
public boolean isForegrounded() {
|
||||
return isForegrounded != null && isForegrounded;
|
||||
}
|
||||
|
||||
@MainThread
|
||||
private void onForeground() {
|
||||
isForegrounded = true;
|
||||
|
||||
for (Listener listener : listeners) {
|
||||
listener.onForeground();
|
||||
}
|
||||
}
|
||||
|
||||
@MainThread
|
||||
private void onBackground() {
|
||||
isForegrounded = false;
|
||||
|
||||
for (Listener listener : listeners) {
|
||||
listener.onBackground();
|
||||
}
|
||||
}
|
||||
|
||||
public interface Listener {
|
||||
default void onForeground() {}
|
||||
default void onBackground() {}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
package org.thoughtcrime.securesms.util
|
||||
|
||||
import androidx.annotation.AnyThread
|
||||
import androidx.annotation.MainThread
|
||||
import androidx.lifecycle.DefaultLifecycleObserver
|
||||
import androidx.lifecycle.LifecycleOwner
|
||||
import androidx.lifecycle.ProcessLifecycleOwner
|
||||
import org.signal.core.util.ThreadUtil
|
||||
import java.util.concurrent.CopyOnWriteArraySet
|
||||
import kotlin.concurrent.Volatile
|
||||
|
||||
/**
|
||||
* A wrapper around [ProcessLifecycleOwner] that allows for safely adding/removing observers
|
||||
* on multiple threads.
|
||||
*/
|
||||
object AppForegroundObserver {
|
||||
private val listeners: MutableSet<Listener> = CopyOnWriteArraySet()
|
||||
|
||||
@Volatile
|
||||
private var isInitialized: Boolean = false
|
||||
|
||||
@Volatile
|
||||
private var isForegrounded: Boolean = false
|
||||
|
||||
@MainThread
|
||||
@JvmStatic
|
||||
fun begin() {
|
||||
ThreadUtil.assertMainThread()
|
||||
|
||||
ProcessLifecycleOwner.get().lifecycle.addObserver(object : DefaultLifecycleObserver {
|
||||
override fun onStart(owner: LifecycleOwner) {
|
||||
onForeground()
|
||||
}
|
||||
|
||||
override fun onStop(owner: LifecycleOwner) {
|
||||
onBackground()
|
||||
}
|
||||
})
|
||||
|
||||
isInitialized = true
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a listener to be notified of when the app moves between the background and the foreground.
|
||||
* To mimic the behavior of subscribing to [ProcessLifecycleOwner], this listener will be
|
||||
* immediately notified of the foreground state if we've experienced a foreground/background event
|
||||
* already.
|
||||
*/
|
||||
@AnyThread
|
||||
@JvmStatic
|
||||
fun addListener(listener: Listener) {
|
||||
listeners.add(listener)
|
||||
|
||||
if (isInitialized) {
|
||||
if (isForegrounded) {
|
||||
listener.onForeground()
|
||||
} else {
|
||||
listener.onBackground()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@AnyThread
|
||||
@JvmStatic
|
||||
fun removeListener(listener: Listener) {
|
||||
listeners.remove(listener)
|
||||
}
|
||||
|
||||
@JvmStatic
|
||||
fun isForegrounded(): Boolean {
|
||||
return isInitialized && isForegrounded
|
||||
}
|
||||
|
||||
@MainThread
|
||||
private fun onForeground() {
|
||||
isForegrounded = true
|
||||
|
||||
for (listener in listeners) {
|
||||
listener.onForeground()
|
||||
}
|
||||
}
|
||||
|
||||
@MainThread
|
||||
private fun onBackground() {
|
||||
isForegrounded = false
|
||||
|
||||
for (listener in listeners) {
|
||||
listener.onBackground()
|
||||
}
|
||||
}
|
||||
|
||||
interface Listener {
|
||||
fun onForeground() {}
|
||||
fun onBackground() {}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user