forked from Baseflow/flutter-geocoding
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMethodCallHandlerImpl.java
More file actions
214 lines (185 loc) · 7.5 KB
/
MethodCallHandlerImpl.java
File metadata and controls
214 lines (185 loc) · 7.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package com.baseflow.geocoding;
import android.location.Address;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.baseflow.geocoding.utils.AddressMapper;
import com.baseflow.geocoding.utils.LocaleConverter;
import java.io.IOException;
import java.util.List;
import java.util.Locale;
import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;
import io.flutter.plugin.common.StandardMethodCodec;
/**
* Translates incoming Geocoding MethodCalls into well formed Java function calls for {@link
* Geocoding}.
*/
final class MethodCallHandlerImpl implements MethodCallHandler {
private static final String TAG = "MethodCallHandlerImpl";
private final Geocoding geocoding;
@Nullable
private MethodChannel channel;
/**
* Forwards all incoming MethodChannel calls to the given {@code geocoding}.
*/
MethodCallHandlerImpl(Geocoding geocoding) {
this.geocoding = geocoding;
}
@Override
public void onMethodCall(
final MethodCall call,
@NonNull final Result result
) {
switch (call.method) {
case "setLocaleIdentifier":
setLocaleIdentifier(call, result);
break;
case "locationFromAddress":
onLocationFromAddress(call, result);
break;
case "placemarkFromAddress":
onPlacemarkFromAddress(call, result);
break;
case "placemarkFromCoordinates":
onPlacemarkFromCoordinates(call, result);
break;
case "isPresent":
onIsPresent(call, result);
break;
default:
result.notImplemented();
break;
}
}
/**
* Registers this instance as a method call handler on the given {@code messenger}.
*
* <p>Stops any previously started and unstopped calls.
*
* <p>This should be cleaned with {@link #stopListening} once the messenger is disposed of.
*/
void startListening(BinaryMessenger messenger) {
if (channel != null) {
Log.wtf(TAG, "Setting a method call handler before the last was disposed.");
stopListening();
}
final BinaryMessenger.TaskQueue taskQueue = messenger.makeBackgroundTaskQueue();
channel = new MethodChannel(messenger, "flutter.baseflow.com/geocoding", StandardMethodCodec.INSTANCE, taskQueue);
channel.setMethodCallHandler(this);
}
/**
* Clears this instance from listening to method calls.
*
* <p>Does nothing if {@link #startListening} hasn't been called, or if we're already stopped.
*/
void stopListening() {
if (channel == null) {
Log.d(TAG, "Tried to stop listening when no MethodChannel had been initialized.");
return;
}
channel.setMethodCallHandler(null);
channel = null;
}
private void setLocaleIdentifier(MethodCall call, Result result) {
final String languageTag = call.argument("localeIdentifier");
geocoding.setLocaleIdentifier(LocaleConverter.fromLanguageTag(languageTag));
result.success(true);
}
private void onLocationFromAddress(MethodCall call, Result result) {
final String address = call.argument("address");
if (address == null || address.isEmpty()) {
result.error(
"ARGUMENT_ERROR",
"Supply a valid value for the 'address' parameter.",
null);
}
geocoding.placemarkFromAddress(address, new GeocodeListenerAdapter() {
@Override
public void onGeocode(List<Address> addresses) {
if (addresses != null && addresses.size() > 0) {
result.success(AddressMapper.toLocationHashMapList(addresses));
} else {
result.error(
"NOT_FOUND",
String.format("No coordinates found for '%s'", address),
null);
}
}
@Override
public void onError(@Nullable String errorMessage) {
result.error(
"IO_ERROR",
errorMessage != null ? errorMessage : "Unknown error occurred",
null);
}
});
}
private void onPlacemarkFromAddress(final MethodCall call, final Result result) {
final String address = call.argument("address");
if (address == null || address.isEmpty()) {
result.error(
"ARGUMENT_ERROR",
"Supply a valid value for the 'address' parameter.",
null);
}
geocoding.placemarkFromAddress(address, new GeocodeListenerAdapter() {
@Override
public void onGeocode(List<Address> addresses) {
if (addresses != null && addresses.size() > 0) {
result.success(AddressMapper.toAddressHashMapList(addresses));
} else {
result.error(
"NOT_FOUND",
String.format("No coordinates found for '%s'", address),
null);
}
}
@Override
public void onError(@Nullable String errorMessage) {
result.error(
"IO_ERROR",
errorMessage != null ? errorMessage : "Unknown error occurred",
null);
}
});
}
private void onPlacemarkFromCoordinates(final MethodCall call, final Result result) {
final double latitude = call.argument("latitude");
final double longitude = call.argument("longitude");
geocoding.placemarkFromCoordinates(
latitude,
longitude, new GeocodeListenerAdapter() {
@Override
public void onGeocode(List<Address> addresses) {
if (addresses != null && addresses.size() > 0) {
result.success(AddressMapper.toAddressHashMapList(addresses));
} else {
result.error(
"NOT_FOUND",
String.format(
Locale.ENGLISH,
"No address information found for supplied coordinates (latitude: %f, longitude: %f).",
latitude,
longitude
),
null);
}
}
@Override
public void onError(@Nullable String errorMessage) {
result.error(
"IO_ERROR",
errorMessage != null ? errorMessage : "Unknown error occurred",
null);
}
});
}
private void onIsPresent(final MethodCall call, final Result result) {
boolean isPresent = geocoding.isPresent();
result.success(isPresent);
}
}