[Storage] Add new enum and new methods (#7) 5.0.0-preview1-00453
authorhyotaekshim <35134695+hyotaekshim@users.noreply.github.com>
Mon, 29 Jan 2018 07:36:22 +0000 (16:36 +0900)
committerprjung <33618885+prjung@users.noreply.github.com>
Mon, 29 Jan 2018 07:36:22 +0000 (16:36 +0900)
* [Tizen.System.Storage]Add new enum and new class

- Add ExtendedInternal on StorageArea
- Add StorageDevice enumerations
- Add SetChangedEvent and UnsetChangedEvent on StorageManager class for storage_set_changed_cb and storage_unset_changed_cb apis

Change-Id: Ic5af889a8bcc40b3667081e0ec8088df697006ee
Signed-off-by: pr.jung <pr.jung@samsung.com>
* Update Storage.cs

* Update StorageDevice.cs

* Update StorageManager.cs

src/Tizen.System.Storage/Interop/Interop.Storage.cs
src/Tizen.System.Storage/Storage/Storage.cs
src/Tizen.System.Storage/Storage/StorageArea.cs
src/Tizen.System.Storage/Storage/StorageDevice.cs [new file with mode: 0644]
src/Tizen.System.Storage/Storage/StorageManager.cs

index 5f04e08..df6473e 100644 (file)
@@ -35,6 +35,7 @@ internal static partial class Interop
         {
             Internal = 0,
             External = 1,
+            ExtendedInternal = 2,
         }
 
         // Any change here might require changes in Tizen.System.StorageState enum
@@ -60,6 +61,17 @@ internal static partial class Interop
             Ringtones = 8,
         }
 
+        // Any change here might require changes in Tizen.System.StorageDevice enum
+        internal enum StorageDevice
+        {
+            ExternalSDCard = 1001,
+            ExternalUSBMassStorage = 1002,
+            ExtendedInternalStorage = 1003,
+        }
+
+        [UnmanagedFunctionPointerAttribute(CallingConvention.Cdecl)]
+        internal delegate void StorageChangedCallback(int id, StorageDevice devicetype, StorageState state, string fstype, string fsuuid, string mountpath, bool primary, int flags, IntPtr userData);
+
         [UnmanagedFunctionPointerAttribute(CallingConvention.Cdecl)]
         internal delegate void StorageStateChangedCallback(int id, StorageState state, IntPtr userData);
 
@@ -92,5 +104,11 @@ internal static partial class Interop
 
         [DllImport(Libraries.Storage, EntryPoint = "storage_foreach_device_supported")]
         public static extern ErrorCode StorageManagerGetForeachDeviceSupported(StorageDeviceSupportedCallback callback, IntPtr userData);
+
+        [DllImport(Libraries.Storage, EntryPoint = "storage_set_changed_cb")]
+        internal static extern ErrorCode StorageSetChanged(int id, StorageChangedCallback callback, IntPtr userData);
+
+        [DllImport(Libraries.Storage, EntryPoint = "storage_unset_changed_cb")]
+        internal static extern ErrorCode StorageUnsetChanged(int id, StorageChangedCallback callback);
     }
 }
index 67b327d..6746081 100644 (file)
@@ -26,7 +26,13 @@ namespace Tizen.System
     {
         private const string LogTag = "Tizen.System";
         private Interop.Storage.StorageState _state;
+        private StorageDevice _devicetype;
+        private string _fstype;
+        private string _fsuuid;
         private ulong _totalSpace;
+        private bool _primary;
+        private int _flags;
+        private bool information_set = false;
 
         internal Storage(int storageID, Interop.Storage.StorageArea storageType, Interop.Storage.StorageState storagestate, string rootDirectory)
         {
@@ -35,6 +41,36 @@ namespace Tizen.System
             RootDirectory = rootDirectory;
             _state = storagestate;
 
+
+            Interop.Storage.ErrorCode err = Interop.Storage.StorageGetTotalSpace(Id, out _totalSpace);
+            if (err != Interop.Storage.ErrorCode.None)
+            {
+                Log.Warn(LogTag, string.Format("Failed to get total storage space for storage Id: {0}. err = {1}", Id, err));
+            }
+
+            s_stateChangedEventCallback = (id, state, userData) =>
+            {
+                if (id == Id)
+                {
+                    _state = state;
+                    s_stateChangedEventHandler?.Invoke(this, EventArgs.Empty);
+                }
+            };
+        }
+
+        internal Storage(int storageID, Interop.Storage.StorageArea storageType, Interop.Storage.StorageState storagestate, string rootDirectory, Interop.Storage.StorageDevice devicetype, string fstype, string fsuuid, bool primary, int flags)
+        {
+            Id = storageID;
+            StorageType = (StorageArea)storageType;
+            RootDirectory = rootDirectory;
+            _state = storagestate;
+            _devicetype = (StorageDevice)devicetype;
+            _fstype = fstype;
+            _fsuuid = fsuuid;
+            _primary = primary;
+            _flags = flags;
+            information_set = true;
+
             Interop.Storage.ErrorCode err = Interop.Storage.StorageGetTotalSpace(Id, out _totalSpace);
             if (err != Interop.Storage.ErrorCode.None)
             {
@@ -154,6 +190,96 @@ namespace Tizen.System
         }
 
         /// <summary>
+        /// The StorageDevice
+        /// </summary>
+        /// <since_tizen> 4 </since_tizen>
+        /// <exception cref="InvalidOperationException">Thrown when DeviceType is not initialized.</exception>
+        public StorageDevice DeviceType
+        {
+            get
+            {
+                if (!information_set)
+                {
+                    Log.Error(LogTag, string.Format("Doesn't know StorageDevice type."));
+                    throw new InvalidOperationException("Doesn't know StorageDevice type");
+                }
+                return (StorageDevice)_devicetype;
+            }
+        }
+
+        /// <summary>
+        /// The type of file system
+        /// </summary>
+        /// <since_tizen> 4 </since_tizen>
+        /// <exception cref="InvalidOperationException">Thrown when Fstype is not initialized.</exception>
+        public string Fstype
+        {
+            get
+            {
+                if (!information_set)
+                {
+                    Log.Error(LogTag, string.Format("Doesn't know fstype."));
+                    throw new InvalidOperationException("Doesn't know type of file system");
+                }
+                return _fstype;
+            }
+        }
+
+        /// <summary>
+        /// The uuid of the file system
+        /// </summary>
+        /// <since_tizen> 4 </since_tizen>
+        /// <exception cref="InvalidOperationException">Thrown when Fsuuid is not initialized.</exception>
+        public string Fsuuid
+        {
+            get
+            {
+                if (!information_set)
+                {
+                    Log.Error(LogTag, string.Format("Doesn't know fsuuid."));
+                    throw new InvalidOperationException("Doesn't know uuid of file system");
+                }
+                return _fsuuid;
+            }
+        }
+
+        /// <summary>
+        /// Information about whether this is primary partition
+        /// </summary>
+        /// <since_tizen> 4 </since_tizen>
+        /// <exception cref="InvalidOperationException">Thrown when Primary is not initialized.</exception>
+        public bool Primary
+        {
+            get
+            {
+                if (!information_set)
+                {
+                    Log.Error(LogTag, string.Format("Doesn't know primary information."));
+                    throw new InvalidOperationException("Doesn't know primary information");
+                }
+                return _primary;
+            }
+        }
+
+        /// <summary>
+        /// The flags for the storage status
+        /// </summary>
+        /// <since_tizen> 4 </since_tizen>
+        /// <exception cref="InvalidOperationException">Thrown when Flags is not initialized.</exception>
+        public int Flags
+        {
+            get
+            {
+                if (!information_set)
+                {
+                    Log.Error(LogTag, string.Format("Doesn't know flags."));
+                    throw new InvalidOperationException("Doesn't know flags");
+                }
+                return _flags;
+            }
+        }
+
+        /// <summary>
         /// The available storage size in bytes.
         /// </summary>
         /// <since_tizen> 3 </since_tizen>
index 2d9a26b..4ae3a12 100644 (file)
@@ -32,5 +32,10 @@ namespace Tizen.System
         /// </summary>
         /// <since_tizen> 3 </since_tizen>
         External = Interop.Storage.StorageArea.External,
+        /// <summary>
+        /// Extended internal storage
+        /// </summary>
+        /// <since_tizen> 4 </since_tizen>
+        ExtendedInternal = Interop.Storage.StorageArea.ExtendedInternal,
     }
 }
diff --git a/src/Tizen.System.Storage/Storage/StorageDevice.cs b/src/Tizen.System.Storage/Storage/StorageDevice.cs
new file mode 100644 (file)
index 0000000..ba88ea0
--- /dev/null
@@ -0,0 +1,41 @@
+/*
+* Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
+*
+* Licensed under the Apache License, Version 2.0 (the License);
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+* http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an AS IS BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+
+namespace Tizen.System
+{
+    /// <summary>
+    /// Enumeration for storage devices.
+    /// </summary>
+    /// <since_tizen> 4 </since_tizen>
+    public enum StorageDevice
+    {
+        /// <summary>
+        /// External sd card device
+        /// </summary>
+        /// <since_tizen> 4 </since_tizen>
+        ExternalSDCard = Interop.Storage.StorageDevice.ExternalSDCard,
+        /// <summary>
+        /// External usb mass storage
+        /// </summary>
+        /// <since_tizen> 4 </since_tizen>
+        ExternalUSBMassStorage = Interop.Storage.StorageDevice.ExternalUSBMassStorage,
+        /// <summary>
+        /// Extended internal storage
+        /// </summary>
+        /// <since_tizen> 4 </since_tizen>
+        ExtendedInternalStorage = Interop.Storage.StorageDevice.ExtendedInternalStorage,
+    }
+}
index 740f47d..55bcc52 100644 (file)
@@ -50,5 +50,85 @@ namespace Tizen.System
                 return storageList;
             }
         }
+        
+        private static EventHandler s_ExternalStorageChangedEventHandler;
+        private static EventHandler s_ExtendedInternalStorageChangedEventHandler;
+        private static Interop.Storage.StorageChangedCallback s_ChangedEventCallback = (int id, Interop.Storage.StorageDevice devicetype, Interop.Storage.StorageState state, string fstype, string fsuuid, string rootDirectory, bool primary, int flags, IntPtr userData) =>
+        {
+            Storage storage = new Storage(id, Interop.Storage.StorageArea.External, state, rootDirectory, devicetype, fstype, fsuuid, primary, flags);
+
+            if (devicetype == Interop.Storage.StorageDevice.ExtendedInternalStorage)
+                s_ExtendedInternalStorageChangedEventHandler?.Invoke(storage, EventArgs.Empty);
+            else
+                s_ExternalStorageChangedEventHandler?.Invoke(storage, EventArgs.Empty);
+        };
+
+        private static void RegisterChangedEvent(StorageArea type)
+        {
+            Interop.Storage.ErrorCode err = Interop.Storage.StorageSetChanged((int)type, s_ChangedEventCallback, IntPtr.Zero);
+            if (err != Interop.Storage.ErrorCode.None)
+            {
+                Log.Warn(LogTag, string.Format("Failed to Register changed event callback for external storage. err = {0}", err));
+            }
+        }
+
+        private static void UnregisterChangedEvent(StorageArea type)
+        {
+            Interop.Storage.ErrorCode err = Interop.Storage.StorageUnsetChanged((int)type, s_ChangedEventCallback);
+            if (err != Interop.Storage.ErrorCode.None)
+            {
+                Log.Warn(LogTag, string.Format("Failed to Unreegister changed event callback for external storage. err = {0}", err));
+            }
+        }
+
+        /// <summary>
+        /// Registers an eventhandler for state chages of specific storage type
+        /// </summary>
+        /// <param name="type">Storage type</param>
+        /// <param name="handler">An eventhandler to register</param>
+        /// <since_tizen> 4 </since_tizen>
+        public static void SetChangedEvent(StorageArea type, EventHandler handler)
+        {
+            if (type == StorageArea.Internal)
+            {
+                Log.Warn(LogTag, "Internal storage state is not changed");
+            }
+            if (s_ExternalStorageChangedEventHandler == null && s_ExtendedInternalStorageChangedEventHandler == null)
+            {
+                RegisterChangedEvent(type);
+            }
+            if ((type == StorageArea.External && s_ExternalStorageChangedEventHandler == null) || 
+                (type == StorageArea.ExtendedInternal && s_ExtendedInternalStorageChangedEventHandler == null))
+            {
+                RegisterChangedEvent(type);
+            }
+            if (type == StorageArea.External)
+                s_ExternalStorageChangedEventHandler += handler;
+            else if (type == StorageArea.ExtendedInternal)
+                s_ExtendedInternalStorageChangedEventHandler += handler;
+        }
+
+        /// <summary>
+        /// Unregisters an eventhandler for state chages of specific storage type
+        /// </summary>
+        /// <param name="type">Storage type</param>
+        /// <param name="handler">An eventhandler to unregister</param>
+        /// <since_tizen> 4 </since_tizen>
+        public static void UnsetChangedEvent(StorageArea type, EventHandler handler)
+        {
+            if (type == StorageArea.Internal)
+            {
+                Log.Warn(LogTag, "Internal storage state is not changed");
+            }
+            if (type == StorageArea.External)
+                s_ExternalStorageChangedEventHandler -= handler;
+            else if (type == StorageArea.ExtendedInternal)
+                s_ExtendedInternalStorageChangedEventHandler -= handler;
+            if ((type == StorageArea.External && s_ExternalStorageChangedEventHandler == null) ||
+                (type == StorageArea.ExtendedInternal && s_ExtendedInternalStorageChangedEventHandler == null))
+            {
+                UnregisterChangedEvent(type);
+            }
+        }
     }
 }