co.easimart.InstallationId Maven / Gradle / Ivy
package co.easimart;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.UUID;
/**
* Since we cannot save dirty EasimartObjects to disk and we must be able to persist UUIDs across
* restarts even if the EasimartInstallation is not saved, we use this legacy file still as a
* boostrapping environment as well until the full EasimartInstallation is cached to disk.
*
* TODO: Allow dirty objects to be saved to disk.
*/
/* package */ class InstallationId {
private static final String TAG = "InstallationId";
private final Object lock = new Object();
private final File file;
private String installationId;
public InstallationId(File file) {
this.file = file;
}
/**
* Loads the installationId from memory, then tries to loads the legacy installationId from disk
* if it is present, or creates a new random UUID.
*/
public String get() {
synchronized (lock) {
if (installationId == null) {
try {
installationId = EasimartFileUtils.readFileToString(file, "UTF-8");
} catch (FileNotFoundException e) {
EasimartLog.i(TAG, "Couldn't find existing installationId file. Creating one instead.");
} catch (IOException e) {
EasimartLog.e(TAG, "Unexpected exception reading installation id from disk", e);
}
}
if (installationId == null) {
setInternal(UUID.randomUUID().toString());
}
}
return installationId;
}
/**
* Sets the installationId and persists it to disk.
*/
public void set(String newInstallationId) {
synchronized (lock) {
if (EasimartTextUtils.isEmpty(newInstallationId)
|| newInstallationId.equals(get())) {
return;
}
setInternal(newInstallationId);
}
}
private void setInternal(String newInstallationId) {
synchronized (lock) {
try {
EasimartFileUtils.writeStringToFile(file, newInstallationId, "UTF-8");
} catch (IOException e) {
EasimartLog.e(TAG, "Unexpected exception writing installation id to disk", e);
}
installationId = newInstallationId;
}
}
/* package for tests */ void clear() {
synchronized (lock) {
installationId = null;
EasimartFileUtils.deleteQuietly(file);
}
}
}