public void execute() { AdView adView = mWeakAdView.get(); if (adView == null || adView.isDestroyed()) { return; } adView.setIsLoading(false); MoPubView mpv = adView.mMoPubView; mpv.loadNativeSDK(mParamsMap); }
@Override public void onPageStarted(WebView view, String url, Bitmap favicon) { // If the URL being loaded shares the redirectUrl prefix, open it in the browser. AdView adView = (AdView) view; String redirectUrl = adView.getRedirectUrl(); if (redirectUrl != null && url.startsWith(redirectUrl)) { url = urlWithClickTrackingRedirect(adView, url); view.stopLoading(); showBrowserAfterFollowingRedirectsForUrl(url); } }
/* This helper function is called when a 4XX or 5XX error is received during an ad fetch. * It exponentially increases the parent AdView's refreshTime up to a specified cap. */ private void exponentialBackoff() { if (mAdView == null) { return; } int refreshTimeMilliseconds = mAdView.getRefreshTimeMilliseconds(); refreshTimeMilliseconds = (int) (refreshTimeMilliseconds * EXPONENTIAL_BACKOFF_FACTOR); if (refreshTimeMilliseconds > MAXIMUM_REFRESH_TIME_MILLISECONDS) { refreshTimeMilliseconds = MAXIMUM_REFRESH_TIME_MILLISECONDS; } mAdView.setRefreshTimeMilliseconds(refreshTimeMilliseconds); }
@Override protected void onWindowVisibilityChanged(int visibility) { if (mAdView == null) return; if (visibility == VISIBLE) { Log.d("MoPub", "Ad Unit (" + mAdView.getAdUnitId() + ") going visible: enabling refresh"); mIsInForeground = true; mAdView.setAutorefreshEnabled(true); } else { Log.d("MoPub", "Ad Unit (" + mAdView.getAdUnitId() + ") going invisible: disabling refresh"); mIsInForeground = false; mAdView.setAutorefreshEnabled(false); } }
public void execute() { AdView adView = mWeakAdView.get(); if (adView == null || adView.isDestroyed()) { return; } if (mData == null) { return; } adView.setResponseString(mData); adView.loadDataWithBaseURL( "http://" + adView.getServerHostname() + "/", mData, "text/html", "utf-8", null); }
public boolean getAutorefreshEnabled() { if (mAdView != null) return mAdView.getAutorefreshEnabled(); else { Log.d("MoPub", "Can't get autorefresh status for destroyed MoPubView. " + "Returning false."); return false; } }
private String urlWithClickTrackingRedirect(AdView adView, String url) { String clickthroughUrl = adView.getClickthroughUrl(); if (clickthroughUrl == null) return url; else { String encodedUrl = Uri.encode(url); return clickthroughUrl + "&r=" + encodedUrl; } }
@Override protected void onPostExecute(AdFetchResult result) { if (!isMostCurrentTask()) { Log.d("MoPub", "Ad response is stale."); releaseResources(); return; } // If cleanup() has already been called on the AdView, don't proceed. if (mAdView == null || mAdView.isDestroyed()) { if (result != null) { result.cleanup(); } mAdFetcher.markTaskCompleted(mTaskId); releaseResources(); return; } if (result == null) { if (mException != null) { Log.d("MoPub", "Exception caught while loading ad: " + mException); } mAdView.loadFailUrl(); /* * There are numerous reasons for the ad fetch to fail, but only in the specific * case of actual server failure should we exponentially back off. * * Note: When we call AdView's loadFailUrl() from this block, AdView's mFailUrl * will always be null, forcing a scheduled refresh. Here, we place the exponential * backoff after AdView's loadFailUrl because we only want to increase refresh times * after the first failure refresh timer is scheduled, and not before. */ if (mFetchStatus == FetchStatus.INVALID_SERVER_RESPONSE_BACKOFF) { exponentialBackoff(); mFetchStatus = FetchStatus.NOT_SET; } } else { result.execute(); result.cleanup(); } mAdFetcher.markTaskCompleted(mTaskId); releaseResources(); }
public void execute() { AdView adView = mWeakAdView.get(); if (adView == null || adView.isDestroyed()) { return; } adView.setIsLoading(false); MoPubView moPubView = adView.mMoPubView; if (mParamsMap == null) { Log.i("MoPub", "Couldn't invoke custom event because the server did not specify one."); moPubView.adFailed(); return; } moPubView.loadCustomEvent(mParamsMap); }
protected void registerClick() { if (mAdView != null) { mAdView.registerClick(); // Let any listeners know that an ad was clicked adClicked(); } }
@Override public boolean shouldOverrideUrlLoading(WebView view, String url) { AdView adView = (AdView) view; // Handle the special mopub:// scheme calls. if (url.startsWith("mopub://")) { Uri uri = Uri.parse(url); String host = uri.getHost(); if (host.equals("finishLoad")) adView.adDidLoad(); else if (host.equals("close")) adView.adDidClose(); else if (host.equals("failLoad")) adView.loadFailUrl(); else if (host.equals("custom")) adView.handleCustomIntentFromUri(uri); return true; } // Handle other phone intents. else if (url.startsWith("tel:") || url.startsWith("voicemail:") || url.startsWith("sms:") || url.startsWith("mailto:") || url.startsWith("geo:") || url.startsWith("google.streetview:")) { Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); try { getContext().startActivity(intent); } catch (ActivityNotFoundException e) { Log.w( "MoPub", "Could not handle intent with URI: " + url + ". Is this intent unsupported on your phone?"); } return true; } url = urlWithClickTrackingRedirect(adView, url); Log.d("MoPub", "Ad clicked. Click URL: " + url); mMoPubView.adClicked(); showBrowserAfterFollowingRedirectsForUrl(url); return true; }
/* * Tears down the ad view: no ads will be shown once this method executes. The parent * Activity's onDestroy implementation must include a call to this method. */ public void destroy() { unregisterScreenStateBroadcastReceiver(); if (mAdView != null) { mAdView.cleanup(); mAdView = null; } if (mAdapter != null) { mAdapter.invalidate(); mAdapter = null; } }
public void execute() { AdView adView = mWeakAdView.get(); if (adView == null || adView.isDestroyed()) { return; } adView.setIsLoading(false); MoPubView mpv = adView.mMoPubView; if (mHeader == null) { Log.i("MoPub", "Couldn't call custom method because the server did not specify one."); mpv.loadFailUrl(); return; } String methodName = mHeader.getValue(); Log.i("MoPub", "Trying to call method named " + methodName); Class<? extends Activity> c; Method method; Activity userActivity = mpv.getActivity(); try { c = userActivity.getClass(); method = c.getMethod(methodName, MoPubView.class); method.invoke(userActivity, mpv); } catch (NoSuchMethodException e) { Log.d( "MoPub", "Couldn't perform custom method named " + methodName + "(MoPubView view) because your activity class has no such method"); mpv.loadFailUrl(); return; } catch (Exception e) { Log.d("MoPub", "Couldn't perform custom method named " + methodName); mpv.loadFailUrl(); return; } }
public void customEventDidFailToLoadAd() { if (mAdView != null) mAdView.customEventDidFailToLoadAd(); }
public void customEventActionWillBegin() { if (mAdView != null) mAdView.customEventActionWillBegin(); }
private AdFetchResult fetch(String url) throws Exception { HttpGet httpget = new HttpGet(url); httpget.addHeader("User-Agent", mAdFetcher.mUserAgent); // We check to see if this AsyncTask was cancelled, as per // http://developer.android.com/reference/android/os/AsyncTask.html if (isCancelled()) { mFetchStatus = FetchStatus.FETCH_CANCELLED; return null; } if (mAdView == null || mAdView.isDestroyed()) { Log.d("MoPub", "Error loading ad: AdView has already been GCed or destroyed."); return null; } HttpResponse response = mHttpClient.execute(httpget); HttpEntity entity = response.getEntity(); if (response == null || entity == null) { Log.d("MoPub", "MoPub server returned null response."); mFetchStatus = FetchStatus.INVALID_SERVER_RESPONSE_NOBACKOFF; return null; } final int statusCode = response.getStatusLine().getStatusCode(); // Client and Server HTTP errors should result in an exponential backoff if (statusCode >= 400) { Log.d( "MoPub", "Server error: returned HTTP status code " + Integer.toString(statusCode) + ". Please try again."); mFetchStatus = FetchStatus.INVALID_SERVER_RESPONSE_BACKOFF; return null; } // Other non-200 HTTP status codes should still fail else if (statusCode != HttpStatus.SC_OK) { Log.d( "MoPub", "MoPub server returned invalid response: HTTP status code " + Integer.toString(statusCode) + "."); mFetchStatus = FetchStatus.INVALID_SERVER_RESPONSE_NOBACKOFF; return null; } mAdView.configureAdViewUsingHeadersFromHttpResponse(response); // Ensure that the ad is not warming up. Header warmupHeader = response.getFirstHeader("X-Warmup"); if (warmupHeader != null && warmupHeader.getValue().equals("1")) { Log.d( "MoPub", "Ad Unit (" + mAdView.getAdUnitId() + ") is still warming up. " + "Please try again in a few minutes."); mFetchStatus = FetchStatus.AD_WARMING_UP; return null; } // Ensure that the ad type header is valid and not "clear". Header atHeader = response.getFirstHeader("X-Adtype"); if (atHeader == null || atHeader.getValue().equals("clear")) { Log.d("MoPub", "No inventory found for adunit (" + mAdView.getAdUnitId() + ")."); mFetchStatus = FetchStatus.CLEAR_AD_TYPE; return null; } // Handle custom native ad type. else if (atHeader.getValue().equals("custom")) { Log.i("MoPub", "Performing custom event."); // If applicable, try to invoke the new custom event system (which uses custom classes) Header customEventClassNameHeader = response.getFirstHeader("X-Custom-Event-Class-Name"); if (customEventClassNameHeader != null) { Map<String, String> paramsMap = new HashMap<String, String>(); paramsMap.put("X-Custom-Event-Class-Name", customEventClassNameHeader.getValue()); Header customEventClassDataHeader = response.getFirstHeader("X-Custom-Event-Class-Data"); if (customEventClassDataHeader != null) { paramsMap.put("X-Custom-Event-Class-Data", customEventClassDataHeader.getValue()); } return new PerformCustomEventTaskResult(mAdView, paramsMap); } // Otherwise, use the (deprecated) legacy custom event system for older clients Header oldCustomEventHeader = response.getFirstHeader("X-Customselector"); return new PerformLegacyCustomEventTaskResult(mAdView, oldCustomEventHeader); } // Handle mraid ad type. else if (atHeader.getValue().equals("mraid")) { Log.i("MoPub", "Loading mraid ad"); Map<String, String> paramsMap = new HashMap<String, String>(); paramsMap.put("X-Adtype", atHeader.getValue()); String data = httpEntityToString(entity); paramsMap.put("X-Nativeparams", data); return new LoadNativeAdTaskResult(mAdView, paramsMap); } // Handle native SDK ad type. else if (!atHeader.getValue().equals("html")) { Log.i("MoPub", "Loading native ad"); Map<String, String> paramsMap = new HashMap<String, String>(); paramsMap.put("X-Adtype", atHeader.getValue()); Header npHeader = response.getFirstHeader("X-Nativeparams"); paramsMap.put("X-Nativeparams", "{}"); if (npHeader != null) { paramsMap.put("X-Nativeparams", npHeader.getValue()); } Header ftHeader = response.getFirstHeader("X-Fulladtype"); if (ftHeader != null) { paramsMap.put("X-Fulladtype", ftHeader.getValue()); } return new LoadNativeAdTaskResult(mAdView, paramsMap); } // Handle HTML ad. String data = httpEntityToString(entity); return new LoadHtmlAdTaskResult(mAdView, data); }
public void setAdUnitId(String adUnitId) { if (mAdView != null) mAdView.setAdUnitId(adUnitId); }
public int getAdHeight() { return (mAdView != null) ? mAdView.getAdHeight() : 0; }
public void setKeywords(String keywords) { if (mAdView != null) mAdView.setKeywords(keywords); }
public void setAdContentView(View view) { if (mAdView != null) mAdView.setAdContentView(view); }
public void setLocation(Location location) { if (mAdView != null) mAdView.setLocation(location); }
public void setAutorefreshEnabled(boolean enabled) { if (mAdView != null) mAdView.setAutorefreshEnabled(enabled); }
public Location getLocation() { return (mAdView != null) ? mAdView.getLocation() : null; }
public String getClickthroughUrl() { return (mAdView != null) ? mAdView.getClickthroughUrl() : null; }
public void setTimeout(int milliseconds) { if (mAdView != null) mAdView.setTimeout(milliseconds); }
public String getResponseString() { return (mAdView != null) ? mAdView.getResponseString() : null; }
protected void adAppeared() { if (mAdView != null) mAdView.adAppeared(); }
public String getKeywords() { return (mAdView != null) ? mAdView.getKeywords() : null; }
public void setClickthroughUrl(String url) { if (mAdView != null) mAdView.setClickthroughUrl(url); }
public int getAdWidth() { return (mAdView != null) ? mAdView.getAdWidth() : 0; }