TestNativeInfo.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. import React, {Component, useCallback, memo} from 'react';
  2. import {
  3. ScrollView,
  4. StyleSheet,
  5. Text,
  6. SafeAreaView,
  7. View,
  8. TouchableOpacity,
  9. NativeModules,
  10. } from 'react-native';
  11. import DeviceInfo from 'react-native-device-info';
  12. import {
  13. getManufacturer,
  14. getManufacturerSync,
  15. syncUniqueId,
  16. getUniqueId,
  17. getUniqueIdSync,
  18. useBatteryLevel,
  19. useBatteryLevelIsLow,
  20. usePowerState,
  21. useFirstInstallTime,
  22. useDeviceName,
  23. useManufacturer,
  24. useHasSystemFeature,
  25. useIsEmulator,
  26. useIsHeadphonesConnected,
  27. useBrightness,
  28. } from 'react-native-device-info';
  29. const FunctionalComponent = () => {
  30. const batteryLevel = useBatteryLevel();
  31. const batteryLevelIsLow = useBatteryLevelIsLow();
  32. const powerState = usePowerState();
  33. const firstInstallTime = useFirstInstallTime();
  34. const deviceName = useDeviceName();
  35. const manufacturer = useManufacturer();
  36. const hasSystemFeature = useHasSystemFeature('amazon.hardware.fire_tv');
  37. const isEmulator = useIsEmulator();
  38. const isHeadphonesConnected = useIsHeadphonesConnected();
  39. const brightness = useBrightness();
  40. const deviceJSON = {
  41. batteryLevel,
  42. batteryLevelIsLow,
  43. powerState,
  44. firstInstallTime,
  45. deviceName,
  46. manufacturer,
  47. hasSystemFeature,
  48. isEmulator,
  49. isHeadphonesConnected,
  50. brightness,
  51. };
  52. return (
  53. <ScrollView>
  54. <Text style={styles.instructions} testID="hooks tab contents">
  55. {JSON.stringify(deviceJSON, null, ' ')}
  56. </Text>
  57. </ScrollView>
  58. );
  59. };
  60. const ActionExtensionHeader = memo(({isActionExtension}) => {
  61. const onDonePress = useCallback(() => {
  62. NativeModules.ActionExtension.done();
  63. }, []);
  64. return isActionExtension ? (
  65. <View style={{minHeight: 50, flexDirection: 'row', margin: 10}}>
  66. <TouchableOpacity onPress={onDonePress}>
  67. <View
  68. style={{
  69. backgroundColor: 'red',
  70. borderRadius: 20,
  71. minWidth: 80,
  72. minHeight: 40,
  73. alignContent: 'center',
  74. justifyContent: 'center',
  75. alignItems: 'center',
  76. }}>
  77. <Text>Done</Text>
  78. </View>
  79. </TouchableOpacity>
  80. </View>
  81. ) : (
  82. <View />
  83. );
  84. });
  85. export default class TestNativeInfo extends Component {
  86. constructor(props) {
  87. super(props);
  88. this.state = {
  89. activeTab: 'constant',
  90. constantdeviceinfo: this.getConstantDeviceInfo(),
  91. asyncdeviceinfo: {},
  92. syncdeviceinfo: this.getSyncDeviceInfo(),
  93. };
  94. }
  95. getConstantDeviceInfo() {
  96. let deviceJSON = {};
  97. deviceJSON.deviceId = DeviceInfo.getDeviceId();
  98. deviceJSON.bundleId = DeviceInfo.getBundleId();
  99. deviceJSON.systemName = DeviceInfo.getSystemName();
  100. deviceJSON.systemVersion = DeviceInfo.getSystemVersion();
  101. deviceJSON.version = DeviceInfo.getVersion();
  102. deviceJSON.readableVersion = DeviceInfo.getReadableVersion();
  103. deviceJSON.buildNumber = DeviceInfo.getBuildNumber();
  104. deviceJSON.isTablet = DeviceInfo.isTablet();
  105. deviceJSON.appName = DeviceInfo.getApplicationName();
  106. deviceJSON.brand = DeviceInfo.getBrand();
  107. deviceJSON.model = DeviceInfo.getModel();
  108. deviceJSON.deviceType = DeviceInfo.getDeviceType();
  109. return deviceJSON;
  110. }
  111. getSyncDeviceInfo() {
  112. let deviceJSON = {};
  113. deviceJSON.uniqueId = getUniqueIdSync();
  114. deviceJSON.manufacturer = getManufacturerSync();
  115. deviceJSON.buildId = DeviceInfo.getBuildIdSync();
  116. deviceJSON.isCameraPresent = DeviceInfo.isCameraPresentSync();
  117. deviceJSON.deviceName = DeviceInfo.getDeviceNameSync();
  118. deviceJSON.usedMemory = DeviceInfo.getUsedMemorySync();
  119. deviceJSON.instanceId = DeviceInfo.getInstanceIdSync();
  120. deviceJSON.installReferrer = DeviceInfo.getInstallReferrerSync();
  121. deviceJSON.installerPackageName = DeviceInfo.getInstallerPackageNameSync();
  122. deviceJSON.isEmulator = DeviceInfo.isEmulatorSync();
  123. deviceJSON.fontScale = DeviceInfo.getFontScaleSync();
  124. deviceJSON.hasNotch = DeviceInfo.hasNotch();
  125. deviceJSON.hasDynamicIsland = DeviceInfo.hasDynamicIsland();
  126. deviceJSON.firstInstallTime = DeviceInfo.getFirstInstallTimeSync();
  127. deviceJSON.lastUpdateTime = DeviceInfo.getLastUpdateTimeSync();
  128. deviceJSON.serialNumber = DeviceInfo.getSerialNumberSync();
  129. deviceJSON.androidId = DeviceInfo.getAndroidIdSync();
  130. deviceJSON.IpAddress = DeviceInfo.getIpAddressSync();
  131. deviceJSON.MacAddress = DeviceInfo.getMacAddressSync(); // needs android.permission.ACCESS_WIFI_STATE
  132. deviceJSON.phoneNumber = "DeviceInfo.getPhoneNumberSync()"; // needs android.permission.READ_PHONE_STATE
  133. deviceJSON.ApiLevel = DeviceInfo.getApiLevelSync();
  134. deviceJSON.carrier = DeviceInfo.getCarrierSync();
  135. deviceJSON.totalMemory = DeviceInfo.getTotalMemorySync();
  136. deviceJSON.maxMemory = DeviceInfo.getMaxMemorySync();
  137. deviceJSON.totalDiskCapacity = DeviceInfo.getTotalDiskCapacitySync();
  138. deviceJSON.totalDiskCapacityOld = DeviceInfo.getTotalDiskCapacityOldSync();
  139. deviceJSON.freeDiskStorage = DeviceInfo.getFreeDiskStorageSync();
  140. deviceJSON.freeDiskStorageOld = DeviceInfo.getFreeDiskStorageOldSync();
  141. deviceJSON.batteryLevel = DeviceInfo.getBatteryLevelSync();
  142. deviceJSON.isLandscape = DeviceInfo.isLandscapeSync();
  143. deviceJSON.isAirplaneMode = DeviceInfo.isAirplaneModeSync();
  144. deviceJSON.isBatteryCharging = DeviceInfo.isBatteryChargingSync();
  145. deviceJSON.isPinOrFingerprintSet = DeviceInfo.isPinOrFingerprintSetSync();
  146. deviceJSON.supportedAbis = DeviceInfo.supportedAbisSync();
  147. deviceJSON.hasSystemFeature = DeviceInfo.hasSystemFeatureSync(
  148. 'android.software.webview',
  149. );
  150. deviceJSON.getSystemAvailableFeatures = DeviceInfo.getSystemAvailableFeaturesSync();
  151. deviceJSON.powerState = DeviceInfo.getPowerStateSync();
  152. deviceJSON.isLocationEnabled = DeviceInfo.isLocationEnabledSync();
  153. deviceJSON.headphones = DeviceInfo.isHeadphonesConnectedSync();
  154. deviceJSON.getAvailableLocationProviders = DeviceInfo.getAvailableLocationProvidersSync();
  155. deviceJSON.bootloader = DeviceInfo.getBootloaderSync();
  156. deviceJSON.device = DeviceInfo.getDeviceSync();
  157. deviceJSON.display = DeviceInfo.getDisplaySync();
  158. deviceJSON.fingerprint = DeviceInfo.getFingerprintSync();
  159. deviceJSON.hardware = DeviceInfo.getHardwareSync();
  160. deviceJSON.host = DeviceInfo.getHostSync();
  161. deviceJSON.product = DeviceInfo.getProductSync();
  162. deviceJSON.tags = DeviceInfo.getTagsSync();
  163. deviceJSON.type = DeviceInfo.getTypeSync();
  164. deviceJSON.baseOS = DeviceInfo.getBaseOsSync();
  165. deviceJSON.previewSdkInt = DeviceInfo.getPreviewSdkIntSync();
  166. deviceJSON.securityPatch = DeviceInfo.getSecurityPatchSync();
  167. deviceJSON.codename = DeviceInfo.getCodenameSync();
  168. deviceJSON.incremental = DeviceInfo.getIncrementalSync();
  169. deviceJSON.brightness = DeviceInfo.getBrightnessSync();
  170. deviceJSON.supported32BitAbis = DeviceInfo.supported32BitAbisSync();
  171. deviceJSON.supported64BitAbis = DeviceInfo.supported64BitAbisSync();
  172. deviceJSON.hasGms = DeviceInfo.hasGmsSync();
  173. deviceJSON.hasHms = DeviceInfo.hasHmsSync();
  174. deviceJSON.isMouseConnected = DeviceInfo.isMouseConnectedSync();
  175. deviceJSON.isKeyboardConnected = DeviceInfo.isKeyboardConnectedSync();
  176. return deviceJSON;
  177. }
  178. async componentDidMount() {
  179. let deviceJSON = {};
  180. try {
  181. deviceJSON.uniqueId = await getUniqueId();
  182. deviceJSON.syncUniqueId = await syncUniqueId();
  183. deviceJSON.manufacturer = await getManufacturer();
  184. deviceJSON.buildId = await DeviceInfo.getBuildId();
  185. deviceJSON.isCameraPresent = await DeviceInfo.isCameraPresent();
  186. deviceJSON.deviceName = await DeviceInfo.getDeviceName();
  187. deviceJSON.usedMemory = await DeviceInfo.getUsedMemory();
  188. deviceJSON.userAgent = await DeviceInfo.getUserAgent();
  189. deviceJSON.instanceId = await DeviceInfo.getInstanceId();
  190. deviceJSON.installReferrer = await DeviceInfo.getInstallReferrer();
  191. deviceJSON.installerPackageName = await DeviceInfo.getInstallerPackageName();
  192. deviceJSON.isEmulator = await DeviceInfo.isEmulator();
  193. deviceJSON.fontScale = await DeviceInfo.getFontScale();
  194. deviceJSON.hasNotch = await DeviceInfo.hasNotch();
  195. deviceJSON.hasDynamicIsland = await DeviceInfo.hasDynamicIsland();
  196. deviceJSON.firstInstallTime = await DeviceInfo.getFirstInstallTime();
  197. deviceJSON.lastUpdateTime = await DeviceInfo.getLastUpdateTime();
  198. deviceJSON.serialNumber = await DeviceInfo.getSerialNumber();
  199. deviceJSON.androidId = await DeviceInfo.getAndroidId();
  200. deviceJSON.IpAddress = await DeviceInfo.getIpAddress();
  201. deviceJSON.MacAddress = await DeviceInfo.getMacAddress(); // needs android.permission.ACCESS_WIFI_STATE
  202. deviceJSON.phoneNumber = "await DeviceInfo.getPhoneNumber()"; // needs android.permission.READ_PHONE_STATE
  203. deviceJSON.ApiLevel = await DeviceInfo.getApiLevel();
  204. deviceJSON.carrier = await DeviceInfo.getCarrier();
  205. deviceJSON.totalMemory = await DeviceInfo.getTotalMemory();
  206. deviceJSON.maxMemory = await DeviceInfo.getMaxMemory();
  207. deviceJSON.totalDiskCapacity = await DeviceInfo.getTotalDiskCapacity();
  208. deviceJSON.totalDiskCapacityOld = await DeviceInfo.getTotalDiskCapacityOld();
  209. deviceJSON.freeDiskStorage = await DeviceInfo.getFreeDiskStorage();
  210. deviceJSON.freeDiskStorageOld = await DeviceInfo.getFreeDiskStorageOld();
  211. deviceJSON.batteryLevel = await DeviceInfo.getBatteryLevel();
  212. deviceJSON.isLandscape = await DeviceInfo.isLandscape();
  213. deviceJSON.isAirplaneMode = await DeviceInfo.isAirplaneMode();
  214. deviceJSON.isBatteryCharging = await DeviceInfo.isBatteryCharging();
  215. deviceJSON.isPinOrFingerprintSet = await DeviceInfo.isPinOrFingerprintSet();
  216. deviceJSON.supportedAbis = await DeviceInfo.supportedAbis();
  217. deviceJSON.hasSystemFeature = await DeviceInfo.hasSystemFeature(
  218. 'android.software.webview',
  219. );
  220. deviceJSON.getSystemAvailableFeatures = await DeviceInfo.getSystemAvailableFeatures();
  221. deviceJSON.powerState = await DeviceInfo.getPowerState();
  222. deviceJSON.isLocationEnabled = await DeviceInfo.isLocationEnabled();
  223. deviceJSON.headphones = await DeviceInfo.isHeadphonesConnected();
  224. deviceJSON.getAvailableLocationProviders = await DeviceInfo.getAvailableLocationProviders();
  225. deviceJSON.bootloader = await DeviceInfo.getBootloader();
  226. deviceJSON.device = await DeviceInfo.getDevice();
  227. deviceJSON.display = await DeviceInfo.getDisplay();
  228. deviceJSON.fingerprint = await DeviceInfo.getFingerprint();
  229. deviceJSON.hardware = await DeviceInfo.getHardware();
  230. deviceJSON.host = await DeviceInfo.getHost();
  231. deviceJSON.product = await DeviceInfo.getProduct();
  232. deviceJSON.tags = await DeviceInfo.getTags();
  233. deviceJSON.type = await DeviceInfo.getType();
  234. deviceJSON.baseOS = await DeviceInfo.getBaseOs();
  235. deviceJSON.previewSdkInt = await DeviceInfo.getPreviewSdkInt();
  236. deviceJSON.securityPatch = await DeviceInfo.getSecurityPatch();
  237. deviceJSON.codename = await DeviceInfo.getCodename();
  238. deviceJSON.incremental = await DeviceInfo.getIncremental();
  239. deviceJSON.brightness = await DeviceInfo.getBrightness();
  240. deviceJSON.supported32BitAbis = await DeviceInfo.supported32BitAbis();
  241. deviceJSON.supported64BitAbis = await DeviceInfo.supported64BitAbis();
  242. deviceJSON.hasGms = await DeviceInfo.hasGms();
  243. deviceJSON.hasHms = await DeviceInfo.hasHms();
  244. deviceJSON.synchronizedUniqueId = await DeviceInfo.syncUniqueId();
  245. deviceJSON.isMouseConnected = await DeviceInfo.isMouseConnected();
  246. deviceJSON.isKeyboardConnected = await DeviceInfo.isKeyboardConnected();
  247. deviceJSON.isTabletMode = await DeviceInfo.isTabletMode();
  248. try {
  249. deviceJSON.deviceToken = await DeviceInfo.getDeviceToken();
  250. } catch (e) {
  251. console.log(
  252. 'Trouble getting device token, likely a simulator or not iOS11+',
  253. );
  254. }
  255. } catch (e) {
  256. console.log('Trouble getting device info ', e);
  257. }
  258. // eslint-disable-next-line react/no-did-mount-set-state
  259. this.setState({asyncdeviceinfo: deviceJSON});
  260. this.forceUpdate();
  261. }
  262. render() {
  263. return (
  264. <SafeAreaView style={styles.container}>
  265. <ActionExtensionHeader
  266. isActionExtension={this.props.isActionExtension}
  267. />
  268. {this.state.activeTab === 'constant' ? (
  269. <>
  270. <Text style={styles.welcome}>
  271. react-native-device-info example - constant info:
  272. </Text>
  273. <ScrollView>
  274. <Text style={styles.instructions} testID="constant tab contents">
  275. {JSON.stringify(this.state.constantdeviceinfo, null, ' ')}
  276. </Text>
  277. </ScrollView>
  278. </>
  279. ) : this.state.activeTab === 'sync' ? (
  280. <>
  281. <Text style={styles.welcome}>
  282. react-native-device-info example - sync info:
  283. </Text>
  284. <ScrollView>
  285. <Text style={styles.instructions} testID="sync tab contents">
  286. {JSON.stringify(this.state.syncdeviceinfo, null, ' ')}
  287. </Text>
  288. </ScrollView>
  289. </>
  290. ) : this.state.activeTab === 'async' ? (
  291. <>
  292. <Text style={styles.welcome}>
  293. react-native-device-info example - async info:
  294. </Text>
  295. <ScrollView>
  296. <Text style={styles.instructions} testID="async tab contents">
  297. {JSON.stringify(this.state.asyncdeviceinfo, null, ' ')}
  298. </Text>
  299. </ScrollView>
  300. </>
  301. ) : this.state.activeTab === 'hooks' ? (
  302. <>
  303. <Text style={styles.welcome}>
  304. react-native-device-info example - hooks:
  305. </Text>
  306. <FunctionalComponent />
  307. </>
  308. ) : null}
  309. <View style={styles.tabBar}>
  310. <TouchableOpacity
  311. style={styles.tab}
  312. testID="constant button"
  313. onPress={() => this.setState({activeTab: 'constant'})}>
  314. <Text
  315. style={[
  316. styles.tabText,
  317. this.state.activeTab === 'constant' && styles.boldText,
  318. ]}>
  319. Constant
  320. </Text>
  321. </TouchableOpacity>
  322. <TouchableOpacity
  323. style={styles.tab}
  324. testID="sync button"
  325. onPress={() => this.setState({activeTab: 'sync'})}>
  326. <Text
  327. style={[
  328. styles.tabText,
  329. this.state.activeTab === 'sync' && styles.boldText,
  330. ]}>
  331. Sync
  332. </Text>
  333. </TouchableOpacity>
  334. <TouchableOpacity
  335. style={styles.tab}
  336. testID="async button"
  337. onPress={() => this.setState({activeTab: 'async'})}>
  338. <Text
  339. style={[
  340. styles.tabText,
  341. this.state.activeTab === 'async' && styles.boldText,
  342. ]}>
  343. Async
  344. </Text>
  345. </TouchableOpacity>
  346. <TouchableOpacity
  347. style={styles.tab}
  348. testID="hooks button"
  349. onPress={() => this.setState({activeTab: 'hooks'})}>
  350. <Text
  351. style={[
  352. styles.tabText,
  353. this.state.activeTab === 'hooks' && styles.boldText,
  354. ]}>
  355. Hooks
  356. </Text>
  357. </TouchableOpacity>
  358. </View>
  359. </SafeAreaView>
  360. );
  361. }
  362. }
  363. const styles = StyleSheet.create({
  364. container: {
  365. flex: 1,
  366. backgroundColor: '#F5FCFF',
  367. },
  368. welcome: {
  369. fontSize: 20,
  370. textAlign: 'center',
  371. margin: 10,
  372. },
  373. instructions: {
  374. textAlign: 'left',
  375. color: '#333333',
  376. margin: 5,
  377. },
  378. tabBar: {
  379. flexDirection: 'row',
  380. borderTopColor: '#333333',
  381. borderTopWidth: 1,
  382. },
  383. tab: {
  384. height: 50,
  385. flex: 1,
  386. alignItems: 'center',
  387. justifyContent: 'center',
  388. },
  389. tabText: {
  390. color: '#333333',
  391. },
  392. boldText: {
  393. fontWeight: '700',
  394. },
  395. });