/
LittleWatson.cs
183 lines (166 loc) · 6.87 KB
/
LittleWatson.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
using System;
using System.Globalization;
using System.IO;
using System.IO.IsolatedStorage;
using System.Net;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;
using System.Xml;
using Microsoft.Phone.Info;
using Microsoft.Phone.Tasks;
namespace UKTrains
{
public static class LittleWatson
{
private const string filename = "LittleWatson.txt";
public static void ReportException(Exception ex, string header)
{
try
{
using (var store = IsolatedStorageFile.GetUserStoreForApplication())
{
using (var output = new StreamWriter(store.OpenFile(filename, FileMode.Append, FileAccess.Write)))
{
output.WriteLine(header);
if (ex != null)
{
output.WriteLine(ex.ToString());
}
output.WriteLine();
}
}
}
catch
{
}
}
public static void CheckForPreviousException(bool startingUp)
{
try
{
string contents = null;
using (var store = IsolatedStorageFile.GetUserStoreForApplication())
{
if (store.FileExists(filename))
{
using (var reader = new StreamReader(store.OpenFile(filename, FileMode.Open, FileAccess.Read, FileShare.None)))
{
contents = reader.ReadToEnd();
}
SafeDeleteFile(store);
}
}
if (contents != null)
{
var title = "A problem occurred" + (startingUp ? " the last time you ran this application" : "") + ". Would you like to send an email to report it?";
if (MessageBox.Show(title, "Problem Report", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
{
var email = new EmailComposeTask();
email.To = "uktrains@codebeside.org";
email.Subject = "UK Trains auto-generated problem report";
if (contents.Length > 32000)
{
contents = contents.Substring(0, 32000) + " ...";
}
email.Body = GetMailBody(contents);
email.Show();
}
}
}
catch
{
}
}
public static string GetMailBody(string contents)
{
return "\n\n" + contents + "\n\n" +
"App Version: " + new AssemblyName(Assembly.GetExecutingAssembly().FullName).Version + "\n" +
"OS Version: " + Environment.OSVersion.Version + "\n" +
"Phone: " + DeviceStatus.DeviceManufacturer + " " + DeviceStatus.DeviceName + "\n" +
"Culture: " + CultureInfo.CurrentCulture + "/" + CultureInfo.CurrentUICulture;
}
private static void SafeDeleteFile(IsolatedStorageFile store)
{
try
{
store.DeleteFile(filename);
}
catch
{
}
}
private static string GetManifestAttributeValue(string attributeName)
{
var xmlReaderSettings = new XmlReaderSettings
{
XmlResolver = new XmlXapResolver()
};
using (var xmlReader = XmlReader.Create("WMAppManifest.xml", xmlReaderSettings))
{
xmlReader.ReadToDescendant("App");
return xmlReader.GetAttribute(attributeName);
}
}
public static void CheckForNewVersion(Page page)
{
var lastNewVersionCheck = Settings.GetDateTime(Setting.LastNewVersionCheck);
if (!lastNewVersionCheck.HasValue)
{
Settings.Set(Setting.LastNewVersionCheck, DateTime.UtcNow);
return;
}
if ((DateTime.UtcNow - lastNewVersionCheck.Value).TotalDays < 7)
{
//only check once a week
return;
}
try
{
var cultureInfoName = CultureInfo.CurrentUICulture.Name;
var url = string.Format("http://marketplaceedgeservice.windowsphone.com/v8/catalog/apps/{0}?os={1}&cc={2}&oc=&lang={3}",
GetManifestAttributeValue("ProductID"),
Environment.OSVersion.Version,
cultureInfoName.Substring(cultureInfoName.Length - 2).ToUpperInvariant(),
cultureInfoName);
var request = WebRequest.Create(url);
request.BeginGetResponse(result =>
{
try
{
var response = (HttpWebResponse)request.EndGetResponse(result);
if (response.StatusCode == HttpStatusCode.OK)
{
using (var outputStream = response.GetResponseStream())
{
using (var reader = XmlReader.Create(outputStream))
{
reader.MoveToContent();
var aNamespace = reader.LookupNamespace("a");
reader.ReadToFollowing("entry", aNamespace);
reader.ReadToDescendant("version");
var updatedVersion = new Version(reader.ReadElementContentAsString());
var currentVersion = new Version(GetManifestAttributeValue("Version"));
Settings.Set(Setting.LastNewVersionCheck, DateTime.UtcNow);
if (updatedVersion > currentVersion)
{
page.Dispatcher.BeginInvoke(() =>
{
if (MessageBox.Show("Do you want to install the new version now?", "Update Available", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
{
new MarketplaceDetailTask().Show();
}
});
}
}
}
}
}
catch { }
},
null);
}
catch { }
}
}
}