Example usage for java.util UUID UUID

List of usage examples for java.util UUID UUID

Introduction

In this page you can find the example usage for java.util UUID UUID.

Prototype

public UUID(long mostSigBits, long leastSigBits) 

Source Link

Document

Constructs a new UUID using the specified data.

Usage

From source file:Main.java

/**
 * Return pseudo unique ID/*from  www  .j a v  a2 s  .c  o m*/
 * @return ID
 */
public static String getUniquePsuedoID() {
    // If all else fails, if the user does have lower than API 9 (lower
    // than Gingerbread), has reset their phone or 'Secure.ANDROID_ID'
    // returns 'null', then simply the ID returned will be solely based
    // off their Android device information. This is where the collisions
    // can happen.
    // Thanks http://www.pocketmagic.net/?p=1662!
    // Try not to use DISPLAY, HOST or ID - these items could change.
    // If there are collisions, there will be overlapping data
    String m_szDevIDShort = "35" + (Build.BOARD.length() % 10) + (Build.BRAND.length() % 10)
            + (Build.CPU_ABI.length() % 10) + (Build.DEVICE.length() % 10) + (Build.MANUFACTURER.length() % 10)
            + (Build.MODEL.length() % 10) + (Build.PRODUCT.length() % 10);

    // Thanks to @Roman SL!
    // http://stackoverflow.com/a/4789483/950427
    // Only devices with API >= 9 have android.os.Build.SERIAL
    // http://developer.android.com/reference/android/os/Build.html#SERIAL
    // If a user upgrades software or roots their phone, there will be a duplicate entry
    String serial = null;
    try {
        serial = android.os.Build.class.getField("SERIAL").get(null).toString();

        // Go ahead and return the serial for api => 9
        return new UUID(m_szDevIDShort.hashCode(), serial.hashCode()).toString();
    } catch (Exception e) {
        // String needs to be initialized
        serial = "serial"; // some value
    }

    // Thanks @Joe!
    // http://stackoverflow.com/a/2853253/950427
    // Finally, combine the values we have found by using the UUID class to create a unique identifier
    return new UUID(m_szDevIDShort.hashCode(), serial.hashCode()).toString();
}

From source file:Main.java

/**
 * this generate the device Id//from w  w w .  j a  va 2 s  . c om
 *
 * @param baseContext
 * @param contentResolver
 * @return
 */
//http://stackoverflow.com/questions/2785485/is-there-a-unique-android-device-id
public static String generateDeviceId(Context baseContext, ContentResolver contentResolver) {

    final TelephonyManager tm = (TelephonyManager) baseContext.getSystemService(Context.TELEPHONY_SERVICE);

    final String tmDevice, tmSerial, androidId;
    tmDevice = String.valueOf(tm.getDeviceId());
    tmSerial = String.valueOf(tm.getSimSerialNumber());
    androidId = String.valueOf(android.provider.Settings.Secure.getString(contentResolver,
            android.provider.Settings.Secure.ANDROID_ID));

    UUID deviceUuid = new UUID(androidId.hashCode(), ((long) tmDevice.hashCode() << 32) | tmSerial.hashCode());
    return deviceUuid.toString();

}

From source file:Main.java

/**
 * Return pseudo unique ID/*from   w  w w  .  j  ava  2 s. c om*/
 * 
 * @return ID
 */
public static String getUniquePsuedoID() {
    // If all else fails, if the user does have lower than API 9 (lower
    // than Gingerbread), has reset their phone or 'Secure.ANDROID_ID'
    // returns 'null', then simply the ID returned will be solely based
    // off their Android device information. This is where the collisions
    // can happen.
    // Thanks http://www.pocketmagic.net/?p=1662!
    // Try not to use DISPLAY, HOST or ID - these items could change.
    // If there are collisions, there will be overlapping data
    String m_szDevIDShort = "35" + (Build.BOARD.length() % 10) + (Build.BRAND.length() % 10)
            + (Build.CPU_ABI.length() % 10) + (Build.DEVICE.length() % 10) + (Build.MANUFACTURER.length() % 10)
            + (Build.MODEL.length() % 10) + (Build.PRODUCT.length() % 10);

    // Thanks to @Roman SL!
    // http://stackoverflow.com/a/4789483/950427
    // Only devices with API >= 9 have android.os.Build.SERIAL
    // http://developer.android.com/reference/android/os/Build.html#SERIAL
    // If a user upgrades software or roots their phone, there will be a
    // duplicate entry
    String serial = null;
    try {
        serial = android.os.Build.class.getField("SERIAL").get(null).toString();

        // Go ahead and return the serial for api => 9
        return new UUID(m_szDevIDShort.hashCode(), serial.hashCode()).toString();
    } catch (Exception e) {
        // String needs to be initialized
        serial = "serial"; // some value
    }

    // Thanks @Joe!
    // http://stackoverflow.com/a/2853253/950427
    // Finally, combine the values we have found by using the UUID class to
    // create a unique identifier
    return new UUID(m_szDevIDShort.hashCode(), serial.hashCode()).toString();
}

From source file:Main.java

private static String getDeviceId(Context context) {
    TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    String tmDevice = null;/* w  w w  . j  a  va2  s. c o m*/
    String tmSerial = null;
    String androidId = null;
    try {
        tmDevice = tm.getDeviceId();
    } catch (Exception e) {
        Log.w(LOG_TAG, e);
    }
    try {
        tmSerial = tm.getSimSerialNumber();
    } catch (Exception e) {
        Log.w(LOG_TAG, e);
    }
    androidId = Secure.getString(context.getContentResolver(), Secure.ANDROID_ID);
    if (tmDevice == null) {
        tmDevice = "";
    }
    if (tmSerial == null) {
        tmSerial = "";
    }
    if (androidId == null) {
        androidId = "";
    }
    UUID deviceUuid = new UUID(androidId.hashCode(), ((long) tmDevice.hashCode() << 32) | tmSerial.hashCode());
    return "lsa-kp" + deviceUuid.toString();
}

From source file:org.i3xx.step.mongo.core.util.IdGen.java

/**
 * Gets a 128 bit IdRep from an URL save representation
 * //  w  ww  . j a v  a2 s  .c o m
 * @param urlSave
 * @return The IdRep object
 */
public static final IdRep fromURLSaveString(String urlSave) {

    byte[] bytes = Base64.decodeBase64(urlSave);
    ByteBuffer buffer = ByteBuffer.wrap(bytes);
    UUID uuid = new UUID(buffer.getLong(0), buffer.getLong(8));

    return new JavaIdRep(uuid);
}

From source file:org.jaggeryjs.hostobjects.uuid.UUIDHostObject.java

public static String generate(Context cx, Scriptable thisObj, Object[] args, Function funObj)
        throws ScriptException {
    String functionName = "generate";
    int argsCount = args.length;
    if (argsCount != 0 && argsCount != 2) {
        HostObjectUtil.invalidNumberOfArgs(MODULE_NAME, functionName, argsCount, true);
    }//w  ww  .  j a v  a 2  s  .  co  m
    if (argsCount == 2) {
        if (!(args[0] instanceof Number)) {
            HostObjectUtil.invalidArgsError(MODULE_NAME, functionName, "1", "number", args[1], true);
        }

        if (!(args[1] instanceof Number)) {
            HostObjectUtil.invalidArgsError(MODULE_NAME, functionName, "2", "number", args[1], true);
        }
        return new UUID(((Number) args[0]).longValue(), ((Number) args[1]).longValue()).toString();
    }
    return UUID.randomUUID().toString();
}

From source file:com.njlabs.amrita.aid.util.Identifier.java

@SuppressLint("HardwareIds")
public static String identify(Context context) {
    final TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);

    final String tmDevice, tmSerial, androidId;
    tmDevice = "" + tm.getDeviceId();
    tmSerial = "" + tm.getSimSerialNumber();
    androidId = "" + android.provider.Settings.Secure.getString(context.getContentResolver(),
            android.provider.Settings.Secure.ANDROID_ID);

    UUID deviceUuid = new UUID(androidId.hashCode(), ((long) tmDevice.hashCode() << 32) | tmSerial.hashCode());
    return deviceUuid.toString();
}

From source file:com.hengyi.japp.tools.UuidUtils.java

public static String decodeBase58Uuid(String base58uuid) {
    byte[] byUuid = Base58.decode(base58uuid);
    ByteBuffer bb = ByteBuffer.wrap(byUuid);
    UUID uuid = new UUID(bb.getLong(), bb.getLong());
    return uuid.toString();
}

From source file:org.i3xx.step.mongo.core.util.IdGen.java

/**
 * Gets a 128 bit IdRep from a BigInteger
 * /*from   w w  w  .  j  a v a 2  s. co m*/
 * @return The IdRep object
 */
public static final IdRep fromBigInteger(BigInteger ui) {

    UUID uuid = new UUID(ui.shiftRight(64).longValue(), ui.longValue());

    return new JavaIdRep(uuid);
}

From source file:Main.java

/**
 * Parse UUID from bytes. The {@code uuidBytes} can represent a 16-bit, 32-bit or 128-bit UUID,
 * but the returned UUID is always in 128-bit format.
 * Note UUID is little endian in Bluetooth.
 *
 * @param uuidBytes Byte representation of uuid.
 * @return {@link ParcelUuid} parsed from bytes.
 * @throws IllegalArgumentException If the {@code uuidBytes} cannot be parsed.
 *//*  ww  w . j a va 2s .  com*/
public static ParcelUuid parseUuidFrom(byte[] uuidBytes) {
    if (uuidBytes == null) {
        throw new IllegalArgumentException("uuidBytes cannot be null");
    }
    int length = uuidBytes.length;
    if (length != UUID_BYTES_16_BIT && length != UUID_BYTES_32_BIT && length != UUID_BYTES_128_BIT) {
        throw new IllegalArgumentException("uuidBytes length invalid - " + length);
    }

    // Construct a 128 bit UUID.
    if (length == UUID_BYTES_128_BIT) {
        ByteBuffer buf = ByteBuffer.wrap(uuidBytes).order(ByteOrder.LITTLE_ENDIAN);
        long msb = buf.getLong(8);
        long lsb = buf.getLong(0);
        return new ParcelUuid(new UUID(msb, lsb));
    }

    // For 16 bit and 32 bit UUID we need to convert them to 128 bit value.
    // 128_bit_value = uuid * 2^96 + BASE_UUID
    long shortUuid;
    if (length == UUID_BYTES_16_BIT) {
        shortUuid = uuidBytes[0] & 0xFF;
        shortUuid += (uuidBytes[1] & 0xFF) << 8;
    } else {
        shortUuid = uuidBytes[0] & 0xFF;
        shortUuid += (uuidBytes[1] & 0xFF) << 8;
        shortUuid += (uuidBytes[2] & 0xFF) << 16;
        shortUuid += (uuidBytes[3] & 0xFF) << 24;
    }
    long msb = BASE_UUID.getUuid().getMostSignificantBits() + (shortUuid << 32);
    long lsb = BASE_UUID.getUuid().getLeastSignificantBits();
    return new ParcelUuid(new UUID(msb, lsb));
}