forked from m0ngr31/react-native-device-detection
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
73 lines (64 loc) · 1.96 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import React, {
PixelRatio,
Platform,
Dimensions
} from 'react-native';
// NOTE: Screen and window dimensions are different on android
// window: reports width/height without the soft menu bar
// screen: reports entire screen's width/height
const windowSize = Dimensions.get('screen');
class DetectDeviceService {
constructor() {
this.pixelDensity = PixelRatio.get();
this.width = windowSize.width;
this.height = windowSize.height;
this.adjustedWidth = this.width * this.pixelDensity;
this.adjustedHeight = this.height * this.pixelDensity;
this.isPhoneOrTabletOrTv();
this.isIosOrAndroid();
this.detectIphoneX();
}
isPhoneOrTabletOrTv() {
if (Platform.isTV) {
this.isTV = true;
this.isTablet = false;
this.isPhone = false;
return;
}
// An Android device is considered a tablet if its smallest width >= 600dp (layout-sw600dp)
// source: https://developer.android.com/training/multiscreen/screensizes.html#TaskUseSWQuali
if(Platform.OS === 'android') {
this.isTablet = Math.min(this.width, this.height) >= 600;
} else {
if(this.pixelDensity < 2 && (this.adjustedWidth >= 1000 || this.adjustedHeight >= 1000)) {
this.isTablet = true;
} else if(this.pixelDensity === 2 && (this.adjustedWidth >= 1920 || this.adjustedHeight >= 1920)) {
this.isTablet = true;
} else {
this.isTablet = false;
}
}
this.isPhone = !this.isTablet;
}
isIosOrAndroid() {
if(Platform.OS === 'ios') {
this.isIos = true;
this.isAndroid = false;
} else {
this.isIos = false;
this.isAndroid = true;
}
}
detectIphoneX(){
if( Platform.OS === 'ios' &&
!Platform.isTVOS &&
!Platform.isTVOS &&
(windowSize.height === 812 || windowSize.width === 812)) {
this.isIphoneX = true;
} else {
this.isIphoneX = false;
}
}
}
const detectDeviceService = new DetectDeviceService();
export default detectDeviceService;