session-android/src/org/thoughtcrime/securesms/util/ObservingLiveData.java
Greyson Parrelli 2a644437fb Add sticker support.
No sticker packs are available for use yet, but we now have the
latent ability to send and receive.
2019-05-30 01:08:01 -04:00

46 lines
1.1 KiB
Java

package org.thoughtcrime.securesms.util;
import android.arch.lifecycle.MutableLiveData;
import android.database.ContentObserver;
import android.support.annotation.NonNull;
import org.thoughtcrime.securesms.database.ObservableContent;
import java.io.Closeable;
/**
* Implementation of {@link android.arch.lifecycle.LiveData} that will handle closing the contained
* {@link Closeable} when the value changes.
*/
public class ObservingLiveData<E extends ObservableContent> extends MutableLiveData<E> {
private ContentObserver observer;
@Override
public void setValue(E value) {
E previous = getValue();
if (previous != null) {
previous.unregisterContentObserver(observer);
Util.close(previous);
}
value.registerContentObserver(observer);
super.setValue(value);
}
public void close() {
E value = getValue();
if (value != null) {
value.unregisterContentObserver(observer);
Util.close(value);
}
}
public void registerContentObserver(@NonNull ContentObserver observer) {
this.observer = observer;
}
}