Last active
July 27, 2020 17:36
-
-
Save ItsBenyaamin/23dc32613c01f867f45b9f8e5bb7ac7d to your computer and use it in GitHub Desktop.
sometimes we have to make a unique identifier for detecting each device. this class provides a UUID combined with Time in Millisec to generate a unique identifier.
This file contains 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
package com.graymind; | |
import android.content.Context; | |
import java.io.IOException; | |
import java.util.UUID; | |
public class UUIDHelper { | |
private static final String TAG = "UUIDHelper"; | |
private static final String PATH_ON_INTERNAL_STORAGE = "/Android/data/com.graymind/"; | |
private static final String FILE_NAME = ".uuid_content"; | |
public static final String UNIQUE_ID_KEY = "unique_id_value"; | |
private Context mContext; | |
private String mUniqueID = ""; | |
public UUIDHelper(Context context){ | |
this.mContext = context; | |
} | |
public void checkForUUID(){ | |
String uuidFromPreference = getUniqueIDFromPreference(); | |
if (uuidFromPreference.equalsIgnoreCase("")){ | |
if (StorageHelper.isFileExist(getUniqueIDContainerFile())){ | |
String fileContent = ""; | |
try { | |
fileContent = StorageHelper.readFromFile(getUniqueIDContainerFile()); | |
}catch (IOException e){ | |
e.printStackTrace(); | |
} | |
if (!fileContent.equalsIgnoreCase("")){ | |
mUniqueID = fileContent; | |
saveUniqueIDToPreference(); | |
}else{ | |
mUniqueID = getCombinedUniqueID(); | |
saveUniqueIDToPreference(); | |
saveUniqueIDToFile(); | |
} | |
} | |
} | |
} | |
/** | |
* generate random app unique id from UUID class and finally combine them | |
*/ | |
private String getNewUUID(){ | |
return UUID.randomUUID().toString(); | |
} | |
private long getCurrentMillis(){ | |
return System.currentTimeMillis(); | |
} | |
private String getCombinedUniqueID(){ | |
return getNewUUID() + "_" + getCurrentMillis(); | |
} | |
/** | |
* store & restore in SharedPreference | |
*/ | |
private void saveUniqueIDToPreference(){ | |
SharedPreferenceHelper.getInstance(mContext).storeStringValue(UNIQUE_ID_KEY, mUniqueID); | |
} | |
private String getUniqueIDFromPreference(){ | |
return SharedPreferenceHelper.getInstance(mContext).restoreStringValue(UNIQUE_ID_KEY); | |
} | |
/** | |
* store & restore in Internal Storage | |
*/ | |
private void saveUniqueIDToFile(){ | |
StorageHelper.createIfNotExist(getUniqueIDContainerFolder()); | |
try { | |
StorageHelper.writeToFile(getUniqueIDContainerFile(), mUniqueID); | |
}catch (IOException io){ | |
io.printStackTrace(); | |
} | |
} | |
private String getUniqueIDFromFile(){ | |
String content = ""; | |
try { | |
content = StorageHelper.readFromFile(getUniqueIDContainerFile()); | |
}catch (IOException io){ | |
io.printStackTrace(); | |
} | |
return content; | |
} | |
private String getUniqueIDContainerFolder(){ | |
return StorageHelper.getInternalStoragePath() + PATH_ON_INTERNAL_STORAGE; | |
} | |
private String getUniqueIDContainerFile(){ | |
return getUniqueIDContainerFolder() + FILE_NAME; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment