501 lines
20 KiB
Java
501 lines
20 KiB
Java
package com.discord.utilities.persister;
|
|
|
|
import android.annotation.SuppressLint;
|
|
import android.app.ActivityManager;
|
|
import android.content.Context;
|
|
import c.d.b.a.a;
|
|
import com.discord.utilities.rx.ObservableExtensionsKt;
|
|
import com.discord.utilities.time.Clock;
|
|
import com.discord.utilities.time.TimeElapsed;
|
|
import com.esotericsoftware.kryo.Kryo;
|
|
import com.esotericsoftware.kryo.io.Input;
|
|
import com.esotericsoftware.kryo.io.Output;
|
|
import d0.t.n;
|
|
import d0.z.d.m;
|
|
import d0.z.d.o;
|
|
import j0.k.b;
|
|
import j0.l.e.j;
|
|
import java.io.File;
|
|
import java.io.FileInputStream;
|
|
import java.io.FileOutputStream;
|
|
import java.lang.ref.WeakReference;
|
|
import java.util.List;
|
|
import java.util.concurrent.CopyOnWriteArrayList;
|
|
import kotlin.Unit;
|
|
import kotlin.jvm.functions.Function0;
|
|
import kotlin.jvm.functions.Function1;
|
|
import kotlin.jvm.functions.Function3;
|
|
import kotlin.jvm.internal.DefaultConstructorMarker;
|
|
import rx.Observable;
|
|
import rx.subjects.BehaviorSubject;
|
|
import rx.subjects.SerializedSubject;
|
|
import rx.subjects.Subject;
|
|
/* compiled from: Persister.kt */
|
|
public final class Persister<T> {
|
|
public static final Companion Companion = new Companion(null);
|
|
private static Clock clock;
|
|
@SuppressLint({"StaticFieldLeak"})
|
|
private static Context context;
|
|
private static boolean initialized;
|
|
private static Function1<? super Kryo, Unit> kryoConfig = Persister$Companion$kryoConfig$1.INSTANCE;
|
|
private static final Persister$Companion$kryos$1 kryos = new Persister$Companion$kryos$1();
|
|
private static Function3<? super Integer, ? super String, ? super Exception, Unit> logger = Persister$Companion$logger$1.INSTANCE;
|
|
private static final List<WeakReference<Persister<?>>> preferences = new CopyOnWriteArrayList();
|
|
private static Preloader preferencesPreloader;
|
|
private final T defaultValue;
|
|
private final String key;
|
|
private T value;
|
|
private boolean valueDirty;
|
|
private Subject<T, T> valueSubject;
|
|
private boolean valueUnset = true;
|
|
|
|
/* compiled from: Persister.kt */
|
|
public static final class Companion {
|
|
private Companion() {
|
|
}
|
|
|
|
public /* synthetic */ Companion(DefaultConstructorMarker defaultConstructorMarker) {
|
|
this();
|
|
}
|
|
|
|
public static final /* synthetic */ long access$availableBytes(Companion companion) {
|
|
return companion.availableBytes();
|
|
}
|
|
|
|
public static final /* synthetic */ void access$persistAll(Companion companion) {
|
|
companion.persistAll();
|
|
}
|
|
|
|
private final long availableBytes() {
|
|
Context access$getContext$cp = Persister.access$getContext$cp();
|
|
if (access$getContext$cp == null) {
|
|
m.throwUninitializedPropertyAccessException("context");
|
|
}
|
|
Object systemService = access$getContext$cp.getSystemService("activity");
|
|
if (!(systemService instanceof ActivityManager)) {
|
|
systemService = null;
|
|
}
|
|
ActivityManager activityManager = (ActivityManager) systemService;
|
|
if (activityManager == null) {
|
|
return Long.MAX_VALUE;
|
|
}
|
|
ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo();
|
|
activityManager.getMemoryInfo(memoryInfo);
|
|
return memoryInfo.availMem;
|
|
}
|
|
|
|
private final void persistAll() {
|
|
for (T t : Persister.access$getPreferences$cp()) {
|
|
j jVar = new j(t.get());
|
|
m.checkNotNullExpressionValue(jVar, "Observable\n .just(weakPreference.get())");
|
|
ObservableExtensionsKt.appSubscribe$default(ObservableExtensionsKt.computationBuffered(jVar), Persister.class, (Context) null, (Function1) null, (Function1) null, (Function0) null, (Function0) null, Persister$Companion$persistAll$1$1.INSTANCE, 62, (Object) null);
|
|
}
|
|
}
|
|
|
|
public final Function1<Kryo, Unit> getKryoConfig() {
|
|
return Persister.access$getKryoConfig$cp();
|
|
}
|
|
|
|
public final Function3<Integer, String, Exception, Unit> getLogger() {
|
|
return Persister.access$getLogger$cp();
|
|
}
|
|
|
|
public final void init(Context context, Clock clock, Observable<Boolean> observable) {
|
|
m.checkNotNullParameter(context, "context");
|
|
m.checkNotNullParameter(clock, "clock");
|
|
m.checkNotNullParameter(observable, "persistenceStrategy");
|
|
if (!Persister.access$getInitialized$cp()) {
|
|
Persister.access$setInitialized$cp(true);
|
|
Persister.access$setContext$cp(context);
|
|
Persister.access$setClock$cp(clock);
|
|
Persister.access$setPreferencesPreloader$cp(new Preloader(clock));
|
|
Observable<Boolean> x2 = observable.x(Persister$Companion$init$1.INSTANCE);
|
|
m.checkNotNullExpressionValue(x2, "persistenceStrategy\n …er { persist -> persist }");
|
|
ObservableExtensionsKt.appSubscribe$default(ObservableExtensionsKt.computationBuffered(x2), Persister.class, (Context) null, (Function1) null, (Function1) null, (Function0) null, (Function0) null, Persister$Companion$init$2.INSTANCE, 62, (Object) null);
|
|
}
|
|
}
|
|
|
|
public final Observable<Boolean> isPreloaded() {
|
|
Preloader access$getPreferencesPreloader$cp = Persister.access$getPreferencesPreloader$cp();
|
|
if (access$getPreferencesPreloader$cp == null) {
|
|
m.throwUninitializedPropertyAccessException("preferencesPreloader");
|
|
}
|
|
return access$getPreferencesPreloader$cp.isPreloaded();
|
|
}
|
|
|
|
public final void reset() {
|
|
for (T t : Persister.access$getPreferences$cp()) {
|
|
Persister persister = (Persister) t.get();
|
|
if (persister != null) {
|
|
Persister.clear$default(persister, false, 1, null);
|
|
}
|
|
}
|
|
}
|
|
|
|
public final void setKryoConfig(Function1<? super Kryo, Unit> function1) {
|
|
m.checkNotNullParameter(function1, "<set-?>");
|
|
Persister.access$setKryoConfig$cp(function1);
|
|
}
|
|
|
|
public final void setLogger(Function3<? super Integer, ? super String, ? super Exception, Unit> function3) {
|
|
m.checkNotNullParameter(function3, "<set-?>");
|
|
Persister.access$setLogger$cp(function3);
|
|
}
|
|
}
|
|
|
|
/* compiled from: Persister.kt */
|
|
public static final class Preloader {
|
|
private final List<String> preloadCacheKeys = n.mutableListOf("STORE_USER_RELATIONSHIPS_V9", "STORE_CHANNELS_V26", "STORE_GUILDS_V34");
|
|
private final SerializedSubject<Persister<?>, Persister<?>> preloadSubject;
|
|
private final TimeElapsed preloadTime;
|
|
|
|
/* compiled from: Persister.kt */
|
|
/* renamed from: com.discord.utilities.persister.Persister$Preloader$1 reason: invalid class name */
|
|
public static final class AnonymousClass1<T, R> implements b<Persister<?>, Boolean> {
|
|
public static final AnonymousClass1 INSTANCE = new AnonymousClass1();
|
|
|
|
public final Boolean call(Persister<?> persister) {
|
|
return Boolean.valueOf(persister == null);
|
|
}
|
|
}
|
|
|
|
/* compiled from: Persister.kt */
|
|
/* renamed from: com.discord.utilities.persister.Persister$Preloader$2 reason: invalid class name */
|
|
public static final class AnonymousClass2 extends o implements Function1<Persister<?>, Unit> {
|
|
public final /* synthetic */ Preloader this$0;
|
|
|
|
/* JADX INFO: super call moved to the top of the method (can break code semantics) */
|
|
public AnonymousClass2(Preloader preloader) {
|
|
super(1);
|
|
this.this$0 = preloader;
|
|
}
|
|
|
|
/* Return type fixed from 'java.lang.Object' to match base method */
|
|
/* JADX DEBUG: Method arguments types fixed to match base method, original types: [java.lang.Object] */
|
|
@Override // kotlin.jvm.functions.Function1
|
|
public /* bridge */ /* synthetic */ Unit invoke(Persister<?> persister) {
|
|
invoke(persister);
|
|
return Unit.a;
|
|
}
|
|
|
|
public final void invoke(Persister<?> persister) {
|
|
if (persister != null) {
|
|
Preloader.access$handlePreload(this.this$0, persister);
|
|
}
|
|
}
|
|
}
|
|
|
|
public Preloader(Clock clock) {
|
|
m.checkNotNullParameter(clock, "clock");
|
|
this.preloadTime = new TimeElapsed(clock, 0, 2, null);
|
|
SerializedSubject<Persister<?>, Persister<?>> serializedSubject = new SerializedSubject<>(BehaviorSubject.k0());
|
|
this.preloadSubject = serializedSubject;
|
|
Observable<Persister<?>> b02 = serializedSubject.b0(AnonymousClass1.INSTANCE);
|
|
m.checkNotNullExpressionValue(b02, "preloadSubject\n .takeUntil { it == null }");
|
|
ObservableExtensionsKt.appSubscribe$default(ObservableExtensionsKt.computationBuffered(b02), Preloader.class, (Context) null, (Function1) null, (Function1) null, (Function0) null, (Function0) null, new AnonymousClass2(this), 62, (Object) null);
|
|
}
|
|
|
|
public static final /* synthetic */ void access$handlePreload(Preloader preloader, Persister persister) {
|
|
preloader.handlePreload(persister);
|
|
}
|
|
|
|
private final synchronized <T> void handlePreload(Persister<T> persister) {
|
|
if (this.preloadCacheKeys.contains(persister.getKey())) {
|
|
this.preloadCacheKeys.remove(persister.getKey());
|
|
persister.get();
|
|
}
|
|
if (this.preloadCacheKeys.isEmpty()) {
|
|
this.preloadSubject.j.onNext(null);
|
|
Function3<Integer, String, Exception, Unit> logger = Persister.Companion.getLogger();
|
|
logger.invoke(4, "Preloaded preferences in " + this.preloadTime.getSeconds() + " seconds.", null);
|
|
}
|
|
}
|
|
|
|
/* JADX DEBUG: Type inference failed for r0v2. Raw type applied. Possible types: rx.Observable<com.discord.utilities.persister.Persister<?>>, java.lang.Object, rx.Observable<java.lang.Boolean> */
|
|
public final Observable<Boolean> isPreloaded() {
|
|
Observable r = this.preloadSubject.F(Persister$Preloader$isPreloaded$1.INSTANCE).r();
|
|
m.checkNotNullExpressionValue(r, "preloadSubject\n … .distinctUntilChanged()");
|
|
return r;
|
|
}
|
|
|
|
public final synchronized <T> void preload(Persister<T> persister) {
|
|
m.checkNotNullParameter(persister, "preference");
|
|
if (this.preloadCacheKeys.contains(persister.getKey())) {
|
|
this.preloadSubject.j.onNext(persister);
|
|
}
|
|
}
|
|
}
|
|
|
|
public Persister(String str, T t) {
|
|
m.checkNotNullParameter(str, "key");
|
|
m.checkNotNullParameter(t, "defaultValue");
|
|
this.key = str;
|
|
this.defaultValue = t;
|
|
this.value = t;
|
|
preferences.add(new WeakReference<>(this));
|
|
Preloader preloader = preferencesPreloader;
|
|
if (preloader == null) {
|
|
m.throwUninitializedPropertyAccessException("preferencesPreloader");
|
|
}
|
|
preloader.preload(this);
|
|
}
|
|
|
|
public static final /* synthetic */ Clock access$getClock$cp() {
|
|
return clock;
|
|
}
|
|
|
|
public static final /* synthetic */ Context access$getContext$cp() {
|
|
return context;
|
|
}
|
|
|
|
public static final /* synthetic */ boolean access$getInitialized$cp() {
|
|
return initialized;
|
|
}
|
|
|
|
public static final /* synthetic */ Function1 access$getKryoConfig$cp() {
|
|
return kryoConfig;
|
|
}
|
|
|
|
public static final /* synthetic */ Function3 access$getLogger$cp() {
|
|
return logger;
|
|
}
|
|
|
|
public static final /* synthetic */ List access$getPreferences$cp() {
|
|
return preferences;
|
|
}
|
|
|
|
public static final /* synthetic */ Preloader access$getPreferencesPreloader$cp() {
|
|
return preferencesPreloader;
|
|
}
|
|
|
|
public static final /* synthetic */ void access$persist(Persister persister) {
|
|
persister.persist();
|
|
}
|
|
|
|
public static final /* synthetic */ void access$setClock$cp(Clock clock2) {
|
|
clock = clock2;
|
|
}
|
|
|
|
public static final /* synthetic */ void access$setContext$cp(Context context2) {
|
|
context = context2;
|
|
}
|
|
|
|
public static final /* synthetic */ void access$setInitialized$cp(boolean z2) {
|
|
initialized = z2;
|
|
}
|
|
|
|
public static final /* synthetic */ void access$setKryoConfig$cp(Function1 function1) {
|
|
kryoConfig = function1;
|
|
}
|
|
|
|
public static final /* synthetic */ void access$setLogger$cp(Function3 function3) {
|
|
logger = function3;
|
|
}
|
|
|
|
public static final /* synthetic */ void access$setPreferencesPreloader$cp(Preloader preloader) {
|
|
preferencesPreloader = preloader;
|
|
}
|
|
|
|
public static /* synthetic */ Object clear$default(Persister persister, boolean z2, int i, Object obj) {
|
|
if ((i & 1) != 0) {
|
|
z2 = true;
|
|
}
|
|
return persister.clear(z2);
|
|
}
|
|
|
|
public static /* synthetic */ Object getAndSet$default(Persister persister, boolean z2, Function1 function1, int i, Object obj) {
|
|
if ((i & 1) != 0) {
|
|
z2 = false;
|
|
}
|
|
return persister.getAndSet(z2, function1);
|
|
}
|
|
|
|
private final File getFileInput() {
|
|
StringBuilder sb = new StringBuilder();
|
|
Context context2 = context;
|
|
if (context2 == null) {
|
|
m.throwUninitializedPropertyAccessException("context");
|
|
}
|
|
sb.append(context2.getFilesDir());
|
|
sb.append('/');
|
|
sb.append(this.key);
|
|
return new File(sb.toString());
|
|
}
|
|
|
|
private final Input getFileInputStream() {
|
|
File fileInput = getFileInput();
|
|
if (!fileInput.exists() || fileInput.length() >= Companion.access$availableBytes(Companion)) {
|
|
return null;
|
|
}
|
|
return new Input(new FileInputStream(fileInput));
|
|
}
|
|
|
|
private final FileOutputStream getFileOutput() {
|
|
Context context2 = context;
|
|
if (context2 == null) {
|
|
m.throwUninitializedPropertyAccessException("context");
|
|
}
|
|
FileOutputStream openFileOutput = context2.openFileOutput(this.key, 0);
|
|
m.checkNotNullExpressionValue(openFileOutput, "context.openFileOutput(key, Context.MODE_PRIVATE)");
|
|
return openFileOutput;
|
|
}
|
|
|
|
/* JADX WARNING: Code restructure failed: missing block: B:20:0x00a6, code lost:
|
|
r3 = move-exception;
|
|
*/
|
|
/* JADX WARNING: Code restructure failed: missing block: B:21:0x00a7, code lost:
|
|
d0.y.b.closeFinally(r1, r2);
|
|
*/
|
|
/* JADX WARNING: Code restructure failed: missing block: B:22:0x00aa, code lost:
|
|
throw r3;
|
|
*/
|
|
private final T getFileValue() {
|
|
T t;
|
|
try {
|
|
Input fileInputStream = getFileInputStream();
|
|
if (fileInputStream != null) {
|
|
Kryo kryo = kryos.get();
|
|
if (kryo != null) {
|
|
Object readClassAndObject = kryo.readClassAndObject(fileInputStream);
|
|
t = !(readClassAndObject instanceof Object) ? null : (T) readClassAndObject;
|
|
if (t == null) {
|
|
t = this.value;
|
|
StringBuilder sb = new StringBuilder();
|
|
sb.append("Found " + readClassAndObject.getClass() + " for " + this.key + ", ");
|
|
StringBuilder sb2 = new StringBuilder();
|
|
sb2.append("but expecting ");
|
|
sb2.append(t.getClass());
|
|
sb2.append('\"');
|
|
sb.append(sb2.toString());
|
|
String sb3 = sb.toString();
|
|
m.checkNotNullExpressionValue(sb3, "StringBuilder()\n … .toString()");
|
|
Function3<? super Integer, ? super String, ? super Exception, Unit> function3 = logger;
|
|
function3.invoke(6, "Unable to cast deserialized preference " + this.key + '.', new Exception(sb3));
|
|
}
|
|
} else {
|
|
t = this.value;
|
|
}
|
|
d0.y.b.closeFinally(fileInputStream, null);
|
|
if (t != null) {
|
|
return t;
|
|
}
|
|
}
|
|
return this.value;
|
|
} catch (Exception e) {
|
|
Function3<? super Integer, ? super String, ? super Exception, Unit> function32 = logger;
|
|
StringBuilder L = a.L("Unable to deserialize preference ");
|
|
L.append(this.key);
|
|
L.append('.');
|
|
function32.invoke(5, L.toString(), new Exception(this.key, e));
|
|
return this.value;
|
|
}
|
|
}
|
|
|
|
private static /* synthetic */ void getFileValue$annotations() {
|
|
}
|
|
|
|
public static final Observable<Boolean> isPreloaded() {
|
|
return Companion.isPreloaded();
|
|
}
|
|
|
|
/* JADX WARNING: Code restructure failed: missing block: B:16:0x0027, code lost:
|
|
r2 = move-exception;
|
|
*/
|
|
/* JADX WARNING: Code restructure failed: missing block: B:17:0x0028, code lost:
|
|
d0.y.b.closeFinally(r0, r1);
|
|
*/
|
|
/* JADX WARNING: Code restructure failed: missing block: B:18:0x002b, code lost:
|
|
throw r2;
|
|
*/
|
|
private final synchronized void persist() {
|
|
if (this.valueDirty) {
|
|
this.valueDirty = false;
|
|
try {
|
|
Output output = new Output(getFileOutput());
|
|
Kryo kryo = kryos.get();
|
|
if (kryo != null) {
|
|
kryo.writeClassAndObject(output, this.value);
|
|
}
|
|
d0.y.b.closeFinally(output, null);
|
|
} catch (Exception e) {
|
|
Function3<? super Integer, ? super String, ? super Exception, Unit> function3 = logger;
|
|
function3.invoke(5, "Unable to serialize preference " + this.key + '.', new Exception(this.key, e));
|
|
}
|
|
}
|
|
}
|
|
|
|
public static final void reset() {
|
|
Companion.reset();
|
|
}
|
|
|
|
public static /* synthetic */ Object set$default(Persister persister, Object obj, boolean z2, int i, Object obj2) {
|
|
if ((i & 2) != 0) {
|
|
z2 = false;
|
|
}
|
|
return persister.set(obj, z2);
|
|
}
|
|
|
|
public final T clear() {
|
|
return (T) clear$default(this, false, 1, null);
|
|
}
|
|
|
|
public final T clear(boolean z2) {
|
|
return set(this.defaultValue, z2);
|
|
}
|
|
|
|
public final synchronized T get() {
|
|
if (this.valueUnset) {
|
|
this.valueUnset = false;
|
|
this.value = getFileValue();
|
|
}
|
|
return this.value;
|
|
}
|
|
|
|
public final T getAndSet(Function1<? super T, ? extends T> function1) {
|
|
return (T) getAndSet$default(this, false, function1, 1, null);
|
|
}
|
|
|
|
/* JADX DEBUG: Multi-variable search result rejected for r1v0, resolved type: com.discord.utilities.persister.Persister<T> */
|
|
/* JADX WARN: Multi-variable type inference failed */
|
|
public final synchronized T getAndSet(boolean z2, Function1<? super T, ? extends T> function1) {
|
|
m.checkNotNullParameter(function1, "setter");
|
|
return (T) set(function1.invoke((Object) get()), z2);
|
|
}
|
|
|
|
public final String getKey() {
|
|
return this.key;
|
|
}
|
|
|
|
public final synchronized Observable<T> getObservable() {
|
|
Subject<T, T> subject;
|
|
Persister$getObservable$1 persister$getObservable$1 = new Persister$getObservable$1(this);
|
|
subject = this.valueSubject;
|
|
if (subject == null) {
|
|
subject = persister$getObservable$1.mo1invoke();
|
|
this.valueSubject = subject;
|
|
}
|
|
return subject;
|
|
}
|
|
|
|
public final T set(T t) {
|
|
return (T) set$default(this, t, false, 2, null);
|
|
}
|
|
|
|
public final synchronized T set(T t, boolean z2) {
|
|
T t2;
|
|
m.checkNotNullParameter(t, "newValue");
|
|
this.valueDirty = true;
|
|
this.valueUnset = false;
|
|
t2 = this.value;
|
|
this.value = t;
|
|
Subject<T, T> subject = this.valueSubject;
|
|
if (subject != null) {
|
|
subject.onNext(t);
|
|
}
|
|
if (z2) {
|
|
persist();
|
|
}
|
|
return t2;
|
|
}
|
|
}
|