Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(android)Full screen support for android html video #1004

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ instance, or the system browser.
- __shouldPauseOnSuspend__: Set to `yes` to make InAppBrowser WebView to pause/resume with the app to stop background audio (this may be required to avoid Google Play issues like described in [CB-11013](https://issues.apache.org/jira/browse/CB-11013)).
- __useWideViewPort__: Sets whether the WebView should enable support for the "viewport" HTML meta tag or should use a wide viewport. When the value of the setting is `no`, the layout width is always set to the width of the WebView control in device-independent (CSS) pixels. When the value is `yes` and the page contains the viewport meta tag, the value of the width specified in the tag is used. If the page does not contain the tag or does not provide a width, then a wide viewport will be used. (defaults to `yes`).
- __fullscreen__: Sets whether the InappBrowser WebView is displayed fullscreen or not. In fullscreen mode, the status bar is hidden. Default value is `yes`.
- __videoFullscreen__: Sets whether the InappBrowser WebView in full screen when clicking the full screen button of the video of html. Default value is `no`.

iOS supports these additional options:

Expand Down
1 change: 1 addition & 0 deletions plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
<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/InAppVideoChromeClient.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
87 changes: 73 additions & 14 deletions src/android/InAppBrowser.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,13 @@ Licensed to the Apache Software Foundation (ASF) under one
import android.os.Build;
import android.os.Bundle;
import android.text.InputType;
import android.util.Log;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
Expand All @@ -57,6 +60,7 @@ Licensed to the Apache Software Foundation (ASF) under one
import android.webkit.DownloadListener;
import android.webkit.WebViewClient;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
Expand Down Expand Up @@ -118,6 +122,7 @@ public class InAppBrowser extends CordovaPlugin {
private static final String FOOTER_COLOR = "footercolor";
private static final String BEFORELOAD = "beforeload";
private static final String FULLSCREEN = "fullscreen";
private static final String VIDEO_FULLSCREEN = "videofullscreen";

private static final int TOOLBAR_HEIGHT = 48;

Expand Down Expand Up @@ -152,6 +157,11 @@ public class InAppBrowser extends CordovaPlugin {
private String[] allowedSchemes;
private InAppBrowserClient currentClient;

//Video FullScreen
private boolean useVideoFullScreen = false;
private FullscreenHolder fullscreenHolder;
private InAppVideoChromeClient.InAppVideoFullScreenHelper videoHelper;

/**
* Executes the request and returns PluginResult.
*
Expand Down Expand Up @@ -561,6 +571,14 @@ public void onPageFinished(WebView view, String url) {
* Checks to see if it is possible to go back one page in history, then does so.
*/
public void goBack() {
//if useVideo FullScreen
if (useVideoFullScreen) {
//check fullscreen and goback
if(videoHelper.goBack()) {
return;
}
}

if (this.inAppWebView.canGoBack()) {
this.inAppWebView.goBack();
}
Expand Down Expand Up @@ -715,6 +733,10 @@ public String showWebPage(final String url, HashMap<String, String> features) {
if (fullscreenSet != null) {
fullscreen = fullscreenSet.equals("yes") ? true : false;
}
String videoFullscreen = features.get(VIDEO_FULLSCREEN);
if (videoFullscreen != null) {
useVideoFullScreen = videoFullscreen.equals("yes") ? true : false;
}
}

final CordovaWebView thatWebView = this.webView;
Expand Down Expand Up @@ -924,23 +946,25 @@ public boolean onKey(View v, int keyCode, KeyEvent event) {
inAppWebView.setWebChromeClient(new InAppChromeClient(thatWebView) {
public boolean onShowFileChooser (WebView webView, ValueCallback<Uri[]> filePathCallback, WebChromeClient.FileChooserParams fileChooserParams)
{
LOG.d(LOG_TAG, "File Chooser 5.0+");
// If callback exists, finish it.
if(mUploadCallback != null) {
mUploadCallback.onReceiveValue(null);
}
mUploadCallback = filePathCallback;

// Create File Chooser Intent
Intent content = new Intent(Intent.ACTION_GET_CONTENT);
content.addCategory(Intent.CATEGORY_OPENABLE);
content.setType("*/*");

// Run cordova startActivityForResult
cordova.startActivityForResult(InAppBrowser.this, Intent.createChooser(content, "Select File"), FILECHOOSER_REQUESTCODE);
openFileChooser(filePathCallback);
return true;
}
});
// if useVideoFullScreen
if (useVideoFullScreen) {
// init holder
fullscreenHolder = new FullscreenHolder(cordova.getContext());
// init videoHelper
videoHelper = new InAppVideoChromeClient.InAppVideoFullScreenHelper(cordova.getActivity(), fullscreenHolder);
// change to InAppVideoChromeClient
inAppWebView.setWebChromeClient(new InAppVideoChromeClient(thatWebView,videoHelper) {
public boolean onShowFileChooser (WebView webView, ValueCallback<Uri[]> filePathCallback, WebChromeClient.FileChooserParams fileChooserParams)
{
openFileChooser(filePathCallback);
return true;
}
});
}
currentClient = new InAppBrowserClient(thatWebView, edittext, beforeload);
inAppWebView.setWebViewClient(currentClient);
WebSettings settings = inAppWebView.getSettings();
Expand Down Expand Up @@ -1047,6 +1071,10 @@ public void postMessage(String data) {
// Add our webview to our main view/layout
RelativeLayout webViewLayout = new RelativeLayout(cordova.getActivity());
webViewLayout.addView(inAppWebView);
if(useVideoFullScreen) {
// if useVideoFullSceen add FullScreenHolder
webViewLayout.addView(fullscreenHolder,new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT));
}
main.addView(webViewLayout);

// Don't add the footer unless it's been enabled
Expand Down Expand Up @@ -1487,4 +1515,35 @@ public void onReceivedHttpAuthRequest(WebView view, HttpAuthHandler handler, Str
super.onReceivedHttpAuthRequest(view, handler, host, realm);
}
}

// bind the same function For ChromeClient
private void openFileChooser(ValueCallback<Uri[]> filePathCallback) {
LOG.d(LOG_TAG, "File Chooser 5.0+");
// If callback exists, finish it.
if(mUploadCallback != null) {
mUploadCallback.onReceiveValue(null);
}
mUploadCallback = filePathCallback;

// Create File Chooser Intent
Intent content = new Intent(Intent.ACTION_GET_CONTENT);
content.addCategory(Intent.CATEGORY_OPENABLE);
content.setType("*/*");

// Run cordova startActivityForResult
cordova.startActivityForResult(InAppBrowser.this, Intent.createChooser(content, "Select File"), FILECHOOSER_REQUESTCODE);
}

//FullScreen Video Helper Method
private static class FullscreenHolder extends FrameLayout {
public FullscreenHolder(Context context) {
super(context);
setBackgroundColor(Color.BLACK);
setVisibility(View.INVISIBLE);
}
@Override
public boolean onTouchEvent(MotionEvent evt) {
return true;
}
}
}
108 changes: 108 additions & 0 deletions src/android/InAppVideoChromeClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
*/
package org.apache.cordova.inappbrowser;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.GeolocationPermissions.Callback;
import android.webkit.JsPromptResult;
import android.webkit.WebChromeClient;
import android.webkit.WebStorage;
import android.webkit.WebView;
import android.widget.FrameLayout;

import org.apache.cordova.CordovaWebView;
import org.apache.cordova.LOG;
import org.apache.cordova.PluginResult;
import org.json.JSONArray;
import org.json.JSONException;

public class InAppVideoChromeClient extends InAppChromeClient {

InAppVideoFullScreenHelper helper;
public InAppVideoChromeClient(CordovaWebView webView ,InAppVideoFullScreenHelper helper) {
super(webView);
this.helper = helper;
}

@Override
public void onShowCustomView(View view, CustomViewCallback callback) {
helper.onShowCustomView(view,callback);
}

@Override
public void onShowCustomView(View view, int requestedOrientation, CustomViewCallback callback) {
super.onShowCustomView(view,callback);
}

@Override
public void onHideCustomView() {
helper.onHideCustomView();
}

public static class InAppVideoFullScreenHelper {
Activity activity;
FrameLayout holder;

private int originalOrientation;
private View customView;
private WebChromeClient.CustomViewCallback customViewCallback;

public InAppVideoFullScreenHelper(Activity activity, FrameLayout holder) {
this.activity = activity;
this.holder = holder;
}

public void onShowCustomView(View view, WebChromeClient.CustomViewCallback callback) {
holder.addView(view, new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
customView = view;
customViewCallback = callback;
originalOrientation = activity.getRequestedOrientation();
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
holder.setVisibility(View.VISIBLE);
}

private void resetValues() {
activity.setRequestedOrientation(originalOrientation);
holder.removeView(customView);
customViewCallback.onCustomViewHidden();
customView = null;
customViewCallback = null;
holder.setVisibility(View.INVISIBLE);
}

public void onHideCustomView() {
if (customView == null) {
return;
}
resetValues();
}

public boolean goBack() {
if (customView == null) {
return false;
}
resetValues();
return true;
}
}

}