calling observer fixed

This commit is contained in:
Gavriil Sitnikov 2016-04-14 19:09:18 +03:00
parent 26c9067a7d
commit a840abe9d6
1 changed files with 15 additions and 12 deletions

View File

@ -19,13 +19,14 @@
package ru.touchin.roboswag.components.telephony;
import android.Manifest;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.RequiresPermission;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import rx.Observable;
import rx.subjects.BehaviorSubject;
/**
* Created by Gavriil Sitnikov on 02/11/2015.
@ -37,25 +38,27 @@ public final class IsCallingObserver {
return state != TelephonyManager.CALL_STATE_IDLE;
}
private final BehaviorSubject<Boolean> isCallingSubject = BehaviorSubject.create();
private final Observable<Boolean> isCallingObservable;
public IsCallingObserver(@NonNull final Context context) {
final TelephonyManager phoneStateManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
phoneStateManager.listen(new PhoneStateListener() {
@Override
public void onCallStateChanged(final int state, final String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
isCallingSubject.onNext(isCallingState(state));
}
}, PhoneStateListener.LISTEN_CALL_STATE);
isCallingSubject.onNext(isCallingState(phoneStateManager.getCallState()));
isCallingObservable = isCallingSubject
isCallingObservable = Observable
.<Boolean>create(subscriber -> {
final TelephonyManager phoneStateManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
phoneStateManager.listen(new PhoneStateListener() {
@Override
public void onCallStateChanged(final int state, final String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
subscriber.onNext(isCallingState(state));
}
}, PhoneStateListener.LISTEN_CALL_STATE);
subscriber.onNext(isCallingState(phoneStateManager.getCallState()));
})
.distinctUntilChanged()
.replay(1)
.refCount();
}
@RequiresPermission(Manifest.permission.READ_PHONE_STATE)
@NonNull
public Observable<Boolean> observeIsCalling() {
return isCallingObservable;