/// <summary> /// Adds an item into the bundle. /// </summary> /// <param name="key">The key to identify the item with. If an item with the key already exists in the bundle, this method will not succeed.</param> /// <param name="value">The value of the item.</param> /// <param name="offset">The zero-based byte offset in value from which to add to the bundle.</param> /// <param name="count">The maximum number of bytes to add to the bundle starting with offset.</param> /// <exception cref="System.ArgumentOutOfRangeException">Thrown when the offset or count is out of range.</exception> /// <exception cref="System.ArgumentException">Thrown when the key already exists or when there is an invalid parameter.</exception> /// <exception cref="System.ArgumentNullException">Thrown when a value is null.</exception> /// <exception cref="System.InvalidOperationException">Thrown when out of memory or when the bundle instance has been disposed.</exception> /// <example> /// <code> /// Tizen.Applications.Bundle bundle = new Tizen.Applications.Bundle(); /// byte[] byteArray = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; /// bundle.AddItem("byte_array", byteArray, 2, 3); /// </code> /// </example> /// <since_tizen> 3 </since_tizen> public void AddItem(string key, byte[] value, int offset, int count) { if (!_keys.Contains(key)) { if (value == null) { throw new ArgumentNullException("value"); } if (offset < 0) { throw new ArgumentOutOfRangeException("offset", offset, "Cannot be less than 0"); } if (offset > value.Length - 1) { throw new ArgumentOutOfRangeException("offset", offset, "Greater than last index of array"); } if (count < 1) { throw new ArgumentOutOfRangeException("count", count, "Must be at least 1"); } if (offset + count > value.Length) { throw new ArgumentException("The count is too large for the specified offset"); } // Code is in Interop file because it is unsafe int ret = Interop.Bundle.UnsafeCode.AddItem(_handle, key, value, offset, count); BundleErrorFactory.CheckAndThrowException(ret, _handle); _keys.Add(key); } else { throw new ArgumentException("Key already exists", "key"); } }
/// <summary> /// Adds an item into the bundle. /// </summary> /// <param name="key">The key to identify the item with. If an item with the key already exists in the bundle, this method will not succeed.</param> /// <param name="value">The value of the item.</param> /// <exception cref="System.ArgumentException">Thrown when the key already exists or when there is an invalid parameter.</exception> /// <exception cref="System.InvalidOperationException">Thrown when out of memory or when the bundle instance has been disposed.</exception> /// <example> /// <code> /// Tizen.Applications.Bundle bundle = new Tizen.Applications.Bundle(); /// bundle.AddItem("string", "a_string"); /// </code> /// </example> /// <since_tizen> 3 </since_tizen> public void AddItem(string key, string value) { if (!_keys.Contains(key)) { int ret = Interop.Bundle.AddString(_handle, key, value); BundleErrorFactory.CheckAndThrowException(ret, _handle); _keys.Add(key); } else { throw new ArgumentException("Key already exists", "key"); } }
/// <summary> /// Adds an item into the bundle. /// </summary> /// <param name="key">The key to identify the item with. If an item with the key already exists in the bundle, this method will not succeed.</param> /// <param name="value">The value of the item.</param> /// <exception cref="System.ArgumentException">Thrown when the key already exists or when there is an invalid parameter.</exception> /// <exception cref="System.InvalidOperationException">Thrown when out of memory or when the bundle instance has been disposed.</exception> /// <example> /// <code> /// Tizen.Applications.Bundle bundle = new Tizen.Applications.Bundle(); /// string[] stringArray = { "a", "b", "c" }; /// bundle.AddItem("string_array", stringArray); /// </code> /// </example> /// <since_tizen> 3 </since_tizen> public void AddItem(string key, IEnumerable <string> value) { if (!_keys.Contains(key)) { string[] valueArray = value.Select(v => v == null ? string.Empty : v).ToArray(); int ret = Interop.Bundle.AddStringArray(_handle, key, valueArray, valueArray.Count()); BundleErrorFactory.CheckAndThrowException(ret, _handle); _keys.Add(key); } else { throw new ArgumentException("Key already exists", "key"); } }
/// <summary> /// Gets the value of a bundle item with a specified key. /// </summary> /// <param name="key">The key of the bundle item whose value is desired.</param> /// <returns>The value of the bundle item.</returns> /// <exception cref="System.ArgumentException">Thrown when the key does not exist or when there is an invalid parameter.</exception> /// <exception cref="System.InvalidOperationException">Thrown when the bundle instance has been disposed.</exception> /// <example> /// <code> /// Tizen.Applications.Bundle bundle = new Tizen.Applications.Bundle(); /// bundle.AddItem("string", "a_string"); /// if (bundle.Contains("string")) /// { /// object aValue = bundle.GetItem("string"); /// if (bundle.Is<string>("string");) /// { /// string aString = (string)aValue; /// Console.WriteLine(aString); /// } /// } /// </code> /// </example> /// <since_tizen> 3 </since_tizen> public object GetItem(string key) { if (_keys.Contains(key)) { int type = Interop.Bundle.GetType(_handle, key); BundleErrorFactory.CheckAndThrowException(ErrorFacts.GetLastResult(), _handle); switch (type) { case (int)BundleType.String: // get string IntPtr stringPtr; int retString = Interop.Bundle.GetString(_handle, key, out stringPtr); BundleErrorFactory.CheckAndThrowException(retString, _handle); string stringValue = Marshal.PtrToStringAnsi(stringPtr); if (stringValue == null) { return(string.Empty); } return(stringValue); case (int)BundleType.StringArray: // get string array int stringArraySize; IntPtr stringArrayPtr = Interop.Bundle.GetStringArray(_handle, key, out stringArraySize); BundleErrorFactory.CheckAndThrowException(ErrorFacts.GetLastResult(), _handle); string[] stringArray; IntPtrToStringArray(stringArrayPtr, stringArraySize, out stringArray); return(stringArray); case (int)BundleType.Byte: // get byte array IntPtr byteArrayPtr; int byteArraySize; int retByte = Interop.Bundle.GetByte(_handle, key, out byteArrayPtr, out byteArraySize); BundleErrorFactory.CheckAndThrowException(retByte, _handle); byte[] byteArray = new byte[byteArraySize]; Marshal.Copy(byteArrayPtr, byteArray, 0, byteArraySize); return(byteArray); default: throw new ArgumentException("Key does not exist in the bundle", "key"); } } else { throw new ArgumentException("Key does not exist in the bundle (may be null or empty string)", "key"); } }
/// <summary> /// Removes a bundle item with a specific key from a Bundle. /// </summary> /// <param name="key">The key of the item to delete.</param> /// <returns>true if the item is successfully found and removed, false otherwise (even if the item is not found).</returns> /// <exception cref="System.ArgumentException">Thrown when there is an invalid parameter.</exception> /// <exception cref="System.InvalidOperationException">Thrown when the bundle instance has been disposed.</exception> /// <example> /// <code> /// Tizen.Applications.Bundle bundle = new Tizen.Applications.Bundle(); /// bundle.AddItem("string", "a_string"); /// if (bundle.Contains("string")) /// { /// if (bundle.RemoveItem("string")) /// { /// Console.WriteLine("Removed"); /// } /// } /// </code> /// </example> /// <since_tizen> 3 </since_tizen> public bool RemoveItem(string key) { if (_keys.Contains(key)) { int ret = Interop.Bundle.RemoveItem(_handle, key); if (ret == (int)BundleErrorFactory.BundleError.KeyNotAvailable) { return(false); } BundleErrorFactory.CheckAndThrowException(ret, _handle); _keys.Remove(key); return(true); } else { return(false); } }
/// <summary> /// The bundle constructor. /// </summary> /// <exception cref="System.InvalidOperationException">Thrown when out of memory.</exception> /// <example> /// <code> /// Tizen.Applications.Bundle bundle = new Tizen.Applications.Bundle(); /// </code> /// </example> /// <since_tizen> 3 </since_tizen> public Bundle() { _handle = Interop.Bundle.Create(); BundleErrorFactory.CheckAndThrowException(ErrorFacts.GetLastResult(), _handle); _keys = new HashSet <string>(); }