Add bundle encode/decode APIs
authorHyunho Kang <hhstark.kang@samsung.com>
Mon, 20 Mar 2017 04:32:27 +0000 (13:32 +0900)
committerSemun Lee <sm79.lee@samsung.com>
Tue, 21 Mar 2017 01:25:20 +0000 (10:25 +0900)
- Encode
- Decode

Change-Id: I16dfe77cde61e9bf7624dfa4cc126200b6520a4f
Signed-off-by: Hyunho Kang <hhstark.kang@samsung.com>
src/Tizen.Applications.Common/Interop/Interop.Bundle.cs
src/Tizen.Applications.Common/Tizen.Applications/Bundle.cs

index ab996f0..65c314a 100755 (executable)
@@ -59,6 +59,12 @@ internal static partial class Interop
         [DllImport(Libraries.Bundle, EntryPoint = "bundle_foreach")]
         internal static extern void Foreach(SafeBundleHandle handle, Iterator iterator, IntPtr userData);
 
+        [DllImport(Libraries.Bundle, EntryPoint = "bundle_encode")]
+        internal static extern void BundleEncode(SafeBundleHandle handle, out string str, out int len);
+
+        [DllImport(Libraries.Bundle, EntryPoint = "bundle_decode")]
+        internal static extern SafeBundleHandle BundleDecode(string bundleRaw, int len);
+
         [DllImport(Libraries.Bundle, EntryPoint = "bundle_dup")]
         internal static extern SafeBundleHandle DangerousClone(IntPtr handle);
 
index eafb307..1b56e8e 100755 (executable)
@@ -586,6 +586,53 @@ namespace Tizen.Applications
         }
 
         /// <summary>
+        /// Decodes an encoded bundle data.
+        /// </summary>
+        /// <param name="bundleRaw">The encoded bundle data. bundleRaw should be return value of Tizen.Applications.Bundle.Encode, otherwise this method will not succeed</param>
+        /// <returns>Decoded Bundle object.</returns>
+        /// <exception cref="System.ArgumentException">Thrown when there is an invalid parameter.</exception>
+        /// <code>
+        /// Tizen.Applications.Bundle bundle = new Tizen.Applications.Bundle();
+        /// string bundleRaw = bundle.Encode();
+        /// Bundle data = bundle.Decode(bundleRaw);
+        /// </code>
+        public static Bundle Decode(string bundleRaw)
+        {
+            SafeBundleHandle handle;
+
+            handle = Interop.Bundle.BundleDecode(bundleRaw, bundleRaw.Length);
+            if (ErrorFacts.GetLastResult() == (int)BundleErrorFactory.BundleError.InvalidParameter)
+            {
+                throw new ArgumentException("Invalid bundle raw");
+            }
+
+            return new Bundle(handle);
+        }
+
+        /// <summary>
+        /// Encodes bundle to string.
+        /// </summary>
+        /// <returns>Encoded Bundle data in string.</returns>
+        /// <exception cref="System.InvalidOperationException">Thrown when out of memory or when the Bundle instance has been disposed.</exception>
+        /// <code>
+        /// Tizen.Applications.Bundle bundle = new Tizen.Applications.Bundle();
+        /// string bundleRaw = bundle.Encode();
+        /// </code>
+        public string Encode()
+        {
+            string bundleRaw;
+            int len;
+
+            Interop.Bundle.BundleEncode(_handle, out bundleRaw, out len);
+            if (ErrorFacts.GetLastResult() == (int)BundleErrorFactory.BundleError.InvalidParameter)
+            {
+                throw new InvalidOperationException("Invalid bundle");
+            }
+
+            return bundleRaw;
+        }
+
+        /// <summary>
         /// Releases any unmanaged resources used by this object. Can also dispose any other disposable objects.
         /// </summary>
         /// <param name="disposing">If true, disposes any disposable objects. If false, does not dispose disposable objects.</param>