Created
September 4, 2016 14:35
-
-
Save sahir/b6a238e538e26e53f075460854cf1a7f to your computer and use it in GitHub Desktop.
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.unique.identifyingdevices; | |
/** | |
* Created by sahir on 31/08/16. | |
*/ | |
import android.content.Context; | |
import android.provider.Settings; | |
import android.telephony.TelephonyManager; | |
import java.io.File; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
import java.io.RandomAccessFile; | |
import java.util.Random; | |
import java.util.UUID; | |
public class DeviceId { | |
private static String sID = null; | |
private static final String INSTALLATION = "CONSTANT_INSTALLATION_APP_GENERATED_ID"; | |
public static final String FIXED_ANDROID_ID = "9774d56d682e549c"; | |
public synchronized static String getId(Context context) { | |
if (sID == null) { | |
File installation = new File(context.getFilesDir(), INSTALLATION); | |
try { | |
if (!installation.exists()) { | |
UUID deviceUuid = new UUID(getDeviceIMEI(context).hashCode(), context.getApplicationContext() | |
.getPackageName().hashCode()); | |
sID = deviceUuid.toString(); | |
writeInstallationFile(installation, sID); | |
} | |
sID = readInstallationFile(installation); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
return sID; | |
} | |
private static String readInstallationFile(File installation) throws IOException { | |
RandomAccessFile f = new RandomAccessFile(installation, "r"); | |
byte[] bytes = new byte[(int) f.length()]; | |
f.readFully(bytes); | |
f.close(); | |
return new String(bytes); | |
} | |
private static void writeInstallationFile(File installation, String id) throws IOException { | |
FileOutputStream out = new FileOutputStream(installation); | |
out.write(id.getBytes()); | |
out.close(); | |
} | |
private static String getDeviceIMEI(Context context) { | |
TelephonyManager mTelephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); | |
String deviceIMEI = mTelephonyManager.getDeviceId(); | |
if (deviceIMEI == null || deviceIMEI.equals("")) { | |
return getAndroidId(context); | |
} else { | |
return deviceIMEI; | |
} | |
} | |
private static String getAndroidId(Context context) { | |
String androidId = Settings.Secure.getString(context.getContentResolver(), | |
android.provider.Settings.Secure.ANDROID_ID); | |
if (androidId.equalsIgnoreCase(FIXED_ANDROID_ID)) { | |
return getRandomID(context); | |
} else | |
return androidId; | |
} | |
private static String getRandomID(Context context) { | |
// If nothing is found for this device, generate a random number, although this case is not possible. | |
Random tmpRand = new Random(); | |
return String.valueOf(tmpRand.nextLong()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
get android device unique id