From e8ca3db284f04cb4d65664d7cf88607813082277 Mon Sep 17 00:00:00 2001 From: hyotaekshim <35134695+hyotaekshim@users.noreply.github.com> Date: Mon, 29 Jan 2018 16:36:22 +0900 Subject: [PATCH] [Storage] Add new enum and new methods (#7) * [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 * Update Storage.cs * Update StorageDevice.cs * Update StorageManager.cs --- .../Interop/Interop.Storage.cs | 18 +++ src/Tizen.System.Storage/Storage/Storage.cs | 126 +++++++++++++++++++++ src/Tizen.System.Storage/Storage/StorageArea.cs | 5 + src/Tizen.System.Storage/Storage/StorageDevice.cs | 41 +++++++ src/Tizen.System.Storage/Storage/StorageManager.cs | 80 +++++++++++++ 5 files changed, 270 insertions(+) create mode 100644 src/Tizen.System.Storage/Storage/StorageDevice.cs diff --git a/src/Tizen.System.Storage/Interop/Interop.Storage.cs b/src/Tizen.System.Storage/Interop/Interop.Storage.cs index 5f04e08..df6473e 100644 --- a/src/Tizen.System.Storage/Interop/Interop.Storage.cs +++ b/src/Tizen.System.Storage/Interop/Interop.Storage.cs @@ -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); } } diff --git a/src/Tizen.System.Storage/Storage/Storage.cs b/src/Tizen.System.Storage/Storage/Storage.cs index 67b327d..6746081 100644 --- a/src/Tizen.System.Storage/Storage/Storage.cs +++ b/src/Tizen.System.Storage/Storage/Storage.cs @@ -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 } /// + /// The StorageDevice + /// + /// 4 + /// Thrown when DeviceType is not initialized. + 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; + } + } + + /// + /// The type of file system + /// + /// 4 + /// Thrown when Fstype is not initialized. + 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; + } + } + + /// + /// The uuid of the file system + /// + /// 4 + /// Thrown when Fsuuid is not initialized. + 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; + } + } + + /// + /// Information about whether this is primary partition + /// + /// 4 + /// Thrown when Primary is not initialized. + 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; + } + } + + /// + /// The flags for the storage status + /// + /// 4 + /// Thrown when Flags is not initialized. + 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; + } + } + + /// /// The available storage size in bytes. /// /// 3 diff --git a/src/Tizen.System.Storage/Storage/StorageArea.cs b/src/Tizen.System.Storage/Storage/StorageArea.cs index 2d9a26b..4ae3a12 100644 --- a/src/Tizen.System.Storage/Storage/StorageArea.cs +++ b/src/Tizen.System.Storage/Storage/StorageArea.cs @@ -32,5 +32,10 @@ namespace Tizen.System /// /// 3 External = Interop.Storage.StorageArea.External, + /// + /// Extended internal storage + /// + /// 4 + 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 index 0000000..ba88ea0 --- /dev/null +++ b/src/Tizen.System.Storage/Storage/StorageDevice.cs @@ -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 +{ + /// + /// Enumeration for storage devices. + /// + /// 4 + public enum StorageDevice + { + /// + /// External sd card device + /// + /// 4 + ExternalSDCard = Interop.Storage.StorageDevice.ExternalSDCard, + /// + /// External usb mass storage + /// + /// 4 + ExternalUSBMassStorage = Interop.Storage.StorageDevice.ExternalUSBMassStorage, + /// + /// Extended internal storage + /// + /// 4 + ExtendedInternalStorage = Interop.Storage.StorageDevice.ExtendedInternalStorage, + } +} diff --git a/src/Tizen.System.Storage/Storage/StorageManager.cs b/src/Tizen.System.Storage/Storage/StorageManager.cs index 740f47d..55bcc52 100644 --- a/src/Tizen.System.Storage/Storage/StorageManager.cs +++ b/src/Tizen.System.Storage/Storage/StorageManager.cs @@ -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)); + } + } + + /// + /// Registers an eventhandler for state chages of specific storage type + /// + /// Storage type + /// An eventhandler to register + /// 4 + 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; + } + + /// + /// Unregisters an eventhandler for state chages of specific storage type + /// + /// Storage type + /// An eventhandler to unregister + /// 4 + 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); + } + } } } -- 2.7.4