This repository has been archived by the owner on Apr 3, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 43
/
DeviceInfo.uwp.cs
224 lines (194 loc) · 6.77 KB
/
DeviceInfo.uwp.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
using Plugin.DeviceInfo.Abstractions;
using System;
using Windows.System.Profile;
using Windows.Security.ExchangeActiveSyncProvisioning;
using Windows.Foundation.Metadata;
using Windows.ApplicationModel.Resources.Core;
using Windows.UI.ViewManagement;
using Windows.ApplicationModel;
namespace Plugin.DeviceInfo
{
/// <summary>
/// Implementation for DeviceInfo
/// </summary>
public class DeviceInfoImplementation : IDeviceInfo
{
/// <summary>
/// Get the name of the device
/// </summary>
public string Manufacturer => deviceInfo.SystemManufacturer;
/// <summary>
/// Get the name of the device
/// </summary>
public string DeviceName => deviceInfo.FriendlyName;
EasClientDeviceInformation deviceInfo;
public DeviceInfoImplementation()
{
deviceInfo = new EasClientDeviceInformation();
}
/// <inheritdoc/>
public string GenerateAppId(bool usingPhoneId = false, string prefix = null, string suffix = null)
{
var appId = "";
if (!string.IsNullOrEmpty(prefix))
appId += prefix;
appId += Guid.NewGuid().ToString();
if (usingPhoneId)
appId += Id;
if (!string.IsNullOrEmpty(suffix))
appId += suffix;
return appId;
}
string id = null;
/// <inheritdoc/>
public string Id
{
get
{
if (id != null)
return id;
try
{
if (ApiInformation.IsTypePresent("Windows.System.Profile.SystemIdentification"))
{
var systemId = SystemIdentification.GetSystemIdForPublisher();
// Make sure this device can generate the IDs
if (systemId.Source != SystemIdentificationSource.None)
{
// The Id property has a buffer with the unique ID
var hardwareId = systemId.Id;
var dataReader = Windows.Storage.Streams.DataReader.FromBuffer(hardwareId);
var bytes = new byte[hardwareId.Length];
dataReader.ReadBytes(bytes);
id = Convert.ToBase64String(bytes);
}
}
else if (ApiInformation.IsTypePresent("Windows.System.Profile.HardwareIdentification"))
{
var token = HardwareIdentification.GetPackageSpecificToken(null);
var hardwareId = token.Id;
var dataReader = Windows.Storage.Streams.DataReader.FromBuffer(hardwareId);
var bytes = new byte[hardwareId.Length];
dataReader.ReadBytes(bytes);
id = Convert.ToBase64String(bytes);
}
else
{
id = "unsupported";
}
}
catch (Exception)
{
}
return id;
}
}
/// <inheritdoc/>
public string Model => deviceInfo.SystemProductName;
/// <inheritdoc/>
public string Version
{
get
{
var sv = AnalyticsInfo.VersionInfo.DeviceFamilyVersion;
try
{
var v = ulong.Parse(sv);
var v1 = (v & 0xFFFF000000000000L) >> 48;
var v2 = (v & 0x0000FFFF00000000L) >> 32;
var v3 = (v & 0x00000000FFFF0000L) >> 16;
var v4 = v & 0x000000000000FFFFL;
return $"{v1}.{v2}.{v3}.{v4}";
}
catch { }
return sv;
}
}
/// <inheritdoc/>
public Abstractions.Platform Platform
{
get
{
switch (AnalyticsInfo.VersionInfo.DeviceFamily)
{
case "Windows.Mobile":
return Abstractions.Platform.WindowsPhone;
case "Windows.Desktop":
try
{
return UIViewSettings.GetForCurrentView().UserInteractionMode == UserInteractionMode.Mouse
? Abstractions.Platform.Windows
: Abstractions.Platform.WindowsTablet;
}
catch
{
return Abstractions.Platform.Windows;
}
case "Windows.IoT":
return Abstractions.Platform.IoT;
case "Windows.Xbox":
return Abstractions.Platform.Xbox;
case "Windows.Team":
return Abstractions.Platform.SurfaceHub;
default:
return Abstractions.Platform.Windows;
}
}
}
/// <inheritdoc/>
public Version VersionNumber
{
get
{
try
{
return new Version(Version);
}
catch
{
return new Version(10, 0);
}
}
}
/// <summary>
/// Returns the current version of the app, as defined in the PList, e.g. "4.3".
/// </summary>
/// <value>The current version.</value>
public string AppVersion
{
get
{
var version = Package.Current.Id.Version;
return string.Format("{0}.{1}.{2}.{3}", version.Major, version.Minor, version.Build, version.Revision);
}
}
/// <summary>
/// Returns the current build of the app, as defined in the PList, e.g. "4300".
/// </summary>
/// <value>The current build.</value>
public string AppBuild => Package.Current.Id.Version.Build.ToString();
public Idiom Idiom
{
get
{
switch (Platform)
{
case Abstractions.Platform.Windows:
return Idiom.Desktop;
case Abstractions.Platform.WindowsPhone:
return Idiom.Phone;
case Abstractions.Platform.WindowsTablet:
return Idiom.Tablet;
default:
return Idiom.Unknown;
}
}
}
/// <summary>
/// Checks whether this is a real device or an emulator/simulator
///
/// Source: http://igrali.com/2014/07/17/get-device-information-windows-phone-8-1-winrt/
/// </summary>
public bool IsDevice => deviceInfo.SystemProductName != "Virtual";
}
}