Skip to content

Instantly share code, notes, and snippets.

@ingmarh
Forked from tvandervossen/environment.js
Last active August 29, 2015 14:07
Show Gist options
  • Save ingmarh/3d2d77fc6a62b4227170 to your computer and use it in GitHub Desktop.
Save ingmarh/3d2d77fc6a62b4227170 to your computer and use it in GitHub Desktop.
Basic environment flags for Cordova apps with installed Device plugin. Note that the Device plugin is not available before the Cordova "deviceready" event has been fired.
env = (function(){
var flags = {}, i, osVersion = device.version + '', androidVersion,
el = document.createElement('div'), root = document.documentElement
function flag(names) {
names = names.split(' ')
for (i = 0; i < names.length; i++) flags[names[i]] = true
}
function classnames() {
var names = [], name
for(name in flags) if (flags.hasOwnProperty(name)) names.push(name)
root.className += (root.className ? ' ' : '') + names.join(' ')
}
if (device.platform) flag(device.platform.replace(/-/g, '').toLowerCase())
if (flags.amazonfireos) flag('android')
if (flags.ios) {
flag('ios' + osVersion.match(/\d+/))
if ('i386 x86_64'.indexOf(device.model) > -1) {
flag('simulator')
} else if (device.model.match(/^(iPad|iPod|iPhone)/)) {
flag(RegExp.$1.toLowerCase())
}
}
if (flags.android) {
androidVersion = osVersion.match(/^\d+\.\d+/) + ''
if (androidVersion === '2.2') flag('froyo')
if (androidVersion === '2.3') flag('gingerbread')
if (androidVersion === '3.2') flag('honeycomb')
if (androidVersion === '4.0') flag('icecreamsandwich')
if ('4.1 4.2 4.3'.indexOf(androidVersion) > -1) flag('jellybean')
if (androidVersion === '4.4') flag('kitkat')
if (androidVersion === '5.0') flag('lollipop')
// Android Virtual Device (emulator)
if ('sdk google_sdk'.indexOf(device.model) > -1) flag('avd')
}
el.setAttribute('ontouchstart', 'return;')
if (typeof el.ontouchstart === 'function') flag('touch')
if (navigator.msPointerEnabled && navigator.msMaxTouchPoints > 1) flag('touch')
if (navigator.devicePixelRatio && navigator.devicePixelRatio >= 2) flag('retina')
if (screen.width > 1280) flag('hd')
if (screen.width < 768) flag('narrow')
classnames()
return flags
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment