OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | |
2 // for details. All rights reserved. Use of this source code is governed by a | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 // TODO(jacobr): cache these results. | |
6 // TODO(jacobr): figure out how to test this. | |
7 /** | |
8 * Utils for device detection. | |
9 */ | |
10 class Device { | |
11 | |
12 /** | |
13 * The regular expression for detecting an iPhone or iPod. | |
14 */ | |
15 static final _IPHONE_REGEX = const RegExp('iPhone|iPod'); | |
16 | |
17 /** | |
18 * The regular expression for detecting an iPhone or iPod or iPad. | |
19 */ | |
20 static final _MOBILE_SAFARI_REGEX = const RegExp('iPhone|iPod|iPad'); | |
21 | |
22 /** | |
23 * The regular expression for detecting an iPhone or iPod or iPad simulator. | |
24 */ | |
25 static final _APPLE_SIM_REGEX = const RegExp('iP.*Simulator'); | |
26 | |
27 /** | |
28 * Gets the browser's user agent. Using this function allows tests to inject | |
29 * the user agent. | |
30 * Returns the user agent. | |
31 */ | |
32 static String get userAgent() => window.navigator.userAgent; | |
33 | |
34 /** | |
35 * Determines if the current device is an iPhone or iPod. | |
36 * Returns true if the current device is an iPhone or iPod. | |
37 */ | |
38 static bool get isIPhone() => _IPHONE_REGEX.hasMatch(userAgent); | |
39 | |
40 /** | |
41 * Determines if the current device is an iPad. | |
42 * Returns true if the current device is an iPad. | |
43 */ | |
44 static bool get isIPad() => userAgent.contains("iPad", 0); | |
45 | |
46 /** | |
47 * Determines if the current device is running Firefox. | |
48 */ | |
49 static bool get isFirefox() => userAgent.contains("Firefox", 0); | |
50 | |
51 /** | |
52 * Determines if the current device is an iPhone or iPod or iPad. | |
53 * Returns true if the current device is an iPhone or iPod or iPad. | |
54 */ | |
55 static bool get isMobileSafari() => _MOBILE_SAFARI_REGEX.hasMatch(userAgent); | |
56 | |
57 /** | |
58 * Determines if the current device is the iP* Simulator. | |
59 * Returns true if the current device is an iP* Simulator. | |
60 */ | |
61 static bool get isAppleSimulator() => _APPLE_SIM_REGEX.hasMatch(userAgent); | |
62 | |
63 /** | |
64 * Determines if the current device is an Android. | |
65 * Returns true if the current device is an Android. | |
66 */ | |
67 static bool get isAndroid() => userAgent.contains("Android", 0); | |
68 | |
69 /** | |
70 * Determines if the current device is WebOS WebKit. | |
71 * Returns true if the current device is WebOS WebKit. | |
72 */ | |
73 static bool get isWebOs() => userAgent.contains("webOS", 0); | |
74 | |
75 static bool _supportsTouch; | |
76 static bool get supportsTouch() { | |
77 if (_supportsTouch == null) { | |
78 _supportsTouch = isMobileSafari || isAndroid; | |
79 } | |
80 return _supportsTouch; | |
81 } | |
82 } | |
OLD | NEW |