Ads.java :  » UnTagged » easy-dialer » com » uip » EasyDialer » Android Open Source

Android Open Source » UnTagged » easy dialer 
easy dialer » com » uip » EasyDialer » Ads.java
package com.uip.EasyDialer;
//import com.uip.EasyDialer.paid.R;
import java.util.Random;

import android.app.Activity;
import android.os.Handler;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.TranslateAnimation;
import android.view.animation.Animation.AnimationListener;
import android.widget.ViewFlipper;
import android.widget.TableLayout.LayoutParams;

import com.admob.android.ads.AdView;

public class Ads {
  private static Activity mParent;
  private static AdView mAdView;
  private static String[] KEYWORDS = {"casino", "insurance", "hotel", "mortgage", "credit", "debt"}; 
  private static Random rnd = new Random();
  
  public static void Init(Activity a){
    mParent = a;
    mAdView = (AdView)mParent.findViewById(R.id.ads);
    
    mAdView.setSearchQuery(KEYWORDS[rnd.nextInt(KEYWORDS.length)]);
    
    final Handler handler = new Handler();
    final Runnable runnable = new Runnable() {
       private boolean shownOnce = false;
       public void run() {
           
         if (!shownOnce && mAdView.hasAd()){
           SlideAd(800, 0, 1);
           handler.postDelayed(this, 10000); //remove the add in 10 seconds
         }
         else {
           if (mAdView.hasAd()) SlideAd(800, 1, 0);
             handler.removeCallbacks(this);
         }
         shownOnce = true;
       }
        };
        handler.postDelayed(runnable, 1000); // check if add loaded in 1 seconds 
  }
  
    private static void SlideAd(int duration, float from, float to) {
    final ViewFlipper flipper = (ViewFlipper)mParent.findViewById(R.id.flipper);
    
        Animation slide = new TranslateAnimation(
            Animation.ABSOLUTE, 0, 
            Animation.ABSOLUTE, 0, 
            Animation.RELATIVE_TO_SELF, from,
            Animation.RELATIVE_TO_SELF, to);
        slide.setDuration(duration);
        slide.setFillAfter(true);
        slide.setInterpolator(new AccelerateInterpolator());

    AlphaAnimation fade = new AlphaAnimation(from, to);
    fade.setDuration(duration);
    
    AnimationSet slideAndFade = new AnimationSet(true);
    slideAndFade.setFillAfter(true);
    slideAndFade.addAnimation(slide);
    slideAndFade.addAnimation(fade);
    slideAndFade.setInterpolator(new AccelerateInterpolator());
    if (to > from) {
      slideAndFade.setAnimationListener(new AnimationListener() {
        
        public void onAnimationStart(Animation animation) {}
        public void onAnimationRepeat(Animation animation) {}
        
        public void onAnimationEnd(Animation animation) {
          flipper.clearAnimation();
          mAdView.clearAnimation();
          LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
          mAdView.setLayoutParams(lp);
        }
      });
    }
    else {
      LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
      lp.setMargins(0, -48, 0, 0);
      mAdView.setLayoutParams(lp);
    }
    
        mAdView.startAnimation(slideAndFade);
        flipper.startAnimation(slide);
    }

}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.