2
0
mirror of https://github.com/KDE/kdeconnect-android synced 2025-08-30 13:47:41 +00:00

Do not use the ANDROID_ID as device ID

Since Android 8, this ID is stable across reinstalls of the app. This
causes other devices that already were paired with us to find a cert
mismatch and refuse to communicate with us after a reinstall or data wipe.
This commit is contained in:
Albert Vaca Cintora
2023-03-25 18:45:36 +01:00
committed by Albert Vaca Cintora
parent 554769e1ef
commit cbc0749997
3 changed files with 33 additions and 2 deletions

View File

@@ -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

View File

@@ -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);
}
}

View File

@@ -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());