1 // Copyright 2016 by Samsung Electronics, Inc.,
3 // This software is the confidential and proprietary information
4 // of Samsung Electronics, Inc. ("Confidential Information"). You
5 // shall not disclose such Confidential Information and shall use
6 // it only in accordance with the terms of the license agreement
7 // you entered into with Samsung.
11 namespace Tizen.System
14 /// class to access storage device information
18 private const string LogTag = "Tizen.System";
19 private Interop.Storage.StorageState _state;
20 private ulong _totalSpace;
22 internal Storage(int storageID, Interop.Storage.StorageArea storageType, Interop.Storage.StorageState storagestate, string rootDirectory)
25 StorageType = (StorageArea)storageType;
26 RootDirectory = rootDirectory;
27 _state = storagestate;
29 Interop.Storage.ErrorCode err = Interop.Storage.StorageGetTotalSpace(Id, out _totalSpace);
30 if (err != Interop.Storage.ErrorCode.None)
32 Log.Warn(LogTag, string.Format("Failed to get total storage space for storage Id: {0}. err = {1}", Id, err));
35 s_stateChangedEventCallback = (id, state, userData) =>
40 s_stateChangedEventHandler?.Invoke(this, EventArgs.Empty);
45 private EventHandler s_stateChangedEventHandler;
46 private Interop.Storage.StorageStateChangedCallback s_stateChangedEventCallback;
48 private void RegisterStateChangedEvent()
50 Interop.Storage.ErrorCode err = Interop.Storage.StorageSetStateChanged(Id, s_stateChangedEventCallback, IntPtr.Zero);
51 if (err != Interop.Storage.ErrorCode.None)
53 Log.Warn(LogTag, string.Format("Failed to Register state changed event callback for storage Id: {0}. err = {1}", Id, err));
57 private void UnregisterStateChangedEvent()
59 Interop.Storage.ErrorCode err = Interop.Storage.StorageUnsetStateChanged(Id, s_stateChangedEventCallback);
60 if (err != Interop.Storage.ErrorCode.None)
62 Log.Warn(LogTag, string.Format("Failed to Register state changed event callback for storage Id: {0}. err = {1}", Id, err));
67 /// StorageStateChanged event. This event is occurred when a storage state changes.
70 /// Storage state will be updated before calling event handler.
74 /// myStorage.StorageStateChanged += (s, e) =>
76 /// var storage = s as Storage;
77 /// Console.WriteLine(string.Format("State Changed to {0}", storage.State));
81 public event EventHandler StorageStateChanged
85 if (s_stateChangedEventHandler == null)
87 _state = (Interop.Storage.StorageState)State;
88 RegisterStateChangedEvent();
90 s_stateChangedEventHandler += value;
94 if (s_stateChangedEventHandler != null)
96 s_stateChangedEventHandler -= value;
97 if (s_stateChangedEventHandler == null)
99 UnregisterStateChangedEvent();
108 public int Id { get; }
110 /// Type of the storage
112 public StorageArea StorageType { get; }
114 /// Root directory for the storage
116 public string RootDirectory { get; }
118 /// Total storage size in bytes
120 public ulong TotalSpace { get { return _totalSpace; } }
125 public StorageState State
129 if (s_stateChangedEventHandler == null)
131 Interop.Storage.ErrorCode err = Interop.Storage.StorageGetState(Id, out _state);
132 if (err != Interop.Storage.ErrorCode.None)
134 Log.Warn(LogTag, string.Format("Failed to get storage state for storage Id: {0}. err = {1}", Id, err));
137 return (StorageState)_state;
142 /// Available storage size in bytes
144 public ulong AvaliableSpace
149 Interop.Storage.ErrorCode err = Interop.Storage.StorageGetAvailableSpace(Id, out available);
150 if (err != Interop.Storage.ErrorCode.None)
152 Log.Warn(LogTag, string.Format("Failed to get available storage stace for storage Id: {0}. err = {1}", Id, err));
160 /// Absolute path for given directory type in storage
163 /// returned directory path may not exist, so you must make sure that it exists before using it.
164 /// For accessing internal storage except Ringtones directory, app should have http://tizen.org/privilege/mediastorage privilege.
165 /// For accessing Ringtones directory, app should have http://tizen.org/privilege/systemsettings privilege.
166 /// For accessing external storage, app should have http://tizen.org/privilege/externalstorage privilege.
168 /// <param name="dirType">Directory type</param>
169 /// <returns>Absolute path for given directory type in storage</returns>
170 /// <exception cref="ArgumentException">Thrown when failed because of a invalid arguament</exception>
171 /// <exception cref="OutOfMemoryException">Thrown when failed due to out of memory exception</exception>
172 /// <exception cref="NotSupportedException">Thrown when failed if storage is not supported or app does not have permission to access directory path</exception>
173 /// <privilege>http://tizen.org/privilege/mediastorage</privilege>
174 /// <privilege>http://tizen.org/privilege/systemsettings</privilege>
175 /// <privilege>http://tizen.org/privilege/externalstorage</privilege>
178 /// // To get video directories for all supported storage,
179 /// var storageList = StorageManager.Storages as List<Storage>;
180 /// foreach (var storage in storageList)
182 /// string pathForVideoDir = storage.GetAbsolutePath(DirectoryType.Videos);
186 public string GetAbsolutePath(DirectoryType dirType)
188 string path = string.Empty;
189 Interop.Storage.ErrorCode err = Interop.Storage.StorageGetAbsoluteDirectory(Id, (Interop.Storage.DirectoryType)dirType, out path);
190 if (err != Interop.Storage.ErrorCode.None)
192 Log.Warn(LogTag, string.Format("Failed to get package Id. err = {0}", err));
195 case Interop.Storage.ErrorCode.InvalidParameter:
196 throw new ArgumentException("Invalid Arguments");
197 case Interop.Storage.ErrorCode.OutOfMemory:
198 throw new OutOfMemoryException("Out of Memory");
199 case Interop.Storage.ErrorCode.NotSupported:
200 throw new NotSupportedException("Operation Not Supported");
202 throw new InvalidOperationException("Error = " + err);