-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Code Quality: Introduced IWindowsAppLauncherService #15927
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
14 changes: 14 additions & 0 deletions
14
src/Files.App/Data/Contracts/IWindowsAppLauncherService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// Copyright (c) 2024 Files Community | ||
// Licensed under the MIT License. See the LICENSE. | ||
|
||
namespace Files.App.Data.Contracts | ||
{ | ||
public interface IWindowsAppLauncherService | ||
{ | ||
Task<bool> LaunchStorageSensePolicySettingsAsync(); | ||
|
||
Task<bool> LaunchProgramCompatibilityTroubleshooterAsync(string path); | ||
|
||
Task<bool> LaunchApplicationAsync(string path, string arguments, string workingDirectory); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
245 changes: 245 additions & 0 deletions
245
src/Files.App/Services/Windows/WindowsAppLauncherService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,245 @@ | ||
// Copyright (c) 2024 Files Community | ||
// Licensed under the MIT License. See the LICENSE. | ||
|
||
using Files.Shared.Helpers; | ||
using Microsoft.Extensions.Logging; | ||
using Windows.System; | ||
|
||
namespace Files.App.Services | ||
{ | ||
/// <inheritdoc cref="IWindowsAppLauncherService"/> | ||
public class WindowsAppLauncherService : IWindowsAppLauncherService | ||
{ | ||
/// <inheritdoc/> | ||
public async Task<bool> LaunchStorageSensePolicySettingsAsync() | ||
{ | ||
bool result = await Launcher.LaunchUriAsync(new Uri("ms-settings:storagepolicies")); | ||
return result; | ||
} | ||
|
||
/// <inheritdoc/> | ||
public async Task<bool> LaunchProgramCompatibilityTroubleshooterAsync(string path) | ||
{ | ||
var compatibilityTroubleshooterAnswerFile = SystemIO.Path.Combine(SystemIO.Path.GetTempPath(), "CompatibilityTroubleshooterAnswerFile.xml"); | ||
string troubleshooterAnswerFileText = $@"<?xml version=""1.0"" encoding=""UTF-8""?><Answers Version=""1.0""><Interaction ID=""IT_LaunchMethod""><Value>CompatTab</Value></Interaction><Interaction ID=""IT_BrowseForFile""><Value>{path}</Value></Interaction></Answers>"; | ||
|
||
try | ||
{ | ||
SystemIO.File.WriteAllText(compatibilityTroubleshooterAnswerFile, troubleshooterAnswerFileText); | ||
} | ||
catch (SystemIO.IOException) | ||
{ | ||
// Try with a different file name | ||
SafetyExtensions.IgnoreExceptions(() => | ||
{ | ||
compatibilityTroubleshooterAnswerFile = SystemIO.Path.Combine(SystemIO.Path.GetTempPath(), "CompatibilityTroubleshooterAnswerFile1.xml"); | ||
SystemIO.File.WriteAllText(compatibilityTroubleshooterAnswerFile, troubleshooterAnswerFileText); | ||
}); | ||
} | ||
|
||
return await LaunchApplicationAsync("MSDT.exe", @$"/id PCWDiagnostic /af ""{compatibilityTroubleshooterAnswerFile}""", ""); | ||
} | ||
|
||
/// <inheritdoc/> | ||
public async Task<bool> LaunchApplicationAsync(string path, string arguments, string workingDirectory) | ||
{ | ||
var currentWindows = Win32Helper.GetDesktopWindows(); | ||
|
||
// Use PowerShell to mount Vhd Disk as this requires admin rights | ||
if (FileExtensionHelpers.IsVhdFile(path)) | ||
return await Win32Helper.MountVhdDisk(path); | ||
|
||
try | ||
{ | ||
using Process process = new Process(); | ||
process.StartInfo.UseShellExecute = false; | ||
process.StartInfo.FileName = path; | ||
|
||
// Show window if workingDirectory (opening terminal) | ||
process.StartInfo.CreateNoWindow = string.IsNullOrEmpty(workingDirectory); | ||
|
||
if (arguments.Equals("RunAs", StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
process.StartInfo.UseShellExecute = true; | ||
process.StartInfo.Verb = "RunAs"; | ||
|
||
if (FileExtensionHelpers.IsMsiFile(path)) | ||
{ | ||
process.StartInfo.FileName = "MSIEXEC.exe"; | ||
process.StartInfo.Arguments = $"/a \"{path}\""; | ||
} | ||
} | ||
else if (arguments.Equals("RunAsUser", StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
process.StartInfo.UseShellExecute = true; | ||
process.StartInfo.Verb = "RunAsUser"; | ||
|
||
if (FileExtensionHelpers.IsMsiFile(path)) | ||
{ | ||
process.StartInfo.FileName = "MSIEXEC.exe"; | ||
process.StartInfo.Arguments = $"/i \"{path}\""; | ||
} | ||
} | ||
else | ||
{ | ||
process.StartInfo.Arguments = arguments; | ||
|
||
// Refresh env variables for the child process | ||
foreach (DictionaryEntry ent in Environment.GetEnvironmentVariables(EnvironmentVariableTarget.User)) | ||
process.StartInfo.EnvironmentVariables[(string)ent.Key] = (string)ent.Value; | ||
|
||
process.StartInfo.EnvironmentVariables["PATH"] = string.Join(';', | ||
Environment.GetEnvironmentVariable("PATH", EnvironmentVariableTarget.Machine), | ||
Environment.GetEnvironmentVariable("PATH", EnvironmentVariableTarget.User)); | ||
} | ||
|
||
process.StartInfo.WorkingDirectory = string.IsNullOrEmpty(workingDirectory) ? PathNormalization.GetParentDir(path) : workingDirectory; | ||
process.Start(); | ||
|
||
Win32Helper.BringToForeground(currentWindows); | ||
|
||
return true; | ||
} | ||
catch (Win32Exception) | ||
{ | ||
using Process process = new Process(); | ||
process.StartInfo.UseShellExecute = true; | ||
process.StartInfo.FileName = path; | ||
process.StartInfo.CreateNoWindow = true; | ||
process.StartInfo.Arguments = arguments; | ||
process.StartInfo.WorkingDirectory = string.IsNullOrEmpty(workingDirectory) ? PathNormalization.GetParentDir(path) : workingDirectory; | ||
|
||
try | ||
{ | ||
process.Start(); | ||
|
||
Win32Helper.BringToForeground(currentWindows); | ||
|
||
return true; | ||
} | ||
catch (Win32Exception ex) when (ex.NativeErrorCode == 50) | ||
{ | ||
// ShellExecute return code 50 (ERROR_NOT_SUPPORTED) for some exes (#15179) | ||
return Win32Helper.RunPowershellCommand($"\"{path}\"", PowerShellExecutionOptions.Hidden); | ||
} | ||
catch (Win32Exception) | ||
{ | ||
try | ||
{ | ||
var opened = await Win32Helper.StartSTATask(async () => | ||
{ | ||
var split = path.Split('|').Where(x => !string.IsNullOrWhiteSpace(x)).Select(x => GetMtpPath(x)); | ||
if (split.Count() == 1) | ||
{ | ||
Process.Start(split.First()); | ||
|
||
Win32Helper.BringToForeground(currentWindows); | ||
} | ||
else | ||
{ | ||
var groups = split.GroupBy(x => new | ||
{ | ||
Dir = SystemIO.Path.GetDirectoryName(x), | ||
Prog = Win32Helper.GetFileAssociationAsync(x).Result ?? SystemIO.Path.GetExtension(x) | ||
}); | ||
|
||
foreach (var group in groups) | ||
{ | ||
if (!group.Any()) | ||
continue; | ||
|
||
using var cMenu = await ContextMenu.GetContextMenuForFiles(group.ToArray(), Vanara.PInvoke.Shell32.CMF.CMF_DEFAULTONLY); | ||
|
||
if (cMenu is not null) | ||
await cMenu.InvokeVerb(Vanara.PInvoke.Shell32.CMDSTR_OPEN); | ||
} | ||
} | ||
|
||
return true; | ||
}); | ||
|
||
if (!opened) | ||
{ | ||
if (path.StartsWith(@"\\SHELL\", StringComparison.Ordinal)) | ||
{ | ||
opened = await Win32Helper.StartSTATask(async () => | ||
{ | ||
using var cMenu = await ContextMenu.GetContextMenuForFiles(new[] { path }, Vanara.PInvoke.Shell32.CMF.CMF_DEFAULTONLY); | ||
|
||
if (cMenu is not null) | ||
await cMenu.InvokeItem(cMenu.Items.FirstOrDefault()?.ID ?? -1); | ||
|
||
return true; | ||
}); | ||
} | ||
} | ||
|
||
if (!opened) | ||
{ | ||
var isAlternateStream = RegexHelpers.AlternateStream().IsMatch(path); | ||
if (isAlternateStream) | ||
{ | ||
var basePath = SystemIO.Path.Combine(Environment.GetEnvironmentVariable("TEMP"), Guid.NewGuid().ToString("n")); | ||
Vanara.PInvoke.Kernel32.CreateDirectory(basePath); | ||
|
||
var tempPath = SystemIO.Path.Combine(basePath, new string(SystemIO.Path.GetFileName(path).SkipWhile(x => x != ':').Skip(1).ToArray())); | ||
using var hFileSrc = Vanara.PInvoke.Kernel32.CreateFile(path, Vanara.PInvoke.Kernel32.FileAccess.GENERIC_READ, SystemIO.FileShare.ReadWrite, null, SystemIO.FileMode.Open, Vanara.PInvoke.FileFlagsAndAttributes.FILE_ATTRIBUTE_NORMAL); | ||
using var hFileDst = Vanara.PInvoke.Kernel32.CreateFile(tempPath, Vanara.PInvoke.Kernel32.FileAccess.GENERIC_WRITE, 0, null, SystemIO.FileMode.Create, Vanara.PInvoke.FileFlagsAndAttributes.FILE_ATTRIBUTE_NORMAL | Vanara.PInvoke.FileFlagsAndAttributes.FILE_ATTRIBUTE_READONLY); | ||
|
||
if (!hFileSrc.IsInvalid && !hFileDst.IsInvalid) | ||
{ | ||
// Copy ADS to temp folder and open | ||
using (var inStream = new SystemIO.FileStream(hFileSrc.DangerousGetHandle(), SystemIO.FileAccess.Read)) | ||
using (var outStream = new SystemIO.FileStream(hFileDst.DangerousGetHandle(), SystemIO.FileAccess.Write)) | ||
{ | ||
await inStream.CopyToAsync(outStream); | ||
await outStream.FlushAsync(); | ||
} | ||
|
||
opened = await LaunchApplicationAsync(tempPath, arguments, workingDirectory); | ||
} | ||
} | ||
} | ||
|
||
return opened; | ||
} | ||
catch (Win32Exception) | ||
{ | ||
// Cannot open file (e.g DLL) | ||
return false; | ||
} | ||
catch (ArgumentException) | ||
{ | ||
// Cannot open file (e.g DLL) | ||
return false; | ||
} | ||
} | ||
} | ||
catch (InvalidOperationException) | ||
{ | ||
// Invalid file path | ||
return false; | ||
} | ||
catch (Exception ex) | ||
{ | ||
// Generic error, log | ||
App.Logger.LogWarning(ex, $"Error launching: {path}"); | ||
return false; | ||
} | ||
|
||
string GetMtpPath(string executable) | ||
{ | ||
if (executable.StartsWith("\\\\?\\", StringComparison.Ordinal)) | ||
{ | ||
using var computer = new Vanara.Windows.Shell.ShellFolder(Vanara.PInvoke.Shell32.KNOWNFOLDERID.FOLDERID_ComputerFolder); | ||
using var device = computer.FirstOrDefault(i => executable.Replace("\\\\?\\", "", StringComparison.Ordinal).StartsWith(i.Name, StringComparison.Ordinal)); | ||
var deviceId = device?.ParsingName; | ||
var itemPath = RegexHelpers.WindowsPath().Replace(executable, ""); | ||
return deviceId is not null ? SystemIO.Path.Combine(deviceId, itemPath) : executable; | ||
} | ||
|
||
return executable; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should you include the path in the arguments?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The first implementation to include path is made by Gave (3 yrs ago) but I'm not sure why because Storage Sense works for only system drive and for the whole system drive not for specifically a path.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually path isn't used to be passed to settings to give extra context.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is a thread on GitHub about this with more details, but in any event, it makes a difference for which page is opened in Settings.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Both open wrong page.
For other than C (system) drive:
For thers:
Both should open:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My implementapention opens what should be opened.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's supposed to open the Storage Page for the
C:\
drive, and the "Storage used on other drives" page for other drives.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Even so, how would you free up storage for other drives? The "Storage used on other drives" doesn't contains any free up page.
As to C, why not open Policy page directly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I assume this weird page navigation might come from win10 when Gave implemented
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can access the "Free up storage" page by clicking on a drive. Ideally we'd open directly to that page, but that's not supported by the Settings app.