Skip to content

Commit

Permalink
feat(device): Add operatingSystem field (#2086)
Browse files Browse the repository at this point in the history
Southgarden116 authored and jcesarmobile committed Dec 10, 2019
1 parent fd28a2c commit dcdf675
Showing 7 changed files with 33 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -28,6 +28,7 @@ public void getInfo(PluginCall call) {
r.put("diskFree", getDiskFree());
r.put("diskTotal", getDiskTotal());
r.put("model", android.os.Build.MODEL);
r.put("operatingSystem", "android");
r.put("osVersion", android.os.Build.VERSION.RELEASE);
r.put("appVersion", getAppVersion());
r.put("appBuild", getAppBuild());
6 changes: 6 additions & 0 deletions core/src/core-plugin-definitions.ts
Original file line number Diff line number Diff line change
@@ -401,6 +401,8 @@ export interface DevicePlugin extends Plugin {
getLanguageCode(): Promise<DeviceLanguageCodeResult>;
}

export type OperatingSystem = 'ios' | 'android' | 'windows' | 'mac' | 'unknown';

export interface DeviceInfo {
/**
* The device model. For example, "iPhone"
@@ -423,6 +425,10 @@ export interface DeviceInfo {
* The current bundle build of the app
*/
appBuild: string;
/**
* The operating system of the device
*/
operatingSystem: OperatingSystem;
/**
* The version of the device OS
*/
13 changes: 13 additions & 0 deletions core/src/web/device.ts
Original file line number Diff line number Diff line change
@@ -34,6 +34,7 @@ export class DevicePluginWeb extends WebPlugin implements DevicePlugin {
platform: <'web'> 'web',
appVersion: '',
appBuild: '',
operatingSystem: uaFields.operatingSystem,
osVersion: uaFields.osVersion,
manufacturer: navigator.vendor,
isVirtual: false,
@@ -74,6 +75,18 @@ export class DevicePluginWeb extends WebPlugin implements DevicePlugin {
}
}

if (/android/i.test(_ua)) {
uaFields.operatingSystem = 'android';
} else if (/iPad|iPhone|iPod/.test(_ua) && !window.MSStream) {
uaFields.operatingSystem = 'ios';
} else if (/Win/.test(_ua)) {
uaFields.operatingSystem = 'windows';
} else if (/Mac/i.test(_ua)) {
uaFields.operatingSystem = 'mac';
} else {
uaFields.operatingSystem = 'unknown';
}

return uaFields;
}

3 changes: 2 additions & 1 deletion electron/src/electron/device.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DeviceInfo, DeviceLanguageCodeResult, DevicePlugin, DevicePluginWeb, WebPlugin } from "@capacitor/core";
import { DeviceInfo, DeviceLanguageCodeResult, DevicePlugin, DevicePluginWeb, WebPlugin } from "@capacitor/core";

declare var navigator: any;
const webDevice = new DevicePluginWeb();
@@ -19,6 +19,7 @@ export class DevicePluginElectron extends WebPlugin implements DevicePlugin {
platform: <'electron'> 'electron',
appVersion: '',
appBuild: '',
operatingSystem: info.operatingSystem,
osVersion: info.osVersion,
manufacturer: navigator.vendor,
isVirtual: false,
1 change: 1 addition & 0 deletions ios/Capacitor/Capacitor/Plugins/Device.swift
Original file line number Diff line number Diff line change
@@ -23,6 +23,7 @@ public class CAPDevicePlugin: CAPPlugin {
"diskFree": diskFree,
"diskTotal": diskTotal,
"model": UIDevice.current.model,
"operatingSystem": "ios",
"osVersion": UIDevice.current.systemVersion,
"appVersion": Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String ?? "",
"appBuild": Bundle.main.infoDictionary?["CFBundleVersion"] as? String ?? "",
1 change: 1 addition & 0 deletions site/docs-md/apis/device/index.md
Original file line number Diff line number Diff line change
@@ -31,6 +31,7 @@ console.log(info);
"diskFree": 12228108288,
"appVersion": "1.0.2",
"appBuild": "123",
"operatingSystem": "ios",
"osVersion": "11.2",
"platform": "ios",
"memUsed": 93851648,
10 changes: 9 additions & 1 deletion site/src/assets/docs-content/apis/device/api.html
Original file line number Diff line number Diff line change
@@ -142,6 +142,14 @@ <h4 class="avc-code-interface-name">DeviceInfo</h4>
</div>
</div>

<div class="avc-code-interface-param">
<div class="avc-code-param-comment">// The operating system of the device</div>
<div class="avc-code-line"><span class="avc-code-param-name">operatingSystem</span>
:
<avc-code-type>'ios' | 'android' | 'windows' | 'mac' | 'unknown'</avc-code-type>;
</div>
</div>

<div class="avc-code-interface-param">
<div class="avc-code-param-comment">// The version of the device OS</div>
<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>
<div class="avc-code-param-comment">// The device platform (lowercase).</div>
<div class="avc-code-line"><span class="avc-code-param-name">platform</span>
:
<avc-code-type>any</avc-code-type>;
<avc-code-type>'ios' | 'android' | 'electron' | 'web'</avc-code-type>;
</div>
</div>

0 comments on commit dcdf675

Please sign in to comment.