@kafitra/lynx-device-info 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,142 @@
1
+ # @kafitra/lynx-device-info
2
+
3
+ Lynx Native Module for accessing Android device information. Provides a Promise-based API for retrieving device brand, model, and SDK version.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ # npm
9
+ npm install @kafitra/lynx-device-info
10
+
11
+ # pnpm
12
+ pnpm add @kafitra/lynx-device-info
13
+
14
+ # In a monorepo workspace
15
+ pnpm add @kafitra/lynx-device-info@workspace:*
16
+ ```
17
+
18
+ ## Android Setup
19
+
20
+ ### 1. Register the Native Module
21
+
22
+ In your Android host app's initialization code (e.g., `Application.onCreate()` or your Lynx setup), register the module:
23
+
24
+ ```java
25
+ import com.kafitra.lynxdeviceinfo.LynxDeviceInfoModule;
26
+
27
+ // Register the native module with Lynx runtime
28
+ LynxEnv.inst().registerModule("LynxDeviceInfo", LynxDeviceInfoModule.class);
29
+ ```
30
+
31
+ ### 2. Add the module dependency
32
+
33
+ In your Android host app's `build.gradle`:
34
+
35
+ ```gradle
36
+ dependencies {
37
+ implementation project(':lynx-device-info')
38
+ }
39
+ ```
40
+
41
+ Or if using the published package, include the `android/` directory in your build.
42
+
43
+ ## Usage
44
+
45
+ ### Basic Usage
46
+
47
+ ```typescript
48
+ import { DeviceInfo } from "@kafitra/lynx-device-info";
49
+
50
+ // All methods return Promises
51
+ const brand = await DeviceInfo.getBrand(); // "Samsung"
52
+ const model = await DeviceInfo.getModel(); // "Galaxy S24"
53
+ const sdkVersion = await DeviceInfo.getSDKVersion(); // 34
54
+ ```
55
+
56
+ ### Usage with React (Lynx)
57
+
58
+ ```tsx
59
+ import { useState, useEffect } from "@lynx-js/react";
60
+ import { DeviceInfo } from "@kafitra/lynx-device-info";
61
+
62
+ function App() {
63
+ const [brand, setBrand] = useState("Loading...");
64
+ const [model, setModel] = useState("Loading...");
65
+ const [sdk, setSdk] = useState<number | null>(null);
66
+ const [error, setError] = useState(false);
67
+
68
+ useEffect(() => {
69
+ async function loadDeviceInfo() {
70
+ try {
71
+ const b = await DeviceInfo.getBrand();
72
+ const m = await DeviceInfo.getModel();
73
+ const s = await DeviceInfo.getSDKVersion();
74
+ setBrand(b);
75
+ setModel(m);
76
+ setSdk(s);
77
+ } catch (e) {
78
+ setError(true);
79
+ }
80
+ }
81
+ loadDeviceInfo();
82
+ }, []);
83
+
84
+ if (error) {
85
+ return <text>⚠ Native module not registered</text>;
86
+ }
87
+
88
+ return (
89
+ <view>
90
+ <text>Brand: {brand}</text>
91
+ <text>Model: {model}</text>
92
+ <text>SDK: {sdk}</text>
93
+ </view>
94
+ );
95
+ }
96
+ ```
97
+
98
+ ## API Reference
99
+
100
+ | Method | Return Type | Description |
101
+ | ----------------- | ----------------- | --------------------------------- |
102
+ | `getBrand()` | `Promise<string>` | Device brand (e.g., "Samsung") |
103
+ | `getModel()` | `Promise<string>` | Device model (e.g., "Galaxy S24") |
104
+ | `getSDKVersion()` | `Promise<number>` | Android SDK version (e.g., 34) |
105
+
106
+ ## Error Handling
107
+
108
+ The library provides two levels of error handling:
109
+
110
+ 1. **Module not registered**: If `LynxDeviceInfoModule` is not registered in the Android host, importing the module will throw:
111
+
112
+ ```
113
+ [@kafitra/lynx-device-info] Native module not linked.
114
+ Please register LynxDeviceInfoModule in Android host.
115
+ ```
116
+
117
+ 2. **Method failure**: If any individual method call fails, the Promise rejects with a descriptive error:
118
+ ```
119
+ [lynx-device-info] Failed to get brand: <original error>
120
+ ```
121
+
122
+ ## Troubleshooting
123
+
124
+ ### "Native module not linked" error
125
+
126
+ **Cause**: `LynxDeviceInfoModule` was not registered in your Android host app.
127
+
128
+ **Fix**: Add the registration call in your app's initialization:
129
+
130
+ ```java
131
+ LynxEnv.inst().registerModule("LynxDeviceInfo", LynxDeviceInfoModule.class);
132
+ ```
133
+
134
+ ### Methods return "unknown"
135
+
136
+ **Cause**: The Android `Build` class returned null for the requested property. This can happen on emulators or non-standard devices.
137
+
138
+ **Fix**: This is expected behavior — the module gracefully falls back to `"unknown"` instead of crashing.
139
+
140
+ ## License
141
+
142
+ MIT
@@ -0,0 +1,21 @@
1
+ apply plugin: 'com.android.library'
2
+
3
+ android {
4
+ namespace 'com.kafitra.lynxdeviceinfo'
5
+ compileSdkVersion 34
6
+
7
+ defaultConfig {
8
+ minSdkVersion 21
9
+ targetSdkVersion 34
10
+ }
11
+
12
+ compileOptions {
13
+ sourceCompatibility JavaVersion.VERSION_1_8
14
+ targetCompatibility JavaVersion.VERSION_1_8
15
+ }
16
+ }
17
+
18
+ dependencies {
19
+ // Lynx SDK should be provided by the host app
20
+ compileOnly 'com.lynx:lynx-sdk:+'
21
+ }
@@ -0,0 +1,69 @@
1
+ package com.kafitra.lynxdeviceinfo;
2
+
3
+ import android.content.Context;
4
+ import android.os.Build;
5
+
6
+ import com.lynx.jsbridge.LynxModule;
7
+ import com.lynx.jsbridge.LynxMethod;
8
+
9
+ /**
10
+ * LynxDeviceInfoModule — Lynx Native Module for Android device information.
11
+ *
12
+ * Provides synchronous access to device brand, model, and SDK version
13
+ * through the Lynx Native Module system.
14
+ *
15
+ * <h3>Registration:</h3>
16
+ * <pre>
17
+ * LynxEnv.inst().registerModule("LynxDeviceInfo", LynxDeviceInfoModule.class);
18
+ * </pre>
19
+ */
20
+ public class LynxDeviceInfoModule extends LynxModule {
21
+
22
+ public LynxDeviceInfoModule(Context context) {
23
+ super(context);
24
+ }
25
+
26
+ /**
27
+ * Returns the device brand (manufacturer brand name).
28
+ * Falls back to "unknown" if the value is null.
29
+ *
30
+ * @return Device brand string, e.g., "Samsung", "Google", "Xiaomi"
31
+ */
32
+ @LynxMethod
33
+ public String getBrand() {
34
+ try {
35
+ return Build.BRAND != null ? Build.BRAND : "unknown";
36
+ } catch (Exception e) {
37
+ return "unknown";
38
+ }
39
+ }
40
+
41
+ /**
42
+ * Returns the device model name.
43
+ * Falls back to "unknown" if the value is null.
44
+ *
45
+ * @return Device model string, e.g., "Galaxy S24", "Pixel 8"
46
+ */
47
+ @LynxMethod
48
+ public String getModel() {
49
+ try {
50
+ return Build.MODEL != null ? Build.MODEL : "unknown";
51
+ } catch (Exception e) {
52
+ return "unknown";
53
+ }
54
+ }
55
+
56
+ /**
57
+ * Returns the Android SDK version number.
58
+ *
59
+ * @return SDK version integer, e.g., 34 for Android 14
60
+ */
61
+ @LynxMethod
62
+ public int getSDKVersion() {
63
+ try {
64
+ return Build.VERSION.SDK_INT;
65
+ } catch (Exception e) {
66
+ return -1;
67
+ }
68
+ }
69
+ }
@@ -0,0 +1,22 @@
1
+ /**
2
+ * @kafitra/lynx-device-info
3
+ *
4
+ * Public API — Promise-based wrappers around the native module.
5
+ */
6
+ import type { DeviceInfoInterface } from "./types";
7
+ /**
8
+ * DeviceInfo provides Promise-based access to Android device information
9
+ * through the Lynx Native Module system.
10
+ *
11
+ * @example
12
+ * ```ts
13
+ * import { DeviceInfo } from '@kafitra/lynx-device-info';
14
+ *
15
+ * const brand = await DeviceInfo.getBrand(); // "Samsung"
16
+ * const model = await DeviceInfo.getModel(); // "Galaxy S24"
17
+ * const sdk = await DeviceInfo.getSDKVersion(); // 34
18
+ * ```
19
+ */
20
+ export declare const DeviceInfo: DeviceInfoInterface;
21
+ export type { DeviceInfoInterface, NativeLynxDeviceInfo } from "./types";
22
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAGH,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,SAAS,CAAC;AAEnD;;;;;;;;;;;;GAYG;AACH,eAAO,MAAM,UAAU,EAAE,mBA8BxB,CAAC;AAGF,YAAY,EAAE,mBAAmB,EAAE,oBAAoB,EAAE,MAAM,SAAS,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,46 @@
1
+ /**
2
+ * @kafitra/lynx-device-info
3
+ *
4
+ * Public API — Promise-based wrappers around the native module.
5
+ */
6
+ import { NativeDeviceInfo } from "./native";
7
+ /**
8
+ * DeviceInfo provides Promise-based access to Android device information
9
+ * through the Lynx Native Module system.
10
+ *
11
+ * @example
12
+ * ```ts
13
+ * import { DeviceInfo } from '@kafitra/lynx-device-info';
14
+ *
15
+ * const brand = await DeviceInfo.getBrand(); // "Samsung"
16
+ * const model = await DeviceInfo.getModel(); // "Galaxy S24"
17
+ * const sdk = await DeviceInfo.getSDKVersion(); // 34
18
+ * ```
19
+ */
20
+ export const DeviceInfo = {
21
+ async getBrand() {
22
+ try {
23
+ return NativeDeviceInfo.getBrand();
24
+ }
25
+ catch (e) {
26
+ throw new Error(`[lynx-device-info] Failed to get brand: ${e instanceof Error ? e.message : String(e)}`);
27
+ }
28
+ },
29
+ async getModel() {
30
+ try {
31
+ return NativeDeviceInfo.getModel();
32
+ }
33
+ catch (e) {
34
+ throw new Error(`[lynx-device-info] Failed to get model: ${e instanceof Error ? e.message : String(e)}`);
35
+ }
36
+ },
37
+ async getSDKVersion() {
38
+ try {
39
+ return NativeDeviceInfo.getSDKVersion();
40
+ }
41
+ catch (e) {
42
+ throw new Error(`[lynx-device-info] Failed to get SDK version: ${e instanceof Error ? e.message : String(e)}`);
43
+ }
44
+ },
45
+ };
46
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,gBAAgB,EAAE,MAAM,UAAU,CAAC;AAG5C;;;;;;;;;;;;GAYG;AACH,MAAM,CAAC,MAAM,UAAU,GAAwB;IAC7C,KAAK,CAAC,QAAQ;QACZ,IAAI,CAAC;YACH,OAAO,gBAAgB,CAAC,QAAQ,EAAE,CAAC;QACrC,CAAC;QAAC,OAAO,CAAC,EAAE,CAAC;YACX,MAAM,IAAI,KAAK,CACb,2CAA2C,CAAC,YAAY,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,CACxF,CAAC;QACJ,CAAC;IACH,CAAC;IAED,KAAK,CAAC,QAAQ;QACZ,IAAI,CAAC;YACH,OAAO,gBAAgB,CAAC,QAAQ,EAAE,CAAC;QACrC,CAAC;QAAC,OAAO,CAAC,EAAE,CAAC;YACX,MAAM,IAAI,KAAK,CACb,2CAA2C,CAAC,YAAY,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,CACxF,CAAC;QACJ,CAAC;IACH,CAAC;IAED,KAAK,CAAC,aAAa;QACjB,IAAI,CAAC;YACH,OAAO,gBAAgB,CAAC,aAAa,EAAE,CAAC;QAC1C,CAAC;QAAC,OAAO,CAAC,EAAE,CAAC;YACX,MAAM,IAAI,KAAK,CACb,iDAAiD,CAAC,YAAY,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,CAC9F,CAAC;QACJ,CAAC;IACH,CAAC;CACF,CAAC"}
@@ -0,0 +1,12 @@
1
+ /**
2
+ * @kafitra/lynx-device-info
3
+ *
4
+ * Native module declaration and access layer.
5
+ * Accesses the LynxDeviceInfo module from the Lynx runtime's NativeModules global.
6
+ */
7
+ import type { NativeLynxDeviceInfo } from "./types";
8
+ /**
9
+ * The validated native module instance.
10
+ */
11
+ export declare const NativeDeviceInfo: NativeLynxDeviceInfo;
12
+ //# sourceMappingURL=native.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"native.d.ts","sourceRoot":"","sources":["../src/native.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,SAAS,CAAC;AAwBpD;;GAEG;AACH,eAAO,MAAM,gBAAgB,EAAE,oBAAwC,CAAC"}
package/dist/native.js ADDED
@@ -0,0 +1,23 @@
1
+ /**
2
+ * @kafitra/lynx-device-info
3
+ *
4
+ * Native module declaration and access layer.
5
+ * Accesses the LynxDeviceInfo module from the Lynx runtime's NativeModules global.
6
+ */
7
+ /**
8
+ * Retrieve and validate the LynxDeviceInfo native module.
9
+ * Throws a descriptive error if the module is not registered.
10
+ */
11
+ function getNativeModule() {
12
+ if (!NativeModules?.LynxDeviceInfo) {
13
+ throw new Error("[@kafitra/lynx-device-info] Native module not linked. " +
14
+ "Please register LynxDeviceInfoModule in your Android host:\n\n" +
15
+ ' LynxEnv.inst().registerModule("LynxDeviceInfo", LynxDeviceInfoModule.class);');
16
+ }
17
+ return NativeModules.LynxDeviceInfo;
18
+ }
19
+ /**
20
+ * The validated native module instance.
21
+ */
22
+ export const NativeDeviceInfo = getNativeModule();
23
+ //# sourceMappingURL=native.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"native.js","sourceRoot":"","sources":["../src/native.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAWH;;;GAGG;AACH,SAAS,eAAe;IACtB,IAAI,CAAC,aAAa,EAAE,cAAc,EAAE,CAAC;QACnC,MAAM,IAAI,KAAK,CACb,wDAAwD;YACtD,gEAAgE;YAChE,gFAAgF,CACnF,CAAC;IACJ,CAAC;IACD,OAAO,aAAa,CAAC,cAAc,CAAC;AACtC,CAAC;AAED;;GAEG;AACH,MAAM,CAAC,MAAM,gBAAgB,GAAyB,eAAe,EAAE,CAAC"}
@@ -0,0 +1,26 @@
1
+ /**
2
+ * @kafitra/lynx-device-info
3
+ *
4
+ * Type definitions for the DeviceInfo native module.
5
+ */
6
+ /**
7
+ * Interface for the Lynx DeviceInfo native module methods.
8
+ */
9
+ export interface DeviceInfoInterface {
10
+ /** Returns the device brand (e.g., "Samsung", "Google") */
11
+ getBrand(): Promise<string>;
12
+ /** Returns the device model (e.g., "Galaxy S24", "Pixel 8") */
13
+ getModel(): Promise<string>;
14
+ /** Returns the Android SDK version number (e.g., 34) */
15
+ getSDKVersion(): Promise<number>;
16
+ }
17
+ /**
18
+ * Raw native module shape as exposed by Lynx runtime.
19
+ * These methods are synchronous at the native level.
20
+ */
21
+ export interface NativeLynxDeviceInfo {
22
+ getBrand(): string;
23
+ getModel(): string;
24
+ getSDKVersion(): number;
25
+ }
26
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH;;GAEG;AACH,MAAM,WAAW,mBAAmB;IAClC,2DAA2D;IAC3D,QAAQ,IAAI,OAAO,CAAC,MAAM,CAAC,CAAC;IAE5B,+DAA+D;IAC/D,QAAQ,IAAI,OAAO,CAAC,MAAM,CAAC,CAAC;IAE5B,wDAAwD;IACxD,aAAa,IAAI,OAAO,CAAC,MAAM,CAAC,CAAC;CAClC;AAED;;;GAGG;AACH,MAAM,WAAW,oBAAoB;IACnC,QAAQ,IAAI,MAAM,CAAC;IACnB,QAAQ,IAAI,MAAM,CAAC;IACnB,aAAa,IAAI,MAAM,CAAC;CACzB"}
package/dist/types.js ADDED
@@ -0,0 +1,7 @@
1
+ /**
2
+ * @kafitra/lynx-device-info
3
+ *
4
+ * Type definitions for the DeviceInfo native module.
5
+ */
6
+ export {};
7
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;;GAIG"}
package/package.json ADDED
@@ -0,0 +1,35 @@
1
+ {
2
+ "name": "@kafitra/lynx-device-info",
3
+ "version": "0.1.0",
4
+ "description": "Lynx Native Module for accessing Android device information",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "files": [
8
+ "dist",
9
+ "android"
10
+ ],
11
+ "peerDependencies": {
12
+ "lynx": "*"
13
+ },
14
+ "devDependencies": {
15
+ "typescript": "^5.3.0",
16
+ "rimraf": "^5.0.0"
17
+ },
18
+ "keywords": [
19
+ "lynx",
20
+ "native-module",
21
+ "device-info",
22
+ "android"
23
+ ],
24
+ "author": "Kafitra",
25
+ "license": "MIT",
26
+ "repository": {
27
+ "type": "git",
28
+ "url": "https://github.com/kafitra/lynx-native.git",
29
+ "directory": "packages/lynx-device-info"
30
+ },
31
+ "scripts": {
32
+ "build": "tsc",
33
+ "clean": "rimraf dist"
34
+ }
35
+ }