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(react-native): add getDeviceName util #13015

Merged
merged 7 commits into from Apr 30, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -1,29 +1,43 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

package com.amazonaws.amplify.rtncore

import android.os.Build
import com.facebook.react.bridge.Promise
import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.bridge.ReactContextBaseJavaModule
import com.facebook.react.bridge.ReactMethod
import com.facebook.react.bridge.Promise
import com.facebook.react.bridge.ReadableMap

class AmplifyRTNCoreModule(reactContext: ReactApplicationContext) :
ReactContextBaseJavaModule(reactContext) {

override fun getName(): String {
return NAME
}
ReactContextBaseJavaModule(reactContext) {
override fun getName(): String {
return NAME
}

@ReactMethod
fun computeModPow(payload: ReadableMap, promise: Promise) {
fun computeModPow(
payload: ReadableMap,
promise: Promise,
) {
BigInteger.computeModPow(payload, promise)
}

@ReactMethod
fun computeS(payload: ReadableMap, promise: Promise) {
fun computeS(
payload: ReadableMap,
promise: Promise,
) {
BigInteger.computeS(payload, promise)
}

companion object {
const val NAME = "AmplifyRTNCore"
}
@ReactMethod
fun getDeviceName(promise: Promise) {
promise.resolve(Build.MODEL)
}

companion object {
const val NAME = "AmplifyRTNCore"
}
}
3 changes: 3 additions & 0 deletions packages/react-native/ios/AmplifyRTNCore.mm
Expand Up @@ -13,6 +13,9 @@ @interface RCT_EXTERN_MODULE(AmplifyRTNCore, NSObject)
withResolver:(RCTPromiseResolveBlock)resolve
withRejecter:(RCTPromiseRejectBlock)reject)

RCT_EXTERN_METHOD(getDeviceName:(RCTPromiseResolveBlock)resolve
withResolver:(RCTPromiseRejectBlock)reject)

+ (BOOL)requiresMainQueueSetup
{
return NO;
Expand Down
62 changes: 39 additions & 23 deletions packages/react-native/ios/AmplifyRTNCore.swift
@@ -1,29 +1,45 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

#if canImport(UIKit)
import UIKit
#else
import Foundation
#endif

@objc(AmplifyRTNCore)
class AmplifyRTNCore: NSObject {

@objc(multiply:withB:withResolver:withRejecter:)
func multiply(a: Float, b: Float, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
resolve(a*b)
}

@objc(computeModPow:withResolver:withRejecter:)
func computeModPow(
payload: [String: String],
resolve: RCTPromiseResolveBlock,
reject: RCTPromiseRejectBlock
) -> Void {
BigInteger.computeModPow(payload, resolve: resolve, reject: reject)
}

@objc(computeS:withResolver:withRejecter:)
func computeS(
_ payload: [String: String],
resolve: RCTPromiseResolveBlock,
reject: RCTPromiseRejectBlock
) -> Void {
BigInteger.computeS(payload, resolve: resolve, reject: reject)
}
@objc(multiply:withB:withResolver:withRejecter:)
func multiply(a: Float, b: Float, resolve: RCTPromiseResolveBlock, reject _: RCTPromiseRejectBlock) {
resolve(a * b)
}

@objc(computeModPow:withResolver:withRejecter:)
func computeModPow(
payload: [String: String],
resolve: RCTPromiseResolveBlock,
reject: RCTPromiseRejectBlock
) {
BigInteger.computeModPow(payload, resolve: resolve, reject: reject)
}

@objc(computeS:withResolver:withRejecter:)
func computeS(
_ payload: [String: String],
resolve: RCTPromiseResolveBlock,
reject: RCTPromiseRejectBlock
) {
BigInteger.computeS(payload, resolve: resolve, reject: reject)
}

@objc(getDeviceName:withResolver:)
func getDeviceName(resolve: @escaping RCTPromiseResolveBlock, reject _: @escaping RCTPromiseRejectBlock) {
var deviceName: String
#if canImport(UIKit)
deviceName = UIDevice.current.name
Copy link
Contributor

@HuiSF HuiSF Apr 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use of the name getter of the UIDevice class requires the host app to be assigned with a corresponding entitlement (when running on iOS 16.0+) otherwise it returns the generic string "iPhone", are we going to instruct developers to do so in order to use this API?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we should instruct developers to add a corresponding entitlement to use the API since it defaults to a generic device name, example "iPhone"

We could add a callout saying by default

  • iOS 15 and earlier: name configured on device "Bob's iPhone X" (iPhone settings>General>About>Name)
  • iOS 16 and later: a generic device name "iPhone"/"iPad". Optionally an entitlement is required to get actual device name

#else
deviceName = ProcessInfo.processInfo.hostName
#endif
resolve(deviceName)
}
}
8 changes: 8 additions & 0 deletions packages/react-native/src/apis/getDeviceName.ts
@@ -0,0 +1,8 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

import { nativeModule } from '../nativeModule';
import { RTNCore } from '../types';

export const getDeviceName: RTNCore['getDeviceName'] = () =>
nativeModule.getDeviceName();
1 change: 1 addition & 0 deletions packages/react-native/src/apis/index.ts
Expand Up @@ -4,3 +4,4 @@
export { computeModPow } from './computeModPow';
export { computeS } from './computeS';
export { getOperatingSystem } from './getOperatingSystem';
export { getDeviceName } from './getDeviceName';
7 changes: 6 additions & 1 deletion packages/react-native/src/index.ts
@@ -1,7 +1,12 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

export { computeModPow, computeS, getOperatingSystem } from './apis';
export {
computeModPow,
computeS,
getOperatingSystem,
getDeviceName,
} from './apis';
export {
loadAmplifyPushNotification,
loadAmplifyWebBrowser,
Expand Down
2 changes: 2 additions & 0 deletions packages/react-native/src/types.ts
Expand Up @@ -16,4 +16,6 @@ export interface RTNCore {
b: string;
u: string;
}): Promise<string>;

getDeviceName(): Promise<string>;
}