Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(device): Add getId function #370

Merged
merged 6 commits into from
May 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 36 additions & 14 deletions device/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const logBatteryInfo = async () => {

<docgen-index>

* [`getId()`](#getid)
* [`getInfo()`](#getinfo)
* [`getBatteryInfo()`](#getbatteryinfo)
* [`getLanguageCode()`](#getlanguagecode)
Expand All @@ -42,6 +43,21 @@ const logBatteryInfo = async () => {
<docgen-api>
<!--Update the source file JSDoc comments and rerun docgen to update the docs below-->

### getId()

```typescript
getId() => Promise<DeviceId>
```

Return an unique identifier for the device.

**Returns:** <code>Promise&lt;<a href="#deviceid">DeviceId</a>&gt;</code>

**Since:** 1.0.0

--------------------


### getInfo()

```typescript
Expand Down Expand Up @@ -90,22 +106,28 @@ Get the device's current language locale code.
### Interfaces


#### DeviceId

| Prop | Type | Description | Since |
| ---------- | ------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----- |
| **`uuid`** | <code>string</code> | The UUID of the device as available to the app. This identifier may change on modern mobile platforms that only allow per-app install UUIDs. On web, a random identifier is generated and stored on localStorage for subsequent calls. | 1.0.0 |


#### DeviceInfo

| Prop | Type | Description | Since |
| --------------------- | ----------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------- | ----- |
| **`name`** | <code>string</code> | The name of the device. For example, "John's iPhone". This is only supported on iOS and Android 7.1 or above. | 1.0.0 |
| **`model`** | <code>string</code> | The device model. For example, "iPhone". | 1.0.0 |
| **`platform`** | <code>'ios' \| 'android' \| 'web'</code> | The device platform (lowercase). | 1.0.0 |
| **`uuid`** | <code>string</code> | The UUID of the device as available to the app. This identifier may change on modern mobile platforms that only allow per-app install UUIDs. | 1.0.0 |
| **`operatingSystem`** | <code><a href="#operatingsystem">OperatingSystem</a></code> | The operating system of the device. | 1.0.0 |
| **`osVersion`** | <code>string</code> | The version of the device OS. | 1.0.0 |
| **`manufacturer`** | <code>string</code> | The manufacturer of the device. | 1.0.0 |
| **`isVirtual`** | <code>boolean</code> | Whether the app is running in a simulator/emulator. | 1.0.0 |
| **`memUsed`** | <code>number</code> | Approximate memory used by the current app, in bytes. Divide by 1048576 to get the number of MBs used. | 1.0.0 |
| **`diskFree`** | <code>number</code> | How much free disk space is available on the the normal data storage. path for the os, in bytes | 1.0.0 |
| **`diskTotal`** | <code>number</code> | The total size of the normal data storage path for the OS, in bytes. | 1.0.0 |
| **`webViewVersion`** | <code>string</code> | The web view browser version | 1.0.0 |
| Prop | Type | Description | Since |
| --------------------- | ----------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------- | ----- |
| **`name`** | <code>string</code> | The name of the device. For example, "John's iPhone". This is only supported on iOS and Android 7.1 or above. | 1.0.0 |
| **`model`** | <code>string</code> | The device model. For example, "iPhone". | 1.0.0 |
| **`platform`** | <code>'ios' \| 'android' \| 'web'</code> | The device platform (lowercase). | 1.0.0 |
| **`operatingSystem`** | <code><a href="#operatingsystem">OperatingSystem</a></code> | The operating system of the device. | 1.0.0 |
| **`osVersion`** | <code>string</code> | The version of the device OS. | 1.0.0 |
| **`manufacturer`** | <code>string</code> | The manufacturer of the device. | 1.0.0 |
| **`isVirtual`** | <code>boolean</code> | Whether the app is running in a simulator/emulator. | 1.0.0 |
| **`memUsed`** | <code>number</code> | Approximate memory used by the current app, in bytes. Divide by 1048576 to get the number of MBs used. | 1.0.0 |
| **`diskFree`** | <code>number</code> | How much free disk space is available on the the normal data storage. path for the os, in bytes | 1.0.0 |
| **`diskTotal`** | <code>number</code> | The total size of the normal data storage path for the OS, in bytes. | 1.0.0 |
| **`webViewVersion`** | <code>string</code> | The web view browser version | 1.0.0 |


#### BatteryInfo
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ public void load() {
implementation = new Device(getContext());
}

@PluginMethod
public void getId(PluginCall call) {
JSObject r = new JSObject();

r.put("uuid", implementation.getUuid());

call.resolve(r);
}

@PluginMethod
public void getInfo(PluginCall call) {
JSObject r = new JSObject();
Expand All @@ -29,7 +38,6 @@ public void getInfo(PluginCall call) {
r.put("osVersion", android.os.Build.VERSION.RELEASE);
r.put("platform", implementation.getPlatform());
r.put("manufacturer", android.os.Build.MANUFACTURER);
r.put("uuid", implementation.getUuid());
r.put("isVirtual", implementation.isVirtual());
r.put("name", implementation.getName());
r.put("webViewVersion", implementation.getWebViewVersion());
Expand Down
1 change: 1 addition & 0 deletions device/ios/Plugin/DevicePlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// Define the plugin using the CAP_PLUGIN Macro, and
// each method the plugin supports using the CAP_PLUGIN_METHOD macro.
CAP_PLUGIN(DevicePlugin, "Device",
CAP_PLUGIN_METHOD(getId, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(getInfo, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(getBatteryInfo, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(getLanguageCode, CAPPluginReturnPromise);
Expand Down
10 changes: 9 additions & 1 deletion device/ios/Plugin/DevicePlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@ import Capacitor
public class DevicePlugin: CAPPlugin {
private let implementation = Device()

@objc func getId(_ call: CAPPluginCall) {
if let uuid = UIDevice.current.identifierForVendor {
call.resolve([
"uuid": uuid.uuidString
])
} else {
call.reject("Id not available")
}
}
@objc func getInfo(_ call: CAPPluginCall) {
var isSimulator = false
#if arch(i386) || arch(x86_64)
Expand All @@ -25,7 +34,6 @@ public class DevicePlugin: CAPPlugin {
"osVersion": UIDevice.current.systemVersion,
"platform": "ios",
"manufacturer": "Apple",
"uuid": UIDevice.current.identifierForVendor!.uuidString,
"isVirtual": isSimulator,
"webViewVersion": UIDevice.current.systemVersion
])
Expand Down
27 changes: 19 additions & 8 deletions device/src/definitions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
export type OperatingSystem = 'ios' | 'android' | 'windows' | 'mac' | 'unknown';

export interface DeviceId {
/**
* The UUID of the device as available to the app. This identifier may change
* on modern mobile platforms that only allow per-app install UUIDs.
*
* On web, a random identifier is generated and stored on localStorage for subsequent calls.
*
* @since 1.0.0
*/
uuid: string;
}

export interface DeviceInfo {
/**
* The name of the device. For example, "John's iPhone".
Expand All @@ -24,14 +36,6 @@ export interface DeviceInfo {
*/
platform: 'ios' | 'android' | 'web';

/**
* The UUID of the device as available to the app. This identifier may change
* on modern mobile platforms that only allow per-app install UUIDs.
*
* @since 1.0.0
*/
uuid: string;

/**
* The operating system of the device.
*
Expand Down Expand Up @@ -117,6 +121,13 @@ export interface GetLanguageCodeResult {
}

export interface DevicePlugin {
/**
* Return an unique identifier for the device.
*
* @since 1.0.0
*/
getId(): Promise<DeviceId>;

/**
* Return information about the underlying device/os/platform.
*
Expand Down
8 changes: 7 additions & 1 deletion device/src/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { WebPlugin } from '@capacitor/core';

import type {
BatteryInfo,
DeviceId,
DeviceInfo,
DevicePlugin,
GetLanguageCodeResult,
Expand All @@ -21,6 +22,12 @@ declare global {
}

export class DeviceWeb extends WebPlugin implements DevicePlugin {
async getId(): Promise<DeviceId> {
return {
uuid: this.getUid(),
};
}

async getInfo(): Promise<DeviceInfo> {
if (typeof navigator === 'undefined' || !navigator.userAgent) {
throw this.unavailable('Device API not available in this browser');
Expand All @@ -36,7 +43,6 @@ export class DeviceWeb extends WebPlugin implements DevicePlugin {
osVersion: uaFields.osVersion,
manufacturer: navigator.vendor,
isVirtual: false,
uuid: this.getUid(),
webViewVersion: uaFields.browserVersion,
};
}
Expand Down