@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); } }
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); }