device.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // Copyright 2013 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /**
  15. * @fileoverview Closure user device detection (based on user agent).
  16. * @see http://en.wikipedia.org/wiki/User_agent
  17. * For more information on browser brand, platform, or engine see the other
  18. * sub-namespaces in goog.labs.userAgent (browser, platform, and engine).
  19. *
  20. */
  21. goog.provide('goog.labs.userAgent.device');
  22. goog.require('goog.labs.userAgent.util');
  23. /**
  24. * Currently we detect the iPhone, iPod and Android mobiles (devices that have
  25. * both Android and Mobile in the user agent string).
  26. *
  27. * @return {boolean} Whether the user is using a mobile device.
  28. */
  29. goog.labs.userAgent.device.isMobile = function() {
  30. return !goog.labs.userAgent.device.isTablet() &&
  31. (goog.labs.userAgent.util.matchUserAgent('iPod') ||
  32. goog.labs.userAgent.util.matchUserAgent('iPhone') ||
  33. goog.labs.userAgent.util.matchUserAgent('Android') ||
  34. goog.labs.userAgent.util.matchUserAgent('IEMobile'));
  35. };
  36. /**
  37. * Currently we detect Kindle Fire, iPad, and Android tablets (devices that have
  38. * Android but not Mobile in the user agent string).
  39. *
  40. * @return {boolean} Whether the user is using a tablet.
  41. */
  42. goog.labs.userAgent.device.isTablet = function() {
  43. return goog.labs.userAgent.util.matchUserAgent('iPad') ||
  44. (goog.labs.userAgent.util.matchUserAgent('Android') &&
  45. !goog.labs.userAgent.util.matchUserAgent('Mobile')) ||
  46. goog.labs.userAgent.util.matchUserAgent('Silk');
  47. };
  48. /**
  49. * @return {boolean} Whether the user is using a desktop computer (which we
  50. * assume to be the case if they are not using either a mobile or tablet
  51. * device).
  52. */
  53. goog.labs.userAgent.device.isDesktop = function() {
  54. return !goog.labs.userAgent.device.isMobile() &&
  55. !goog.labs.userAgent.device.isTablet();
  56. };