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

fix: added synchronization to RudderContext #372

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,39 @@

class RudderApp {
@SerializedName("build")
private String build;
private final String build;
@SerializedName("name")
private String name;
private final String name;
@SerializedName("namespace")
private String nameSpace;
private final String nameSpace;
@SerializedName("version")
private String version;
private final String version;

// internal constructor
// to be used while creating a cache of context
RudderApp(Application application) {
String buildValue = null;
String nameValue = null;
String nameSpaceValue = null;
String versionValue = null;
try {
String packageName = application.getPackageName();
PackageManager packageManager = application.getPackageManager();
PackageInfo packageInfo = packageManager.getPackageInfo(packageName, 0);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P)
this.build = Long.toString(packageInfo.getLongVersionCode());
else this.build = Integer.toString(packageInfo.versionCode);
this.name = packageInfo.applicationInfo.loadLabel(packageManager).toString();
this.nameSpace = packageName;
this.version = packageInfo.versionName;
buildValue = (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P)?
Long.toString(packageInfo.getLongVersionCode()): Integer.toString(packageInfo.versionCode);
nameValue = packageInfo.applicationInfo.loadLabel(packageManager).toString();
nameSpaceValue = packageName;
versionValue = packageInfo.versionName;
} catch (PackageManager.NameNotFoundException ex) {
ReportManager.reportError(ex);
RudderLogger.logError(ex.getCause());
}
finally {
this.build = buildValue;
this.name = nameValue;
this.nameSpace = nameSpaceValue;
this.version = versionValue;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class RudderClient {
private static RudderOption defaultOptions;
private static String deviceToken;
private static String authToken;
private static ReentrantLock reentrantLock = new ReentrantLock();
private static final ReentrantLock reentrantLock = new ReentrantLock();


private static final int NUMBER_OF_FLUSH_CALLS_IN_QUEUE = 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,9 @@ public class RudderContext {
void resetTraits() {
RudderTraits traits = new RudderTraits();
// convert the whole traits to map and take care of the extras
this.traits = Utils.convertToMap(RudderGson.getInstance().toJson(traits));
synchronized (this) {
this.traits = Utils.convertToMap(RudderGson.getInstance().toJson(traits));
}
}

void updateTraits(RudderTraits traits) {
Expand All @@ -135,13 +137,17 @@ void updateTraits(RudderTraits traits) {

// If a user is already loggedIn and then a new user tries to login
if (existingId != null && newId != null && !existingId.equals(newId)) {
this.traits = traitsMap;
synchronized (this) {
this.traits = traitsMap;
}
resetExternalIds();
return;
}

// update traits object here
this.traits.putAll(traitsMap);
synchronized (this) {
this.traits.putAll(traitsMap);
}

}

Expand All @@ -154,7 +160,9 @@ void persistTraits() {
try {
if (RudderClient.getApplication() != null) {
RudderPreferenceManager preferenceManger = RudderPreferenceManager.getInstance(RudderClient.getApplication());
preferenceManger.saveTraits(RudderGson.getInstance().toJson(this.traits));
synchronized (this) {
preferenceManger.saveTraits(RudderGson.getInstance().toJson(this.traits));
}
}
} catch (NullPointerException ex) {
ReportManager.reportError(ex);
Expand Down Expand Up @@ -386,7 +394,9 @@ RudderContext copy() {

copy.app = this.app;
if (this.traits != null) {
copy.traits = new HashMap<>(this.traits);
synchronized (this) {
copy.traits = new HashMap<>(this.traits);
}
}
copy.libraryInfo = this.libraryInfo;
copy.osInfo = this.osInfo;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

class RudderLibraryInfo {
@SerializedName("name")
private String name = BuildConfig.LIBRARY_PACKAGE_NAME;
private final String name = BuildConfig.LIBRARY_PACKAGE_NAME;
@SerializedName("version")
private String version = BuildConfig.VERSION_NAME;
private final String version = BuildConfig.VERSION_NAME;
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class RudderOSInfo {
@SerializedName("name")
private String name = "Android";
private final String name = "Android";
@SerializedName("version")
private String version = Build.VERSION.RELEASE;
private final String version = Build.VERSION.RELEASE;
}
Loading