forked from gamebtc/devicedetector
/
device_info.go
161 lines (132 loc) · 3.47 KB
/
device_info.go
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package devicedetector
import (
regexp "github.com/dlclark/regexp2"
. "github.com/vtomar10/devicedetector/parser"
"github.com/vtomar10/devicedetector/parser/client"
"github.com/vtomar10/devicedetector/parser/device"
)
var (
touchReg = regexp.MustCompile(fixUserAgentRegEx(`Touch`), regexp.IgnoreCase)
adrTabReg = regexp.MustCompile(fixUserAgentRegEx(`Android( [\.0-9]+)?; Tablet;`), regexp.IgnoreCase)
adrMobReg = regexp.MustCompile(fixUserAgentRegEx(`Android( [\.0-9]+)?; Mobile;`), regexp.IgnoreCase)
)
type DeviceInfo struct {
userAgent string
device.DeviceMatchResult
client *client.ClientMatchResult
os *OsMatchResult
bot *BotMatchResult
}
func (d *DeviceInfo) GetDeviceType() int {
return GetDeviceType(d.Type)
}
func (d *DeviceInfo) IsBot() bool {
return d.bot != nil
}
func (d *DeviceInfo) IsTouchEnabled() bool {
find, _ := touchReg.MatchString(d.userAgent)
return find
}
func (d *DeviceInfo) HasAndroidTableFragment() bool {
find, _ := adrTabReg.MatchString(d.userAgent)
return find
}
func (d *DeviceInfo) HasAndroidMobileFragment() bool {
find, _ := adrMobReg.MatchString(d.userAgent)
return find
}
func (d *DeviceInfo) UsesMobileBrowser() bool {
return d.client != nil && d.client.Type == client.ParserNameBrowser && client.IsMobileOnlyBrowser(d.client.ShortName)
}
func (d *DeviceInfo) IsMobile() bool {
if d.Type != "" {
if deviceType := GetDeviceType(d.Type); DEVICE_TYPE_INVALID != deviceType {
switch deviceType {
case DEVICE_TYPE_FEATURE_PHONE,
DEVICE_TYPE_SMARTPHONE,
DEVICE_TYPE_TABLET,
DEVICE_TYPE_PHABLET,
DEVICE_TYPE_CAMERA,
DEVICE_TYPE_PORTABLE_MEDIA_PAYER:
return true
case DEVICE_TYPE_TV,
DEVICE_TYPE_SMART_DISPLAY,
DEVICE_TYPE_CONSOLE:
return false
}
}
}
if d.UsesMobileBrowser() {
return true
}
if d.os == nil || d.os.ShortName == "" || d.os.ShortName == UNKNOWN {
return false
}
return !d.IsBot() && !d.IsDesktop()
}
func (d *DeviceInfo) IsDesktop() bool {
if d.os == nil || d.os.ShortName == "" || d.os.ShortName == UNKNOWN {
return false
}
// Check for browsers available for mobile devices only
if d.UsesMobileBrowser() {
return false
}
if decodedFamily := GetOsFamily(d.os.ShortName); decodedFamily != "" {
return ArrayContainsString(desktopOsArray, decodedFamily)
}
return false
}
func (d *DeviceInfo) GetOs() *OsMatchResult {
if d.os != nil {
return d.os
}
return &OsMatchResult{}
}
func (d *DeviceInfo) GetClient() *client.ClientMatchResult {
if d.client != nil {
return d.client
}
return &client.ClientMatchResult{}
}
func (d *DeviceInfo) GetBrowserClient() *client.BrowserMatchResult {
if d.client != nil && d.client.Type == client.ParserNameBrowser {
return d.client
}
return &client.BrowserMatchResult{}
}
func (d *DeviceInfo) GetDevice() *device.DeviceMatchResult {
return &d.DeviceMatchResult
}
func (d *DeviceInfo) GetDeviceName() string {
return d.Type
}
func (d *DeviceInfo) GetBrand() string {
return d.Brand
}
func (d *DeviceInfo) GetBrandName() string {
return GetFullName(d.Brand)
}
func (d *DeviceInfo) GetModel() string {
return d.Model
}
func (d *DeviceInfo) GetUserAgent() string {
return d.userAgent
}
func (d *DeviceInfo) GetBot() *BotMatchResult {
return d.bot
}
func (d *DeviceInfo) GetOsFamily() string {
if d.os != nil {
return GetOsFamily(d.os.ShortName)
}
return ""
}
func (d *DeviceInfo) GetBrowserFamily() string {
if d.client != nil {
if v, ok := client.GetBrowserFamily(d.client.ShortName); ok {
return v
}
}
return ""
}