Home | History | Annotate | Download | only in ads
      1 /*
      2  * Copyright (C) 2011 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.example.training.ads;
     18 
     19 import android.os.Bundle;
     20 import android.support.v4.app.Fragment;
     21 import android.support.v4.app.FragmentActivity;
     22 import android.support.v4.app.FragmentManager;
     23 import android.support.v4.app.FragmentStatePagerAdapter;
     24 import android.support.v4.view.ViewPager;
     25 import android.view.LayoutInflater;
     26 import android.view.MenuItem;
     27 import android.view.View;
     28 import android.view.ViewGroup;
     29 import android.widget.TextView;
     30 
     31 // Ad network-specific imports (AdMob).
     32 import com.google.ads.Ad;
     33 import com.google.ads.AdListener;
     34 import com.google.ads.AdRequest;
     35 import com.google.ads.AdView;
     36 import com.google.ads.AdRequest.ErrorCode;
     37 
     38 public class AdsCatalogActivity extends FragmentActivity {
     39     private static final int NUM_ITEMS = 4;
     40 
     41     // Ad network-specific mechanism to enable test mode.
     42     private static final String TEST_DEVICE_ID = "...";
     43 
     44     PagerAdapter mAdapter;
     45     ViewPager mPager;
     46 
     47     @Override
     48     protected void onCreate(Bundle savedInstanceState) {
     49         super.onCreate(savedInstanceState);
     50         setContentView(R.layout.fragment_pager);
     51 
     52         mAdapter = new PagerAdapter(getSupportFragmentManager());
     53 
     54         mPager = (ViewPager) findViewById(R.id.pager);
     55         mPager.setAdapter(mAdapter);
     56     }
     57 
     58     @Override
     59     public boolean onOptionsItemSelected(MenuItem item) {
     60         if (item.getItemId() == android.R.id.home) {
     61             mPager.setCurrentItem(0);
     62             return true;
     63         }
     64         return super.onOptionsItemSelected(item);
     65     }
     66 
     67     public static class PagerAdapter extends FragmentStatePagerAdapter {
     68         public PagerAdapter(FragmentManager fm) {
     69             super(fm);
     70         }
     71 
     72         @Override
     73         public Fragment getItem(int position) {
     74             return AdFragment.newInstance(position);
     75         }
     76 
     77         @Override
     78         public int getCount() {
     79             return NUM_ITEMS;
     80         }
     81     }
     82 
     83     public static class AdFragment extends Fragment {
     84         private int mNum;
     85         private AdView mAdView;
     86         private TextView mAdStatus;
     87 
     88         static AdFragment newInstance(int num) {
     89             AdFragment af = new AdFragment();
     90 
     91             // Supply num input as an argument.
     92             Bundle args = new Bundle();
     93             args.putInt("num", num);
     94             af.setArguments(args);
     95 
     96             return af;
     97         }
     98 
     99         /**
    100          * When creating, retrieve this instance's number from its arguments.
    101          */
    102         @Override
    103         public void onCreate(Bundle savedInstanceState) {
    104             super.onCreate(savedInstanceState);
    105             Bundle args = getArguments();
    106             mNum = args != null ? args.getInt("num") : 1;
    107         }
    108 
    109         @Override
    110         public View onCreateView(LayoutInflater inflater, ViewGroup container,
    111                 Bundle savedInstanceState) {
    112             // Set up the various ad layouts on different flip pages.
    113             final int[] layouts = {
    114                     R.layout.ad_top,
    115                     R.layout.ad_bottom,
    116                     R.layout.ad_next_to_button,
    117                     R.layout.ad_covers_content };
    118             int layoutId = layouts[mNum];
    119             View v = inflater.inflate(layoutId, container, false);
    120             mAdStatus = (TextView) v.findViewById(R.id.status);
    121             mAdView = (AdView) v.findViewById(R.id.ad);
    122             mAdView.setAdListener(new MyAdListener());
    123 
    124             AdRequest adRequest = new AdRequest();
    125             // adRequest.addKeyword("ad keywords");
    126 
    127             // Ad network-specific mechanism to enable test mode.  Be sure to disable before
    128             // publishing your application.
    129             adRequest.addTestDevice(TEST_DEVICE_ID);
    130             mAdView.loadAd(adRequest);
    131             return v;
    132         }
    133 
    134         @Override
    135         public void onDestroy() {
    136             super.onDestroy();
    137             mAdView.destroy();
    138         }
    139 
    140         // Receives callbacks on various events related to fetching ads.  In this sample,
    141         // the application displays a message on the screen.  A real application may,
    142         // for example, fill the ad with a banner promoting a feature.
    143         private class MyAdListener implements AdListener {
    144 
    145             @Override
    146             public void onDismissScreen(Ad ad) {}
    147 
    148             @Override
    149             public void onFailedToReceiveAd(Ad ad, ErrorCode errorCode) {
    150                 mAdStatus.setText(R.string.error_receive_ad);
    151             }
    152 
    153             @Override
    154             public void onLeaveApplication(Ad ad) {}
    155 
    156             @Override
    157             public void onPresentScreen(Ad ad) {}
    158 
    159             @Override
    160             public void onReceiveAd(Ad ad) { mAdStatus.setText(""); }
    161         }
    162     }
    163 }
    164