diff --git a/src/org/kde/kdeconnect/BackgroundService.java b/src/org/kde/kdeconnect/BackgroundService.java index db9ffff0..01ecdd17 100644 --- a/src/org/kde/kdeconnect/BackgroundService.java +++ b/src/org/kde/kdeconnect/BackgroundService.java @@ -34,6 +34,7 @@ import androidx.core.content.ContextCompat; import org.kde.kdeconnect.Backends.BaseLink; import org.kde.kdeconnect.Backends.BaseLinkProvider; import org.kde.kdeconnect.Backends.LanBackend.LanLinkProvider; +import org.kde.kdeconnect.Helpers.DeviceHelper; import org.kde.kdeconnect.Helpers.NotificationHelper; import org.kde.kdeconnect.Helpers.SecurityHelpers.RsaHelper; import org.kde.kdeconnect.Helpers.SecurityHelpers.SslHelper; @@ -264,6 +265,8 @@ public class BackgroundService extends Service { instance = this; + DeviceHelper.initializeDeviceId(this); + // Register screen on listener IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON); // See: https://developer.android.com/reference/android/net/ConnectivityManager.html#CONNECTIVITY_ACTION diff --git a/src/org/kde/kdeconnect/Helpers/DeviceHelper.java b/src/org/kde/kdeconnect/Helpers/DeviceHelper.java index 12c50531..1f36d095 100644 --- a/src/org/kde/kdeconnect/Helpers/DeviceHelper.java +++ b/src/org/kde/kdeconnect/Helpers/DeviceHelper.java @@ -6,6 +6,7 @@ package org.kde.kdeconnect.Helpers; +import android.annotation.SuppressLint; import android.content.Context; import android.content.SharedPreferences; import android.content.res.Configuration; @@ -18,11 +19,14 @@ import com.jaredrummler.android.device.DeviceName; import org.kde.kdeconnect.Device; +import java.util.UUID; + public class DeviceHelper { public static final int ProtocolVersion = 7; public static final String KEY_DEVICE_NAME_PREFERENCE = "device_name_preference"; + public static final String KEY_DEVICE_ID_PREFERENCE = "device_id_preference"; private static boolean fetchingName = false; @@ -84,7 +88,29 @@ public class DeviceHelper { preferences.edit().putString(KEY_DEVICE_NAME_PREFERENCE, name).apply(); } - public static String getDeviceId(Context context) { - return Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID); + + @SuppressLint("HardwareIds") + public static void initializeDeviceId(Context context) { + SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); + if (preferences.contains(KEY_DEVICE_ID_PREFERENCE)) { + return; // We already have an ID + } + String deviceName; + if (preferences.getAll().isEmpty()) { + // For new installations, use random IDs + Log.e("DeviceHelper", "No device ID found and this looks like a new installation, creating a random ID"); + deviceName = UUID.randomUUID().toString(); + } else { + // Use the ANDROID_ID as device ID for existing installations, for backwards compatibility + Log.e("DeviceHelper", "No device ID found but this seems an existing installation, using the Android ID"); + deviceName = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID); + } + preferences.edit().putString(KEY_DEVICE_ID_PREFERENCE, deviceName).apply(); } + + public static String getDeviceId(Context context) { + SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); + return preferences.getString(KEY_DEVICE_ID_PREFERENCE, null); + } + } diff --git a/src/org/kde/kdeconnect/UserInterface/MainActivity.java b/src/org/kde/kdeconnect/UserInterface/MainActivity.java index 9f675701..c65eac3b 100644 --- a/src/org/kde/kdeconnect/UserInterface/MainActivity.java +++ b/src/org/kde/kdeconnect/UserInterface/MainActivity.java @@ -80,6 +80,8 @@ public class MainActivity extends AppCompatActivity implements SharedPreferences super.onCreate(savedInstanceState); ThemeUtil.setUserPreferredTheme(this); // Workaround: If the activity starts in landscape orientation and we call this before super.onCreate, the PluginItem entries appears with white on white background + DeviceHelper.initializeDeviceId(this); + final ActivityMainBinding binding = ActivityMainBinding.inflate(getLayoutInflater()); setContentView(binding.getRoot());