package io.reactivex.internal.operators.observable; import io.reactivex.Observable; import io.reactivex.ObservableSource; import io.reactivex.Observer; import io.reactivex.disposables.Disposable; import io.reactivex.exceptions.CompositeException; import io.reactivex.exceptions.Exceptions; import io.reactivex.functions.Consumer; import io.reactivex.functions.Function; import io.reactivex.internal.disposables.DisposableHelper; import io.reactivex.internal.disposables.EmptyDisposable; import io.reactivex.internal.functions.ObjectHelper; import io.reactivex.plugins.RxJavaPlugins; import java.util.concurrent.Callable; import java.util.concurrent.atomic.AtomicBoolean; /* loaded from: classes2.dex */ public final class ObservableUsing extends Observable { final Callable a; final Function> b; final Consumer c; final boolean d; static final class UsingObserver extends AtomicBoolean implements Observer, Disposable { final Observer a; final D b; final Consumer c; final boolean d; Disposable e; UsingObserver(Observer observer, D d, Consumer consumer, boolean z) { this.a = observer; this.b = d; this.c = consumer; this.d = z; } void a() { if (compareAndSet(false, true)) { try { this.c.accept(this.b); } catch (Throwable th) { Exceptions.b(th); RxJavaPlugins.b(th); } } } @Override // io.reactivex.disposables.Disposable public void dispose() { a(); this.e.dispose(); } @Override // io.reactivex.Observer public void onComplete() { if (!this.d) { this.a.onComplete(); this.e.dispose(); a(); return; } if (compareAndSet(false, true)) { try { this.c.accept(this.b); } catch (Throwable th) { Exceptions.b(th); this.a.onError(th); return; } } this.e.dispose(); this.a.onComplete(); } @Override // io.reactivex.Observer public void onError(Throwable th) { if (!this.d) { this.a.onError(th); this.e.dispose(); a(); return; } if (compareAndSet(false, true)) { try { this.c.accept(this.b); } catch (Throwable th2) { Exceptions.b(th2); th = new CompositeException(th, th2); } } this.e.dispose(); this.a.onError(th); } @Override // io.reactivex.Observer public void onNext(T t) { this.a.onNext(t); } @Override // io.reactivex.Observer public void onSubscribe(Disposable disposable) { if (DisposableHelper.validate(this.e, disposable)) { this.e = disposable; this.a.onSubscribe(this); } } } public ObservableUsing(Callable callable, Function> function, Consumer consumer, boolean z) { this.a = callable; this.b = function; this.c = consumer; this.d = z; } @Override // io.reactivex.Observable public void subscribeActual(Observer observer) { try { D call = this.a.call(); try { ObservableSource apply = this.b.apply(call); ObjectHelper.a(apply, "The sourceSupplier returned a null ObservableSource"); apply.subscribe(new UsingObserver(observer, call, this.c, this.d)); } catch (Throwable th) { Exceptions.b(th); try { this.c.accept(call); EmptyDisposable.error(th, observer); } catch (Throwable th2) { Exceptions.b(th2); EmptyDisposable.error(new CompositeException(th, th2), observer); } } } catch (Throwable th3) { Exceptions.b(th3); EmptyDisposable.error(th3, observer); } } }