-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathHelper.cs
510 lines (465 loc) · 15.6 KB
/
Helper.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
using MarkerMetro.Unity.WinIntegration.Resources;
using MarkerMetro.Unity.WinIntegration.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
#if NETFX_CORE
using Windows.ApplicationModel;
using Windows.ApplicationModel.DataTransfer;
using Windows.ApplicationModel.Contacts;
using Windows.System;
using System.Diagnostics;
using System.Threading.Tasks;
using Windows.ApplicationModel.Resources;
using Windows.Networking.Connectivity;
using Windows.UI.Popups;
using Windows.Foundation;
using Windows.Security.ExchangeActiveSyncProvisioning;
using Windows.Networking.PushNotifications;
using Windows.ApplicationModel.Store;
#endif
namespace MarkerMetro.Unity.WinIntegration
{
/// <summary>
/// Integration Helpers
/// </summary>
public class Helper
{
private static Helper _instance;
private static readonly object _sync = new object();
public static Helper Instance
{
get
{
lock (_sync)
{
if (_instance == null)
_instance = new Helper();
}
return _instance;
}
}
/// <summary>
/// Fired when the App visibility has changed
/// </summary>
/// <remarks>
/// This should be activated in CommonMainPage.cs, when a Window.Current.VisibilityChanged happens.
/// </remarks>
public Action<bool> OnVisibilityChanged;
/// <summary>
/// Returns the application package version
/// </summary>
/// <returns></returns>
public string GetAppVersion()
{
#if NETFX_CORE
var major = Package.Current.Id.Version.Major;
var minor = Package.Current.Id.Version.Minor.ToString();
var revision = Package.Current.Id.Version.Revision.ToString();
var build = Package.Current.Id.Version.Build.ToString();
var version = String.Format("{0}.{1}.{2}.{3}", major, minor, build, revision);
return version;
#else
return String.Empty;
#endif
}
/// <summary>
/// Clears all local state
/// </summary>
/// <remarks>
/// used to clear all local state from the app
/// </remarks>
public void ClearLocalState(Action<bool> callback)
{
#if NETFX_CORE
Dispatcher.InvokeOnUIThread(async () =>
{
try
{
await Windows.Storage.ApplicationData.Current.ClearAsync(Windows.Storage.ApplicationDataLocality.Local);
if (callback != null)
{
Dispatcher.InvokeOnAppThread(() => callback(true));
};
}
catch
{
if (callback != null)
{
Dispatcher.InvokeOnAppThread(() => callback(false));
};
}
});
#else
throw new PlatformNotSupportedException("ClearLocalState");
#endif
}
public void GetPushChannel(string channelName, Action<string> callback)
{
#if NETFX_CORE
Dispatcher.InvokeOnUIThread(async () =>
{
try
{
var channel = await PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync();
if (callback != null)
{
Dispatcher.InvokeOnAppThread(() => callback(channel.Uri));
}
}
catch
{
if (callback != null)
{
Dispatcher.InvokeOnAppThread(() => callback(String.Empty));
}
}
});
#else
throw new PlatformNotSupportedException("GetPushChannel(string channelName, Action<string> callback)");
#endif
}
/// <summary>
/// Returns the application language
/// </summary>
/// <remarks>
/// it's important to use this call rather than Unity APIs, which just return the top system language
/// </remarks>
public string GetAppLanguage()
{
#if NETFX_CORE
return Windows.Globalization.ApplicationLanguages.Languages[0];
#else
return System.Globalization.CultureInfo.CurrentUICulture.Name;
#endif
}
/// <summary>
/// Show the Share UI
/// </summary>
public void ShowShareUI()
{
#if NETFX_CORE
try
{
Dispatcher.InvokeOnUIThread(() => DataTransferManager.ShowShareUI());
}
catch (Exception ex)
{
#if DEBUG
Debug.WriteLine("Unable to show the share UI because of: " + ex.Message);
#else
ExceptionLogger.Send(ex);
#endif
}
#endif
}
/// <summary>
/// Show the Share UI with a link - WP8 only
/// </summary>
/// <param name="text"></param>
/// <param name="linkURL"></param>
public void ShowShareUI(string text, string linkURL)
{
ShowShareUI(text, text, linkURL);
}
/// <summary>
/// Show the Share UI with a link - WP8 only
/// </summary>
/// <param name="text"></param>
/// <param name="message"></param>
/// <param name="linkURL"></param>
public void ShowShareUI(string text, string message, string linkURL)
{
#if NETFX_CORE
throw new NotImplementedException("Not implemented for Windows Store Apps, use ShowShareUI() instead.");
#endif
}
/// <summary>
/// Show the Rate UI
/// </summary>
public void ShowRateUI()
{
#if NETFX_CORE
Dispatcher.InvokeOnUIThread(
async () =>
{
try
{
Uri uri;
#if WINDOWS_PHONE_APP
uri = new Uri(String.Format("ms-windows-store:REVIEWAPP?appid={0}", CurrentApp.AppId));
#else
var data = Package.Current.Id.FamilyName;
uri = new Uri(String.Format("ms-windows-store:REVIEW?PFN={0}", data));
#endif
# if DEBUG
Debug.WriteLine(uri.ToString());
# endif
await Launcher.LaunchUriAsync(uri);
}
catch (Exception ex)
{
# if DEBUG
Debug.WriteLine("Unable to show MarketplaceReviewTask because of: " + ex.Message);
# else
ExceptionLogger.Send(ex);
# endif
}
});
#else
throw new NotImplementedException("Not implemented for unknown platform");
#endif
}
/// <summary>
/// Uses a roaming Guid for Windows 8 and the device id for WP8
/// </summary>
/// <returns></returns>
public string GetUserDeviceId()
{
#if NETFX_CORE
const string key = "UserDeviceId";
var values = Windows.Storage.ApplicationData.Current.RoamingSettings.Values;
if (!values.ContainsKey(key))
{
var value = Guid.NewGuid().ToString().Replace("-", "");
values[key] = value;
}
return (string)values[key];
#else
throw new PlatformNotSupportedException("GetUserDeviceId()");
#endif
}
/// <summary>
/// Returns the device manufacturer name.
/// </summary>
public string GetManufacturer()
{
#if NETFX_CORE
return new EasClientDeviceInformation().SystemManufacturer;
#else
throw new PlatformNotSupportedException("GetManufacturer()");
#endif
}
/// <summary>
/// Returns the device model (e.g. "NOKIA Lumia 720").
/// </summary>
/// <remarks>
/// If we need a more friendly name on WP8, read this:
/// http://stackoverflow.com/questions/17425016/information-about-windows-phone-model-number
/// </remarks>
public string GetModel()
{
#if NETFX_CORE
return new EasClientDeviceInformation().SystemProductName;
#else
throw new PlatformNotSupportedException("GetManufacturer()");
#endif
}
/// <summary>
/// Returns the application's store url. Note: This won't be valid until the apps are published
/// </summary>
/// <returns></returns>
public string GetAppStoreUri()
{
#if WINDOWS_PHONE_APP
return "ms-windows-store:navigate?appid=" + CurrentApp.AppId;
#elif NETFX_CORE
return "ms-windows-store:PDP?PFN=" + Package.Current.Id.FamilyName;
#else
return "";
#endif
}
#if NETFX_CORE && !WINDOWS_PHONE_APP
public enum ProcessorArchitecture : ushort
{
INTEL = 0,
MIPS = 1,
ALPHA = 2,
PPC = 3,
SHX = 4,
ARM = 5,
IA64 = 6,
ALPHA64 = 7,
MSIL = 8,
AMD64 = 9,
IA32_ON_WIN64 = 10,
UNKNOWN = 0xFFFF
}
[System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)]
internal struct SYSTEM_INFO
{
public ushort wProcessorArchitecture;
public ushort wReserved;
public uint dwPageSize;
public IntPtr lpMinimumApplicationAddress;
public IntPtr lpMaximumApplicationAddress;
public UIntPtr dwActiveProcessorMask;
public uint dwNumberOfProcessors;
public uint dwProcessorType;
public uint dwAllocationGranularity;
public ushort wProcessorLevel;
public ushort wProcessorRevision;
};
#if WINDOWS_UWP
[System.Runtime.InteropServices.DllImport("api-ms-win-core-sysinfo-l1-2-0.dll")]
internal static extern void GetNativeSystemInfo(ref SYSTEM_INFO lpSystemInfo);
#else
[System.Runtime.InteropServices.DllImport("kernel32.dll")]
internal static extern void GetNativeSystemInfo(ref SYSTEM_INFO lpSystemInfo);
#endif
#endif
/// <summary>
/// Determine whether or not a Windows device is generally considered low end
/// </summary>
/// <returns>Windows 8 Arm or Windows Phone Low Mem returns true</returns>
public bool IsLowEndDevice()
{
#if WINDOWS_PHONE_APP
long result = 0;
try
{
result = (long)MemoryManager.AppMemoryUsageLimit;
}
catch (ArgumentOutOfRangeException)
{
// The device does not support querying for this value. This occurs
// on Windows Phone OS 7.1 and older phones without OS updates.
}
return result <= 188743680; // less than or equal to 180MB including failure scenario
#elif NETFX_CORE
var systemInfo = new SYSTEM_INFO();
GetNativeSystemInfo(ref systemInfo);
return systemInfo.wProcessorArchitecture == (uint)ProcessorArchitecture.ARM;
#else
return false;
#endif
}
/// <summary>
/// This is used to check whether the app is running on Windows Mobile.
/// Should only be used for Windows 10 Universal project.
/// </summary>
public static bool IsWindowsMobile
{
get
{
#if WINDOWS_UWP && NETFX_CORE
return Windows.Foundation.Metadata.ApiInformation.IsApiContractPresent ("Windows.Phone.PhoneContract", 1);
#else
throw new PlatformNotSupportedException("IsWindowsMobile");
#endif
}
}
public bool HasInternetConnection
{
get
{
#if NETFX_CORE
var profile = NetworkInformation.GetInternetConnectionProfile();
return profile != null &&
profile.GetNetworkConnectivityLevel() == NetworkConnectivityLevel.InternetAccess;
#else
throw new PlatformNotSupportedException("HasInternetConnection");
#endif
}
}
public bool IsMeteredConnection
{
get
{
#if NETFX_CORE
var profile = NetworkInformation.GetInternetConnectionProfile();
if (profile == null)
{
return false;
}
return profile.GetConnectionCost().NetworkCostType != NetworkCostType.Unrestricted;
#else
throw new PlatformNotSupportedException("IsMeteredConnection");
#endif
}
}
public void ShowDialog(string contentKey, string titleKey, Action callback)
{
#if NETFX_CORE
if (string.IsNullOrWhiteSpace(contentKey))
throw new ArgumentNullException("You must specify content resource key");
Dispatcher.InvokeOnUIThread(() =>
{
var resourceHelper = ResourceHelper.GetInstance();
var content = resourceHelper.GetString(contentKey);
var title = string.Empty;
if (titleKey != null)
title = resourceHelper.GetString(titleKey);
#if NETFX_CORE
ShowDialogAsync(contentKey, titleKey, callback).ContinueWith(t => { });
});
# endif
#else
throw new PlatformNotSupportedException("ShowDialog");
#endif
}
public void ShowDialog(string content, string title, Action<bool> callback, string okText = "", string cancelText = "")
{
#if NETFX_CORE
Dispatcher.InvokeOnUIThread(() =>
{
ShowDialogAsync(content, title, callback, okText, cancelText).ContinueWith(t => { });
});
#else
throw new PlatformNotSupportedException("ShowDialog");
#endif
}
#if NETFX_CORE
async Task ShowDialogAsync(string content, string title, Action callback)
{
var dialog = string.IsNullOrWhiteSpace(title) ? new MessageDialog(content) : new MessageDialog(content, title);
await dialog.ShowAsync();
if (callback != null)
Dispatcher.InvokeOnAppThread(() => callback());
}
async Task ShowDialogAsync(string content, string title, Action<bool> callback, string okText, string cancelText)
{
var dialog = string.IsNullOrWhiteSpace(title) ? new MessageDialog(content) : new MessageDialog(content, title);
Action<bool> callbackOnApp = b => Dispatcher.InvokeOnAppThread(() => callback(b));
if (!string.IsNullOrWhiteSpace(okText))
{
dialog.Commands.Add(new UICommand(okText, new UICommandInvokedHandler(
(command) => { if (callback != null) callbackOnApp(true); })));
}
if (!string.IsNullOrWhiteSpace(cancelText))
{
dialog.Commands.Add(new UICommand(cancelText, new UICommandInvokedHandler(
(command) => { if (callback != null) callbackOnApp(false); })));
}
if (!string.IsNullOrWhiteSpace(okText) && !string.IsNullOrWhiteSpace(cancelText))
{
// Set the command that will be invoked by default
dialog.DefaultCommandIndex = 0;
// Set the command to be invoked when escape is pressed
dialog.CancelCommandIndex = 1;
}
await dialog.ShowAsync();
}
#endif
/// <summary>
/// Win8 - Launches a mailto: URI.
/// WP8 - Calls email compose task.
/// </summary>
/// <param name="to">A comma-separated list of email addresses.</param>
/// <param name="subject">The subject of the message.</param>
/// <param name="body">The body of the message.</param>
public void SendEmail(string to, string subject, string body)
{
#if NETFX_CORE
Dispatcher.InvokeOnUIThread(async () =>
{
subject = Uri.EscapeDataString(subject);
body = Uri.EscapeDataString(body);
var mailto = new Uri(String.Format("mailto:?to={0}&subject={1}&body={2}", to, subject, body));
await Launcher.LaunchUriAsync(mailto);
});
#endif
}
}
}