1
1
package com .baseflow .geolocator ;
2
2
3
+ import android .app .Activity ;
4
+ import android .content .Context ;
3
5
import androidx .annotation .NonNull ;
4
6
import androidx .annotation .Nullable ;
5
7
9
11
import io .flutter .embedding .engine .plugins .FlutterPlugin ;
10
12
import io .flutter .embedding .engine .plugins .activity .ActivityAware ;
11
13
import io .flutter .embedding .engine .plugins .activity .ActivityPluginBinding ;
14
+ import io .flutter .plugin .common .BinaryMessenger ;
12
15
import io .flutter .plugin .common .PluginRegistry .Registrar ;
13
16
14
17
/** GeolocatorPlugin */
@@ -51,34 +54,13 @@ public GeolocatorPlugin() {
51
54
public static void registerWith (Registrar registrar ) {
52
55
GeolocatorPlugin geolocatorPlugin = new GeolocatorPlugin ();
53
56
geolocatorPlugin .pluginRegistrar = registrar ;
54
- geolocatorPlugin .registerListeners ();
55
-
56
- MethodCallHandlerImpl methodCallHandler =
57
- new MethodCallHandlerImpl (
58
- geolocatorPlugin .permissionManager , geolocatorPlugin .geolocationManager );
59
- methodCallHandler .startListening (registrar .context (), registrar .messenger ());
60
- methodCallHandler .setActivity (registrar .activity ());
61
-
62
- PositionStreamHandlerImpl streamHandler = new PositionStreamHandlerImpl (geolocatorPlugin .geolocationManager );
63
- streamHandler .startListening (registrar .context (), registrar .messenger ());
64
- streamHandler .setActivity (registrar .activity ());
65
-
66
- NmeaStreamHandlerImpl nmeaStream = new NmeaStreamHandlerImpl (geolocatorPlugin .nmeaMessageManager );
67
- nmeaStream .startListening (registrar .context (), registrar .messenger ());
68
- nmeaStream .setActivity (registrar .activity ());
57
+ geolocatorPlugin .configureListeners (registrar .context (), registrar .messenger ());
58
+ geolocatorPlugin .setActivity (registrar .activity ());
69
59
}
70
60
71
61
@ Override
72
62
public void onAttachedToEngine (@ NonNull FlutterPluginBinding flutterPluginBinding ) {
73
- methodCallHandler = new MethodCallHandlerImpl (this .permissionManager , this .geolocationManager );
74
- methodCallHandler .startListening (
75
- flutterPluginBinding .getApplicationContext (), flutterPluginBinding .getBinaryMessenger ());
76
- positionStreamHandler = new PositionStreamHandlerImpl (this .geolocationManager );
77
- positionStreamHandler .startListening (
78
- flutterPluginBinding .getApplicationContext (), flutterPluginBinding .getBinaryMessenger ());
79
- nmeaStreamHandler = new NmeaStreamHandlerImpl (this .nmeaMessageManager );
80
- nmeaStreamHandler .startListening (
81
- flutterPluginBinding .getApplicationContext (), flutterPluginBinding .getBinaryMessenger ());
63
+ configureListeners (flutterPluginBinding .getApplicationContext (), flutterPluginBinding .getBinaryMessenger ());
82
64
}
83
65
84
66
@ Override
@@ -101,19 +83,8 @@ public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
101
83
102
84
@ Override
103
85
public void onAttachedToActivity (@ NonNull ActivityPluginBinding binding ) {
104
- if (methodCallHandler != null ) {
105
- methodCallHandler .setActivity (binding .getActivity ());
106
- }
107
- if (positionStreamHandler != null ) {
108
- positionStreamHandler .setActivity (binding .getActivity ());
109
- }
110
-
111
- if (nmeaStreamHandler != null ) {
112
- nmeaStreamHandler .setActivity (binding .getActivity ());
113
- }
114
-
115
86
this .pluginBinding = binding ;
116
- registerListeners ( );
87
+ setActivity ( binding . getActivity () );
117
88
}
118
89
119
90
@ Override
@@ -128,18 +99,38 @@ public void onReattachedToActivityForConfigChanges(@NonNull ActivityPluginBindin
128
99
129
100
@ Override
130
101
public void onDetachedFromActivity () {
102
+ setActivity (null );
103
+ }
104
+
105
+
106
+ void configureListeners (Context applicationContext , BinaryMessenger messenger ) {
107
+ methodCallHandler =
108
+ new MethodCallHandlerImpl (permissionManager , geolocationManager );
109
+ methodCallHandler .startListening (applicationContext , messenger );
110
+
111
+ positionStreamHandler = new PositionStreamHandlerImpl (geolocationManager );
112
+ positionStreamHandler .startListening (applicationContext , messenger );
113
+
114
+ nmeaStreamHandler = new NmeaStreamHandlerImpl (nmeaMessageManager );
115
+ nmeaStreamHandler .startListening (applicationContext , messenger );
116
+ }
117
+
118
+ void setActivity (@ Nullable Activity activity ) {
131
119
if (methodCallHandler != null ) {
132
- methodCallHandler .setActivity (null );
120
+ methodCallHandler .setActivity (activity );
133
121
}
134
122
if (positionStreamHandler != null ) {
135
- positionStreamHandler .setActivity (null );
123
+ positionStreamHandler .setActivity (activity );
136
124
}
137
-
138
125
if (nmeaStreamHandler != null ) {
139
- nmeaStreamHandler .setActivity (null );
126
+ nmeaStreamHandler .setActivity (activity );
140
127
}
141
128
142
- deregisterListeners ();
129
+ if (activity != null ) {
130
+ registerListeners ();
131
+ } else {
132
+ deregisterListeners ();
133
+ }
143
134
}
144
135
145
136
private void registerListeners () {
0 commit comments