Shubham Pratap
Lurker
I am working on developing an Android application, in which I want to open a URL in WebView. Now, if I open a simple URL in WebView (like www.google.com) is works just fine. But I want to open a URL which uses WebRTC (Web Real-Time Communication) which errs stating: "Looks like you are using unsupported browser version". I searched on google.com and found that WebRTC starts supporting from Android 5.0 (API level 21) and higher.
My version of Webview is:
using webView.getSettings().getUserAgentString() i get my webview details.
Mozilla/5.0 (Linux; Android 9; Android SDK built for x86 Build/PSR1.180720.075; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/69.0.3497.100 Mobile Safari/537.36
My build.gradle (Module:app) is as follows :
apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.cbtech.learningpod"
minSdkVersion 21
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner
"android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
dataBinding {
enabled = true
}
}
configurations.all {
resolutionStrategy.eachDependency { details ->
def requested = details.requested
if (requested.group == "com.android.support") {
if (!requested.name.startsWith("multidex")) {
details.useVersion "26.+"
}
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.android.support:support-v4:28.0.0'
implementation 'com.android.support:design:28.0.0'
implementation 'com.google.firebase:firebase-messaging:17.3.4'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'android.arch.lifecycle:extensions:1.1.1'
implementation 'com.android.volley:volley:1.1.1'
implementation 'com.android.support:recyclerview-v7:28.0.0'
implementation 'com.android.support:cardview-v7:28.0.0'
implementation 'com.google.code.gson:gson:2.8.2'
implementation 'com.squareup.picassoicasso:2.5.2'
implementation 'com.github.bumptech.glide:glide:3.7.0'
implementation 'com.android.support:customtabs:28.0.0'
implementation 'com.squareup.okhttp3khttp:3.10.0'
implementation 'org.apache.commons:commons-lang3:3.6'
implementation('com.paytmgplussdk:1.3.3') {
transitive = true;
}
}
AndroidManifest.xml file is as follows :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:dist="http://schemas.android.com/apk/distribution"
package="com.cbtech.learningpod">
<dist:module dist:instant="true" />
<uses-feature
android:name="android.hardware.camera"
android:required="false" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<uses-feature
android:name="android.hardware.camera.flash"
android:required="false" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<application
android:allowBackup="true"
android:icon="@Mipmap/ic_launcher"
android:label="@String/app_name"
android:roundIcon="@Mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@Style/AppTheme"
android:usesCleartextTraffic="true">
<meta-data
android:name="android.webkit.WebView.EnableSafeBrowsing"
android:value="false" />
<!--<meta-data
android:name="android.webkit.WebView.EnableSafeBrowsing"
android:value="false" />-->
<activity
android:name=".views.MainActivity"
android:label="@String/title_activity_main"
android:theme="@Style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!--
Add this activity to your manifest
it comes with the Paytm SDK
-->
<activity
android:name="com.paytm.pgsdk.PaytmPGActivity"
android:configChanges="keyboardHidden|orientation|keyboard"
android:screenOrientation="portrait" />
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.example.android.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths"></meta-data>
</provider>
</application>
</manifest>
MyFragment.java file is as follows :
public class MyFragment extends Fragment {
String url;
private WebView webView;
private ProgressDialog progressDialog;
final String CUSTOM_TAB_PACKAGE_NAME = "com.android.chrome";
CustomTabsClient mCustomTabsClient;
CustomTabsSession mCustomTabsSession;
CustomTabsServiceConnection mCustomTabsServiceConnection;
CustomTabsIntent mCustomTabsIntent;
public static MyFragment newInstance(String url) {
BBBFragment fragment = new BBBFragment();
Bundle args = new Bundle();
args.putString("URL", url);
fragment.setArguments(args);
return fragment;
}
@override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
url = getArguments().getString("URL");
}
}
@RequiresApi(api = Build.VERSION_CODES.O)
@override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_myfragment, container, false);
webView = view.findViewById(R.id.webviewBBB);
try {
Log.d("BBB URL is ", url);
Log.d("WebView Version", "=======>" + webView.getSettings().getUserAgentString());
// webView = (WebView) view.findViewById(R.id.webviewBBB);
//setUpWebViewDefaults(webView);
webView.setWebChromeClient(new MyWebClient());
webView.setWebViewClient(new WebViewClient());
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webView.getSettings().setUserAgentString("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");
// AppRTC requires third party cookies to work
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setAcceptThirdPartyCookies(webView, true);
webView.loadUrl(url);
//webView.setWebViewClient(new MyBrowser());
//webView.loadUrl("
");
/* CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
CustomTabsIntent customTabsIntent = builder.build();
customTabsIntent.launchUrl(getActivity(), Uri.parse(url));*/
} catch (Exception e) {
e.printStackTrace();
}
return view;
}
private class MyBrowser extends WebViewClient {
@override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
@override
public void onPageStarted(
WebView view, String url, Bitmap favicon) {
Log.i("Tag", "page started:" + url);
super.onPageStarted(view, url, favicon);
}
@override
public void onPageFinished(WebView view, final String url) {
Log.i("Tag", "page finished:" + url);
}
//Show loader on url load
public void onLoadResource(final WebView view, String url) {
Uri uri = Uri.parse(url);
CustomTabsIntent.Builder intentBuilder = new CustomTabsIntent.Builder();
//Open the Custom Tab
intentBuilder.build().launchUrl(getContext(), uri);
}
@override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError er) {
handler.proceed();
}
}
private class MyWebClient extends WebChromeClient {
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@override
public void onPermissionRequest(PermissionRequest request) {
super.onPermissionRequest(request);
request.grant(request.getResources());
}
}
/**
* Convenience method to set some generic defaults for a
* given WebView
*
* @param webView
*/
@TargetApi(Build.VERSION_CODES.O)
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
private void setUpWebViewDefaults(WebView webView) {
WebSettings settings = webView.getSettings();
// Enable Javascript
settings.setJavaScriptEnabled(true);
// Use WideViewport and Zoom out if there is no viewport defined
settings.setUseWideViewPort(true);
settings.setLoadWithOverviewMode(true);
// Enable pinch to zoom without the zoom buttons
settings.setBuiltInZoomControls(true);
// Allow use of Local Storage
settings.setDomStorageEnabled(true);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
// Hide the zoom controls for HONEYCOMB+
settings.setDisplayZoomControls(false);
}
// Enable remote debugging via chrome://inspect
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
WebView.setWebContentsDebuggingEnabled(true);
}
webView.setWebViewClient(new WebViewClient());
webView.canGoBack();
webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
webView.getSettings().setSafeBrowsingEnabled(true);
webView.getSettings().setLoadsImagesAutomatically(true);
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setDatabaseEnabled(true);
webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
webView.getSettings().setAppCacheEnabled(true);
webView.getSettings().setUserAgentString("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");
// AppRTC requires third party cookies to work
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setAcceptThirdPartyCookies(webView, true);
}
}
My version of Webview is:
using webView.getSettings().getUserAgentString() i get my webview details.
Mozilla/5.0 (Linux; Android 9; Android SDK built for x86 Build/PSR1.180720.075; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/69.0.3497.100 Mobile Safari/537.36
My build.gradle (Module:app) is as follows :
apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.cbtech.learningpod"
minSdkVersion 21
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner
"android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
dataBinding {
enabled = true
}
}
configurations.all {
resolutionStrategy.eachDependency { details ->
def requested = details.requested
if (requested.group == "com.android.support") {
if (!requested.name.startsWith("multidex")) {
details.useVersion "26.+"
}
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.android.support:support-v4:28.0.0'
implementation 'com.android.support:design:28.0.0'
implementation 'com.google.firebase:firebase-messaging:17.3.4'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'android.arch.lifecycle:extensions:1.1.1'
implementation 'com.android.volley:volley:1.1.1'
implementation 'com.android.support:recyclerview-v7:28.0.0'
implementation 'com.android.support:cardview-v7:28.0.0'
implementation 'com.google.code.gson:gson:2.8.2'
implementation 'com.squareup.picassoicasso:2.5.2'
implementation 'com.github.bumptech.glide:glide:3.7.0'
implementation 'com.android.support:customtabs:28.0.0'
implementation 'com.squareup.okhttp3khttp:3.10.0'
implementation 'org.apache.commons:commons-lang3:3.6'
implementation('com.paytmgplussdk:1.3.3') {
transitive = true;
}
}
AndroidManifest.xml file is as follows :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:dist="http://schemas.android.com/apk/distribution"
package="com.cbtech.learningpod">
<dist:module dist:instant="true" />
<uses-feature
android:name="android.hardware.camera"
android:required="false" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<uses-feature
android:name="android.hardware.camera.flash"
android:required="false" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<application
android:allowBackup="true"
android:icon="@Mipmap/ic_launcher"
android:label="@String/app_name"
android:roundIcon="@Mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@Style/AppTheme"
android:usesCleartextTraffic="true">
<meta-data
android:name="android.webkit.WebView.EnableSafeBrowsing"
android:value="false" />
<!--<meta-data
android:name="android.webkit.WebView.EnableSafeBrowsing"
android:value="false" />-->
<activity
android:name=".views.MainActivity"
android:label="@String/title_activity_main"
android:theme="@Style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!--
Add this activity to your manifest
it comes with the Paytm SDK
-->
<activity
android:name="com.paytm.pgsdk.PaytmPGActivity"
android:configChanges="keyboardHidden|orientation|keyboard"
android:screenOrientation="portrait" />
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.example.android.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths"></meta-data>
</provider>
</application>
</manifest>
MyFragment.java file is as follows :
public class MyFragment extends Fragment {
String url;
private WebView webView;
private ProgressDialog progressDialog;
final String CUSTOM_TAB_PACKAGE_NAME = "com.android.chrome";
CustomTabsClient mCustomTabsClient;
CustomTabsSession mCustomTabsSession;
CustomTabsServiceConnection mCustomTabsServiceConnection;
CustomTabsIntent mCustomTabsIntent;
public static MyFragment newInstance(String url) {
BBBFragment fragment = new BBBFragment();
Bundle args = new Bundle();
args.putString("URL", url);
fragment.setArguments(args);
return fragment;
}
@override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
url = getArguments().getString("URL");
}
}
@RequiresApi(api = Build.VERSION_CODES.O)
@override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_myfragment, container, false);
webView = view.findViewById(R.id.webviewBBB);
try {
Log.d("BBB URL is ", url);
Log.d("WebView Version", "=======>" + webView.getSettings().getUserAgentString());
// webView = (WebView) view.findViewById(R.id.webviewBBB);
//setUpWebViewDefaults(webView);
webView.setWebChromeClient(new MyWebClient());
webView.setWebViewClient(new WebViewClient());
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webView.getSettings().setUserAgentString("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");
// AppRTC requires third party cookies to work
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setAcceptThirdPartyCookies(webView, true);
webView.loadUrl(url);
//webView.setWebViewClient(new MyBrowser());
//webView.loadUrl("
/* CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
CustomTabsIntent customTabsIntent = builder.build();
customTabsIntent.launchUrl(getActivity(), Uri.parse(url));*/
} catch (Exception e) {
e.printStackTrace();
}
return view;
}
private class MyBrowser extends WebViewClient {
@override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
@override
public void onPageStarted(
WebView view, String url, Bitmap favicon) {
Log.i("Tag", "page started:" + url);
super.onPageStarted(view, url, favicon);
}
@override
public void onPageFinished(WebView view, final String url) {
Log.i("Tag", "page finished:" + url);
}
//Show loader on url load
public void onLoadResource(final WebView view, String url) {
Uri uri = Uri.parse(url);
CustomTabsIntent.Builder intentBuilder = new CustomTabsIntent.Builder();
//Open the Custom Tab
intentBuilder.build().launchUrl(getContext(), uri);
}
@override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError er) {
handler.proceed();
}
}
private class MyWebClient extends WebChromeClient {
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@override
public void onPermissionRequest(PermissionRequest request) {
super.onPermissionRequest(request);
request.grant(request.getResources());
}
}
/**
* Convenience method to set some generic defaults for a
* given WebView
*
* @param webView
*/
@TargetApi(Build.VERSION_CODES.O)
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
private void setUpWebViewDefaults(WebView webView) {
WebSettings settings = webView.getSettings();
// Enable Javascript
settings.setJavaScriptEnabled(true);
// Use WideViewport and Zoom out if there is no viewport defined
settings.setUseWideViewPort(true);
settings.setLoadWithOverviewMode(true);
// Enable pinch to zoom without the zoom buttons
settings.setBuiltInZoomControls(true);
// Allow use of Local Storage
settings.setDomStorageEnabled(true);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
// Hide the zoom controls for HONEYCOMB+
settings.setDisplayZoomControls(false);
}
// Enable remote debugging via chrome://inspect
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
WebView.setWebContentsDebuggingEnabled(true);
}
webView.setWebViewClient(new WebViewClient());
webView.canGoBack();
webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
webView.getSettings().setSafeBrowsingEnabled(true);
webView.getSettings().setLoadsImagesAutomatically(true);
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setDatabaseEnabled(true);
webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
webView.getSettings().setAppCacheEnabled(true);
webView.getSettings().setUserAgentString("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");
// AppRTC requires third party cookies to work
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setAcceptThirdPartyCookies(webView, true);
}
}