Skip to content

Commit

Permalink
Add Android webview detection (fnando#312)
Browse files Browse the repository at this point in the history
  • Loading branch information
artfuldodger authored and fnando committed Aug 9, 2017
1 parent 3726084 commit 55e106e
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 3 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,16 @@ browser.platform.version # e.g. 9 (for iOS9)
browser.platform.adobe_air?
browser.platform.android?
browser.platform.android?(4.2) # detect Android Jelly Bean 4.2
browser.platform.android_app? # detect webview in an Android app
browser.platform.android_webview? # alias for android_app?
browser.platform.blackberry?
browser.platform.blackberry?(10) # detect specific BlackBerry version
browser.platform.chrome_os?
browser.platform.firefox_os?
browser.platform.ios? # detect iOS
browser.platform.ios?(9) # detect specific iOS version
browser.platform.ios_app?
browser.platform.ios_webview?
browser.platform.ios_webview? # request performed by ios' app webview
browser.platform.ios_app? # detect webview in an iOS app
browser.platform.ios_webview? # alias for ios_app?
browser.platform.linux?
browser.platform.mac?
browser.platform.other?
Expand Down
7 changes: 7 additions & 0 deletions lib/browser/platform.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,13 @@ def ios_webview?
ios_app?
end

# Detect if in an Android app webview (Lollipop and newer)
# https://developer.chrome.com/multidevice/user-agent#webview_user_agent
def android_app?
android? && ua =~ /\bwv\b/
end
alias_method :android_webview?, :android_app?

# http://msdn.microsoft.com/fr-FR/library/ms537503.aspx#PltToken
def windows_xp?
windows? && ua =~ /Windows NT 5\.[12]/
Expand Down
1 change: 1 addition & 0 deletions test/ua.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ ANDROID_LOLLIPOP_50: Mozilla/5.0 (Linux; Android 5.0; Nexus 5 Build/LPX13D) Appl
ANDROID_LOLLIPOP_51: Mozilla/5.0 (Linux; Android 5.1; Nexus 5 Build/LMY47I) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/40.0.0.0 Mobile Safari/537.36; DailymotionEmbedSDK 1.0
ANDROID_NEXUS_PLAYER: Mozilla/5.0 (Linux; Android 5.0; Nexus Player Build/LRX21V) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.0
ANDROID_TV: Mozilla/5.0 (Linux; Android 5.0; ADT-1 Build/LRW87K) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/37.0.0.0 Mobile Safari/537.36
ANDROID_WEBVIEW: Mozilla/5.0 (Linux; Android 5.1.1; Nexus 5 Build/LMY48B; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/43.0.2357.65 Mobile Safari/537.36
ANDROID_WITH_SAFARI: 'Mozilla/5.0 (Linux; U; Android 4.3; en-us; SCH-I535 Build/JSS15J) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30'
BLACKBERRY10: 'Mozilla/5.0 (BB10; Touch) AppleWebKit/537.10+ (KHTML, like Gecko) Version/10.0.9.1675 Mobile Safari/537.10+'
BLACKBERRY4: 'BlackBerry8100/4.2.1 Profile/MIDP-2.0 Configuration/CLDC-1.1 VendorID/103'
Expand Down
38 changes: 38 additions & 0 deletions test/unit/android_app_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# frozen_string_literal: true

require "test_helper"

class AndroidAppTest < Minitest::Test
let(:browser) { Browser.new(Browser["ANDROID_WEBVIEW"]) }

test "detect as android" do
assert browser.platform.android?
end

test "detect as webview" do
assert browser.platform.android_webview?
end

test "non-webviews do not detect as webview" do
%w[
ANDROID_CUPCAKE
ANDROID_DONUT
ANDROID_ECLAIR_21
ANDROID_FROYO
ANDROID_GINGERBREAD
ANDROID_HONEYCOMB_30
ANDROID_ICECREAM
ANDROID_JELLYBEAN_41
ANDROID_JELLYBEAN_42
ANDROID_JELLYBEAN_43
ANDROID_KITKAT
ANDROID_LOLLIPOP_50
ANDROID_LOLLIPOP_51
ANDROID_TV
ANDROID_NEXUS_PLAYER
FIREFOX_ANDROID
].each do |android_type|
refute Browser.new(Browser[android_type]).platform.android_webview?
end
end
end

0 comments on commit 55e106e

Please sign in to comment.