1import * as Device from 'expo-device';
2import * as React from 'react';
3import { Button, ScrollView, View, Platform } from 'react-native';
4
5import HeadingText from '../components/HeadingText';
6import MonoText from '../components/MonoText';
7import Colors from '../constants/Colors';
8
9interface DeviceConstant {
10  name?: string;
11  value?: any;
12}
13
14interface DeviceMethod {
15  name?: string;
16  method: () => Promise<any>;
17}
18
19const deviceTypeMap = {
20  [Device.DeviceType.UNKNOWN]: 'unknown',
21  [Device.DeviceType.PHONE]: 'phone',
22  [Device.DeviceType.TABLET]: 'tablet',
23  [Device.DeviceType.DESKTOP]: 'desktop',
24  [Device.DeviceType.TV]: 'tv',
25};
26
27function DeviceConstants({ name, value }: DeviceConstant) {
28  return (
29    <View style={{ marginBottom: 8 }}>
30      <HeadingText>{name}</HeadingText>
31      <MonoText> {typeof value === 'boolean' ? String(value) : value}</MonoText>
32    </View>
33  );
34}
35
36function DeviceMethods({ name = '', method }: DeviceMethod) {
37  const [value, setValue] = React.useState('');
38
39  const getValueAsync = async () => {
40    let value: any;
41    try {
42      value = await method();
43      if (typeof value === 'boolean') {
44        value = value.toString();
45      } else if (Array.isArray(value)) {
46        value = value.join('\n');
47      }
48    } catch (error) {
49      alert(error);
50      value = error.message;
51    }
52    setValue(value);
53  };
54
55  return (
56    <View style={{ paddingVertical: 8 }}>
57      <View style={{ marginBottom: 8 }}>
58        <HeadingText>{name}</HeadingText>
59        <MonoText>{value}</MonoText>
60      </View>
61      <Button onPress={getValueAsync} title={name} color={Colors.tintColor} />
62    </View>
63  );
64}
65
66export default function DeviceScreen() {
67  return (
68    <ScrollView style={{ flex: 1 }} contentContainerStyle={{ padding: 12 }}>
69      <DeviceConstants name="Device Brand" value={Device.brand} />
70      <DeviceConstants name="Device Year" value={Device.deviceYearClass} />
71      <DeviceConstants name="Device manufacturer" value={Device.manufacturer} />
72      <DeviceConstants name="Device modelName" value={Device.modelName} />
73      <DeviceConstants name="Device os name" value={Device.osName} />
74      <DeviceConstants name="Device type" value={deviceTypeMap[Device.deviceType!]} />
75      <DeviceConstants name="Device total Memory" value={Device.totalMemory} />
76      <DeviceConstants name="Device isDevice" value={Device.isDevice} />
77      <DeviceConstants name="Device modelId" value={Device.modelId} />
78      <DeviceConstants
79        name="Device supportedCpuArchitectures"
80        value={Device.supportedCpuArchitectures}
81      />
82      <DeviceConstants name="Device osVersion" value={Device.osVersion} />
83      <DeviceConstants name="Device deviceName" value={Device.deviceName} />
84      <DeviceConstants name="Device osInternalBuildId" value={Device.osInternalBuildId} />
85      {Platform.OS === 'android' && (
86        <View>
87          <DeviceConstants name="Device osBuildFingerprint" value={Device.osBuildFingerprint} />
88          <DeviceConstants name="Device designName" value={Device.designName} />
89          <DeviceConstants name="Device osBuildId" value={Device.osBuildId} />
90          <DeviceConstants name="Device productName" value={Device.productName} />
91          <DeviceConstants name="Device platformApiLevel" value={Device.platformApiLevel} />
92        </View>
93      )}
94      <DeviceMethods
95        name="Device deviceType"
96        method={async () => deviceTypeMap[await Device.getDeviceTypeAsync()]}
97      />
98      {Platform.OS === 'android' && (
99        <View>
100          <DeviceMethods
101            name="Device get system features"
102            method={Device.getPlatformFeaturesAsync}
103          />
104          <DeviceMethods name="Device get max memory" method={Device.getMaxMemoryAsync} />
105          <DeviceMethods
106            name="Device is sideloading enabled"
107            method={Device.isSideLoadingEnabledAsync}
108          />
109        </View>
110      )}
111      <DeviceMethods
112        name="Device is rooted experimental"
113        method={Device.isRootedExperimentalAsync}
114      />
115    </ScrollView>
116  );
117}
118