|
23 | 23 | using System.Collections.ObjectModel; |
24 | 24 | using Windows.Devices.Enumeration; |
25 | 25 | using System.Text.RegularExpressions; |
| 26 | +using Windows.Devices.Portable; |
| 27 | +using Windows.UI.Xaml.Media.Imaging; |
| 28 | +using Windows.ApplicationModel.Core; |
| 29 | +using Windows.UI.Core; |
| 30 | +using Windows.Storage.Search; |
26 | 31 |
|
27 | 32 | namespace Files |
28 | 33 | { |
29 | 34 | sealed partial class App : Application |
30 | 35 | { |
31 | 36 | public static ProHome selectedTabInstance { get; set; } |
| 37 | + DeviceWatcher watcher; |
32 | 38 | public App() |
33 | 39 | { |
34 | 40 | this.InitializeComponent(); |
@@ -113,91 +119,207 @@ public App() |
113 | 119 | localSettings.Values["DrivesDisplayed_NewTab"] = false; |
114 | 120 | } |
115 | 121 |
|
116 | | - FindDrives(); |
117 | | - //DeviceWatcher watcher = DeviceInformation.CreateWatcher(); |
118 | | - //watcher.Added += (sender, info) => FindDrives(); |
119 | | - //watcher.Removed += (sender, info) => FindDrives(); |
120 | | - //watcher.Start(); |
| 122 | + //FindDrives(); |
| 123 | + |
121 | 124 | } |
122 | 125 |
|
123 | | - private async void FindDrives() |
| 126 | + public void PopulateDrivesListWithLocalDisks() |
124 | 127 | { |
125 | | - foundDrives.Clear(); |
126 | | - var knownRemDevices = new ObservableCollection<string>(); |
127 | | - foreach (var f in await KnownFolders.RemovableDevices.GetFoldersAsync()) |
128 | | - { |
129 | | - var path = f.Path; |
130 | | - knownRemDevices.Add(path); |
131 | | - } |
132 | | - |
133 | 128 | var driveLetters = DriveInfo.GetDrives().Select(x => x.RootDirectory.Root).ToList().OrderBy(x => x.Root.FullName).ToList(); |
134 | | - |
135 | | - if (!driveLetters.Any()) return; |
136 | | - |
137 | 129 | driveLetters.ForEach(async roots => |
138 | 130 | { |
139 | 131 | try |
140 | 132 | { |
141 | | - //if (roots.Name == @"C:\") return; |
142 | 133 | var content = string.Empty; |
143 | | - string icon; |
144 | | - if (knownRemDevices.Contains(roots.Name)) |
| 134 | + string icon = null; |
| 135 | + if (!(await KnownFolders.RemovableDevices.GetFoldersAsync()).Select(x => x.Path).ToList().Contains(roots.Name)) |
145 | 136 | { |
146 | | - content = $"Removable Drive ({roots.Name})"; |
147 | | - icon = "\uE88E"; |
148 | | - } |
149 | | - else |
150 | | - { |
151 | | - content = $"Local Disk ({roots.Name})"; |
| 137 | + // TODO: Display Custom Names for Local Disks as well |
| 138 | + content = $"Local Disk ({roots.Name.TrimEnd('\\')})"; |
152 | 139 | icon = "\uEDA2"; |
153 | | - } |
154 | | - StorageFolder drive = await StorageFolder.GetFolderFromPathAsync(roots.Name); |
155 | | - var retrivedProperties = await drive.Properties.RetrievePropertiesAsync(new string[] { "System.FreeSpace", "System.Capacity" }); |
156 | | - |
157 | | - ulong totalSpaceProg = 0; |
158 | | - ulong freeSpaceProg = 0; |
159 | | - string free_space_text = "Unknown"; |
160 | | - string total_space_text = "Unknown"; |
161 | | - Visibility capacityBarVis = Visibility.Visible; |
162 | | - try |
163 | | - { |
164 | | - var sizeAsGBString = ByteSizeLib.ByteSize.FromBytes((ulong)retrivedProperties["System.FreeSpace"]).GigaBytes; |
165 | | - freeSpaceProg = Convert.ToUInt64(sizeAsGBString); |
166 | 140 |
|
167 | | - sizeAsGBString = ByteSizeLib.ByteSize.FromBytes((ulong)retrivedProperties["System.Capacity"]).GigaBytes; |
168 | | - totalSpaceProg = Convert.ToUInt64(sizeAsGBString); |
169 | | - |
170 | | - |
171 | | - free_space_text = ByteSizeLib.ByteSize.FromBytes((ulong)retrivedProperties["System.FreeSpace"]).ToString(); |
172 | | - total_space_text = ByteSizeLib.ByteSize.FromBytes((ulong)retrivedProperties["System.Capacity"]).ToString(); |
173 | | - } |
174 | | - catch (UnauthorizedAccessException) |
175 | | - { |
176 | | - capacityBarVis = Visibility.Collapsed; |
177 | | - } |
178 | | - catch (NullReferenceException) |
179 | | - { |
180 | | - capacityBarVis = Visibility.Collapsed; |
| 141 | + await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Low, |
| 142 | + async () => |
| 143 | + { |
| 144 | + StorageFolder drive = await StorageFolder.GetFolderFromPathAsync(roots.Name); |
| 145 | + var retrivedProperties = await drive.Properties.RetrievePropertiesAsync(new string[] { "System.FreeSpace", "System.Capacity" }); |
| 146 | + |
| 147 | + ulong totalSpaceProg = 0; |
| 148 | + ulong freeSpaceProg = 0; |
| 149 | + string free_space_text = "Unknown"; |
| 150 | + string total_space_text = "Unknown"; |
| 151 | + Visibility capacityBarVis = Visibility.Visible; |
| 152 | + try |
| 153 | + { |
| 154 | + var sizeAsGBString = ByteSizeLib.ByteSize.FromBytes((ulong)retrivedProperties["System.FreeSpace"]).GigaBytes; |
| 155 | + freeSpaceProg = Convert.ToUInt64(sizeAsGBString); |
| 156 | + |
| 157 | + sizeAsGBString = ByteSizeLib.ByteSize.FromBytes((ulong)retrivedProperties["System.Capacity"]).GigaBytes; |
| 158 | + totalSpaceProg = Convert.ToUInt64(sizeAsGBString); |
| 159 | + |
| 160 | + |
| 161 | + free_space_text = ByteSizeLib.ByteSize.FromBytes((ulong)retrivedProperties["System.FreeSpace"]).ToString(); |
| 162 | + total_space_text = ByteSizeLib.ByteSize.FromBytes((ulong)retrivedProperties["System.Capacity"]).ToString(); |
| 163 | + } |
| 164 | + catch (UnauthorizedAccessException) |
| 165 | + { |
| 166 | + capacityBarVis = Visibility.Collapsed; |
| 167 | + } |
| 168 | + catch (NullReferenceException) |
| 169 | + { |
| 170 | + capacityBarVis = Visibility.Collapsed; |
| 171 | + } |
| 172 | + |
| 173 | + App.foundDrives.Add(new DriveItem() |
| 174 | + { |
| 175 | + driveText = content, |
| 176 | + glyph = icon, |
| 177 | + maxSpace = totalSpaceProg, |
| 178 | + spaceUsed = totalSpaceProg - freeSpaceProg, |
| 179 | + tag = roots.Name, |
| 180 | + progressBarVisibility = capacityBarVis, |
| 181 | + spaceText = free_space_text + " free of " + total_space_text, |
| 182 | + }); |
| 183 | + }); |
181 | 184 | } |
182 | 185 |
|
183 | | - foundDrives.Add(new DriveItem() |
184 | | - { |
185 | | - driveText = content, |
186 | | - glyph = icon, |
187 | | - maxSpace = totalSpaceProg, |
188 | | - spaceUsed = totalSpaceProg - freeSpaceProg, |
189 | | - tag = roots.Name, |
190 | | - progressBarVisibility = capacityBarVis, |
191 | | - spaceText = free_space_text + " free of " + total_space_text, |
192 | | - }); |
193 | 186 | } |
194 | 187 | catch (UnauthorizedAccessException e) |
195 | 188 | { |
196 | 189 | Debug.WriteLine(e.Message); |
197 | 190 | } |
| 191 | + |
| 192 | + }); |
| 193 | + |
| 194 | + |
| 195 | + |
| 196 | + } |
| 197 | + |
| 198 | + private async void Watcher_EnumerationCompleted(DeviceWatcher sender, object args) |
| 199 | + { |
| 200 | + PopulateDrivesListWithLocalDisks(); |
| 201 | + DeviceAdded(sender, null); |
| 202 | + await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Low, |
| 203 | + () => |
| 204 | + { |
| 205 | + App.foundDrives.Add(new DriveItem() |
| 206 | + { |
| 207 | + driveText = "OneDrive", |
| 208 | + tag = "OneDrive", |
| 209 | + cloudGlyphVisibility = Visibility.Visible, |
| 210 | + driveGlyphVisibility = Visibility.Collapsed |
| 211 | + }); |
198 | 212 | }); |
199 | 213 | } |
200 | 214 |
|
| 215 | + private void DeviceUpdated(DeviceWatcher sender, DeviceInformationUpdate args) |
| 216 | + { |
| 217 | + Debug.WriteLine("Devices updated"); |
| 218 | + } |
| 219 | + |
| 220 | + |
| 221 | + private async void DeviceRemoved(DeviceWatcher sender, DeviceInformationUpdate args) |
| 222 | + { |
| 223 | + var devices = DriveInfo.GetDrives().Select(x => x.RootDirectory.Root).ToList().OrderBy(x => x.Root.FullName).ToList(); |
| 224 | + |
| 225 | + foreach (DriveItem driveItem in foundDrives) |
| 226 | + { |
| 227 | + if (!driveItem.tag.Equals("OneDrive")) |
| 228 | + { |
| 229 | + if (!devices.Any(x => x.Name == driveItem.tag) || devices.Equals(null)) |
| 230 | + { |
| 231 | + await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Low, |
| 232 | + () => |
| 233 | + { |
| 234 | + foundDrives.Remove(driveItem); |
| 235 | + }); |
| 236 | + return; |
| 237 | + |
| 238 | + } |
| 239 | + } |
| 240 | + |
| 241 | + } |
| 242 | + } |
| 243 | + |
| 244 | + private async void DeviceAdded(DeviceWatcher sender, DeviceInformation args) |
| 245 | + { |
| 246 | + try |
| 247 | + { |
| 248 | + //var device = StorageDevice.FromId(args.Id); |
| 249 | + var devices = (await KnownFolders.RemovableDevices.GetFoldersAsync()).OrderBy(x => x.Path); |
| 250 | + foreach(StorageFolder device in devices) |
| 251 | + { |
| 252 | + var letter = device.Path; |
| 253 | + if(!foundDrives.Any(x => x.tag == letter)) |
| 254 | + { |
| 255 | + //if (roots.Name == @"C:\") return; |
| 256 | + var content = device.DisplayName; |
| 257 | + string icon = null; |
| 258 | + await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Low, |
| 259 | + async () => |
| 260 | + { |
| 261 | + if (content.Contains("DVD")) |
| 262 | + { |
| 263 | + icon = "\uE958"; |
| 264 | + } |
| 265 | + else |
| 266 | + { |
| 267 | + icon = "\uE88E"; |
| 268 | + } |
| 269 | + |
| 270 | + StorageFolder drive = await StorageFolder.GetFolderFromPathAsync(letter); |
| 271 | + var retrivedProperties = await drive.Properties.RetrievePropertiesAsync(new string[] { "System.FreeSpace", "System.Capacity" }); |
| 272 | + |
| 273 | + ulong totalSpaceProg = 0; |
| 274 | + ulong freeSpaceProg = 0; |
| 275 | + string free_space_text = "Unknown"; |
| 276 | + string total_space_text = "Unknown"; |
| 277 | + Visibility capacityBarVis = Visibility.Visible; |
| 278 | + try |
| 279 | + { |
| 280 | + var sizeAsGBString = ByteSizeLib.ByteSize.FromBytes((ulong)retrivedProperties["System.FreeSpace"]).GigaBytes; |
| 281 | + freeSpaceProg = Convert.ToUInt64(sizeAsGBString); |
| 282 | + |
| 283 | + sizeAsGBString = ByteSizeLib.ByteSize.FromBytes((ulong)retrivedProperties["System.Capacity"]).GigaBytes; |
| 284 | + totalSpaceProg = Convert.ToUInt64(sizeAsGBString); |
| 285 | + |
| 286 | + |
| 287 | + free_space_text = ByteSizeLib.ByteSize.FromBytes((ulong)retrivedProperties["System.FreeSpace"]).ToString(); |
| 288 | + total_space_text = ByteSizeLib.ByteSize.FromBytes((ulong)retrivedProperties["System.Capacity"]).ToString(); |
| 289 | + } |
| 290 | + catch (UnauthorizedAccessException) |
| 291 | + { |
| 292 | + capacityBarVis = Visibility.Collapsed; |
| 293 | + } |
| 294 | + catch (NullReferenceException) |
| 295 | + { |
| 296 | + capacityBarVis = Visibility.Collapsed; |
| 297 | + } |
| 298 | + |
| 299 | + if (!foundDrives.Any(x => x.tag == letter)) |
| 300 | + { |
| 301 | + foundDrives.Add(new DriveItem() |
| 302 | + { |
| 303 | + driveText = content, |
| 304 | + glyph = icon, |
| 305 | + maxSpace = totalSpaceProg, |
| 306 | + spaceUsed = totalSpaceProg - freeSpaceProg, |
| 307 | + tag = letter, |
| 308 | + progressBarVisibility = capacityBarVis, |
| 309 | + spaceText = free_space_text + " free of " + total_space_text, |
| 310 | + }); |
| 311 | + } |
| 312 | + }); |
| 313 | + |
| 314 | + } |
| 315 | + } |
| 316 | + } |
| 317 | + catch (UnauthorizedAccessException e) |
| 318 | + { |
| 319 | + Debug.WriteLine(e.Message); |
| 320 | + } |
| 321 | + } |
| 322 | + |
201 | 323 | public static Windows.UI.Xaml.UnhandledExceptionEventArgs exceptionInfo { get; set; } |
202 | 324 | public static string exceptionStackTrace { get; set; } |
203 | 325 | public Dialogs.ExceptionDialog exceptionDialog; |
@@ -264,6 +386,12 @@ protected override void OnLaunched(LaunchActivatedEventArgs e) |
264 | 386 |
|
265 | 387 |
|
266 | 388 | } |
| 389 | + watcher = DeviceInformation.CreateWatcher(StorageDevice.GetDeviceSelector()); |
| 390 | + watcher.Added += DeviceAdded; |
| 391 | + watcher.Removed += DeviceRemoved; |
| 392 | + watcher.Updated += DeviceUpdated; |
| 393 | + watcher.EnumerationCompleted += Watcher_EnumerationCompleted; |
| 394 | + watcher.Start(); |
267 | 395 | // Ensure the current window is active |
268 | 396 | Window.Current.Activate(); |
269 | 397 |
|
@@ -294,6 +422,12 @@ protected override void OnActivated(IActivatedEventArgs args) |
294 | 422 | rootFrame.Navigate(typeof(InstanceTabsView), @trimmedPath, new SuppressNavigationTransitionInfo()); |
295 | 423 | } |
296 | 424 | // Ensure the current window is active. |
| 425 | + watcher = DeviceInformation.CreateWatcher(StorageDevice.GetDeviceSelector()); |
| 426 | + watcher.Added += DeviceAdded; |
| 427 | + watcher.Removed += DeviceRemoved; |
| 428 | + watcher.Updated += DeviceUpdated; |
| 429 | + watcher.EnumerationCompleted += Watcher_EnumerationCompleted; |
| 430 | + watcher.Start(); |
297 | 431 | Window.Current.Activate(); |
298 | 432 | return; |
299 | 433 | } |
@@ -329,6 +463,7 @@ private void OnSuspending(object sender, SuspendingEventArgs e) |
329 | 463 | { |
330 | 464 | var deferral = e.SuspendingOperation.GetDeferral(); |
331 | 465 | //TODO: Save application state and stop any background activity |
| 466 | + watcher.Stop(); |
332 | 467 | deferral.Complete(); |
333 | 468 | } |
334 | 469 | } |
|
0 commit comments