From 523c8817463d8098232c846fcc52a4ff2b169863 Mon Sep 17 00:00:00 2001 From: Arseniy Borisov Date: Tue, 31 Jan 2017 18:03:59 +0300 Subject: [PATCH] follow comments --- build.gradle | 2 +- .../utils/audio/HeadsetStateObserver.java | 60 ++++++++++--------- 2 files changed, 32 insertions(+), 30 deletions(-) diff --git a/build.gradle b/build.gradle index b134c60..00f7e22 100644 --- a/build.gradle +++ b/build.gradle @@ -11,7 +11,7 @@ android { } defaultConfig { - minSdkVersion 11 + minSdkVersion 9 targetSdkVersion 24 } } diff --git a/src/main/java/ru/touchin/roboswag/components/utils/audio/HeadsetStateObserver.java b/src/main/java/ru/touchin/roboswag/components/utils/audio/HeadsetStateObserver.java index 69b4b71..c1928e5 100644 --- a/src/main/java/ru/touchin/roboswag/components/utils/audio/HeadsetStateObserver.java +++ b/src/main/java/ru/touchin/roboswag/components/utils/audio/HeadsetStateObserver.java @@ -28,12 +28,12 @@ import android.media.AudioManager; import android.support.annotation.NonNull; import rx.Observable; -import rx.subjects.PublishSubject; +import rx.subjects.BehaviorSubject; /** * Created by Gavriil Sitnikov on 02/11/2015. - * Simple observer of wired and wireless (bluetooth A2DP) headsets state (plugged in or not). - *
You require android.permission.BLUETOOTH if want to observe wireless headset state + * Simple observer of wired or wireless (bluetooth A2DP) headsets state (plugged in or not). + *
You require android.permission.BLUETOOTH and API level >= 11 if want to observe wireless headset state */ public final class HeadsetStateObserver { @@ -46,32 +46,25 @@ public final class HeadsetStateObserver { audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); isPluggedInObservable = Observable .create(subscriber -> { - subscriber.onNext(new IsPluggedInReceiver()); + subscriber.onNext(new IsPluggedInReceiver(audioManager)); subscriber.onCompleted(); }) - .switchMap(isPluggedInReceiver -> Observable - .just(isPluggedIn()) - .concatWith(isPluggedInReceiver.isPluggedInChangedEvent - .doOnSubscribe(() -> { - final IntentFilter headsetStateIntentFilter = new IntentFilter(Intent.ACTION_HEADSET_PLUG); - headsetStateIntentFilter.addAction(BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED); - context.registerReceiver(isPluggedInReceiver, headsetStateIntentFilter); - }) - .doOnUnsubscribe(() -> context.unregisterReceiver(isPluggedInReceiver)))) + .switchMap(isPluggedInReceiver -> Observable.combineLatest(isPluggedInReceiver.isWiredPluggedInChangedEvent, + isPluggedInReceiver.isWirelessPluggedInChangedEvent, + (isWiredPluggedIn, isWirelessPluggedIn) -> isWiredPluggedIn || isWirelessPluggedIn) + .distinctUntilChanged() + .doOnSubscribe(() -> { + final IntentFilter headsetStateIntentFilter = new IntentFilter(Intent.ACTION_HEADSET_PLUG); + if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) { + headsetStateIntentFilter.addAction(BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED); + } + context.registerReceiver(isPluggedInReceiver, headsetStateIntentFilter); + }) + .doOnUnsubscribe(() -> context.unregisterReceiver(isPluggedInReceiver))) .replay(1) .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. * @@ -85,21 +78,30 @@ public final class HeadsetStateObserver { private static class IsPluggedInReceiver extends BroadcastReceiver { @NonNull - private final PublishSubject isPluggedInChangedEvent = PublishSubject.create(); + private final BehaviorSubject isWiredPluggedInChangedEvent; + @NonNull + private final BehaviorSubject isWirelessPluggedInChangedEvent; + + @SuppressWarnings("deprecation") + public IsPluggedInReceiver(@NonNull final AudioManager audioManager) { + isWiredPluggedInChangedEvent = BehaviorSubject.create(audioManager.isWiredHeadsetOn()); + isWirelessPluggedInChangedEvent = BehaviorSubject.create(audioManager.isBluetoothA2dpOn()); + } @Override public void onReceive(final Context context, final Intent intent) { - if (Intent.ACTION_HEADSET_PLUG.equals(intent.getAction())) { - isPluggedInChangedEvent.onNext(intent.getIntExtra("state", 0) != 0); + if (Intent.ACTION_HEADSET_PLUG.equals(intent.getAction()) && !isInitialStickyBroadcast()) { + 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); switch (bluetoothState) { case BluetoothA2dp.STATE_DISCONNECTED: - isPluggedInChangedEvent.onNext(false); + isWirelessPluggedInChangedEvent.onNext(false); break; case BluetoothA2dp.STATE_CONNECTED: - isPluggedInChangedEvent.onNext(true); + isWirelessPluggedInChangedEvent.onNext(true); break; default: break;