[Run][Program]Update icons for MSIX packages on package update (#32428)

* ptrun: update msix package icons on update

* extract duplicate code to functions
This commit is contained in:
HydroH
2024-04-23 04:59:00 +08:00
committed by GitHub
parent 88b709bef3
commit e2de521c1e

View File

@@ -29,16 +29,47 @@ namespace Microsoft.Plugin.Program.Storage
_packageCatalog.PackageInstalling += OnPackageInstalling; _packageCatalog.PackageInstalling += OnPackageInstalling;
_packageCatalog.PackageUninstalling += OnPackageUninstalling; _packageCatalog.PackageUninstalling += OnPackageUninstalling;
_packageCatalog.PackageUpdating += OnPackageUpdating;
} }
public void OnPackageInstalling(PackageCatalog p, PackageInstallingEventArgs args) public void OnPackageInstalling(PackageCatalog p, PackageInstallingEventArgs args)
{ {
if (args.IsComplete) if (args.IsComplete)
{ {
try AddPackage(args.Package);
}
}
public void OnPackageUninstalling(PackageCatalog p, PackageUninstallingEventArgs args)
{ {
var packageWrapper = PackageWrapper.GetWrapperFromPackage(args.Package); if (args.Progress == 0)
if (!string.IsNullOrEmpty(packageWrapper.InstalledLocation)) {
RemovePackage(args.Package);
}
}
public void OnPackageUpdating(PackageCatalog p, PackageUpdatingEventArgs args)
{
if (args.Progress == 0)
{
RemovePackage(args.SourcePackage);
}
if (args.IsComplete)
{
AddPackage(args.TargetPackage);
}
}
private void AddPackage(Package package)
{
var packageWrapper = PackageWrapper.GetWrapperFromPackage(package);
if (string.IsNullOrEmpty(packageWrapper.InstalledLocation))
{
return;
}
try
{ {
var uwp = new UWP(packageWrapper); var uwp = new UWP(packageWrapper);
uwp.InitializeAppInfo(packageWrapper.InstalledLocation); uwp.InitializeAppInfo(packageWrapper.InstalledLocation);
@@ -48,24 +79,20 @@ namespace Microsoft.Plugin.Program.Storage
Add(app); Add(app);
} }
} }
}
// InitializeAppInfo will throw if there is no AppxManifest.xml for the package. // InitializeAppInfo will throw if there is no AppxManifest.xml for the package.
// Note there are sometimes multiple packages per product and this doesn't necessarily mean that we haven't found the app. // Note there are sometimes multiple packages per product and this doesn't necessarily mean that we haven't found the app.
// eg. "Could not find file 'C:\\Program Files\\WindowsApps\\Microsoft.WindowsTerminalPreview_2020.616.45.0_neutral_~_8wekyb3d8bbwe\\AppxManifest.xml'." // eg. "Could not find file 'C:\\Program Files\\WindowsApps\\Microsoft.WindowsTerminalPreview_2020.616.45.0_neutral_~_8wekyb3d8bbwe\\AppxManifest.xml'."
catch (System.IO.FileNotFoundException e) catch (System.IO.FileNotFoundException e)
{ {
ProgramLogger.Exception(e.Message, e, GetType(), args.Package.InstalledLocation.ToString()); ProgramLogger.Exception(e.Message, e, GetType(), package.InstalledLocation.ToString());
}
} }
} }
public void OnPackageUninstalling(PackageCatalog p, PackageUninstallingEventArgs args) private void RemovePackage(Package package)
{
if (args.Progress == 0)
{ {
// find apps associated with this package. // find apps associated with this package.
var packageWrapper = PackageWrapper.GetWrapperFromPackage(args.Package); var packageWrapper = PackageWrapper.GetWrapperFromPackage(package);
var uwp = new UWP(packageWrapper); var uwp = new UWP(packageWrapper);
var apps = Items.Where(a => a.Package.Equals(uwp)).ToArray(); var apps = Items.Where(a => a.Package.Equals(uwp)).ToArray();
@@ -74,7 +101,6 @@ namespace Microsoft.Plugin.Program.Storage
Remove(app); Remove(app);
} }
} }
}
public void IndexPrograms() public void IndexPrograms()
{ {