diff --git a/runtime/android/core_internal/src/org/xwalk/core/internal/XWalkContent.java b/runtime/android/core_internal/src/org/xwalk/core/internal/XWalkContent.java index 47fc8e5c88..932e08e005 100644 --- a/runtime/android/core_internal/src/org/xwalk/core/internal/XWalkContent.java +++ b/runtime/android/core_internal/src/org/xwalk/core/internal/XWalkContent.java @@ -133,7 +133,7 @@ public XWalkContent(Context context, AttributeSet attrs, XWalkViewInternal xwVie mGeolocationPermissions = new XWalkGeolocationPermissions(sharedPreferences); MediaPlayerBridge.setResourceLoadingFilter( - new XWalkMediaPlayerResourceLoadingFilter()); + new XWalkMediaPlayerResourceLoadingFilter(mContentsClientBridge)); setNativeContent(nativeInit()); diff --git a/runtime/android/core_internal/src/org/xwalk/core/internal/XWalkContentsClient.java b/runtime/android/core_internal/src/org/xwalk/core/internal/XWalkContentsClient.java index f7079775b7..7ce712b821 100644 --- a/runtime/android/core_internal/src/org/xwalk/core/internal/XWalkContentsClient.java +++ b/runtime/android/core_internal/src/org/xwalk/core/internal/XWalkContentsClient.java @@ -5,12 +5,15 @@ package org.xwalk.core.internal; +import android.content.Context; import android.content.pm.ActivityInfo; +import android.media.MediaPlayer; import android.graphics.Bitmap; import android.graphics.Picture; import android.graphics.Rect; import android.graphics.RectF; import android.net.http.SslError; +import android.net.Uri; import android.os.Handler; import android.os.Looper; import android.os.Message; @@ -23,6 +26,7 @@ import java.security.PrivateKey; import java.util.ArrayList; import java.util.HashMap; +import java.util.Map; import org.chromium.content.browser.ContentViewClient; import org.chromium.content_public.browser.WebContents; @@ -171,6 +175,9 @@ public abstract XWalkWebResourceResponseInternal shouldInterceptRequest( public abstract boolean shouldOverrideUrlLoading(String url); + public abstract boolean shouldOverrideMediaPlaying(MediaPlayer mediaPlayer, + Context context, Uri uri, Map headers); + public abstract void onUnhandledKeyEvent(KeyEvent event); public abstract boolean onConsoleMessage(ConsoleMessage consoleMessage); diff --git a/runtime/android/core_internal/src/org/xwalk/core/internal/XWalkContentsClientBridge.java b/runtime/android/core_internal/src/org/xwalk/core/internal/XWalkContentsClientBridge.java index af960beb62..b5375052c3 100644 --- a/runtime/android/core_internal/src/org/xwalk/core/internal/XWalkContentsClientBridge.java +++ b/runtime/android/core_internal/src/org/xwalk/core/internal/XWalkContentsClientBridge.java @@ -7,10 +7,12 @@ import android.content.ActivityNotFoundException; import android.content.ContentResolver; +import android.content.Context; import android.content.Intent; import android.database.Cursor; import android.graphics.Bitmap; import android.graphics.Picture; +import android.media.MediaPlayer; import android.net.Uri; import android.net.http.SslCertificate; import android.net.http.SslError; @@ -210,6 +212,16 @@ public boolean shouldOverrideUrlLoading(String url) { return false; } + @Override + public boolean shouldOverrideMediaPlaying(MediaPlayer mediaPlayer, + Context context, Uri uri, Map headers) { + if (mXWalkResourceClient != null && mXWalkView != null) { + return mXWalkResourceClient.shouldOverrideMediaPlaying(mXWalkView, + mediaPlayer, context, uri, headers); + } + return false; + } + @Override public boolean shouldOverrideKeyEvent(KeyEvent event) { boolean overridden = false; diff --git a/runtime/android/core_internal/src/org/xwalk/core/internal/XWalkMediaPlayerResourceLoadingFilter.java b/runtime/android/core_internal/src/org/xwalk/core/internal/XWalkMediaPlayerResourceLoadingFilter.java index 2ad2d07591..54e4a6d97b 100644 --- a/runtime/android/core_internal/src/org/xwalk/core/internal/XWalkMediaPlayerResourceLoadingFilter.java +++ b/runtime/android/core_internal/src/org/xwalk/core/internal/XWalkMediaPlayerResourceLoadingFilter.java @@ -12,6 +12,7 @@ import org.chromium.media.MediaPlayerBridge; import java.io.File; +import java.util.Map; import java.util.HashMap; import java.util.List; @@ -22,9 +23,15 @@ class XWalkMediaPlayerResourceLoadingFilter extends MediaPlayerBridge.ResourceLoadingFilter { + private XWalkContentsClientBridge mContentsClientBridge; + + XWalkMediaPlayerResourceLoadingFilter(XWalkContentsClientBridge clientBridge) { + mContentsClientBridge = clientBridge; + } + @Override public boolean shouldOverrideResourceLoading(MediaPlayer mediaPlayer, - Context context, Uri uri) { + Context context, Uri uri, Map headers) { String scheme = uri.getScheme(); if (scheme == null) return false; if (scheme.equals(AndroidProtocolHandler.APP_SCHEME)) { @@ -32,17 +39,18 @@ public boolean shouldOverrideResourceLoading(MediaPlayer mediaPlayer, } scheme = uri.getScheme(); - if (!scheme.equals(AndroidProtocolHandler.FILE_SCHEME)) return false; - - try { - AssetFileDescriptor afd = - context.getAssets().openFd(AndroidProtocolHandler.getAssetPath(uri)); - mediaPlayer.setDataSource( - afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength()); - - return true; - } catch (Exception e) { - return false; + if (scheme.equals(AndroidProtocolHandler.FILE_SCHEME)) { + try { + AssetFileDescriptor afd = + context.getAssets().openFd(AndroidProtocolHandler.getAssetPath(uri)); + mediaPlayer.setDataSource( + afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength()); + return true; + } catch (Exception e) { + return false; + } } + + return mContentsClientBridge.shouldOverrideMediaPlaying(mediaPlayer, context, uri, headers); } } diff --git a/runtime/android/core_internal/src/org/xwalk/core/internal/XWalkResourceClientInternal.java b/runtime/android/core_internal/src/org/xwalk/core/internal/XWalkResourceClientInternal.java index c33b5fdae9..41f02f6680 100644 --- a/runtime/android/core_internal/src/org/xwalk/core/internal/XWalkResourceClientInternal.java +++ b/runtime/android/core_internal/src/org/xwalk/core/internal/XWalkResourceClientInternal.java @@ -8,7 +8,9 @@ import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; +import android.media.MediaPlayer; import android.net.http.SslError; +import android.net.Uri; import android.os.Build; import android.text.InputType; import android.util.Log; @@ -260,6 +262,26 @@ public boolean shouldOverrideUrlLoading(XWalkViewInternal view, String url) { return false; } + /** + * Give the host application a chance to take over the control when a new + * media is about to be played in the current XWalkViewInternal. + * + * @param view The XWalkViewInternal that is initiating the callback. + * @param mediaPalyer The android system services. + * @param context The context to use when resolving the Uri. + * @param uri Tthe Content URI of data you want to play + * @param header The headers to be sent together with the request for the data Note + * that the cross domain redirection is allowed by default. + * @return True if the host application wants to play itself, otherwise return false. + * + * @since 6.0 + */ + @XWalkAPI + public boolean shouldOverrideMediaPlaying(XWalkViewInternal view, + MediaPlayer mediaPlayer, Context context, Uri uri, Map headers) { + return false; + } + /** * Notify the host application that an SSL error occurred while loading a * resource. The host application must call either callback.onReceiveValue(true)