Skip to content

Instantly share code, notes, and snippets.

@fredgrott
Created February 4, 2016 19:30
Show Gist options
  • Save fredgrott/c702ee46600b90ee7772 to your computer and use it in GitHub Desktop.
Save fredgrott/c702ee46600b90ee7772 to your computer and use it in GitHub Desktop.
public class DroidUuidFactory {
protected static final String PREFS_FILE = "device_uuid.xml";
protected static final String PREFS_DEVICE_UUID = "device_uuid";
protected volatile static UUID uuid;
public DroidUuidFactory(Context context){
if( uuid ==null ) {
synchronized (DroidUuidFactory.class) {
if( uuid == null) {
final SharedPreferences prefs = context.getSharedPreferences( PREFS_FILE, 0);
final String id = prefs.getString(PREFS_DEVICE_UUID, null );
if (id != null) {
// Use the ids previously computed and stored in the prefs file
uuid = UUID.fromString(id);
} else {
final UUID randomUUID;
randomUUID = UUID.randomUUID();
uuid = randomUUID;
prefs.edit().putString(PREFS_DEVICE_UUID, uuid.toString() ).apply();
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment