2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 namespace Tizen.System
22 /// The class to access the storage device information.
24 /// <since_tizen> 3 </since_tizen>
27 private const string LogTag = "Tizen.System";
28 private Interop.Storage.StorageState _state;
29 private ulong _totalSpace;
31 internal Storage(int storageID, Interop.Storage.StorageArea storageType, Interop.Storage.StorageState storagestate, string rootDirectory)
34 StorageType = (StorageArea)storageType;
35 RootDirectory = rootDirectory;
36 _state = storagestate;
38 Interop.Storage.ErrorCode err = Interop.Storage.StorageGetTotalSpace(Id, out _totalSpace);
39 if (err != Interop.Storage.ErrorCode.None)
41 Log.Warn(LogTag, string.Format("Failed to get total storage space for storage Id: {0}. err = {1}", Id, err));
44 s_stateChangedEventCallback = (id, state, userData) =>
49 s_stateChangedEventHandler?.Invoke(this, EventArgs.Empty);
54 private EventHandler s_stateChangedEventHandler;
55 private Interop.Storage.StorageStateChangedCallback s_stateChangedEventCallback;
57 private void RegisterStateChangedEvent()
59 Interop.Storage.ErrorCode err = Interop.Storage.StorageSetStateChanged(Id, s_stateChangedEventCallback, IntPtr.Zero);
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));
66 private void UnregisterStateChangedEvent()
68 Interop.Storage.ErrorCode err = Interop.Storage.StorageUnsetStateChanged(Id, s_stateChangedEventCallback);
69 if (err != Interop.Storage.ErrorCode.None)
71 Log.Warn(LogTag, string.Format("Failed to Register state changed event callback for storage Id: {0}. err = {1}", Id, err));
76 /// StorageStateChanged event. This event is occurred when a storage state changes.
79 /// The storage state will be updated before calling the event handler.
81 /// <since_tizen> 3 </since_tizen>
84 /// myStorage.StorageStateChanged += (s, e) =>
86 /// var storage = s as Storage;
87 /// Console.WriteLine(string.Format("State Changed to {0}", storage.State));
91 public event EventHandler StorageStateChanged
95 if (s_stateChangedEventHandler == null)
97 _state = (Interop.Storage.StorageState)State;
98 RegisterStateChangedEvent();
100 s_stateChangedEventHandler += value;
104 if (s_stateChangedEventHandler != null)
106 s_stateChangedEventHandler -= value;
107 if (s_stateChangedEventHandler == null)
109 UnregisterStateChangedEvent();
118 /// <since_tizen> 3 </since_tizen>
119 public int Id { get; }
121 /// The type of storage.
123 /// <since_tizen> 3 </since_tizen>
124 public StorageArea StorageType { get; }
126 /// The root directory for the storage.
128 /// <since_tizen> 3 </since_tizen>
129 public string RootDirectory { get; }
131 /// The total storage size in bytes.
133 /// <since_tizen> 3 </since_tizen>
134 public ulong TotalSpace { get { return _totalSpace; } }
137 /// The StorageState.
139 /// <since_tizen> 3 </since_tizen>
140 public StorageState State
144 if (s_stateChangedEventHandler == null)
146 Interop.Storage.ErrorCode err = Interop.Storage.StorageGetState(Id, out _state);
147 if (err != Interop.Storage.ErrorCode.None)
149 Log.Warn(LogTag, string.Format("Failed to get storage state for storage Id: {0}. err = {1}", Id, err));
152 return (StorageState)_state;
157 /// The available storage size in bytes.
159 /// <since_tizen> 3 </since_tizen>
160 public ulong AvaliableSpace
165 Interop.Storage.ErrorCode err = Interop.Storage.StorageGetAvailableSpace(Id, out available);
166 if (err != Interop.Storage.ErrorCode.None)
168 Log.Warn(LogTag, string.Format("Failed to get available storage stace for storage Id: {0}. err = {1}", Id, err));
176 /// Absolute path for a given directory type in the storage.
179 /// The returned directory path may not exist, so you must make sure that it exists before using it.
180 /// For accessing internal storage except the ringtones directory, the application should have http://tizen.org/privilege/mediastorage privilege.
181 /// For accessing ringtones directory, the application should have http://tizen.org/privilege/systemsettings privilege.
182 /// For accessing external storage, the application should have http://tizen.org/privilege/externalstorage privilege.
184 /// <since_tizen> 3 </since_tizen>
185 /// <param name="dirType">Directory type.</param>
186 /// <returns>Absolute path for a given directory type in the storage.</returns>
187 /// <exception cref="ArgumentException">Thrown when failed because of an invalid argument.</exception>
188 /// <exception cref="OutOfMemoryException">Thrown when failed due to out of memory exception.</exception>
189 /// <exception cref="NotSupportedException">Thrown when failed if the storage is not supported or the application does not have the permission to access the directory path.</exception>
190 /// <privilege>http://tizen.org/privilege/mediastorage</privilege>
191 /// <privilege>http://tizen.org/privilege/systemsettings</privilege>
192 /// <privilege>http://tizen.org/privilege/externalstorage</privilege>
195 /// // To get the video directories for all the supported storage,
196 /// var storageList = StorageManager.Storages as List<Storage>;
197 /// foreach (var storage in storageList)
199 /// string pathForVideoDir = storage.GetAbsolutePath(DirectoryType.Videos);
203 public string GetAbsolutePath(DirectoryType dirType)
206 Interop.Storage.ErrorCode err = Interop.Storage.StorageGetAbsoluteDirectory(Id, (Interop.Storage.DirectoryType)dirType, out path);
207 if (err != Interop.Storage.ErrorCode.None)
209 Log.Warn(LogTag, string.Format("Failed to get package Id. err = {0}", err));
212 case Interop.Storage.ErrorCode.InvalidParameter:
213 throw new ArgumentException("Invalid Arguments");
214 case Interop.Storage.ErrorCode.OutOfMemory:
215 throw new OutOfMemoryException("Out of Memory");
216 case Interop.Storage.ErrorCode.NotSupported:
217 throw new NotSupportedException("Operation Not Supported");
219 throw new InvalidOperationException("Error = " + err);