Ejemplo n.º 1
0
    public void execute() {
      AdView adView = mWeakAdView.get();
      if (adView == null || adView.isDestroyed()) {
        return;
      }

      adView.setIsLoading(false);
      MoPubView mpv = adView.mMoPubView;
      mpv.loadNativeSDK(mParamsMap);
    }
Ejemplo n.º 2
0
    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);
    }
Ejemplo n.º 3
0
    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);
    }
Ejemplo n.º 4
0
    @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();
    }
Ejemplo n.º 5
0
    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;
      }
    }
Ejemplo n.º 6
0
    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);
    }