2016-08-18 08:16:40 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
using System.Security.Principal;
|
|
|
|
|
using System.Threading.Tasks;
|
2016-08-18 08:56:26 +08:00
|
|
|
|
using System.Windows.Media;
|
2016-08-18 08:16:40 +08:00
|
|
|
|
using System.Windows.Media.Imaging;
|
|
|
|
|
using Windows.ApplicationModel;
|
|
|
|
|
using Windows.ApplicationModel.Core;
|
2016-08-20 06:24:21 +08:00
|
|
|
|
using Windows.Foundation;
|
|
|
|
|
using Windows.Management.Deployment;
|
2016-08-18 08:16:40 +08:00
|
|
|
|
using Windows.Storage.Streams;
|
|
|
|
|
using AppxPackaing;
|
|
|
|
|
using Shell;
|
2016-08-20 08:17:28 +08:00
|
|
|
|
using Wox.Infrastructure;
|
2016-08-18 08:16:40 +08:00
|
|
|
|
using Wox.Infrastructure.Logger;
|
|
|
|
|
using IStream = AppxPackaing.IStream;
|
2016-08-20 06:24:21 +08:00
|
|
|
|
using Rect = System.Windows.Rect;
|
2016-08-18 08:16:40 +08:00
|
|
|
|
|
2016-08-20 08:17:28 +08:00
|
|
|
|
namespace Wox.Plugin.Program.Programs
|
2016-08-18 08:16:40 +08:00
|
|
|
|
{
|
2016-08-18 08:56:26 +08:00
|
|
|
|
public class UWP
|
2016-08-18 08:16:40 +08:00
|
|
|
|
{
|
|
|
|
|
public string Name { get; }
|
|
|
|
|
public string FullName { get; }
|
|
|
|
|
public string FamilyName { get; }
|
|
|
|
|
|
|
|
|
|
public string DisplayName { get; set; }
|
|
|
|
|
public string Description { get; set; }
|
|
|
|
|
public string PublisherDisplayName { get; set; }
|
|
|
|
|
public string Location { get; set; }
|
|
|
|
|
|
|
|
|
|
public Application[] Apps { get; set; }
|
|
|
|
|
public Package Package { get; }
|
|
|
|
|
|
2016-08-18 08:56:26 +08:00
|
|
|
|
public UWP(Package package)
|
2016-08-18 08:16:40 +08:00
|
|
|
|
{
|
|
|
|
|
Package = package;
|
|
|
|
|
Name = Package.Id.Name;
|
|
|
|
|
FullName = Package.Id.FullName;
|
|
|
|
|
FamilyName = Package.Id.FamilyName;
|
|
|
|
|
Location = Package.InstalledLocation.Path;
|
|
|
|
|
|
|
|
|
|
InitializeAppDisplayInfo(package);
|
2016-08-18 08:56:26 +08:00
|
|
|
|
InitializeAppInfo();
|
2016-08-18 08:16:40 +08:00
|
|
|
|
Apps = Apps.Where(a =>
|
|
|
|
|
{
|
|
|
|
|
var valid = !string.IsNullOrEmpty(a.Executable) &&
|
|
|
|
|
!string.IsNullOrEmpty(a.UserModelId) &&
|
|
|
|
|
!string.IsNullOrEmpty(a.DisplayName);
|
|
|
|
|
return valid;
|
|
|
|
|
}).ToArray();
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-18 08:56:26 +08:00
|
|
|
|
private void InitializeAppInfo()
|
2016-08-18 08:16:40 +08:00
|
|
|
|
{
|
2016-08-19 10:00:52 +08:00
|
|
|
|
var path = Path.Combine(Location, "AppxManifest.xml");
|
|
|
|
|
var appx = new AppxFactory();
|
|
|
|
|
IStream stream;
|
|
|
|
|
const uint noAttribute = 0x80;
|
|
|
|
|
const Stgm exclusiveRead = Stgm.Read | Stgm.ShareExclusive;
|
|
|
|
|
var result = SHCreateStreamOnFileEx(path, exclusiveRead, noAttribute, false, null, out stream);
|
2016-08-18 08:16:40 +08:00
|
|
|
|
|
|
|
|
|
if (result == Hresult.Ok)
|
|
|
|
|
{
|
2016-08-19 10:00:52 +08:00
|
|
|
|
var reader = appx.CreateManifestReader(stream);
|
2016-08-18 08:16:40 +08:00
|
|
|
|
|
|
|
|
|
var properties = reader.GetProperties();
|
|
|
|
|
PublisherDisplayName = properties.GetStringValue("PublisherDisplayName");
|
|
|
|
|
DisplayName = properties.GetStringValue("DisplayName");
|
|
|
|
|
Description = properties.GetStringValue("Description");
|
|
|
|
|
|
|
|
|
|
var apps = reader.GetApplications();
|
|
|
|
|
int i = 0;
|
|
|
|
|
while (apps.GetHasCurrent() != 0 && i <= Apps.Length)
|
|
|
|
|
{
|
|
|
|
|
var currentApp = apps.GetCurrent();
|
2016-08-21 02:19:33 +08:00
|
|
|
|
var appListEntry = currentApp.GetStringValue("AppListEntry");
|
|
|
|
|
if (appListEntry != "nonoe")
|
|
|
|
|
{
|
|
|
|
|
Apps[i].UserModelId = currentApp.GetAppUserModelId();
|
|
|
|
|
Apps[i].Executable = currentApp.GetStringValue("Executable") ?? string.Empty;
|
|
|
|
|
Apps[i].BackgroundColor = currentApp.GetStringValue("BackgroundColor") ?? string.Empty;
|
|
|
|
|
Apps[i].LogoPath = Path.Combine(Location, currentApp.GetStringValue("Square44x44Logo"));
|
|
|
|
|
Apps[i].Location = Location;
|
|
|
|
|
}
|
2016-08-18 08:16:40 +08:00
|
|
|
|
apps.MoveNext();
|
|
|
|
|
i++;
|
|
|
|
|
}
|
|
|
|
|
if (i != Apps.Length)
|
|
|
|
|
{
|
|
|
|
|
var message = $"Wrong application number - {Name}: {i}";
|
|
|
|
|
Console.WriteLine(message);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void InitializeAppDisplayInfo(Package package)
|
|
|
|
|
{
|
|
|
|
|
IReadOnlyList<AppListEntry> apps;
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
apps = package.GetAppListEntriesAsync().AsTask().Result;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
var message = $"{e.Message} @ {Name}";
|
|
|
|
|
Console.WriteLine(message);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
Apps = apps.Select(a => new Application
|
|
|
|
|
{
|
|
|
|
|
DisplayName = a.DisplayInfo.DisplayName,
|
|
|
|
|
Description = a.DisplayInfo.Description,
|
2016-08-19 10:00:52 +08:00
|
|
|
|
// todo: which size is valid?
|
|
|
|
|
LogoStream = a.DisplayInfo.GetLogo(new Size(44, 44))
|
2016-08-18 08:16:40 +08:00
|
|
|
|
}).ToArray();
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-21 01:25:55 +08:00
|
|
|
|
public static Application[] All()
|
2016-08-18 08:16:40 +08:00
|
|
|
|
{
|
2016-08-20 08:17:28 +08:00
|
|
|
|
var windows10 = new Version(10, 0);
|
|
|
|
|
var support = Environment.OSVersion.Version.Major >= windows10.Major;
|
|
|
|
|
if (support)
|
2016-08-18 08:16:40 +08:00
|
|
|
|
{
|
2016-08-21 01:25:55 +08:00
|
|
|
|
var application = CurrentUserPackages().AsParallel().SelectMany(p =>
|
2016-08-18 08:16:40 +08:00
|
|
|
|
{
|
2016-08-20 08:17:28 +08:00
|
|
|
|
try
|
2016-08-18 08:16:40 +08:00
|
|
|
|
{
|
2016-08-20 08:17:28 +08:00
|
|
|
|
var u = new UWP(p);
|
2016-08-21 01:25:55 +08:00
|
|
|
|
return u.Apps;
|
2016-08-18 08:16:40 +08:00
|
|
|
|
}
|
2016-08-20 08:17:28 +08:00
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
// if there are errors, just ignore it and continue
|
|
|
|
|
var message = $"Can't parse {p.Id.Name}: {e.Message}";
|
|
|
|
|
Log.Error(message);
|
2016-08-21 01:25:55 +08:00
|
|
|
|
return new Application[] { };
|
2016-08-20 08:17:28 +08:00
|
|
|
|
}
|
2016-08-21 01:25:55 +08:00
|
|
|
|
});
|
|
|
|
|
return application.ToArray();
|
2016-08-20 08:17:28 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2016-08-21 01:25:55 +08:00
|
|
|
|
return new Application[] { };
|
2016-08-20 08:17:28 +08:00
|
|
|
|
}
|
2016-08-18 08:16:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static IEnumerable<Package> CurrentUserPackages()
|
|
|
|
|
{
|
2016-08-21 02:19:33 +08:00
|
|
|
|
var user = WindowsIdentity.GetCurrent().User;
|
2016-08-18 08:16:40 +08:00
|
|
|
|
|
|
|
|
|
if (user != null)
|
|
|
|
|
{
|
|
|
|
|
var userSecurityId = user.Value;
|
|
|
|
|
var packageManager = new PackageManager();
|
|
|
|
|
var packages = packageManager.FindPackagesForUser(userSecurityId);
|
2016-08-21 02:19:33 +08:00
|
|
|
|
packages = packages.Where(p => !p.IsFramework && !string.IsNullOrEmpty(p.InstalledLocation.Path));
|
2016-08-18 08:16:40 +08:00
|
|
|
|
return packages;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return new Package[] { };
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override string ToString()
|
|
|
|
|
{
|
|
|
|
|
return FamilyName;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override bool Equals(object obj)
|
|
|
|
|
{
|
2016-08-18 08:56:26 +08:00
|
|
|
|
var uwp = obj as UWP;
|
2016-08-18 08:16:40 +08:00
|
|
|
|
if (uwp != null)
|
|
|
|
|
{
|
|
|
|
|
return FamilyName.Equals(uwp.FamilyName);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override int GetHashCode()
|
|
|
|
|
{
|
|
|
|
|
return FamilyName.GetHashCode();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class Application
|
|
|
|
|
{
|
|
|
|
|
public string DisplayName { get; set; }
|
|
|
|
|
public string Description { get; set; }
|
|
|
|
|
public RandomAccessStreamReference LogoStream { get; set; }
|
|
|
|
|
public string UserModelId { get; set; }
|
|
|
|
|
public string Executable { get; set; }
|
|
|
|
|
public string PublisherDisplayName { get; set; }
|
2016-08-18 08:56:26 +08:00
|
|
|
|
public string BackgroundColor { get; set; }
|
|
|
|
|
public string LogoPath { get; set; }
|
2016-08-21 02:19:33 +08:00
|
|
|
|
public string AppListEntry { get; set; }
|
2016-08-21 01:50:14 +08:00
|
|
|
|
|
2016-08-21 01:25:55 +08:00
|
|
|
|
public int Score { get; set; }
|
2016-08-21 01:50:14 +08:00
|
|
|
|
public string Location { get; set; }
|
|
|
|
|
|
2016-08-18 08:16:40 +08:00
|
|
|
|
|
|
|
|
|
// todo: wrap with try exception
|
|
|
|
|
public void Launch()
|
|
|
|
|
{
|
|
|
|
|
var appManager = new ApplicationActivationManager();
|
|
|
|
|
uint unusedPid;
|
|
|
|
|
const string noArgs = "";
|
|
|
|
|
const ACTIVATEOPTIONS noFlags = ACTIVATEOPTIONS.AO_NONE;
|
|
|
|
|
appManager.ActivateApplication(UserModelId, noArgs, noFlags, out unusedPid);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2016-08-18 08:56:26 +08:00
|
|
|
|
public ImageSource Logo()
|
2016-08-18 08:16:40 +08:00
|
|
|
|
{
|
2016-08-19 10:00:52 +08:00
|
|
|
|
var logo = !string.IsNullOrEmpty(LogoPath) ? ImageFromPath(LogoPath) : ImageFromStream(LogoStream);
|
2016-08-18 08:56:26 +08:00
|
|
|
|
|
2016-08-19 10:00:52 +08:00
|
|
|
|
var validBaground = !string.IsNullOrEmpty(BackgroundColor) && BackgroundColor != "transparent";
|
|
|
|
|
var plated = validBaground ? PlatedImage(logo) : logo;
|
2016-08-18 08:56:26 +08:00
|
|
|
|
|
2016-08-19 10:00:52 +08:00
|
|
|
|
// todo magic! temp fix for cross thread object
|
|
|
|
|
plated.Freeze();
|
|
|
|
|
return plated;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private BitmapImage ImageFromPath(string path)
|
|
|
|
|
{
|
|
|
|
|
// https://msdn.microsoft.com/windows/uwp/controls-and-patterns/tiles-and-notifications-app-assets
|
|
|
|
|
var extension = path.Substring(path.Length - 4);
|
|
|
|
|
var filename = path.Substring(0, path.Length - 4);
|
|
|
|
|
// todo: remove hard cod scale
|
|
|
|
|
var path1 = $"{filename}.scale-200{extension}";
|
|
|
|
|
var path2 = $"{filename}.scale-100{extension}";
|
2016-08-20 06:15:40 +08:00
|
|
|
|
if (File.Exists(path1))
|
|
|
|
|
{
|
|
|
|
|
var image = new BitmapImage(new Uri(path1));
|
|
|
|
|
return image;
|
|
|
|
|
}
|
|
|
|
|
else if (File.Exists(path2))
|
|
|
|
|
{
|
|
|
|
|
var image = new BitmapImage(new Uri(path2));
|
|
|
|
|
return image;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2016-08-20 08:17:28 +08:00
|
|
|
|
return new BitmapImage(new Uri(Constant.ErrorIcon));
|
2016-08-20 06:15:40 +08:00
|
|
|
|
}
|
2016-08-19 10:00:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private BitmapImage ImageFromStream(RandomAccessStreamReference reference)
|
|
|
|
|
{
|
|
|
|
|
IRandomAccessStreamWithContentType stream;
|
|
|
|
|
try
|
2016-08-18 08:56:26 +08:00
|
|
|
|
{
|
2016-08-19 10:00:52 +08:00
|
|
|
|
stream = reference.OpenReadAsync().AsTask().Result;
|
2016-08-18 08:56:26 +08:00
|
|
|
|
}
|
2016-08-19 10:00:52 +08:00
|
|
|
|
catch (Exception e)
|
2016-08-18 08:56:26 +08:00
|
|
|
|
{
|
2016-08-19 10:00:52 +08:00
|
|
|
|
var message = $"{e.Message} @ {DisplayName}";
|
|
|
|
|
Log.Error(message);
|
|
|
|
|
throw;
|
2016-08-18 08:56:26 +08:00
|
|
|
|
}
|
2016-08-19 10:00:52 +08:00
|
|
|
|
|
|
|
|
|
var image = new BitmapImage();
|
|
|
|
|
image.BeginInit();
|
|
|
|
|
image.StreamSource = stream.AsStream();
|
|
|
|
|
image.EndInit();
|
|
|
|
|
return image;
|
2016-08-18 08:56:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private ImageSource PlatedImage(BitmapImage image)
|
|
|
|
|
{
|
|
|
|
|
var width = image.Width;
|
|
|
|
|
var height = image.Height;
|
|
|
|
|
var x = 0;
|
|
|
|
|
var y = 0;
|
|
|
|
|
|
|
|
|
|
var group = new DrawingGroup();
|
|
|
|
|
|
|
|
|
|
var converted = ColorConverter.ConvertFromString(BackgroundColor);
|
|
|
|
|
if (converted != null)
|
|
|
|
|
{
|
|
|
|
|
var color = (Color)converted;
|
|
|
|
|
var brush = new SolidColorBrush(color);
|
|
|
|
|
var pen = new Pen(brush, 1);
|
|
|
|
|
var backgroundArea = new Rect(0, 0, width, width);
|
|
|
|
|
var rectabgle = new RectangleGeometry(backgroundArea);
|
|
|
|
|
var rectDrawing = new GeometryDrawing(brush, pen, rectabgle);
|
|
|
|
|
group.Children.Add(rectDrawing);
|
|
|
|
|
|
|
|
|
|
var imageArea = new Rect(x, y, image.Width, image.Height);
|
|
|
|
|
var imageDrawing = new ImageDrawing(image, imageArea);
|
|
|
|
|
group.Children.Add(imageDrawing);
|
|
|
|
|
|
|
|
|
|
// http://stackoverflow.com/questions/6676072/get-system-drawing-bitmap-of-a-wpf-area-using-visualbrush
|
|
|
|
|
var visual = new DrawingVisual();
|
|
|
|
|
var context = visual.RenderOpen();
|
|
|
|
|
context.DrawDrawing(group);
|
|
|
|
|
context.Close();
|
2016-08-19 10:00:52 +08:00
|
|
|
|
const int dpiScale100 = 96;
|
2016-08-18 08:56:26 +08:00
|
|
|
|
var bitmap = new RenderTargetBitmap(
|
|
|
|
|
Convert.ToInt32(width), Convert.ToInt32(height),
|
2016-08-19 10:00:52 +08:00
|
|
|
|
dpiScale100, dpiScale100,
|
2016-08-18 08:56:26 +08:00
|
|
|
|
PixelFormats.Pbgra32
|
|
|
|
|
);
|
|
|
|
|
bitmap.Render(visual);
|
|
|
|
|
return bitmap;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2016-08-20 08:17:28 +08:00
|
|
|
|
return new BitmapImage(new Uri(Constant.ErrorIcon));
|
2016-08-18 08:16:40 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override string ToString()
|
|
|
|
|
{
|
|
|
|
|
return $"{DisplayName}: {Description}";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Flags]
|
|
|
|
|
private enum Stgm : uint
|
|
|
|
|
{
|
|
|
|
|
Read = 0x0,
|
2016-08-20 06:24:21 +08:00
|
|
|
|
ShareExclusive = 0x10
|
2016-08-18 08:16:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private enum Hresult : uint
|
|
|
|
|
{
|
2016-08-20 06:24:21 +08:00
|
|
|
|
Ok = 0x0000
|
2016-08-18 08:16:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[DllImport("shlwapi.dll", CharSet = CharSet.Unicode)]
|
|
|
|
|
private static extern Hresult SHCreateStreamOnFileEx(string fileName, Stgm grfMode, uint attributes, bool create, IStream reserved, out IStream stream);
|
|
|
|
|
}
|
|
|
|
|
}
|