Skip to content

Commit dcdf675

Browse files
Southgarden116jcesarmobile
authored andcommitted
feat(device): Add operatingSystem field (#2086)
1 parent fd28a2c commit dcdf675

File tree

7 files changed

+33
-2
lines changed

7 files changed

+33
-2
lines changed

android/capacitor/src/main/java/com/getcapacitor/plugin/Device.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public void getInfo(PluginCall call) {
2828
r.put("diskFree", getDiskFree());
2929
r.put("diskTotal", getDiskTotal());
3030
r.put("model", android.os.Build.MODEL);
31+
r.put("operatingSystem", "android");
3132
r.put("osVersion", android.os.Build.VERSION.RELEASE);
3233
r.put("appVersion", getAppVersion());
3334
r.put("appBuild", getAppBuild());

core/src/core-plugin-definitions.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,8 @@ export interface DevicePlugin extends Plugin {
401401
getLanguageCode(): Promise<DeviceLanguageCodeResult>;
402402
}
403403

404+
export type OperatingSystem = 'ios' | 'android' | 'windows' | 'mac' | 'unknown';
405+
404406
export interface DeviceInfo {
405407
/**
406408
* The device model. For example, "iPhone"
@@ -423,6 +425,10 @@ export interface DeviceInfo {
423425
* The current bundle build of the app
424426
*/
425427
appBuild: string;
428+
/**
429+
* The operating system of the device
430+
*/
431+
operatingSystem: OperatingSystem;
426432
/**
427433
* The version of the device OS
428434
*/

core/src/web/device.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export class DevicePluginWeb extends WebPlugin implements DevicePlugin {
3434
platform: <'web'> 'web',
3535
appVersion: '',
3636
appBuild: '',
37+
operatingSystem: uaFields.operatingSystem,
3738
osVersion: uaFields.osVersion,
3839
manufacturer: navigator.vendor,
3940
isVirtual: false,
@@ -74,6 +75,18 @@ export class DevicePluginWeb extends WebPlugin implements DevicePlugin {
7475
}
7576
}
7677

78+
if (/android/i.test(_ua)) {
79+
uaFields.operatingSystem = 'android';
80+
} else if (/iPad|iPhone|iPod/.test(_ua) && !window.MSStream) {
81+
uaFields.operatingSystem = 'ios';
82+
} else if (/Win/.test(_ua)) {
83+
uaFields.operatingSystem = 'windows';
84+
} else if (/Mac/i.test(_ua)) {
85+
uaFields.operatingSystem = 'mac';
86+
} else {
87+
uaFields.operatingSystem = 'unknown';
88+
}
89+
7790
return uaFields;
7891
}
7992

electron/src/electron/device.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { DeviceInfo, DeviceLanguageCodeResult, DevicePlugin, DevicePluginWeb, WebPlugin } from "@capacitor/core";
1+
import { DeviceInfo, DeviceLanguageCodeResult, DevicePlugin, DevicePluginWeb, WebPlugin } from "@capacitor/core";
22

33
declare var navigator: any;
44
const webDevice = new DevicePluginWeb();
@@ -19,6 +19,7 @@ export class DevicePluginElectron extends WebPlugin implements DevicePlugin {
1919
platform: <'electron'> 'electron',
2020
appVersion: '',
2121
appBuild: '',
22+
operatingSystem: info.operatingSystem,
2223
osVersion: info.osVersion,
2324
manufacturer: navigator.vendor,
2425
isVirtual: false,

ios/Capacitor/Capacitor/Plugins/Device.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public class CAPDevicePlugin: CAPPlugin {
2323
"diskFree": diskFree,
2424
"diskTotal": diskTotal,
2525
"model": UIDevice.current.model,
26+
"operatingSystem": "ios",
2627
"osVersion": UIDevice.current.systemVersion,
2728
"appVersion": Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String ?? "",
2829
"appBuild": Bundle.main.infoDictionary?["CFBundleVersion"] as? String ?? "",

site/docs-md/apis/device/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ console.log(info);
3131
"diskFree": 12228108288,
3232
"appVersion": "1.0.2",
3333
"appBuild": "123",
34+
"operatingSystem": "ios",
3435
"osVersion": "11.2",
3536
"platform": "ios",
3637
"memUsed": 93851648,

site/src/assets/docs-content/apis/device/api.html

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,14 @@ <h4 class="avc-code-interface-name">DeviceInfo</h4>
142142
</div>
143143
</div>
144144

145+
<div class="avc-code-interface-param">
146+
<div class="avc-code-param-comment">// The operating system of the device</div>
147+
<div class="avc-code-line"><span class="avc-code-param-name">operatingSystem</span>
148+
:
149+
<avc-code-type>'ios' | 'android' | 'windows' | 'mac' | 'unknown'</avc-code-type>;
150+
</div>
151+
</div>
152+
145153
<div class="avc-code-interface-param">
146154
<div class="avc-code-param-comment">// The version of the device OS</div>
147155
<div class="avc-code-line"><span class="avc-code-param-name">osVersion</span>
@@ -154,7 +162,7 @@ <h4 class="avc-code-interface-name">DeviceInfo</h4>
154162
<div class="avc-code-param-comment">// The device platform (lowercase).</div>
155163
<div class="avc-code-line"><span class="avc-code-param-name">platform</span>
156164
:
157-
<avc-code-type>any</avc-code-type>;
165+
<avc-code-type>'ios' | 'android' | 'electron' | 'web'</avc-code-type>;
158166
</div>
159167
</div>
160168

0 commit comments

Comments
 (0)