protected async Task LoadDeviceIDFromStorage() { DeviceId dId = await Storage.Instance.LoadFromFile <DeviceId>(deviceFilename); bool saveAfterLoading = false; if (dId == null) { // if it's null then either there is no device Id saved or it's saved // in the legacy format as just a string. Try deserializing that String backupDeviceId = await Storage.Instance.LoadFromFile <string>(deviceFilename); if (backupDeviceId != null) { //it was in the backup format, assume it's Guid dId = new DeviceId(backupDeviceId, DeviceIdMethodInternal.windowsGUID); saveAfterLoading = true; } } if (dId?.deviceId != null) { deviceId = dId.deviceId; usedIdMethod = dId.deviceIdMethod; } if (saveAfterLoading) { //it must have been in the legacy format, save it before continueing await SaveDeviceIDToStorage(); } }
/// <summary> /// Returns the unique device identificator /// </summary> internal async Task <string> GetDeviceId() { try { if (deviceId != null) { return(deviceId); } await LoadDeviceIDFromStorage(); if (deviceId == null) { DeviceId dId = ComputeDeviceID(); deviceId = dId.deviceId; usedIdMethod = dId.deviceIdMethod; await SaveDeviceIDToStorage(); } return(deviceId); } catch { //todo log return(String.Empty); } }
/// <summary> /// Set preferred method for generating device id. If a Id is already provided, use that and /// set method to developerSupplied /// </summary> /// <param name="deviceIdMethod">preferred method</param> /// <param name="suppliedDeviceId">device id to use</param> /// <returns></returns> internal async Task SetPreferredDeviceIdMethod(DeviceIdMethodInternal deviceIdMethod, String suppliedDeviceId = null) { if (suppliedDeviceId != null) { deviceId = suppliedDeviceId; usedIdMethod = DeviceIdMethodInternal.developerSupplied; preferredIdMethod = usedIdMethod; await SaveDeviceIDToStorage(); } else { preferredIdMethod = deviceIdMethod; } }