MD5 校验值:27cf44ec6e0720408f5ef017a90a3331
MraidBridge.java 文件包含反编译后的源代码,请注意,该内容仅供学习和参考使用,不得用于非法用途。
package com.mopub.mraid; import android.content.ActivityNotFoundException; import android.content.Context; import android.content.Intent; import android.graphics.Rect; import android.net.Uri; import android.os.Build; import android.text.TextUtils; import android.view.MotionEvent; import android.view.View; import android.webkit.ConsoleMessage; import android.webkit.JsResult; import android.webkit.WebChromeClient; import android.webkit.WebView; import android.webkit.WebViewClient; import com.facebook.ads.AudienceNetworkActivity; import com.jb.ga0.commerce.util.DevHelper; import com.mopub.common.AdReport; import com.mopub.common.AdType; import com.mopub.common.CloseableLayout; import com.mopub.common.Constants; import com.mopub.common.VisibleForTesting; import com.mopub.common.logging.MoPubLog; import com.mopub.mobileads.BaseWebView; import com.mopub.mobileads.VastIconXmlManager; import com.mopub.mobileads.ViewGestureDetector; import com.mopub.mraid.MraidNativeCommandHandler; import com.mopub.network.Networking; import java.net.URI; import java.net.URISyntaxException; import java.util.HashMap; import java.util.Map; import org.apache.http.NameValuePair; import org.apache.http.client.utils.URLEncodedUtils; import org.json.JSONObject; public class MraidBridge { private final AdReport a; private final PlacementType b; private final MraidNativeCommandHandler c; private MraidBridgeListener d; private MraidWebView e; private boolean f; private boolean g; private final WebViewClient h; public interface MraidBridgeListener { void onClose(); boolean onConsoleMessage(ConsoleMessage consoleMessage); void onExpand(URI uri, boolean z) throws MraidCommandException; boolean onJsAlert(String str, JsResult jsResult); void onOpen(URI uri); void onPageFailedToLoad(); void onPageLoaded(); void onPlayVideo(URI uri); void onResize(int i, int i2, int i3, int i4, CloseableLayout.ClosePosition closePosition, boolean z) throws MraidCommandException; void onSetOrientationProperties(boolean z, MraidOrientation mraidOrientation) throws MraidCommandException; void onUseCustomClose(boolean z); void onVisibilityChanged(boolean z); } public MraidBridge(AdReport adReport, PlacementType placementType) { this(adReport, placementType, new MraidNativeCommandHandler()); } @VisibleForTesting MraidBridge(AdReport adReport, PlacementType placementType, MraidNativeCommandHandler mraidNativeCommandHandler) { this.h = new MraidWebViewClient() { @Override public boolean shouldOverrideUrlLoading(WebView webView, String str) { return MraidBridge.this.b(str); } @Override public void onPageFinished(WebView webView, String str) { MraidBridge.this.f(); } @Override public void onReceivedError(WebView webView, int i, String str, String str2) { MoPubLog.d("Error: " + str); super.onReceivedError(webView, i, str, str2); } }; this.a = adReport; this.b = placementType; this.c = mraidNativeCommandHandler; } public void a(MraidBridgeListener mraidBridgeListener) { this.d = mraidBridgeListener; } public void a(MraidWebView mraidWebView) { this.e = mraidWebView; this.e.getSettings().setJavaScriptEnabled(true); if (Build.VERSION.SDK_INT >= 17 && this.b == PlacementType.INTERSTITIAL) { mraidWebView.getSettings().setMediaPlaybackRequiresUserGesture(false); } this.e.setScrollContainer(false); this.e.setVerticalScrollBarEnabled(false); this.e.setHorizontalScrollBarEnabled(false); this.e.setBackgroundColor(-16777216); this.e.setWebViewClient(this.h); this.e.setWebChromeClient(new WebChromeClient() { @Override public boolean onJsAlert(WebView webView, String str, String str2, JsResult jsResult) { return MraidBridge.this.d != null ? MraidBridge.this.d.onJsAlert(str2, jsResult) : super.onJsAlert(webView, str, str2, jsResult); } @Override public boolean onConsoleMessage(ConsoleMessage consoleMessage) { return MraidBridge.this.d != null ? MraidBridge.this.d.onConsoleMessage(consoleMessage) : super.onConsoleMessage(consoleMessage); } @Override public void onShowCustomView(View view, WebChromeClient.CustomViewCallback customViewCallback) { super.onShowCustomView(view, customViewCallback); } }); final ViewGestureDetector viewGestureDetector = new ViewGestureDetector(this.e.getContext(), this.e, this.a); viewGestureDetector.setUserClickListener(new ViewGestureDetector.UserClickListener() { @Override public void onUserClick() { MraidBridge.this.f = true; } @Override public void onResetUserClick() { MraidBridge.this.f = false; } @Override public boolean wasClicked() { return MraidBridge.this.f; } }); this.e.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View view, MotionEvent motionEvent) { viewGestureDetector.sendTouchEvent(motionEvent); switch (motionEvent.getAction()) { case 0: case 1: if (!view.hasFocus()) { view.requestFocus(); return false; } return false; default: return false; } } }); this.e.a(new MraidWebView.OnVisibilityChangedListener() { @Override public void onVisibilityChanged(boolean z) { if (MraidBridge.this.d != null) { MraidBridge.this.d.onVisibilityChanged(z); } } }); } public void a() { this.e = null; } public void setContentHtml(String str) { if (this.e == null) { MoPubLog.d("MRAID bridge called setContentHtml before WebView was attached"); } else { this.g = false; this.e.loadDataWithBaseURL(Networking.getBaseUrlScheme() + "://" + Constants.HOST + "/", str, AudienceNetworkActivity.WEBVIEW_MIME_TYPE, "UTF-8", null); } } public void setContentUrl(String str) { if (this.e == null) { MoPubLog.d("MRAID bridge called setContentHtml while WebView was not attached"); } else { this.g = false; this.e.loadUrl(str); } } public void a(String str) { if (this.e == null) { MoPubLog.d("Attempted to inject Javascript into MRAID WebView while was not attached:\n\t" + str); } else { MoPubLog.v("Injecting Javascript into MRAID WebView:\n\t" + str); this.e.loadUrl("javascript:" + str); } } public void a(MraidJavascriptCommand mraidJavascriptCommand, String str) { a("window.mraidbridge.notifyErrorEvent(" + JSONObject.quote(mraidJavascriptCommand.toJavascriptString()) + ", " + JSONObject.quote(str) + ")"); } private void a(MraidJavascriptCommand mraidJavascriptCommand) { a("window.mraidbridge.nativeCallComplete(" + JSONObject.quote(mraidJavascriptCommand.toJavascriptString()) + ")"); } public static class MraidWebView extends BaseWebView { private OnVisibilityChangedListener a; private boolean b; public interface OnVisibilityChangedListener { void onVisibilityChanged(boolean z); } public MraidWebView(Context context) { super(context); this.b = getVisibility() == 0; } void a(OnVisibilityChangedListener onVisibilityChangedListener) { this.a = onVisibilityChangedListener; } @Override protected void onVisibilityChanged(View view, int i) { super.onVisibilityChanged(view, i); boolean z = i == 0; if (z != this.b) { this.b = z; if (this.a != null) { this.a.onVisibilityChanged(this.b); } } } public boolean isVisible() { return this.b; } } @VisibleForTesting boolean b(String str) { boolean z = false; try { URI uri = new URI(str); String scheme = uri.getScheme(); String host = uri.getHost(); if ("mopub".equals(scheme)) { if ("failLoad".equals(host) && this.b == PlacementType.INLINE && this.d != null) { this.d.onPageFailedToLoad(); } return true; } if (AdType.MRAID.equals(scheme)) { HashMap hashMap = new HashMap(); for (NameValuePair nameValuePair : URLEncodedUtils.parse(uri, "UTF-8")) { hashMap.put(nameValuePair.getName(), nameValuePair.getValue()); } MraidJavascriptCommand fromJavascriptString = MraidJavascriptCommand.fromJavascriptString(host); try { a(fromJavascriptString, hashMap); } catch (MraidCommandException e) { a(fromJavascriptString, e.getMessage()); } a(fromJavascriptString); return true; } if (!this.f) { return false; } Intent intent = new Intent(); intent.setAction("android.intent.action.VIEW"); intent.setData(Uri.parse(str)); intent.addFlags(268435456); try { if (this.e == null) { MoPubLog.d("WebView was detached. Unable to load a URL"); z = true; } else { this.e.getContext().startActivity(intent); z = true; } return z; } catch (ActivityNotFoundException e2) { MoPubLog.d("No activity found to handle this URL " + str); return z; } } catch (URISyntaxException e3) { MoPubLog.w("Invalid MRAID URL: " + str); a(MraidJavascriptCommand.UNSPECIFIED, "Mraid command sent an invalid URL"); return true; } } @VisibleForTesting public void f() { if (!this.g) { this.g = true; if (this.d != null) { this.d.onPageLoaded(); } } } @VisibleForTesting void a(final MraidJavascriptCommand mraidJavascriptCommand, Map<String, String> map) throws MraidCommandException { if (mraidJavascriptCommand.requiresClick(this.b) && !this.f) { throw new MraidCommandException("Cannot execute this command unless the user clicks"); } if (this.d == null) { throw new MraidCommandException("Invalid state to execute this command"); } if (this.e == null) { throw new MraidCommandException("The current WebView is being destroyed"); } switch (mraidJavascriptCommand) { case CLOSE: this.d.onClose(); return; case RESIZE: this.d.onResize(a(c(map.get(VastIconXmlManager.WIDTH)), 0, 100000), a(c(map.get(VastIconXmlManager.HEIGHT)), 0, 100000), a(c(map.get("offsetX")), -100000, 100000), a(c(map.get("offsetY")), -100000, 100000), a(map.get("customClosePosition"), CloseableLayout.ClosePosition.TOP_RIGHT), a(map.get("allowOffscreen"), true)); return; case EXPAND: this.d.onExpand(a(map.get("url"), (URI) null), a(map.get("shouldUseCustomClose"), false)); return; case USE_CUSTOM_CLOSE: this.d.onUseCustomClose(a(map.get("shouldUseCustomClose"), false)); return; case OPEN: this.d.onOpen(f(map.get("url"))); return; case SET_ORIENTATION_PROPERTIES: this.d.onSetOrientationProperties(e(map.get("allowOrientationChange")), d(map.get("forceOrientation"))); return; case PLAY_VIDEO: this.d.onPlayVideo(f(map.get("uri"))); return; case STORE_PICTURE: this.c.a(this.e.getContext(), f(map.get("uri")).toString(), new MraidNativeCommandHandler.c() { @Override public void onFailure(MraidCommandException mraidCommandException) { MraidBridge.this.a(mraidJavascriptCommand, mraidCommandException.getMessage()); } }); return; case CREATE_CALENDAR_EVENT: this.c.a(this.e.getContext(), map); return; case UNSPECIFIED: throw new MraidCommandException("Unspecified MRAID Javascript command"); default: return; } } private CloseableLayout.ClosePosition a(String str, CloseableLayout.ClosePosition closePosition) throws MraidCommandException { if (!TextUtils.isEmpty(str)) { if (str.equals("top-left")) { return CloseableLayout.ClosePosition.TOP_LEFT; } if (str.equals("top-right")) { return CloseableLayout.ClosePosition.TOP_RIGHT; } if (str.equals("center")) { return CloseableLayout.ClosePosition.CENTER; } if (str.equals("bottom-left")) { return CloseableLayout.ClosePosition.BOTTOM_LEFT; } if (str.equals("bottom-right")) { return CloseableLayout.ClosePosition.BOTTOM_RIGHT; } if (str.equals("top-center")) { return CloseableLayout.ClosePosition.TOP_CENTER; } if (str.equals("bottom-center")) { return CloseableLayout.ClosePosition.BOTTOM_CENTER; } throw new MraidCommandException("Invalid close position: " + str); } return closePosition; } private int c(String str) throws MraidCommandException { try { return Integer.parseInt(str, 10); } catch (NumberFormatException e) { throw new MraidCommandException("Invalid numeric parameter: " + str); } } private MraidOrientation d(String str) throws MraidCommandException { if ("portrait".equals(str)) { return MraidOrientation.PORTRAIT; } if ("landscape".equals(str)) { return MraidOrientation.LANDSCAPE; } if ("none".equals(str)) { return MraidOrientation.NONE; } throw new MraidCommandException("Invalid orientation: " + str); } private int a(int i, int i2, int i3) throws MraidCommandException { if (i < i2 || i > i3) { throw new MraidCommandException("Integer parameter out of range: " + i); } return i; } private boolean a(String str, boolean z) throws MraidCommandException { return str == null ? z : e(str); } private boolean e(String str) throws MraidCommandException { if (DevHelper.sVALUE_TRUE.equals(str)) { return true; } if (DevHelper.sVALUE_FALSE.equals(str)) { return false; } throw new MraidCommandException("Invalid boolean parameter: " + str); } private URI a(String str, URI uri) throws MraidCommandException { return str == null ? uri : f(str); } private URI f(String str) throws MraidCommandException { if (str == null) { throw new MraidCommandException("Parameter cannot be null"); } try { return new URI(str); } catch (URISyntaxException e) { throw new MraidCommandException("Invalid URL parameter: " + str); } } public void a(boolean z) { a("mraidbridge.setIsViewable(" + z + ")"); } public void a(PlacementType placementType) { a("mraidbridge.setPlacementType(" + JSONObject.quote(placementType.toJavascriptString()) + ")"); } public void a(ViewState viewState) { a("mraidbridge.setState(" + JSONObject.quote(viewState.toJavascriptString()) + ")"); } public void a(boolean z, boolean z2, boolean z3, boolean z4, boolean z5) { a("mraidbridge.setSupports(" + z + "," + z2 + "," + z3 + "," + z4 + "," + z5 + ")"); } private String a(Rect rect) { return rect.left + "," + rect.top + "," + rect.width() + "," + rect.height(); } private String b(Rect rect) { return rect.width() + "," + rect.height(); } public void notifyScreenMetrics(a aVar) { a("mraidbridge.setScreenSize(" + b(aVar.a()) + ");mraidbridge.setMaxSize(" + b(aVar.c()) + ");mraidbridge.setCurrentPosition(" + a(aVar.d()) + ");mraidbridge.setDefaultPosition(" + a(aVar.f()) + ")"); a("mraidbridge.notifySizeChangeEvent(" + b(aVar.d()) + ")"); } public void b() { a("mraidbridge.notifyReadyEvent();"); } public boolean c() { return this.e != null && this.e.isVisible(); } public boolean d() { return this.e != null; } public boolean e() { return this.g; } }