From 17fdde66d94155fc62a034fa6658995bef6fd6e5 Mon Sep 17 00:00:00 2001 From: Jason Smith Date: Tue, 22 Mar 2016 13:02:25 -0700 Subject: Initial import --- Xamarin.Forms.Platform.Android/Deserializer.cs | 89 ++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 Xamarin.Forms.Platform.Android/Deserializer.cs (limited to 'Xamarin.Forms.Platform.Android/Deserializer.cs') diff --git a/Xamarin.Forms.Platform.Android/Deserializer.cs b/Xamarin.Forms.Platform.Android/Deserializer.cs new file mode 100644 index 00000000..4c62ad1a --- /dev/null +++ b/Xamarin.Forms.Platform.Android/Deserializer.cs @@ -0,0 +1,89 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.IO.IsolatedStorage; +using System.Runtime.Serialization; +using System.Threading.Tasks; +using System.Xml; + +namespace Xamarin.Forms.Platform.Android +{ + internal class Deserializer : IDeserializer + { + const string PropertyStoreFile = "PropertyStore.forms"; + + public Task> DeserializePropertiesAsync() + { + // Deserialize property dictionary to local storage + // Make sure to use Internal + return Task.Run(() => + { + using(IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication()) + using(IsolatedStorageFileStream stream = store.OpenFile(PropertyStoreFile, System.IO.FileMode.OpenOrCreate)) + using(XmlDictionaryReader reader = XmlDictionaryReader.CreateBinaryReader(stream, XmlDictionaryReaderQuotas.Max)) + { + if (stream.Length == 0) + return null; + + try + { + var dcs = new DataContractSerializer(typeof(Dictionary)); + return (IDictionary)dcs.ReadObject(reader); + } + catch (Exception e) + { + Debug.WriteLine("Could not deserialize properties: " + e.Message); + Log.Warning("Xamarin.Forms PropertyStore", $"Exception while reading Application properties: {e}"); + } + } + + return null; + }); + } + + public Task SerializePropertiesAsync(IDictionary properties) + { + properties = new Dictionary(properties); + // Serialize property dictionary to local storage + // Make sure to use Internal + return Task.Run(() => + { + var success = false; + using(IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication()) + using(IsolatedStorageFileStream stream = store.OpenFile(PropertyStoreFile + ".tmp", System.IO.FileMode.OpenOrCreate)) + using(XmlDictionaryWriter writer = XmlDictionaryWriter.CreateBinaryWriter(stream)) + { + try + { + var dcs = new DataContractSerializer(typeof(Dictionary)); + dcs.WriteObject(writer, properties); + writer.Flush(); + success = true; + } + catch (Exception e) + { + Debug.WriteLine("Could not serialize properties: " + e.Message); + Log.Warning("Xamarin.Forms PropertyStore", $"Exception while writing Application properties: {e}"); + } + } + + if (!success) + return; + using(IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication()) + { + try + { + if (store.FileExists(PropertyStoreFile)) + store.DeleteFile(PropertyStoreFile); + store.MoveFile(PropertyStoreFile + ".tmp", PropertyStoreFile); + } + catch (Exception e) + { + Debug.WriteLine("Could not move new serialized property file over old: " + e.Message); + Log.Warning("Xamarin.Forms PropertyStore", $"Exception while writing Application properties: {e}"); + } + } + }); + } + } +} \ No newline at end of file -- cgit v1.2.3