Created
February 4, 2016 19:30
-
-
Save fredgrott/c702ee46600b90ee7772 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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