Skip to content
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

Merged
merged 1 commit into from
Jan 19, 2018

Conversation

electrostat
Copy link
Contributor

Make callback optional when creating MapboxTelemetry, so developer does not need to subscribe to the TelemetryClient Callback.

Copy link
Contributor

@Guardiola31337 Guardiola31337 left a 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;
Copy link
Contributor

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);
Copy link
Contributor

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);

Copy link
Contributor

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();

Copy link
Contributor

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) {
Copy link
Contributor

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

Copy link
Contributor

@Guardiola31337 Guardiola31337 left a 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>();
Copy link
Contributor

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.

Copy link
Contributor

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) {
Copy link
Contributor

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)) {
Copy link
Contributor

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).

Copy link
Contributor

Choose a reason for hiding this comment

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

If we'd use a Set (CopyOnWriteArraySet) instead of a List (CopyOnWriteArrayList) this check wouldn't be necessary (it's checked internally).

👀 Set<E> and List<E>

@@ -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);
Copy link
Contributor

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);
Copy link
Contributor

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) {
Copy link
Contributor

@Guardiola31337 Guardiola31337 Jan 15, 2018

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);
Copy link
Contributor

@Guardiola31337 Guardiola31337 Jan 15, 2018

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) {
Copy link
Contributor

@Guardiola31337 Guardiola31337 Jan 15, 2018

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() {
Copy link
Contributor

@Guardiola31337 Guardiola31337 Jan 15, 2018

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;
Copy link
Contributor

@Guardiola31337 Guardiola31337 Jan 15, 2018

Choose a reason for hiding this comment

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


import static com.mapbox.services.android.telemetry.EventReceiver.EVENT_RECEIVER_INTENT;

public class MapboxTelemetry implements FullQueueCallback, EventCallback {
public class MapboxTelemetry implements FullQueueCallback, EventCallback, TelemetryListener {
Copy link
Contributor

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,
Copy link
Contributor

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);
Copy link
Contributor

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)

Copy link
Contributor

@Guardiola31337 Guardiola31337 left a 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

@Guardiola31337 Guardiola31337 force-pushed the aa-optional-callback-param branch 4 times, most recently from 42c9910 to 3fd9eb9 Compare January 18, 2018 23:59
- add separate initializer for mapboxtelemetry
- can now create MapboxTelemetry without a callback
- Create custom TelemetryListener
@Guardiola31337 Guardiola31337 merged commit 81d8403 into master Jan 19, 2018
@Guardiola31337 Guardiola31337 deleted the aa-optional-callback-param branch January 19, 2018 00:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants