Skip to content

Commit

Permalink
Change unique id to persistent random UUID
Browse files Browse the repository at this point in the history
  • Loading branch information
badaix committed Jan 15, 2020
1 parent 757332e commit 0d5d54c
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion Snapcast/src/main/java/de/badaix/snapcast/SnapclientService.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.wifi.WifiManager;
import android.os.Binder;
import android.os.Build;
Expand Down Expand Up @@ -53,6 +54,9 @@ public class SnapclientService extends Service {
public static final String ACTION_STOP = "ACTION_STOP";
public static final String NOTIFICATION_CHANNEL_ID = "de.badaix.snapcast.snapclientservice.defaultchannel";

private static final String PREF_UNIQUE_ID = "PREF_UNIQUE_ID";
private static String uniqueID = null;

private final IBinder mBinder = new LocalBinder();
private java.lang.Process process = null;
private PowerManager.WakeLock wakeLock = null;
Expand Down Expand Up @@ -202,6 +206,21 @@ public static String getUniquePsuedoID() {
return new UUID(m_szDevIDShort.hashCode(), serial.hashCode()).toString();
}

public synchronized static String getUniqueId(Context context) {
if (uniqueID == null) {
SharedPreferences sharedPrefs = context.getSharedPreferences(
PREF_UNIQUE_ID, Context.MODE_PRIVATE);
uniqueID = sharedPrefs.getString(PREF_UNIQUE_ID, null);
if (uniqueID == null) {
uniqueID = UUID.randomUUID().toString();
SharedPreferences.Editor editor = sharedPrefs.edit();
editor.putString(PREF_UNIQUE_ID, uniqueID);
editor.commit();
}
}
return uniqueID;
}

private void start(String host, int port) {
try {
//https://code.google.com/p/android/issues/detail?id=22763
Expand All @@ -219,7 +238,7 @@ private void start(String host, int port) {
wifiWakeLock.acquire();

process = new ProcessBuilder()
.command(this.getApplicationInfo().nativeLibraryDir + "/libsnapclient.so", "-h", host, "-p", Integer.toString(port), "--hostID", getUniquePsuedoID())
.command(this.getApplicationInfo().nativeLibraryDir + "/libsnapclient.so", "-h", host, "-p", Integer.toString(port), "--hostID", getUniqueId(this.getApplicationContext()))
.redirectErrorStream(true)
.start();

Expand Down

0 comments on commit 0d5d54c

Please sign in to comment.