Skip to content

Commit

Permalink
Added a deep clone to data point bundles in HttpTransmitter to work a…
Browse files Browse the repository at this point in the history
…round thread-safety issue in Android.
  • Loading branch information
audaciouscode committed Aug 16, 2017
1 parent abe532f commit 90258c2
Showing 1 changed file with 13 additions and 6 deletions.
Expand Up @@ -5,6 +5,7 @@
import android.net.Uri;
import android.os.BadParcelableException;
import android.os.Bundle;
import android.os.Parcel;
import android.util.Log;

import com.audacious_software.passive_data_kit.DeviceInformation;
Expand Down Expand Up @@ -501,29 +502,35 @@ public long transmittedSize() {

@SuppressWarnings("ConstantConditions")
@Override
public void onGeneratorUpdated(final String identifier, final long timestamp, final Bundle data) {
public void onGeneratorUpdated(final String identifier, final long timestamp, Bundle data) {
final HttpTransmitter me = this;

final Parcel p = Parcel.obtain();
p.writeBundle(data);
p.setDataPosition(0);

Runnable r = new Runnable() {
@Override
public void run() {
if (data.keySet().size() > 1) { // Only transmit non-empty bundles...
Bundle clonedData = p.readBundle();

if (clonedData.keySet().size() > 1) { // Only transmit non-empty bundles...
long generatorTimestamp = timestamp / 1000; // Convert to seconds...

Generators generators = Generators.getInstance(me.mContext);

Bundle metadata = new Bundle();

if (data.containsKey(Generator.PDK_METADATA)) {
metadata = data.getBundle(Generator.PDK_METADATA);
if (clonedData.containsKey(Generator.PDK_METADATA)) {
metadata = clonedData.getBundle(Generator.PDK_METADATA);
}

metadata.putString(Generator.IDENTIFIER, identifier);
metadata.putDouble(Generator.TIMESTAMP, generatorTimestamp);
metadata.putString(Generator.GENERATOR, generators.getGeneratorFullName(identifier));
metadata.putString(Generator.SOURCE, generators.getSource());
metadata.putString(Generator.SOURCE, me.mUserId);
data.putBundle(Generator.PDK_METADATA, metadata);
clonedData.putBundle(Generator.PDK_METADATA, metadata);

synchronized (this) {
if (me.mJsonGenerator == null) {
Expand All @@ -538,7 +545,7 @@ public void run() {
}
}

HttpTransmitter.writeBundle(me.mContext, me.mJsonGenerator, data);
HttpTransmitter.writeBundle(me.mContext, me.mJsonGenerator, clonedData);
}
}
}
Expand Down

0 comments on commit 90258c2

Please sign in to comment.