Skip to content

Commit

Permalink
Merge in video tag changes and fix back button issue
Browse files Browse the repository at this point in the history
  • Loading branch information
macdonst committed Oct 17, 2012
2 parents d3ee322 + 06aafc9 commit a3a215a
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 26 deletions.
51 changes: 50 additions & 1 deletion framework/src/org/apache/cordova/CordovaChromeClient.java
Expand Up @@ -20,14 +20,16 @@ Licensed to the Apache Software Foundation (ASF) under one

import org.apache.cordova.api.CordovaInterface;
import org.apache.cordova.api.LOG;
import org.apache.cordova.api.PluginResult;
import org.json.JSONArray;
import org.json.JSONException;

import android.annotation.TargetApi;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.webkit.ConsoleMessage;
import android.webkit.JsPromptResult;
import android.webkit.JsResult;
Expand All @@ -36,6 +38,9 @@ Licensed to the Apache Software Foundation (ASF) under one
import android.webkit.WebView;
import android.webkit.GeolocationPermissions.Callback;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;

/**
* This class is the WebChromeClient that implements callbacks for our web view.
Expand All @@ -47,6 +52,9 @@ public class CordovaChromeClient extends WebChromeClient {
private CordovaInterface cordova;
private CordovaWebView appView;

// the video progress view
private View mVideoProgressView;

/**
* Constructor.
*
Expand Down Expand Up @@ -316,4 +324,45 @@ public void onGeolocationPermissionsShowPrompt(String origin, Callback callback)
super.onGeolocationPermissionsShowPrompt(origin, callback);
callback.invoke(origin, true, false);
}

// API level 7 is required for this, see if we could lower this using something else
@Override
public void onShowCustomView(View view, WebChromeClient.CustomViewCallback callback) {
this.appView.showCustomView(view, callback);
}

@Override
public void onHideCustomView() {
this.appView.hideCustomView();
}

@Override
/**
* Ask the host application for a custom progress view to show while
* a <video> is loading.
* @return View The progress view.
*/
public View getVideoLoadingProgressView() {

if (mVideoProgressView == null) {
// Create a new Loading view programmatically.

// create the linear layout
LinearLayout layout = new LinearLayout(this.appView.getContext());
layout.setOrientation(LinearLayout.VERTICAL);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
layout.setLayoutParams(layoutParams);
// the proress bar
ProgressBar bar = new ProgressBar(this.appView.getContext());
LinearLayout.LayoutParams barLayoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
barLayoutParams.gravity = Gravity.CENTER;
bar.setLayoutParams(barLayoutParams);
layout.addView(bar);

mVideoProgressView = layout;
}
return mVideoProgressView;
}

}
113 changes: 90 additions & 23 deletions framework/src/org/apache/cordova/CordovaWebView.java
Expand Up @@ -31,7 +31,6 @@ Licensed to the Apache Software Foundation (ASF) under one
import org.apache.cordova.api.LOG;
import org.apache.cordova.api.PluginManager;
import org.apache.cordova.api.PluginResult;
import org.json.JSONException;
import org.xmlpull.v1.XmlPullParserException;

import android.annotation.SuppressLint;
Expand All @@ -41,20 +40,23 @@ Licensed to the Apache Software Foundation (ASF) under one
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.XmlResourceParser;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.webkit.WebBackForwardList;
import android.webkit.WebHistoryItem;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebSettings.LayoutAlgorithm;
import android.widget.FrameLayout;

public class CordovaWebView extends WebView {

Expand Down Expand Up @@ -90,15 +92,21 @@ public class CordovaWebView extends WebView {

private boolean bound;

private boolean volumedownBound;

private boolean volumeupBound;

private boolean handleButton = false;

NativeToJsMessageQueue jsMessageQueue;
ExposedJsApi exposedJsApi;

/** custom view created by the browser (a video player for example) */
private View mCustomView;
private WebChromeClient.CustomViewCallback mCustomViewCallback;

static final FrameLayout.LayoutParams COVER_SCREEN_GRAVITY_CENTER =
new FrameLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT,
Gravity.CENTER);

/**
* Constructor.
*
Expand Down Expand Up @@ -800,22 +808,28 @@ public boolean onKeyUp(int keyCode, KeyEvent event)
{
// If back key
if (keyCode == KeyEvent.KEYCODE_BACK) {
// If back key is bound, then send event to JavaScript
if (this.bound) {
this.loadUrl("javascript:cordova.fireDocumentEvent('backbutton');");
return true;
} else {
// If not bound
// Go to previous page in webview if it is possible to go back
if (this.backHistory()) {
return true;
}
// If not, then invoke default behavior
else {
//this.activityState = ACTIVITY_EXITING;
return false;
}
}
// A custom view is currently displayed (e.g. playing a video)
if(mCustomView != null) {
this.hideCustomView();
} else {
// The webview is currently displayed
// If back key is bound, then send event to JavaScript
if (this.bound) {
this.loadUrl("javascript:cordova.fireDocumentEvent('backbutton');");
return true;
} else {
// If not bound
// Go to previous page in webview if it is possible to go back
if (this.backHistory()) {
return true;
}
// If not, then invoke default behaviour
else {
//this.activityState = ACTIVITY_EXITING;
return false;
}
}
}
}
// Legacy
else if (keyCode == KeyEvent.KEYCODE_MENU) {
Expand Down Expand Up @@ -977,4 +991,57 @@ public boolean startOfHistory()
LOG.d(TAG, "The URL at item 0 is:" + url);
return currentUrl.equals(url);
}

public void showCustomView(View view, WebChromeClient.CustomViewCallback callback) {
// This code is adapted from the original Android Browser code, licensed under the Apache License, Version 2.0
Log.d(TAG, "showing Custom View");
// if a view already exists then immediately terminate the new one
if (mCustomView != null) {
callback.onCustomViewHidden();
return;
}

// Store the view and its callback for later (to kill it properly)
mCustomView = view;
mCustomViewCallback = callback;

// Add the custom view to its container.
ViewGroup parent = (ViewGroup) this.getParent();
parent.addView(view, COVER_SCREEN_GRAVITY_CENTER);

// Hide the content view.
this.setVisibility(View.GONE);

// Finally show the custom view container.
parent.setVisibility(View.VISIBLE);
parent.bringToFront();
}

public void hideCustomView() {
// This code is adapted from the original Android Browser code, licensed under the Apache License, Version 2.0
Log.d(TAG, "Hidding Custom View");
if (mCustomView == null) return;

// Hide the custom view.
mCustomView.setVisibility(View.GONE);

// Remove the custom view from its container.
ViewGroup parent = (ViewGroup) this.getParent();
parent.removeView(mCustomView);
mCustomView = null;
mCustomViewCallback.onCustomViewHidden();

// Show the content view.
this.setVisibility(View.VISIBLE);
}

/**
* if the video overlay is showing then we need to know
* as it effects back button handling
*
* @return
*/
public boolean isCustomViewShowing() {
return mCustomView != null;
}
}
6 changes: 4 additions & 2 deletions framework/src/org/apache/cordova/DroidGap.java
Expand Up @@ -1010,9 +1010,11 @@ public boolean onKeyUp(int keyCode, KeyEvent event)
appView.getHitTestResult().getType() == WebView.HitTestResult.EDIT_TEXT_TYPE &&
keyCode == KeyEvent.KEYCODE_BACK) {
return appView.onKeyUp(keyCode, event);
} else if (appView.isCustomViewShowing() && keyCode == KeyEvent.KEYCODE_BACK) {
return appView.onKeyUp(keyCode, event);
} else {
return super.onKeyUp(keyCode, event);
}
else
return super.onKeyUp(keyCode, event);
}

/*
Expand Down

0 comments on commit a3a215a

Please sign in to comment.