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

Added fullscreen-video support #892

Open
wants to merge 3 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
2 changes: 2 additions & 0 deletions plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,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
140 changes: 118 additions & 22 deletions src/android/InAppBrowser.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ Licensed to the Apache Software Foundation (ASF) under one
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,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The deletion of these lines will probably cause problems with our release process where it checks for proper license headers. Please revert these deletions

software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
Expand All @@ -18,12 +16,14 @@ 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.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.content.pm.ActivityInfo;
import android.os.Parcelable;
import android.provider.Browser;
import android.content.res.Resources;
Expand All @@ -34,6 +34,7 @@ Licensed to the Apache Software Foundation (ASF) under one
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.text.InputType;
import android.util.TypedValue;
import android.view.Gravity;
Expand Down Expand Up @@ -132,8 +133,10 @@ public class InAppBrowser extends CordovaPlugin {
private boolean mediaPlaybackRequiresUserGesture = false;
private boolean shouldPauseInAppBrowser = false;
private boolean useWideViewPort = true;
private ValueCallback<Uri[]> mUploadCallback;
private ValueCallback<Uri> mUploadCallback;
private ValueCallback<Uri[]> mUploadCallbackLollipop;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing code that handles API < 21 should also eliminate the need to have two different class members to handle different API levels.

private final static int FILECHOOSER_REQUESTCODE = 1;
private final static int FILECHOOSER_REQUESTCODE_LOLLIPOP = 2;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same thing here; see comment above.

private String closeButtonCaption = "";
private String closeButtonColor = "";
private boolean leftToRight = false;
Expand Down Expand Up @@ -264,10 +267,7 @@ else if (action.equals("loadAfterBeforeload")) {
@Override
public void run() {
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.O) {
currentClient.waitForBeforeload = false;
inAppWebView.setWebViewClient(currentClient);
} else {
((InAppBrowserClient)inAppWebView.getWebViewClient()).waitForBeforeload = false;
}
inAppWebView.loadUrl(url);
}
Expand Down Expand Up @@ -716,6 +716,8 @@ 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 Down Expand Up @@ -773,6 +775,38 @@ public void onClick(View v) {
return _close;
}

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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As of cordova-android@9, our supported min SDK is 22.

We don't need to have defensive checks here, and can assume that the SDK will be at least 22.

{
//noinspection all
int flags = View.SYSTEM_UI_FLAG_LOW_PROFILE;
if (android.os.Build.VERSION.SDK_INT >= 16)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same situation above.

{
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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See comment above

{
//noinspection all
window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
}
}

@SuppressLint("NewApi")
public void run() {

Expand All @@ -785,13 +819,30 @@ public void run() {
dialog = new InAppBrowserDialog(cordova.getActivity(), android.R.style.Theme_NoTitleBar);
dialog.getWindow().getAttributes().windowAnimations = android.R.style.Animation_Dialog;
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
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 (fullscreen) {
dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
dialog.setCancelable(true);
dialog.setInAppBroswer(getInAppBrowser());

// Main container layout
RelativeLayout fsmain = 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());
LinearLayout main = new LinearLayout(cordova.getActivity());
main.setOrientation(LinearLayout.VERTICAL);

Expand Down Expand Up @@ -917,26 +968,73 @@ public boolean onKey(View v, int keyCode, KeyEvent event) {
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)
{
LOG.d(LOG_TAG, "File Chooser 5.0+");
// If callback exists, finish it.
if(mUploadCallback != null) {
mUploadCallback.onReceiveValue(null);
if(mUploadCallbackLollipop != null) {
mUploadCallbackLollipop.onReceiveValue(null);
}
mUploadCallback = filePathCallback;
mUploadCallbackLollipop = 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);
cordova.startActivityForResult(InAppBrowser.this, Intent.createChooser(content, "Select File"), FILECHOOSER_REQUESTCODE_LOLLIPOP);
return true;
}

// For Android 4.1+
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need to support android 4.x

{
LOG.d(LOG_TAG, "File Chooser 4.1+");
// Call file chooser for Android 3.0+
openFileChooser(uploadMsg, acceptType);
}

// For Android 3.0+
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need to support android 3.x

{
LOG.d(LOG_TAG, "File Chooser 3.0+");
mUploadCallback = uploadMsg;
Intent content = new Intent(Intent.ACTION_GET_CONTENT);
content.addCategory(Intent.CATEGORY_OPENABLE);

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

};

inAppChromeClient.setOnToggledFullscreen(new VideoEnabledWebChromeClient.ToggledFullscreenCallback() {
@Override
public void toggledFullscreen(boolean fullscreenV)
{
// 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 (fullscreenV)
{
enableFullScreen(activity, window);
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
}
else
{
if (!fullscreen)
{
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 @@ -1031,8 +1129,11 @@ public void postMessage(String data) {
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.MATCH_PARENT;

fsmain.addView(main);
fsmain.addView(fullScreenMain);

if (dialog != null) {
dialog.setContentView(main);
dialog.setContentView(fsmain);
dialog.show();
dialog.getWindow().setAttributes(lp);
}
Expand Down Expand Up @@ -1083,12 +1184,12 @@ private void sendUpdate(JSONObject obj, boolean keepCallback, PluginResult.Statu
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
LOG.d(LOG_TAG, "onActivityResult");
// If RequestCode or Callback is Invalid
if(requestCode != FILECHOOSER_REQUESTCODE || mUploadCallback == null) {
if(requestCode != FILECHOOSER_REQUESTCODE_LOLLIPOP || mUploadCallbackLollipop == null) {
super.onActivityResult(requestCode, resultCode, intent);
return;
}
mUploadCallback.onReceiveValue(WebChromeClient.FileChooserParams.parseResult(resultCode, intent));
mUploadCallback = null;
mUploadCallbackLollipop.onReceiveValue(WebChromeClient.FileChooserParams.parseResult(resultCode, intent));
mUploadCallbackLollipop = null;
}

/**
Expand All @@ -1098,7 +1199,6 @@ public class InAppBrowserClient extends WebViewClient {
EditText edittext;
CordovaWebView webView;
String beforeload;
boolean waitForBeforeload;

/**
* Constructor.
Expand All @@ -1110,7 +1210,6 @@ public InAppBrowserClient(CordovaWebView webView, EditText mEditText, String bef
this.webView = webView;
this.edittext = mEditText;
this.beforeload = beforeload;
this.waitForBeforeload = beforeload != null;
}

/**
Expand Down Expand Up @@ -1171,7 +1270,7 @@ public boolean shouldOverrideUrlLoading(String url, String method) {
}

// On first URL change, initiate JS callback. Only after the beforeload event, continue.
if (useBeforeload && this.waitForBeforeload) {
if (useBeforeload) {
if(sendBeforeLoad(url, method)) {
return true;
}
Expand Down Expand Up @@ -1266,9 +1365,6 @@ else if (!url.startsWith("http:") && !url.startsWith("https:") && url.matches("^
}
}

if (useBeforeload) {
this.waitForBeforeload = true;
}
return override;
}

Expand Down Expand Up @@ -1459,4 +1555,4 @@ public void onReceivedHttpAuthRequest(WebView view, HttpAuthHandler handler, Str
super.onReceivedHttpAuthRequest(view, handler, host, realm);
}
}
}
}
9 changes: 6 additions & 3 deletions src/android/InAppChromeClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ 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.annotation.TargetApi;
import android.os.Build;
import android.os.Message;
Expand All @@ -35,14 +38,14 @@ Licensed to the Apache Software Foundation (ASF) under one
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