Android Open Source - Android-SMSDetector Ad Mob Helper






From Project

Back to project page Android-SMSDetector.

License

The source code is released under:

Apache License

If you think the Android project Android-SMSDetector listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/**
 * helper.admob// w  w  w.ja  va2 s .  c  o m
 * AdMobHelper.java
 * 
 * @author Barby
 * 04/02/2012
 */

//
//  Licensed to the Apache Software Foundation (ASF) under one
//  or more contributor license agreements.  See the NOTICE file
//  distributed with this work for additional information
//  regarding copyright ownership.  The ASF licenses this file
//  to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance
//  with the License.  You may obtain a copy of the License at
//
//  http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing,
//  software distributed under the License is distributed on an
//  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
//  KIND, either express or implied.  See the License for the
//  specific language governing permissions and limitations
//  under the License.
//

package helpers.admob;

import java.util.Hashtable;

import messages.detector.R;
import android.app.Activity;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;

import com.google.ads.Ad;
import com.google.ads.AdListener;
import com.google.ads.AdRequest;
import com.google.ads.AdRequest.ErrorCode;
import com.google.ads.AdSize;
import com.google.ads.AdView;
import com.google.ads.InterstitialAd;



/**
 * @author Barby
 * 
 */
public class AdMobHelper implements AdListener {
  private static AdMobHelper instance = null;
  private InterstitialAd interstitial;

  private Hashtable<View, AdView> viewAdViewMapping = null;

  private AdMobHelper() {
    // initializations
    viewAdViewMapping = new Hashtable<View, AdView>();
  }

  private synchronized static void createInstance() {
    if (instance == null) {
      instance = new AdMobHelper();
    }
  }

  public static AdMobHelper getInstance() {
    if (instance == null) {
      createInstance();
    }
    return instance;
  }

  public void initInterstitialAdForActivity(Activity activity) {
    if (interstitial != null) {
      interstitial.stopLoading();
      interstitial = null;
    }
    interstitial = new InterstitialAd(activity, activity
        .getApplicationContext().getString(
            R.string.ADMOB_PUBLISHER_INTERSTITIAL_ID));

    AdRequest adRequest = new AdRequest();
    adRequest.addTestDevice(AdRequest.TEST_EMULATOR);
    interstitial.loadAd(adRequest);
    interstitial.setAdListener(this);

  }
  
  public void removeInterstitial()
  {
    if (interstitial != null) {
      interstitial.stopLoading();
      interstitial = null;
    }
  }

  public void addNewAdViewToView(ViewGroup parentView, Activity inActivity) {
    AdView view = viewAdViewMapping.get(parentView);

    // if there are no adViews yet
    if (view == null) {
      view = new AdView(inActivity, AdSize.BANNER, inActivity
          .getApplicationContext().getString(
              R.string.ADMOB_PUBLISHER_ID));
      view.setGravity(Gravity.BOTTOM);

      viewAdViewMapping.put(parentView, view);
      parentView.addView(view);

      // Initiate a generic request to load it with an ad
      AdRequest adRequest = new AdRequest();
      view.loadAd(adRequest);
    }
  }

  public void removeAdViewForView(ViewGroup parentView) {
    AdView view = viewAdViewMapping.get(parentView);

    if (view != null) {
      viewAdViewMapping.remove(parentView);
      parentView.removeView(view);
      view.destroy();
      view = null;
    }
  }

  /*
   * (non-Javadoc)
   * 
   * @see com.google.ads.AdListener#onDismissScreen(com.google.ads.Ad)
   */
  public void onDismissScreen(Ad arg0) {
    // TODO Auto-generated method stub

  }

  /*
   * (non-Javadoc)
   * 
   * @see com.google.ads.AdListener#onFailedToReceiveAd(com.google.ads.Ad,
   * com.google.ads.AdRequest.ErrorCode)
   */
  public void onFailedToReceiveAd(Ad arg0, ErrorCode arg1) {
    // TODO Auto-generated method stub

  }

  /*
   * (non-Javadoc)
   * 
   * @see com.google.ads.AdListener#onLeaveApplication(com.google.ads.Ad)
   */
  public void onLeaveApplication(Ad arg0) {
    // TODO Auto-generated method stub

  }

  /*
   * (non-Javadoc)
   * 
   * @see com.google.ads.AdListener#onPresentScreen(com.google.ads.Ad)
   */
  public void onPresentScreen(Ad arg0) {
    // TODO Auto-generated method stub

  }

  /*
   * (non-Javadoc)
   * 
   * @see com.google.ads.AdListener#onReceiveAd(com.google.ads.Ad)
   */
  public void onReceiveAd(Ad arg0) {

    if (interstitial.isReady()) {
      interstitial.show();
    }

  }

}




Java Source Code List

exceptions.EmptyArgumentException.java
exceptions.InvalidResourceIdException.java
exceptions.PreviouslyInitializedVariableException.java
fiesta.share.ShareActivity.java
fiesta.smsreceiver.SMSReceiver.java
helpers.admob.AdMobHelper.java
helpers.audio.AudioHelper.java
helpers.facebook.BaseDialogListener.java
helpers.facebook.BaseRequestListener.java
helpers.facebook.LoginButton.java
helpers.facebook.SessionEvents.java
helpers.facebook.SessionStore.java
helpers.fonts.FontsHelper.java
messages.detector.ImportantMessagesDetectorActivity.java