Skip to content

Commit

Permalink
feat: AnalyticsInfo support for WASM
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinZikmund committed Oct 25, 2020
1 parent ba9356c commit 8b7b97d
Show file tree
Hide file tree
Showing 6 changed files with 202 additions and 48 deletions.
5 changes: 5 additions & 0 deletions src/Uno.UI/WasmScripts/Uno.UI.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1114,6 +1114,11 @@ declare namespace Windows.System.Display {
static deactivateScreenLock(): void;
}
}
declare namespace Windows.System.Profile {
class AnalyticsInfo {
static getDeviceType(): string;
}
}
interface Window {
opr: any;
opera: any;
Expand Down
79 changes: 79 additions & 0 deletions src/Uno.UI/WasmScripts/Uno.UI.js
Original file line number Diff line number Diff line change
Expand Up @@ -3667,6 +3667,85 @@ var Windows;
})(System = Windows.System || (Windows.System = {}));
})(Windows || (Windows = {}));
var Windows;
(function (Windows) {
var System;
(function (System) {
var Profile;
(function (Profile) {
class AnalyticsInfo {
static getDeviceType() {
var ua = navigator.userAgent;
if (!ua || ua === '') {
// No user agent.
return "unknown";
}
if (ua.match(/GoogleTV|SmartTV|SMART-TV|Internet TV|NetCast|NETTV|AppleTV|boxee|Kylo|Roku|DLNADOC|hbbtv|CrKey|CE\-HTML/i)) {
// if user agent is a smart TV - http://goo.gl/FocDk
return "Television";
}
else if (ua.match(/Xbox|PLAYSTATION (3|4)|Wii/i)) {
// if user agent is a TV Based Gaming Console
return "GameConsole";
}
else if (ua.match(/QtCarBrowser/i)) {
// if the user agent is a car
return "Car";
}
else if (ua.match(/iP(a|ro)d/i) || (ua.match(/tablet/i) && !ua.match(/RX-34/i)) || ua.match(/FOLIO/i)) {
// if user agent is a Tablet
return "Tablet";
}
else if (ua.match(/Linux/i) && ua.match(/Android/i) && !ua.match(/Fennec|mobi|HTC Magic|HTCX06HT|Nexus One|SC-02B|fone 945/i)) {
// if user agent is an Android Tablet
return "Tablet";
}
else if (ua.match(/Kindle/i) || (ua.match(/Mac OS/i) && ua.match(/Silk/i)) || (ua.match(/AppleWebKit/i) && ua.match(/Silk/i) && !ua.match(/Playstation Vita/i))) {
// if user agent is a Kindle or Kindle Fire
return "Tablet";
}
else if (ua.match(/GT-P10|SC-01C|SHW-M180S|SGH-T849|SCH-I800|SHW-M180L|SPH-P100|SGH-I987|zt180|HTC( Flyer|_Flyer)|Sprint ATP51|ViewPad7|pandigital(sprnova|nova)|Ideos S7|Dell Streak 7|Advent Vega|A101IT|A70BHT|MID7015|Next2|nook/i) || (ua.match(/MB511/i) && ua.match(/RUTEM/i))) {
// if user agent is a pre Android 3.0 Tablet
return "Tablet";
}
else if (ua.match(/BOLT|Fennec|Iris|Maemo|Minimo|Mobi|mowser|NetFront|Novarra|Prism|RX-34|Skyfire|Tear|XV6875|XV6975|Google Wireless Transcoder/i) && !ua.match(/AdsBot-Google-Mobile/i)) {
// if user agent is unique phone User Agent
return "Mobile";
}
else if (ua.match(/Opera/i) && ua.match(/Windows NT 5/i) && ua.match(/HTC|Xda|Mini|Vario|SAMSUNG\-GT\-i8000|SAMSUNG\-SGH\-i9/i)) {
// if user agent is an odd Opera User Agent - http://goo.gl/nK90K
return "Mobile";
}
else if ((ua.match(/Windows( )?(NT|XP|ME|9)/) && !ua.match(/Phone/i)) && !ua.match(/Bot|Spider|ia_archiver|NewsGator/i) || ua.match(/Win( ?9|NT)/i) || ua.match(/Go-http-client/i)) {
// if user agent is Windows Desktop
return "Desktop";
}
else if (ua.match(/Macintosh|PowerPC/i) && !ua.match(/Silk|moatbot/i)) {
// if agent is Mac Desktop
return "Desktop";
}
else if (ua.match(/Linux/i) && ua.match(/X11/i) && !ua.match(/Charlotte|JobBot/i)) {
// if user agent is a Linux Desktop
return "Desktop";
}
else if (ua.match(/CrOS/)) {
// if user agent is a Chrome Book
return "Desktop";
}
else if (ua.match(/Solaris|SunOS|BSD/i)) {
// if user agent is a Solaris, SunOS, BSD Desktop
return "Desktop";
}
else {
// Otherwise returning the unknown type configured
return "Unknown";
}
}
}
Profile.AnalyticsInfo = AnalyticsInfo;
})(Profile = System.Profile || (System.Profile = {}));
})(System = Windows.System || (Windows.System = {}));
})(Windows || (Windows = {}));
var Windows;
(function (Windows) {
var System;
(function (System) {
Expand Down
59 changes: 59 additions & 0 deletions src/Uno.UI/ts/Windows/System/Profile/AnalyticsInfo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
namespace Windows.System.Profile {

export class AnalyticsInfo {
public static getDeviceType(): string {
var ua = navigator.userAgent;

if (!ua || ua === '') {
// No user agent.
return "unknown";
}
if (ua.match(/GoogleTV|SmartTV|SMART-TV|Internet TV|NetCast|NETTV|AppleTV|boxee|Kylo|Roku|DLNADOC|hbbtv|CrKey|CE\-HTML/i)) {
// if user agent is a smart TV - http://goo.gl/FocDk
return "Television";
} else if (ua.match(/Xbox|PLAYSTATION (3|4)|Wii/i)) {
// if user agent is a TV Based Gaming Console
return "GameConsole";
} else if (ua.match(/QtCarBrowser/i)) {
// if the user agent is a car
return "Car";
} else if (ua.match(/iP(a|ro)d/i) || (ua.match(/tablet/i) && !ua.match(/RX-34/i)) || ua.match(/FOLIO/i)) {
// if user agent is a Tablet
return "Tablet";
} else if (ua.match(/Linux/i) && ua.match(/Android/i) && !ua.match(/Fennec|mobi|HTC Magic|HTCX06HT|Nexus One|SC-02B|fone 945/i)) {
// if user agent is an Android Tablet
return "Tablet";
} else if (ua.match(/Kindle/i) || (ua.match(/Mac OS/i) && ua.match(/Silk/i)) || (ua.match(/AppleWebKit/i) && ua.match(/Silk/i) && !ua.match(/Playstation Vita/i))) {
// if user agent is a Kindle or Kindle Fire
return "Tablet";
} else if (ua.match(/GT-P10|SC-01C|SHW-M180S|SGH-T849|SCH-I800|SHW-M180L|SPH-P100|SGH-I987|zt180|HTC( Flyer|_Flyer)|Sprint ATP51|ViewPad7|pandigital(sprnova|nova)|Ideos S7|Dell Streak 7|Advent Vega|A101IT|A70BHT|MID7015|Next2|nook/i) || (ua.match(/MB511/i) && ua.match(/RUTEM/i))) {
// if user agent is a pre Android 3.0 Tablet
return "Tablet";
} else if (ua.match(/BOLT|Fennec|Iris|Maemo|Minimo|Mobi|mowser|NetFront|Novarra|Prism|RX-34|Skyfire|Tear|XV6875|XV6975|Google Wireless Transcoder/i) && !ua.match(/AdsBot-Google-Mobile/i)) {
// if user agent is unique phone User Agent
return "Mobile";
} else if (ua.match(/Opera/i) && ua.match(/Windows NT 5/i) && ua.match(/HTC|Xda|Mini|Vario|SAMSUNG\-GT\-i8000|SAMSUNG\-SGH\-i9/i)) {
// if user agent is an odd Opera User Agent - http://goo.gl/nK90K
return "Mobile";
} else if ((ua.match(/Windows( )?(NT|XP|ME|9)/) && !ua.match(/Phone/i)) && !ua.match(/Bot|Spider|ia_archiver|NewsGator/i) || ua.match(/Win( ?9|NT)/i) || ua.match(/Go-http-client/i)) {
// if user agent is Windows Desktop
return "Desktop";
} else if (ua.match(/Macintosh|PowerPC/i) && !ua.match(/Silk|moatbot/i)) {
// if agent is Mac Desktop
return "Desktop";
} else if (ua.match(/Linux/i) && ua.match(/X11/i) && !ua.match(/Charlotte|JobBot/i)) {
// if user agent is a Linux Desktop
return "Desktop";
} else if (ua.match(/CrOS/)) {
// if user agent is a Chrome Book
return "Desktop";
} else if (ua.match(/Solaris|SunOS|BSD/i)) {
// if user agent is a Solaris, SunOS, BSD Desktop
return "Desktop";
} else {
// Otherwise returning the unknown type configured
return "Unknown";
}
}
}
}
90 changes: 45 additions & 45 deletions src/Uno.UI/ts/Windows/System/Profile/AnalyticsVersionInfo.ts
Original file line number Diff line number Diff line change
@@ -1,54 +1,54 @@
interface Window {
opr: any;
opera: any;
mozVibrate(pattern: number | number[]): boolean;
msVibrate(pattern: number | number[]): boolean;
InstallTrigger: any;
HTMLElement: any;
StyleMedia: any;
chrome: any;
CSS: any;
safari: any;
opr: any;
opera: any;
mozVibrate(pattern: number | number[]): boolean;
msVibrate(pattern: number | number[]): boolean;
InstallTrigger: any;
HTMLElement: any;
StyleMedia: any;
chrome: any;
CSS: any;
safari: any;
}

interface Document {
documentMode: any;
documentMode: any;
}

namespace Windows.System.Profile {

export class AnalyticsVersionInfo {
public static getUserAgent(): string {
return navigator.userAgent;
}

public static getBrowserName(): string {
// Opera 8.0+
if ((!!window.opr && !!window.opr.addons) || !!window.opera || navigator.userAgent.indexOf(' OPR/') >= 0) {
return "Opera";
}

// Firefox 1.0+
if (typeof window.InstallTrigger !== 'undefined') {
return "Firefox";
}

// Safari 3.0+ "[object HTMLElementConstructor]"
if (/constructor/i.test(window.HTMLElement) ||
((p: any) => p.toString() === "[object SafariRemoteNotification]")(
typeof window.safari !== 'undefined' && window.safari.pushNotification)) {
return "Safari";
}

// Edge 20+
if (!!window.StyleMedia) {
return "Edge";
}

// Chrome 1 - 71
if (!!window.chrome && (!!window.chrome.webstore || !!window.chrome.runtime)) {
return "Chrome";
}
}
}
export class AnalyticsVersionInfo {
public static getUserAgent(): string {
return navigator.userAgent;
}

public static getBrowserName(): string {
// Opera 8.0+
if ((!!window.opr && !!window.opr.addons) || !!window.opera || navigator.userAgent.indexOf(' OPR/') >= 0) {
return "Opera";
}

// Firefox 1.0+
if (typeof window.InstallTrigger !== 'undefined') {
return "Firefox";
}

// Safari 3.0+ "[object HTMLElementConstructor]"
if (/constructor/i.test(window.HTMLElement) ||
((p: any) => p.toString() === "[object SafariRemoteNotification]")(
typeof window.safari !== 'undefined' && window.safari.pushNotification)) {
return "Safari";
}

// Edge 20+
if (!!window.StyleMedia) {
return "Edge";
}

// Chrome 1 - 71
if (!!window.chrome && (!!window.chrome.webstore || !!window.chrome.runtime)) {
return "Chrome";
}
}
}
}
16 changes: 13 additions & 3 deletions src/Uno.UWP/System/Profile/AnalyticsInfo.wasm.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
#if __WASM__
using System;
using Uno.Foundation;

namespace Windows.System.Profile
{
public static partial class AnalyticsInfo
{
private static UnoDeviceForm GetDeviceForm() => UnoDeviceForm.Unknown;
private const string JsType = "Windows.System.Profile.AnalyticsInfo";

private static UnoDeviceForm GetDeviceForm()
{
var typeString = WebAssemblyRuntime.InvokeJS($"{JsType}.getDeviceType()");
if (Enum.TryParse(typeString, true, out UnoDeviceForm deviceForm))
{
return deviceForm;
}
return UnoDeviceForm.Unknown;
}
}
}
#endif
1 change: 1 addition & 0 deletions src/Uno.UWP/System/Profile/Internal/UnoDeviceForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ internal enum UnoDeviceForm
Watch,
VirtualReality,
Desktop,
GameConsole,
}
}

0 comments on commit 8b7b97d

Please sign in to comment.