Skip to content

Commit

Permalink
Merged in en-fix-video-full-screen-inside-inappbrowser-android-WKWebV…
Browse files Browse the repository at this point in the history
…iew (pull request apache#10)

Fix video full screen inside inappbrowser Android with WKWebView

Approved-by: João Lourenço <joao.lourenco@growthengineering.co.uk>
  • Loading branch information
Eric Nyangezi committed Jul 24, 2020
2 parents bead171 + 31541f6 commit c48b7e7
Show file tree
Hide file tree
Showing 5 changed files with 546 additions and 10 deletions.
2 changes: 2 additions & 0 deletions plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
<source-file src="src/android/InAppBrowser.java" target-dir="src/org/apache/cordova/inappbrowser" />
<source-file src="src/android/InAppBrowserDialog.java" target-dir="src/org/apache/cordova/inappbrowser" />
<source-file src="src/android/InAppChromeClient.java" target-dir="src/org/apache/cordova/inappbrowser" />
<source-file src="src/android/VideoEnabledWebChromeClient.java" target-dir="src/org/apache/cordova/inappbrowser" />
<source-file src="src/android/VideoEnabledWebView.java" target-dir="src/org/apache/cordova/inappbrowser" />

<!-- drawable src/android/resources -->
<resource-file src="src/android/res/drawable-hdpi/ic_action_next_item.png" target="res/drawable-hdpi/ic_action_next_item.png" />
Expand Down
113 changes: 106 additions & 7 deletions src/android/InAppBrowser.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,18 @@ Licensed to the Apache Software Foundation (ASF) under one
*/
package org.apache.cordova.inappbrowser;

import android.app.Activity;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Parcelable;
import android.provider.Browser;
import android.os.Handler;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
Expand Down Expand Up @@ -97,6 +100,7 @@ public class InAppBrowser extends CordovaPlugin {
private static final String EXIT_EVENT = "exit";
private static final String LOCATION = "location";
private static final String ZOOM = "zoom";
private static final String FULL_SCREEN = "fullscreen";
private static final String HIDDEN = "hidden";
private static final String LOAD_START_EVENT = "loadstart";
private static final String LOAD_STOP_EVENT = "loadstop";
Expand Down Expand Up @@ -134,6 +138,7 @@ public class InAppBrowser extends CordovaPlugin {
private boolean clearSessionCache = false;
private boolean hadwareBackButton = true;
private boolean mediaPlaybackRequiresUserGesture = false;
private boolean fullScreenFeature = false;
private boolean shouldPauseInAppBrowser = false;
private boolean useWideViewPort = true;
private ValueCallback<Uri> mUploadCallback;
Expand Down Expand Up @@ -670,6 +675,10 @@ public String showWebPage(final String url, HashMap<String, String> features) {
if (mediaPlayback != null) {
mediaPlaybackRequiresUserGesture = mediaPlayback.equals("yes") ? true : false;
}
String fullScreen = features.get(FULL_SCREEN);
if (fullScreen != null) {
fullScreenFeature = fullScreen.equals("yes") ? true : false;
}
String cache = features.get(CLEAR_ALL_CACHE);
if (cache != null) {
clearAllCache = cache.equals("yes") ? true : false;
Expand Down Expand Up @@ -727,6 +736,9 @@ public String showWebPage(final String url, HashMap<String, String> features) {

// Create dialog in new thread
Runnable runnable = new Runnable() {

private boolean inFullScreen = false;

/**
* Convert our DIP units to Pixels
*
Expand All @@ -740,6 +752,38 @@ private int dpToPixels(int dipValue) {

return value;
}

private void enableFullScreen(Activity activity, Window window) {
inFullScreen = true;
WindowManager.LayoutParams attrs = window.getAttributes();
attrs.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
attrs.flags |= WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
window.setAttributes(attrs);
if (android.os.Build.VERSION.SDK_INT >= 14)
{
//noinspection all
int flags = View.SYSTEM_UI_FLAG_LOW_PROFILE;
if (android.os.Build.VERSION.SDK_INT >= 16)
{
flags = View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE;
}
window.getDecorView().setSystemUiVisibility(flags);
}

}

private void disableFullScreen(Activity activity, Window window) {
inFullScreen = false;
WindowManager.LayoutParams attrs = window.getAttributes();
attrs.flags &= ~WindowManager.LayoutParams.FLAG_FULLSCREEN;
attrs.flags &= ~WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
window.setAttributes(attrs);
if (android.os.Build.VERSION.SDK_INT >= 14)
{
//noinspection all
window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
}
}

private View createCloseButton(int id) {
View _close;
Expand Down Expand Up @@ -798,8 +842,30 @@ public void run() {
};

// Let's create the main dialog
dialog = new InAppBrowserDialog(cordova.getActivity(), android.R.style.Theme_NoTitleBar);
dialog = new InAppBrowserDialog(cordova.getActivity(), android.R.style.Theme_NoTitleBar_Fullscreen);
dialog.getWindow().getAttributes().windowAnimations = android.R.style.Animation_Dialog;

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
final View decor = dialog.getWindow().getDecorView();
decor.setOnSystemUiVisibilityChangeListener (new View.OnSystemUiVisibilityChangeListener() {
public void onSystemUiVisibilityChange(int visibility) {
new Handler().postDelayed(new Runnable() {
public void run(){
if (inFullScreen)
{
decor.setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE);
}
}
}, 3000);
}
});
}

if (fullScreenFeature)
{
enableFullScreen(cordova.getActivity(), dialog.getWindow());
}

dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
if (fullscreen) {
dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
Expand All @@ -808,8 +874,12 @@ public void run() {
dialog.setInAppBroswer(getInAppBrowser());

// Main container layout
LinearLayout main = new LinearLayout(cordova.getActivity());
main.setOrientation(LinearLayout.VERTICAL);
RelativeLayout main = new RelativeLayout(cordova.getActivity());
RelativeLayout fullScreenMain = new RelativeLayout(cordova.getActivity());
fullScreenMain.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
LinearLayout browserMain = new LinearLayout(cordova.getActivity());
browserMain.setOrientation(LinearLayout.VERTICAL);
browserMain.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

// Toolbar layout
RelativeLayout toolbar = new RelativeLayout(cordova.getActivity());
Expand Down Expand Up @@ -937,11 +1007,11 @@ public boolean onKey(View v, int keyCode, KeyEvent event) {


// WebView
inAppWebView = new WebView(cordova.getActivity());
inAppWebView = new VideoEnabledWebView(cordova.getActivity());
inAppWebView.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
inAppWebView.setId(Integer.valueOf(6));
// File Chooser Implemented ChromeClient
inAppWebView.setWebChromeClient(new InAppChromeClient(thatWebView) {
InAppChromeClient inAppChromeClient = new InAppChromeClient(thatWebView, browserMain, fullScreenMain) {
// For Android 5.0+
public boolean onShowFileChooser (WebView webView, ValueCallback<Uri[]> filePathCallback, WebChromeClient.FileChooserParams fileChooserParams)
{
Expand Down Expand Up @@ -982,7 +1052,33 @@ public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType)
cordova.startActivityForResult(InAppBrowser.this, Intent.createChooser(content, "Select File"), FILECHOOSER_REQUESTCODE);
}

};

inAppChromeClient.setOnToggledFullscreen(new VideoEnabledWebChromeClient.ToggledFullscreenCallback() {
@Override
public void toggledFullscreen(boolean fullscreen)
{
// Your code to handle the full-screen change, for example showing and hiding the title bar. Example:

Activity activity = cordova.getActivity();
Window window = dialog.getWindow();
if (fullscreen)
{
enableFullScreen(activity, window);
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
}
else
{
if (!fullScreenFeature)
{
disableFullScreen(activity, window);
}
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
}
}
});
inAppWebView.setWebChromeClient(inAppChromeClient);

currentClient = new InAppBrowserClient(thatWebView, edittext, beforeload);
inAppWebView.setWebViewClient(currentClient);
WebSettings settings = inAppWebView.getSettings();
Expand Down Expand Up @@ -1060,13 +1156,13 @@ public void postMessage(String data) {
// Don't add the toolbar if its been disabled
if (getShowLocationBar()) {
// Add our toolbar to our main view/layout
main.addView(toolbar);
browserMain.addView(toolbar);
}

// Add our webview to our main view/layout
RelativeLayout webViewLayout = new RelativeLayout(cordova.getActivity());
webViewLayout.addView(inAppWebView);
main.addView(webViewLayout);
browserMain.addView(webViewLayout);

// Don't add the footer unless it's been enabled
if (showFooter) {
Expand All @@ -1078,6 +1174,9 @@ public void postMessage(String data) {
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.MATCH_PARENT;

main.addView(browserMain);
main.addView(fullScreenMain);

if (dialog != null) {
dialog.setContentView(main);
dialog.show();
Expand Down
8 changes: 5 additions & 3 deletions src/android/InAppChromeClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,23 @@ Licensed to the Apache Software Foundation (ASF) under one
import org.json.JSONArray;
import org.json.JSONException;

import android.view.View;
import android.view.ViewGroup;
import android.webkit.JsPromptResult;
import android.webkit.WebChromeClient;
import android.webkit.WebStorage;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.webkit.GeolocationPermissions.Callback;

public class InAppChromeClient extends WebChromeClient {
public class InAppChromeClient extends VideoEnabledWebChromeClient {

private CordovaWebView webView;
private String LOG_TAG = "InAppChromeClient";
private long MAX_QUOTA = 100 * 1024 * 1024;

public InAppChromeClient(CordovaWebView webView) {
super();
public InAppChromeClient(CordovaWebView webView, View activityNonVideoView, ViewGroup activityVideoView) {
super(activityNonVideoView, activityVideoView);
this.webView = webView;
}
/**
Expand Down
Loading

0 comments on commit c48b7e7

Please sign in to comment.