Created
June 20, 2016 17:13
-
-
Save Paul-Brenner-Tangoe/95608753fda1dbe18260c2e26c8a7a3a to your computer and use it in GitHub Desktop.
This file contains hidden or 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
using System; | |
using System.Diagnostics; | |
using Windows.ApplicationModel; | |
using MyMatrixUsage.PlatformSpecific; | |
using Xamarin.Forms; | |
using Windows.UI.Xaml.Media.Imaging; | |
using Windows.ApplicationModel.Core; | |
using Windows.Management.Deployment; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Threading.Tasks; | |
[assembly: Dependency(typeof(MyMatrixUsage.Win10.PlatformSpecific.AppInfo))] | |
[assembly: Dependency(typeof(MyMatrixUsage.Win10.PlatformSpecific.AppInfoFactory))] | |
namespace MyMatrixUsage.Win10.PlatformSpecific | |
{ | |
/// <summary> | |
/// The class represents Windows-specific application information. | |
/// </summary> | |
public class AppInfo : IAppInfo, IEquatable<AppInfo> | |
{ | |
public const string InvalidUid = "invalid"; | |
/// <summary> | |
/// Creates an instance of the class. | |
/// </summary> | |
/// <param name="aPackageName">Application's package name</param> | |
public AppInfo(string aPackageName) | |
{ | |
Debug.Assert(aPackageName != null); | |
PackageName = aPackageName; | |
} | |
/// <summary> | |
/// From IAppInfo. | |
/// </summary> | |
public Version VersionNumber | |
{ | |
get | |
{ | |
var pv = Package.Current.Id.Version; | |
return new Version(pv.Major, pv.Minor, pv.Build, pv.Revision); | |
} | |
} | |
/// <summary> | |
/// From IAppInfo. | |
/// </summary> | |
public string AppName => this.GetApplicationName(); | |
/// <summary> | |
/// Unique package name of an application. | |
/// </summary> | |
public string PackageName { get; set; } | |
public string Uid { get; internal set; } | |
public bool IsSystem { get; internal set; } | |
/// <summary> | |
/// From IAppInfo. | |
/// </summary> | |
public bool IsUninstalled { get; internal set; } | |
private string SimplifiedName => (Uid == InvalidUid) ? Uid : "System"; | |
/// <summary> | |
/// From IEquatable. | |
/// </summary> | |
public bool Equals(IAppInfo aOther) | |
{ | |
if (ReferenceEquals(null, aOther)) | |
{ | |
return false; | |
} | |
if (ReferenceEquals(this, aOther)) | |
{ | |
return true; | |
} | |
return aOther.GetType() == GetType() && Equals((AppInfo)aOther); | |
} | |
/// <summary> | |
/// From IEquatable. | |
/// </summary> | |
public bool Equals(AppInfo aOther) | |
{ | |
if (ReferenceEquals(null, aOther)) | |
{ | |
return false; | |
} | |
if (ReferenceEquals(this, aOther)) | |
{ | |
return true; | |
} | |
bool equals = ((SimplifiedName == aOther.SimplifiedName) || (IsSystem == aOther.IsSystem) || (IsUninstalled && aOther.IsUninstalled)); | |
if (!equals) | |
{ | |
equals = string.Equals(PackageName, aOther.PackageName, StringComparison.OrdinalIgnoreCase); | |
} | |
return equals; | |
} | |
/// <summary> | |
/// From Object. | |
/// </summary> | |
public override bool Equals(object aObject) | |
{ | |
if (ReferenceEquals(null, aObject)) | |
{ | |
return false; | |
} | |
if (ReferenceEquals(this, aObject)) | |
{ | |
return true; | |
} | |
return aObject.GetType() == GetType() && Equals((AppInfo)aObject); | |
} | |
/// <summary> | |
/// From Object. | |
/// </summary> | |
public override int GetHashCode() | |
{ | |
return StringComparer.OrdinalIgnoreCase.GetHashCode(PackageName); | |
} | |
} | |
/// <summary> | |
/// The class represents Windows-specific factory of application information. | |
/// </summary> | |
public class AppInfoFactory : IAppInfoFactory | |
{ | |
/// <summary> | |
/// From IAppInfoFactory. | |
/// </summary> | |
public IAppInfo CurrentAppInfo | |
{ | |
get | |
{ | |
var packageName = Package.Current.Id.Name; | |
var appUid = Package.Current.Id.FamilyName; | |
var appInfo = new AppInfo(packageName) | |
{ | |
Uid = appUid, | |
}; | |
appInfo.ChangeUninstalledAppInfo(); | |
return appInfo; | |
} | |
} | |
/// <summary> | |
/// From IAppInfoFactory. | |
/// </summary> | |
public IAppInfo CreateAppInfo(string aPackageName, string aUid = null) | |
{ | |
var uid = string.IsNullOrEmpty(aUid) ? AppInfo.InvalidUid : aUid; | |
var appInfo = new AppInfo(aPackageName) | |
{ | |
Uid = uid, | |
}; | |
appInfo.ChangeUninstalledAppInfo(); | |
return appInfo; | |
} | |
} | |
/// <summary> | |
/// The class contains extension methods for the application information structure. | |
/// </summary> | |
internal static class AppInfoResolverExtensions | |
{ | |
internal const string UidUninstalledApps = "uninstalled"; | |
internal const string PackageNameUninstalledApps = "com.mymatrixusage.uninstalled.apps"; | |
static readonly HashSet<string> sysProcesses = new HashSet<string> | |
{ | |
"browser_broker.exe", "omadmclient.exe", "IPv6 Control Message", "System", "findmyphoneruntime.exe", "telwp.exe", "ussscan.exe", "devicereg.exe", "taskhostw.exe", "runtimebroker.exe", "{6F3EDD9B-5CBC-DF11-9EAE-00237DE2DB9E}", "{B2D00458-5FBC-DF11-9EAE-00237DE2DB9E}", "{106F9AD9-2966-492E-9E67-6E0C88E59312}", "wifitask.exe", "installagent.exe" | |
}; | |
/// <summary> | |
/// Retrieves application's name. | |
/// </summary> | |
/// <param name="aAppInfo">The application info to retrieve the name from.</param> | |
/// <returns>localized application's name</returns> | |
public static string GetApplicationName(this AppInfo aAppInfo) | |
{ | |
Debug.Assert(aAppInfo != null); | |
var appName = ""; | |
if (aAppInfo.PackageName == PackageNameUninstalledApps || aAppInfo.Uid == AppInfo.InvalidUid || aAppInfo.Uid == UidUninstalledApps) | |
{ | |
appName = "Uninstalled Apps"; | |
} | |
else if (aAppInfo.IsSystem == true) | |
{ | |
appName = "System"; | |
} | |
else | |
{ | |
appName = aAppInfo.PackageName; | |
} | |
return appName; | |
} | |
private static PackageManager _pkgManager; | |
/// <summary> | |
/// Changes application package or uid if the app was uninstalled. | |
/// </summary> | |
/// <param name="aContext"> The context. </param> | |
/// <param name="aPackageName"> The package name. </param> | |
/// <param name="aAppUid"> The app uid. </param> | |
public static void ChangeUninstalledAppInfo(this AppInfo aAppInfo) | |
{ | |
_pkgManager = _pkgManager ?? new PackageManager(); | |
bool isUninstalled = false; | |
if (sysProcesses.Contains(aAppInfo.PackageName)) | |
{ | |
//aAppInfo.PackageName = "System"; // displays duplicates if name is not set | |
aAppInfo.IsSystem = true; | |
} | |
else if (!String.IsNullOrEmpty(aAppInfo.PackageName)) | |
{ | |
try | |
{ | |
var pkgs = _pkgManager.FindPackagesForUser("", aAppInfo.Uid); | |
if (pkgs.Count() == 0) | |
{ | |
aAppInfo.Uid = UidUninstalledApps; | |
isUninstalled = true; | |
//aAppInfo.PackageName = PackageNameUninstalledApps; // displays duplicates if name is not set | |
} | |
} | |
catch (Exception exc) | |
{ | |
Debug.WriteLine(exc.Message); | |
} | |
} | |
aAppInfo.IsUninstalled = isUninstalled; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment