fix UUIDv4 calculation

This commit is contained in:
Daniel Gultsch 2023-09-08 14:19:18 +02:00
parent 11e6cb9693
commit c6501a3ad4
No known key found for this signature in database
GPG key ID: F43D18AD2A0982C2

View file

@ -6,6 +6,10 @@ import android.view.Menu;
import android.view.MenuItem; import android.view.MenuItem;
import android.widget.Toast; import android.widget.Toast;
import com.google.common.primitives.Bytes;
import com.google.common.primitives.Longs;
import java.nio.ByteBuffer;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
@ -24,10 +28,9 @@ public class AccountUtils {
MANAGE_ACCOUNT_ACTIVITY = getManageAccountActivityClass(); MANAGE_ACCOUNT_ACTIVITY = getManageAccountActivityClass();
} }
public static boolean hasEnabledAccounts(final XmppConnectionService service) { public static boolean hasEnabledAccounts(final XmppConnectionService service) {
final List<Account> accounts = service.getAccounts(); final List<Account> accounts = service.getAccounts();
for(Account account : accounts) { for (Account account : accounts) {
if (account.isOptionSet(Account.OPTION_DISABLED)) { if (account.isOptionSet(Account.OPTION_DISABLED)) {
return false; return false;
} }
@ -42,19 +45,21 @@ public class AccountUtils {
} catch (final IllegalArgumentException e) { } catch (final IllegalArgumentException e) {
return account.getUuid(); return account.getUuid();
} }
final UUID publicDeviceId = getUuid(uuid.getLeastSignificantBits(), uuid.getLeastSignificantBits()); final byte[] bytes =
return publicDeviceId.toString(); Bytes.concat(
} Longs.toByteArray(uuid.getLeastSignificantBits()),
Longs.toByteArray(uuid.getLeastSignificantBits()));
protected static UUID getUuid(final long msb, final long lsb) { bytes[6] &= 0x0f; /* clear version */
final long msb0 = (msb & 0xffffffffffff0fffL) | 4; // set version bytes[6] |= 0x40; /* set to version 4 */
final long lsb0 = (lsb & 0x3fffffffffffffffL) | 0x8000000000000000L; // set variant bytes[8] &= 0x3f; /* clear variant */
return new UUID(msb0, lsb0); bytes[8] |= 0x80; /* set to IETF variant */
final ByteBuffer byteBuffer = ByteBuffer.wrap(bytes);
return new UUID(byteBuffer.getLong(), byteBuffer.getLong()).toString();
} }
public static List<String> getEnabledAccounts(final XmppConnectionService service) { public static List<String> getEnabledAccounts(final XmppConnectionService service) {
ArrayList<String> accounts = new ArrayList<>(); final ArrayList<String> accounts = new ArrayList<>();
for (Account account : service.getAccounts()) { for (final Account account : service.getAccounts()) {
if (account.getStatus() != Account.State.DISABLED) { if (account.getStatus() != Account.State.DISABLED) {
if (Config.DOMAIN_LOCK != null) { if (Config.DOMAIN_LOCK != null) {
accounts.add(account.getJid().getEscapedLocal()); accounts.add(account.getJid().getEscapedLocal());
@ -68,7 +73,7 @@ public class AccountUtils {
public static Account getFirstEnabled(XmppConnectionService service) { public static Account getFirstEnabled(XmppConnectionService service) {
final List<Account> accounts = service.getAccounts(); final List<Account> accounts = service.getAccounts();
for(Account account : accounts) { for (Account account : accounts) {
if (!account.isOptionSet(Account.OPTION_DISABLED)) { if (!account.isOptionSet(Account.OPTION_DISABLED)) {
return account; return account;
} }
@ -78,7 +83,7 @@ public class AccountUtils {
public static Account getFirst(XmppConnectionService service) { public static Account getFirst(XmppConnectionService service) {
final List<Account> accounts = service.getAccounts(); final List<Account> accounts = service.getAccounts();
for(Account account : accounts) { for (Account account : accounts) {
return account; return account;
} }
return null; return null;