-
Notifications
You must be signed in to change notification settings - Fork 583
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use Observable.create()
to construct LocationServicesOkObservable
#438
Merged
dariuszseweryn
merged 3 commits into
dariuszseweryn:master
from
thuytrinh:thuy/fix-IllegalStateException
Jun 8, 2018
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 0 additions & 56 deletions
56
.../main/java/com/polidea/rxandroidble2/internal/util/LocationServicesOkObservableApi23.java
This file was deleted.
Oops, something went wrong.
58 changes: 58 additions & 0 deletions
58
...ava/com/polidea/rxandroidble2/internal/util/LocationServicesOkObservableApi23Factory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package com.polidea.rxandroidble2.internal.util; | ||
|
||
import android.annotation.TargetApi; | ||
import android.content.BroadcastReceiver; | ||
import android.content.Context; | ||
import android.content.Intent; | ||
import android.content.IntentFilter; | ||
import android.location.LocationManager; | ||
import android.os.Build; | ||
|
||
import bleshadow.javax.inject.Inject; | ||
import io.reactivex.Observable; | ||
import io.reactivex.ObservableEmitter; | ||
import io.reactivex.ObservableOnSubscribe; | ||
import io.reactivex.functions.Cancellable; | ||
|
||
@TargetApi(Build.VERSION_CODES.KITKAT) | ||
public class LocationServicesOkObservableApi23Factory { | ||
private final Context context; | ||
private final LocationServicesStatus locationServicesStatus; | ||
|
||
@Inject | ||
LocationServicesOkObservableApi23Factory( | ||
final Context context, | ||
final LocationServicesStatus locationServicesStatus) { | ||
this.context = context; | ||
this.locationServicesStatus = locationServicesStatus; | ||
} | ||
|
||
public Observable<Boolean> get() { | ||
return Observable.create(new ObservableOnSubscribe<Boolean>() { | ||
@Override | ||
public void subscribe(final ObservableEmitter<Boolean> emitter) throws Exception { | ||
final boolean initialValue = locationServicesStatus.isLocationProviderOk(); | ||
final BroadcastReceiver broadcastReceiver = new BroadcastReceiver() { | ||
@Override | ||
public void onReceive(Context context, Intent intent) { | ||
final boolean newValue = locationServicesStatus.isLocationProviderOk(); | ||
emitter.onNext(newValue); | ||
} | ||
}; | ||
emitter.setCancellable(new Cancellable() { | ||
@Override | ||
public void cancel() throws Exception { | ||
try { | ||
context.unregisterReceiver(broadcastReceiver); | ||
} catch (IllegalStateException ignored) { | ||
} | ||
} | ||
}); | ||
if (!emitter.isDisposed()) { | ||
context.registerReceiver(broadcastReceiver, new IntentFilter(LocationManager.MODE_CHANGED_ACTION)); | ||
emitter.onNext(initialValue); | ||
} | ||
} | ||
}).distinctUntilChanged(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We may end up leaking a context if this will get called before the
subscribe
finishesThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch @dariuszseweryn 👍 There may be a case like that. For example, considering the following execution flow:
if (!emitter.isDisposed()) {
. There's still at least a subscriber of thisObservable
, so thecontext.registerReceiver()
is about to be executed.Observable
anymore. The code insetCancellable()
gets invoked.context.registerReceiver()
gets executed. Thus the leak has just happened.I'm thinking about doing another check
if (!emitter.isDisposed())
right aftercontext.registerReceiver()
but it looks kinda weird. I never wrote such conditional flow before. Wdyt?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or another idea is to move the
setCancellable()
into theisDispose()
check. For example:Wdyt?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've changed the impl into below based on @akarnokd's suggestion link:
I think this impl is able to prevent the above leak from happening.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup, but we should get rid of the
try/catch
in theCancellable
as it could only hide potential issuesThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dariuszseweryn Good call 👍 Done.