Skip to content
This repository has been archived by the owner on Feb 19, 2020. It is now read-only.

Implemented core functionality for WPF #88

Closed
wants to merge 13 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 29 additions & 12 deletions Src/Kit.WPF/HockeyClientWPFExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.Windows.Threading;
using Microsoft.HockeyApp.Extensibility;
using Microsoft.HockeyApp.Services;
using Microsoft.HockeyApp.Extensibility.Windows;

namespace Microsoft.HockeyApp
{
Expand Down Expand Up @@ -44,20 +45,34 @@ public static IUpdateManager UpdateManager
/// <returns>HockeyClient configurable.</returns>
public static IHockeyClientConfigurable Configure(this IHockeyClient @this, string identifier)
{
@this.AsInternal().AppIdentifier = identifier;
@this.AsInternal().PlatformHelper = new HockeyPlatformHelperWPF();
var applicationService = new ApplicationService();
var deviceService = new DeviceService();
var platformHelper = new HockeyPlatformHelperWPF(applicationService, deviceService);

var appId = Guid.Parse(identifier);

@this.AsInternal().AppIdentifier = appId.ToString("N");
@this.AsInternal().PlatformHelper = platformHelper;

AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
Application.Current.DispatcherUnhandledException += Current_DispatcherUnhandledException;
ServiceLocator.AddService<IPlatformService>(new PlatformService());
TelemetryConfiguration.Active.InstrumentationKey = identifier;

ServiceLocator.AddService<IApplicationService>(applicationService);
ServiceLocator.AddService<IDeviceService>(deviceService);
ServiceLocator.AddService<BaseStorageService>(new StorageService());
ServiceLocator.AddService<IUnhandledExceptionTelemetryModule>(new UnhandledExceptionTelemetryModule());

TelemetryConfiguration.Active.InstrumentationKey = appId.ToString("D");

WindowsAppInitializer
.InitializeAsync(appId.ToString("D"), TelemetryConfiguration.Active)
.ContinueWith(task => HockeyClient.Current.AsInternal().HandleInternalUnhandledException(task.Exception),
TaskContinuationOptions.OnlyOnFaulted);

return (IHockeyClientConfigurable)@this;
}

private static Action<UnhandledExceptionEventArgs> customUnhandledExceptionAction;
private static Action<UnobservedTaskExceptionEventArgs> customUnobservedTaskExceptionAction;
private static Action<DispatcherUnhandledExceptionEventArgs> customDispatcherUnhandledExceptionAction;
internal static Action<UnhandledExceptionEventArgs> customUnhandledExceptionAction;
internal static Action<UnobservedTaskExceptionEventArgs> customUnobservedTaskExceptionAction;
internal static Action<DispatcherUnhandledExceptionEventArgs> customDispatcherUnhandledExceptionAction;

/// <summary>
/// Adds the handler for UnobservedTaskExceptions
Expand Down Expand Up @@ -219,7 +234,7 @@ public static IFeedbackThread CreateFeedbackThread(this IHockeyClient @this)
/// <returns>
/// The Feedback-Thread or, if not found or delete, null.
/// </returns>
public static async Task<IFeedbackThread> OpenFeedbackThreadAsync(this IHockeyClient @this,string feedbackToken)
public static async Task<IFeedbackThread> OpenFeedbackThreadAsync(this IHockeyClient @this, string feedbackToken)
{
return await @this.AsInternal().OpenFeedbackThreadAsync(feedbackToken);
}
Expand Down Expand Up @@ -270,12 +285,14 @@ public static async Task<bool> SendCrashesAsync(this IHockeyClient @this, Boolea
/// </summary>
public static string AppIdHash
{
get {
get
{
if (_appIdHash == null)
{
_appIdHash = GetMD5Hash(HockeyClient.Current.AsInternal().AppIdentifier);
}
return _appIdHash; }
return _appIdHash;
}
}

internal static string GetMD5Hash(string sourceString)
Expand Down
130 changes: 40 additions & 90 deletions Src/Kit.WPF/HockeyPlatformHelperWPF.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.Win32;
using Microsoft.HockeyApp.Services;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
Expand All @@ -18,12 +19,25 @@ namespace Microsoft.HockeyApp
/// <summary>
/// HockeyPlatformHelperWPF class.
/// </summary>
public class HockeyPlatformHelperWPF : IHockeyPlatformHelper
internal class HockeyPlatformHelperWPF : IHockeyPlatformHelper
{

private const string FILE_PREFIX = "HA__SETTING_";
IsolatedStorageFile isoStore = IsolatedStorageFile.GetStore(IsolatedStorageScope.User | IsolatedStorageScope.Assembly, null, null);

private ApplicationService applicationService;
private DeviceService deviceService;

/// <summary>
/// Initializes a new instance of the <see cref="HockeyPlatformHelperWPF"/> class.
/// </summary>
/// <param name="applicationService">The application service.</param>
/// <param name="deviceService">The device service.</param>
internal HockeyPlatformHelperWPF(ApplicationService applicationService, DeviceService deviceService)
{
this.applicationService = applicationService;
this.deviceService = deviceService;
}

private string PostfixWithAppIdHash(string folderName, bool noDirectorySeparator = false)
{
return ((folderName ?? "") + (noDirectorySeparator ? "" : "" + Path.DirectorySeparatorChar) + HockeyClientWPFExtensions.AppIdHash);
Expand Down Expand Up @@ -54,10 +68,12 @@ public void SetSettingValue(string key, string value)
[SuppressMessage("Microsoft.Usage", "CA2202:Do not dispose objects multiple times", Justification = "ToDo: Fix it later.")]
public string GetSettingValue(string key)
{
if(isoStore.FileExists(FILE_PREFIX + key)) {
if (isoStore.FileExists(FILE_PREFIX + key))
{
using (var fileStream = isoStore.OpenFile(PostfixWithAppIdHash(FILE_PREFIX + key, true), FileMode.Open, FileAccess.Read))
{
using(var reader = new StreamReader(fileStream)){
using (var reader = new StreamReader(fileStream))
{
return reader.ReadToEnd();
}
}
Expand Down Expand Up @@ -128,11 +144,13 @@ public async Task<Stream> GetStreamAsync(string fileName, string folderName = nu
public async Task WriteStreamToFileAsync(Stream dataStream, string fileName, string folderName = null)
{
// Ensure crashes folder exists
if (!isoStore.DirectoryExists(PostfixWithAppIdHash(folderName))) {
if (!isoStore.DirectoryExists(PostfixWithAppIdHash(folderName)))
{
isoStore.CreateDirectory(PostfixWithAppIdHash(folderName));
}

using (var fileStream = isoStore.OpenFile(PostfixWithAppIdHash(folderName) + Path.DirectorySeparatorChar + fileName,FileMode.Create,FileAccess.Write)) {
using (var fileStream = isoStore.OpenFile(PostfixWithAppIdHash(folderName) + Path.DirectorySeparatorChar + fileName, FileMode.Create, FileAccess.Write))
{
await dataStream.CopyToAsync(fileStream);
}
}
Expand All @@ -145,9 +163,12 @@ public async Task WriteStreamToFileAsync(Stream dataStream, string fileName, str
/// <returns>Task list.</returns>
public async Task<IEnumerable<string>> GetFileNamesAsync(string folderName = null, string fileNamePattern = null)
{
try {
try
{
return isoStore.GetFileNames(PostfixWithAppIdHash(folderName) + Path.DirectorySeparatorChar + fileNamePattern ?? "*");
} catch (DirectoryNotFoundException) {
}
catch (DirectoryNotFoundException)
{
return new string[0];
}
}
Expand Down Expand Up @@ -182,54 +203,20 @@ public void WriteStreamToFileSync(Stream dataStream, string fileName, string fol

#endregion

string _appPackageName = null;

/// <summary>
/// Gets or sets application package name.
/// </summary>
public string AppPackageName
{
get
{
if(_appPackageName == null) {
_appPackageName = Application.Current.GetType().Namespace;
}
return _appPackageName;
}
set
{
_appPackageName = value;
}
get { return this.applicationService.GetApplicationId(); }
}

string _appVersion = null;

/// <summary>
/// Gets or sets application version.
/// </summary>
public string AppVersion
{
get {
if(_appVersion == null) {
//ClickOnce
try
{
var type = Type.GetType("System.Deployment.Application.ApplicationDeployment");
object deployment = type.GetMethod("CurrentDeployment").Invoke(null, null);
Version version = type.GetMethod("CurrentVersion").Invoke(deployment, null) as Version;
_appVersion = version.ToString();
}
catch (Exception)
{
//Entry Assembly
_appVersion = Assembly.GetEntryAssembly().GetName().Version.ToString();
}
}
return _appVersion ?? "0.0.0.1";
}
set {
_appVersion = value;
}
get { return this.applicationService.GetVersion(); }
}


Expand All @@ -238,29 +225,14 @@ public string AppVersion
/// </summary>
public string OSVersion
{
get
{
//as windows 8.1 lies to us to be 8 we try via registry
try
{
using (RegistryKey registryKey = Registry.LocalMachine.OpenSubKey(@"Software\Microsoft\Windows NT\CurrentVersion"))
{
return (string)registryKey.GetValue("CurrentVersion") + "." + (string)registryKey.GetValue("CurrentBuild") + ".0";
}
}
catch (Exception e)
{
HockeyClient.Current.AsInternal().HandleInternalUnhandledException(e);
}
return Environment.OSVersion.Version.ToString() + " " + Environment.OSVersion.ServicePack;
}
get { return this.deviceService.GetOperatingSystemVersion(); }
}

/// <summary>
/// Gets OS platform name.
/// </summary>
public string OSPlatform
{
{
get { return "Windows"; }
}

Expand All @@ -270,10 +242,7 @@ public string OSPlatform
/// </summary>
public string SDKVersion
{
get
{
return Extensibility.SdkVersionPropertyContextInitializer.GetAssemblyVersion();
}
get { return Extensibility.SdkVersionPropertyContextInitializer.GetAssemblyVersion(); }
}


Expand All @@ -282,8 +251,7 @@ public string SDKVersion
/// </summary>
public string SDKName
{
get
{ return HockeyConstants.SDKNAME; }
get { return HockeyConstants.SDKNAME; }
}

/// <summary>
Expand All @@ -304,42 +272,24 @@ public string ProductID
get { return _productID; }
set { _productID = value; }
}


/// <summary>
/// Gets manufacturer.
/// </summary>
public string Manufacturer
{
get {
//TODO System.Management referenzieren !?
/*
Type.GetType
ManagementClass mc = new ManagementClass("Win32_ComputerSystem");
//collection to store all management objects
ManagementObjectCollection moc = mc.GetInstances();
if (moc.Count != 0)
{
foreach (ManagementObject mo in mc.GetInstances())
{
mo["Manufacturer"].ToString()
*/
return null;
}
get { return deviceService.GetSystemManufacturer(); }
}

/// <summary>
/// Gets model.
/// </summary>
public string Model
{
get
{
//TODO siehe Manufacturer mit "Model"
return null;
}
get { return deviceService.GetDeviceModel(); }
}

}
#pragma warning restore 1998
}
}
Loading