follow comments

This commit is contained in:
Arseniy Borisov 2017-01-31 18:03:59 +03:00
parent fa14b2f559
commit 523c881746
2 changed files with 32 additions and 30 deletions

View File

@ -11,7 +11,7 @@ android {
} }
defaultConfig { defaultConfig {
minSdkVersion 11 minSdkVersion 9
targetSdkVersion 24 targetSdkVersion 24
} }
} }

View File

@ -28,12 +28,12 @@ import android.media.AudioManager;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import rx.Observable; import rx.Observable;
import rx.subjects.PublishSubject; import rx.subjects.BehaviorSubject;
/** /**
* Created by Gavriil Sitnikov on 02/11/2015. * Created by Gavriil Sitnikov on 02/11/2015.
* Simple observer of wired and wireless (bluetooth A2DP) headsets state (plugged in or not). * Simple observer of wired or wireless (bluetooth A2DP) headsets state (plugged in or not).
* <br><font color="yellow"> You require android.permission.BLUETOOTH if want to observe wireless headset state </font> * <br><font color="yellow"> You require android.permission.BLUETOOTH and API level >= 11 if want to observe wireless headset state </font>
*/ */
public final class HeadsetStateObserver { public final class HeadsetStateObserver {
@ -46,32 +46,25 @@ public final class HeadsetStateObserver {
audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
isPluggedInObservable = Observable isPluggedInObservable = Observable
.<IsPluggedInReceiver>create(subscriber -> { .<IsPluggedInReceiver>create(subscriber -> {
subscriber.onNext(new IsPluggedInReceiver()); subscriber.onNext(new IsPluggedInReceiver(audioManager));
subscriber.onCompleted(); subscriber.onCompleted();
}) })
.switchMap(isPluggedInReceiver -> Observable .switchMap(isPluggedInReceiver -> Observable.combineLatest(isPluggedInReceiver.isWiredPluggedInChangedEvent,
.just(isPluggedIn()) isPluggedInReceiver.isWirelessPluggedInChangedEvent,
.concatWith(isPluggedInReceiver.isPluggedInChangedEvent (isWiredPluggedIn, isWirelessPluggedIn) -> isWiredPluggedIn || isWirelessPluggedIn)
.doOnSubscribe(() -> { .distinctUntilChanged()
final IntentFilter headsetStateIntentFilter = new IntentFilter(Intent.ACTION_HEADSET_PLUG); .doOnSubscribe(() -> {
headsetStateIntentFilter.addAction(BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED); final IntentFilter headsetStateIntentFilter = new IntentFilter(Intent.ACTION_HEADSET_PLUG);
context.registerReceiver(isPluggedInReceiver, headsetStateIntentFilter); if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) {
}) headsetStateIntentFilter.addAction(BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED);
.doOnUnsubscribe(() -> context.unregisterReceiver(isPluggedInReceiver)))) }
context.registerReceiver(isPluggedInReceiver, headsetStateIntentFilter);
})
.doOnUnsubscribe(() -> context.unregisterReceiver(isPluggedInReceiver)))
.replay(1) .replay(1)
.refCount(); .refCount();
} }
/**
* Returns if wired or wireless headset plugged in.
*
* @return True if headset is plugged in.
*/
@SuppressWarnings("deprecation")
public boolean isPluggedIn() {
return audioManager.isWiredHeadsetOn() || audioManager.isBluetoothA2dpOn();
}
/** /**
* Observes plugged in state of headset. * Observes plugged in state of headset.
* *
@ -85,21 +78,30 @@ public final class HeadsetStateObserver {
private static class IsPluggedInReceiver extends BroadcastReceiver { private static class IsPluggedInReceiver extends BroadcastReceiver {
@NonNull @NonNull
private final PublishSubject<Boolean> isPluggedInChangedEvent = PublishSubject.create(); private final BehaviorSubject<Boolean> isWiredPluggedInChangedEvent;
@NonNull
private final BehaviorSubject<Boolean> isWirelessPluggedInChangedEvent;
@SuppressWarnings("deprecation")
public IsPluggedInReceiver(@NonNull final AudioManager audioManager) {
isWiredPluggedInChangedEvent = BehaviorSubject.create(audioManager.isWiredHeadsetOn());
isWirelessPluggedInChangedEvent = BehaviorSubject.create(audioManager.isBluetoothA2dpOn());
}
@Override @Override
public void onReceive(final Context context, final Intent intent) { public void onReceive(final Context context, final Intent intent) {
if (Intent.ACTION_HEADSET_PLUG.equals(intent.getAction())) { if (Intent.ACTION_HEADSET_PLUG.equals(intent.getAction()) && !isInitialStickyBroadcast()) {
isPluggedInChangedEvent.onNext(intent.getIntExtra("state", 0) != 0); isWiredPluggedInChangedEvent.onNext(intent.getIntExtra("state", 0) != 0);
} }
if (BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED.equals(intent.getAction())) { if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB &&
BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED.equals(intent.getAction())) {
final int bluetoothState = intent.getIntExtra(BluetoothA2dp.EXTRA_STATE, BluetoothA2dp.STATE_DISCONNECTED); final int bluetoothState = intent.getIntExtra(BluetoothA2dp.EXTRA_STATE, BluetoothA2dp.STATE_DISCONNECTED);
switch (bluetoothState) { switch (bluetoothState) {
case BluetoothA2dp.STATE_DISCONNECTED: case BluetoothA2dp.STATE_DISCONNECTED:
isPluggedInChangedEvent.onNext(false); isWirelessPluggedInChangedEvent.onNext(false);
break; break;
case BluetoothA2dp.STATE_CONNECTED: case BluetoothA2dp.STATE_CONNECTED:
isPluggedInChangedEvent.onNext(true); isWirelessPluggedInChangedEvent.onNext(true);
break; break;
default: default:
break; break;