-
Notifications
You must be signed in to change notification settings - Fork 48
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
MapboxTelemetry - Optional Callback #38
Conversation
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.
Forgetting below comments, what about adding a setter instead of constructor overloading? 👀 https://github.com/mapbox/mapbox-java/blob/mas-2.x.x/mapbox/libandroid-telemetry/src/main/java/com/mapbox/services/android/telemetry/MapboxTelemetry.java
If the listener should be optional it makes total sense to have a setter, it'd translate to a more flexible API which would mean that MapboxTelemetry
is 💪
private final Callback httpCallback; | ||
private final SchedulerFlusher schedulerFlusher; | ||
private Callback httpCallback; | ||
private SchedulerFlusher schedulerFlusher; |
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.
Why does the final
modifier need to be removed?
@@ -55,12 +55,17 @@ | |||
private PermissionCheckRunnable permissionCheckRunnable = null; | |||
|
|||
public MapboxTelemetry(Context context, String accessToken, String userAgent, Callback httpCallback) { | |||
this.httpCallback = httpCallback; | |||
|
|||
new MapboxTelemetry(context, accessToken, userAgent); |
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.
Why not calling the overloading constructor (this
)?
initializeQueue(); | ||
AlarmReceiver alarmReceiver = obtainAlarmReceiver(httpCallback); | ||
|
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.
NIT empty line
} | ||
} | ||
|
||
private boolean startTelemetry() { | ||
if (!isTelemetryEnabled) { | ||
isTelemetryEnabled = true; | ||
optIn(); | ||
|
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.
NIT empty line
@@ -71,7 +76,10 @@ private void sendBatch(List<Event> batch, Callback callback) { | |||
.build(); | |||
|
|||
OkHttpClient client = setting.getClient(); | |||
client.newCall(request).enqueue(callback); | |||
|
|||
if (callback != null) { |
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.
That would mean that if a user doesn't set a callback (MapboxTelemetry#setHttpCallback
) or if a user sets a callback to null
, the request won't be executed, which is not valid. Ultimately, registering to a listener should be optional and should not affect the overall behavior ensuring that everything is still working without any action from the client/user.
This could be achieved if MapboxTelemetry
implements Callback
and assigning httpCallback
to this
, avoiding to change any other classes other than MapboxTelemetry
.
It'd be great though to add a custom callback which would give us more flexibility on what actions to notify (i.e. to notify actions beyond onResponse
and onFailure
), more control over what and when actions need to be notified and it'd open also the possibility of having multiple listeners that users/clients could add/remove as needed.
👀 https://github.com/mapbox/mapbox-java/blob/mas-2.x.x/mapbox/libandroid-telemetry/src/main/java/com/mapbox/services/android/telemetry/TelemetryListener.java
https://github.com/mapbox/mapbox-java/blob/mas-2.x.x/mapbox/libandroid-telemetry/src/main/java/com/mapbox/services/android/telemetry/MapboxTelemetry.java#L656
https://github.com/mapbox/mapbox-java/blob/mas-2.x.x/mapbox/libandroid-telemetry/src/main/java/com/mapbox/services/android/telemetry/MapboxTelemetry.java#L724-L726
https://github.com/mapbox/mapbox-java/blob/mas-2.x.x/mapbox/libandroid-telemetry/src/main/java/com/mapbox/services/android/telemetry/MapboxTelemetry.java#L700-L702
https://github.com/mapbox/mapbox-java/blob/mas-2.x.x/mapbox/libandroid-telemetry/src/main/java/com/mapbox/services/android/telemetry/MapboxTelemetry.java#L78
https://github.com/mapbox/mapbox-java/blob/mas-2.x.x/mapbox/libandroid-telemetry/src/main/java/com/mapbox/services/android/telemetry/MapboxTelemetry.java#L183-L191
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.
This branch has conflicts that must be resolved
We'd need to rebase from master
👀 #38 (comment)
We should adapt/fix the overall implementation and tests based on ☝️
@@ -53,37 +53,38 @@ | |||
private boolean isOpted = false; | |||
private boolean serviceBound = false; | |||
private PermissionCheckRunnable permissionCheckRunnable = null; | |||
protected CopyOnWriteArrayList<TelemetryListener> telemetryListeners = new CopyOnWriteArrayList<TelemetryListener>(); |
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.
Why protected
modifier? If it's only used within MapboxTelemetry
it should be private
.
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.
Why initializing the array here instead of doing it "lazily" (when needed)?
@@ -136,6 +137,16 @@ public void updateLocationPriority(@LocationEnginePriority.PowerMode int locatio | |||
} | |||
} | |||
|
|||
public void addTelemetryListener(TelemetryListener listener) { |
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.
Per consistency with removeTelemetryListener
it'd be great to return a value (boolean
) instead of nothing.
@@ -136,6 +137,16 @@ public void updateLocationPriority(@LocationEnginePriority.PowerMode int locatio | |||
} | |||
} | |||
|
|||
public void addTelemetryListener(TelemetryListener listener) { | |||
if (!this.telemetryListeners.contains(listener)) { |
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.
this
is not adding any value here (not necessary).
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.
@@ -136,6 +137,16 @@ public void updateLocationPriority(@LocationEnginePriority.PowerMode int locatio | |||
} | |||
} | |||
|
|||
public void addTelemetryListener(TelemetryListener listener) { | |||
if (!this.telemetryListeners.contains(listener)) { | |||
this.telemetryListeners.add(listener); |
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.
this
is not adding any value here (not necessary).
} | ||
|
||
public boolean removeTelemetryListener(TelemetryListener listener) { | ||
return this.telemetryListeners.remove(listener); |
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.
this
is not adding any value here (not necessary).
@@ -40,17 +44,17 @@ void updateUserAgent(String userAgent) { | |||
this.userAgent = userAgent; | |||
} | |||
|
|||
void sendEvents(List<Event> events, Callback callback) { | |||
void sendEvents(List<Event> events, CopyOnWriteArrayList<TelemetryListener> telemetryListeners) { |
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.
This is not needed 👀 #38 (comment)
ArrayList<Event> batch = new ArrayList<>(); | ||
batch.addAll(events); | ||
sendBatch(batch, callback); | ||
sendBatch(batch, telemetryListeners); |
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.
This is not needed 👀 #38 (comment)
} | ||
|
||
void updateDebugLoggingEnabled(boolean debugLoggingEnabled) { | ||
setting = setting.toBuilder().debugLoggingEnabled(debugLoggingEnabled).build(); | ||
} | ||
|
||
private void sendBatch(List<Event> batch, Callback callback) { | ||
private void sendBatch(List<Event> batch, final CopyOnWriteArrayList<TelemetryListener> telemetryListeners) { |
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.
This is not needed 👀 #38 (comment)
@@ -71,7 +75,22 @@ private void sendBatch(List<Event> batch, Callback callback) { | |||
.build(); | |||
|
|||
OkHttpClient client = setting.getClient(); | |||
client.newCall(request).enqueue(callback); | |||
|
|||
client.newCall(request).enqueue(new Callback() { |
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.
This is not needed 👀 #38 (comment)
|
||
public interface TelemetryListener { | ||
|
||
void onHttpResponse(Response response) throws IOException; |
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.
adapt the data that we're going to expose (not return directly what we receive from
Callback
👀 https://github.com/mapbox/mapbox-java/blob/mas-2.x.x/mapbox/libandroid-telemetry/src/main/java/com/mapbox/services/android/telemetry/TelemetryListener.java#L21)
|
||
import static com.mapbox.services.android.telemetry.EventReceiver.EVENT_RECEIVER_INTENT; | ||
|
||
public class MapboxTelemetry implements FullQueueCallback, EventCallback { | ||
public class MapboxTelemetry implements FullQueueCallback, EventCallback, TelemetryListener { |
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.
This should be tackled differently 👀 #38 (comment)
} | ||
|
||
// For testing only | ||
MapboxTelemetry(Context context, String accessToken, String userAgent, EventsQueue queue, | ||
TelemetryClient telemetryClient, Callback httpCallback, SchedulerFlusher schedulerFlusher, | ||
Clock clock, LocalBroadcastManager localBroadcastManager) { | ||
TelemetryClient telemetryClient, TelemetryListener telemetryListener, |
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.
This should be tackled differently 👀 #38 (comment)
if (checkRequiredParameters(accessToken, userAgent)) { | ||
telemetryClient.sendEvents(events, httpCallback); | ||
telemetryClient.sendEvents(events, telemetryListeners); |
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.
This should be tackled differently 👀 #38 (comment)
473f0b8
to
5c3a8de
Compare
5afeb36
to
6b39504
Compare
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.
Update from master
and 🚀
Great job @electrostat
42c9910
to
3fd9eb9
Compare
- add separate initializer for mapboxtelemetry - can now create MapboxTelemetry without a callback - Create custom TelemetryListener
3fd9eb9
to
2fbaf81
Compare
Make callback optional when creating MapboxTelemetry, so developer does not need to subscribe to the TelemetryClient Callback.