Skip to content
This repository has been archived by the owner on Oct 5, 2021. It is now read-only.

Enable location service availability stream on Android #1

Merged
merged 8 commits into from
Apr 24, 2019
Merged

Enable location service availability stream on Android #1

merged 8 commits into from
Apr 24, 2019

Conversation

Lootwig
Copy link

@Lootwig Lootwig commented Apr 20, 2019

✨ What kind of change does this PR introduce? (Bug fix, feature, docs update...)

Access to streamed location service enabled/disabled state.

πŸ’₯ Does this PR introduce a breaking change?

Nope.

πŸ› Recommendations for testing

Only Android side implemented.

@Lootwig
Copy link
Author

Lootwig commented Apr 20, 2019

A little confused about the project duplicate flutter-permission-handlers <-> flutter-permission-handler. But this is the project referenced in geolocator, so I thought I'd add the feature in here.

Copy link
Member

@mvanbeusekom mvanbeusekom left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Lootwig, thank you very much for this pull-request. Except from some minor remarks, it looks good. Please have a look at my comments.

}

@Override
public void onCancel(Object arguments) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we remove our self as a registered receiver from the active context here? And maybe clean up the eventSink (e.g. set to null)?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Setting the field to null now.
Unregistering the receiver did not make sense without moving the registry part as well (because there wouldn't be a chance to re-register again with the current implementation), so I moved that part too.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any more remarks here?

@mvanbeusekom
Copy link
Member

Regarding your confusion, you are right it is confusing. The idea is that we split up all permissions that are current packed into the flutter-permission-handler plugin into separate packages. This is because the current version gives a lot of problems on iOS, since we touch all SDKs people using the plugin need to add all permissions to their apps Info.plist (and even then we get reports of Apple rejecting the App).

So to solve this problem we will split up all permissions into their own packages. When this is finished we will stop support on the flutter-permission-handler.

@@ -169,12 +168,20 @@ public void onMethodCall(MethodCall call, Result result) {

@Override
public void onListen(Object arguments, EventSink events) {
mEventSink = events;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
getActiveContext().registerReceiver(mReceiver, mIntentFilter);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should save the context in a class member here to make sure we unsubscribe from the same context when we cancel listening to the stream. Depending on the state of the App it is possible that the getActiveContext returns a different value (e.g. depending if the App is active of in the background). Something like:

mContext = getActiveContext();
mContext.registerReceiver(mReceiver, mIntentFilter);

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't this introduce a potential memory leak? Sorry if this is a misinformed question, but I was under the impression keeping references to Context instances was bad practice.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right indeed, thank you for letting me know.

According to this article we should use the ApplicationContext which is a singleton so we will always have the same instance.

We can access the ApplicationContext through the mRegistrar.context() method (see also: https://docs.flutter.io/javadoc/io/flutter/plugin/common/PluginRegistry.Registrar.html).

So I think it would be good to replace getActiveContext() with mRegistrar.context()

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated the implementation accordingly!

}

@Override
public void onCancel(Object arguments) {
mEventSink = null;
if (mEventSink != null) {
getActiveContext().unregisterReceiver(mReceiver);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And here make sure we unsubscribe from the same context:

if (mEventSink != null && mContext != null) {
  mContext.unregisterReceiver(mReceiver);
  mContext = null;
  mEventSink = null;
}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Resolved by agreeing not to store the context?

Copy link
Member

@mvanbeusekom mvanbeusekom left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, thank you @Lootwig appreciate the effort you put in to help out!

@aytunch
Copy link

aytunch commented Mar 29, 2021

@mvanbeusekom and @Lootwig thanks for this PR. This is the only way we were able to recognize user opening the android status bar and toggling location services.
Afaik iOS has something similar to location services. But this PR is only for Android. Are there any plans on supporting iOS as well? For now, we are limiting this stream to only Android platform.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants