Skip to content

Commit

Permalink
Merge branch 'mraid10' into mad_mraid10_mosen
Browse files Browse the repository at this point in the history
  • Loading branch information
Moritz Becker committed Mar 29, 2012
2 parents efaf329 + 4b940dc commit 9965aab
Showing 1 changed file with 91 additions and 67 deletions.
158 changes: 91 additions & 67 deletions madvertiseSDK/src/de/madvertise/android/sdk/MadvertiseMraidView.java
Expand Up @@ -29,22 +29,25 @@
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
//import android.media.MediaPlayer;
//import android.media.MediaPlayer.OnCompletionListener;
//import android.media.MediaPlayer.OnErrorListener;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnErrorListener;
import android.net.Uri;
import android.os.Handler;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
//import android.webkit.WebChromeClient;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.VideoView;

import java.io.IOException;
//import android.widget.VideoView;

//import java.io.IOException;
Expand All @@ -67,10 +70,10 @@ public class MadvertiseMraidView extends WebView {
private MadvertiseViewCallbackListener mListener;
private AnimationEndListener mAnimationEndListener;
private MadvertiseView mMadView;
private ImageButton mCloseButton;
private boolean mViewable;
private static String mraidJS;

// private VideoView mVideo;
private VideoView mVideo;

public MadvertiseMraidView(Context context, MadvertiseViewCallbackListener listener,
AnimationEndListener animationEndListener, Handler loadingCompletedHandler,
Expand Down Expand Up @@ -104,77 +107,90 @@ public MadvertiseMraidView(Context context) {
addJavascriptInterface(mBridge, "mraid_bridge");

setWebViewClient(new WebViewClient() {
private boolean mError = false;

@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
if (!url.endsWith("mraid.js")) {
if (!url.endsWith("mraid.js") && !mError) {
MadvertiseUtil.logMessage(null, Log.DEBUG, "Setting mraid to default");
checkReady();

// Close button in default size for interstitial ads
if (mPlacementType == MadvertiseUtil.PLACEMENT_TYPE_INTERSTITIAL) {
final ImageButton closeButton = addCloseButtonToViewGroup(((ViewGroup) getParent()));
closeButton.setImageResource(android.R.drawable.ic_menu_close_clear_cancel);
mCloseButton = addCloseButtonToViewGroup(((ViewGroup) getParent()));
mCloseButton
.setImageResource(android.R.drawable.ic_menu_close_clear_cancel);
}
} else {
mError = false;
}
}

@Override
public void onReceivedError(WebView view, int errorCode, String description,
String failingUrl) {
super.onReceivedError(view, errorCode, description, failingUrl);
mError = true;
}
});

// Comment this in to enable video tag-capability.
// this.setWebChromeClient( new WebChromeClient() {
//
// @Override
// public void onShowCustomView(View view, CustomViewCallback callback)
// {
// MadvertiseUtil.logMessage(null, Log.INFO, "showing VideoView");
// super.onShowCustomView(view, callback);
// if (view instanceof FrameLayout) {
// FrameLayout frame = (FrameLayout) view;
// if (frame.getFocusedChild() instanceof VideoView) {
// mVideo = (VideoView) ((FrameLayout) view).getFocusedChild();
// frame.removeView(mVideo);
// ((ViewGroup)getParent()).addView(mVideo);
//
// // Will also be called onError
// mVideo.setOnCompletionListener(new OnCompletionListener() {
//
// @Override
// public void onCompletion(MediaPlayer player) {
// player.stop();
// }
// });
//
// mVideo.setOnErrorListener(new OnErrorListener() {
//
// @Override
// public boolean onError(MediaPlayer mp, int what, int extra) {
// MadvertiseUtil.logMessage(null, Log.WARN,
// "Error while playing video");
//
// if(mListener != null) {
// mListener.onError(new IOException("Error while playing video"));
// }
//
// // We return false in order to call onCompletion()
// return false;
// }
// });
//
// mVideo.start();
// }
// }
// }
//
// @Override
// public void onHideCustomView() {
// if(mVideo != null) {
// ((ViewGroup)getParent()).removeView(mVideo);
// if(mVideo.isPlaying()) {
// mVideo.stopPlayback();
// }
// }
// }
// });
this.setWebChromeClient(new WebChromeClient() {

@Override
public void onShowCustomView(View view, CustomViewCallback callback)
{
MadvertiseUtil.logMessage(null, Log.INFO, "showing VideoView");
super.onShowCustomView(view, callback);
if (view instanceof FrameLayout) {
FrameLayout frame = (FrameLayout) view;
if (frame.getFocusedChild() instanceof VideoView) {
mVideo = (VideoView) ((FrameLayout) view).getFocusedChild();
frame.removeView(mVideo);
((ViewGroup) getParent()).addView(mVideo);

// Will also be called onError
mVideo.setOnCompletionListener(new OnCompletionListener() {

@Override
public void onCompletion(MediaPlayer player) {
player.stop();
}
});

mVideo.setOnErrorListener(new OnErrorListener() {

@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
MadvertiseUtil.logMessage(null, Log.WARN,
"Error while playing video");

if (mListener != null) {
mListener.onError(new IOException("Error while playing video"));
}

// We return false in order to call
// onCompletion()
return false;
}
});

mVideo.start();
}
}
}

@Override
public void onHideCustomView() {
if (mVideo != null) {
((ViewGroup) getParent()).removeView(mVideo);
if (mVideo.isPlaying()) {
mVideo.stopPlayback();
}
}
}
});
}

protected void loadAd(MadvertiseAd ad) {
Expand Down Expand Up @@ -389,11 +405,11 @@ private void resize(final int width, final int height) {
mOriginalParent.removeView(this);
mExpandLayout.addView(this);

final ImageButton closeButton = addCloseButtonToViewGroup(((ViewGroup) getParent()));
closeButton.setId(43);
mCloseButton = addCloseButtonToViewGroup(((ViewGroup) getParent()));
mCloseButton.setId(43);

if (!mExpandProperties.useCustomClose) {
closeButton.setImageResource(android.R.drawable.ic_menu_close_clear_cancel);
mCloseButton.setImageResource(android.R.drawable.ic_menu_close_clear_cancel);
}

content.addView(mExpandLayout);
Expand Down Expand Up @@ -526,6 +542,14 @@ public void readJson(final String json) {
}
if (jsonObject.has(USE_CUSTOM_CLOSE)) {
useCustomClose = jsonObject.getBoolean(USE_CUSTOM_CLOSE);
if (useCustomClose && mCloseButton != null) {
post(new Runnable() {
@Override
public void run() {
mCloseButton.setImageDrawable(null);
}
});
}
}
} catch (JSONException e) {
e.printStackTrace();
Expand All @@ -534,7 +558,7 @@ public void readJson(final String json) {
}

private void checkSizeParams() {
if (width > mMaxWidth || height > mMaxHeight) {
if (width > mMaxWidth || height > mMaxHeight) {
final float ratio = height / (float) width;
// respect the ratio
final int diffWidth = (int) ((float) (width - mMaxWidth) * ratio);
Expand Down

0 comments on commit 9965aab

Please sign in to comment.