Android Open Source - snakk-ads-android-sample-app T V A S T Ad View






From Project

Back to project page snakk-ads-android-sample-app.

License

The source code is released under:

Copyright (c) 2012, Snakk! Media Group All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are m...

If you think the Android project snakk-ads-android-sample-app 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

package com.snakk.vastsdk;
//from ww  w  .  j a  v  a 2  s .  com
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.net.Uri;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import com.snakk.advertising.internal.SnakkAdActivity;

public class TVASTAdView extends WebView {

    private AdViewListener m_listener;
    private boolean isFullscreen = false;
    private boolean isHidden = false;

    private enum State {NEW, LOADING, SHOWN, CLICKED, DONE}

    private State m_state;

    // initial size of the ad creative, not including any borders
    // note, ad may expand to a larger size once user begins to interact
    public TVASTAdView(Context context) {
        super(context);
        init();
    }

    public TVASTAdView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public TVASTAdView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    private void init() {
        setWebViewClient(new AdWebViewClient(getContext()));
        WebChromeClient mWebChromeClient = new WebChromeClient() {
            @Override
            public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
                result.confirm();
                return true;
            }
        };
        setWebChromeClient(mWebChromeClient);
        m_state = State.NEW;
    }

    @Override
    public void loadUrl(String url) {

        boolean isGooglePlay = isGooglePlayUrl(url);

        if (isGooglePlay) {
            openUrlInExternalBrowser(getContext(), url);
            m_state = State.DONE;
        }
        else {
            super.loadUrl(url);
        }
    }

    protected void loadHtml(String html) {
        String baseUrl = TVASTAdsRequest.SERVER_BASE_URL;
        String mimeType = "text/html";
        String encoding = "UTF-8";
        String historyUrl = "about:blank";
        super.loadDataWithBaseURL(baseUrl, html, mimeType, encoding, historyUrl);
    }

    protected void hideWhileLoading() {
        //TODO give user some indication that page is loading...
        if (isHidden) {
            return;
        }
        setVisibility(INVISIBLE);
        isHidden = true;
    }


    protected void expandToFullscreen() {
        if(isFullscreen) {
            return;
        }
        ViewGroup.LayoutParams lp = this.getLayoutParams();
        lp.width = ViewGroup.LayoutParams.MATCH_PARENT;
        lp.height = ViewGroup.LayoutParams.MATCH_PARENT;
        setLayoutParams(lp);
        requestLayout();
        setBackgroundColor(Color.BLACK);
        setVisibility(VISIBLE);
        isFullscreen = true;
    }

    private class AdWebViewClient extends WebViewClient {
        private Context context;

        public AdWebViewClient(Context context) {
            this.context = context;
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {

            if (m_state == State.SHOWN) {
                if (m_listener != null) {
                    m_listener.onClicked(TVASTAdView.this);
                }
                m_state = State.CLICKED;
            }

            boolean isGooglePlay = isGooglePlayUrl(url);

            if (isGooglePlay) {
                openUrlInExternalBrowser(context, url);
                m_state = State.DONE;
                return true;
            }

            return false;
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);

            if (m_state == State.NEW) {
                m_state = State.LOADING;
            }
            else if (m_state == State.CLICKED) {
                hideWhileLoading();
            }
        }

        @Override
        public void onPageFinished(WebView view, String url) {

            if (m_state == State.LOADING) {
                if (m_listener != null) {
                    m_listener.onLoaded(TVASTAdView.this);
                }
                m_state = State.SHOWN;
            } else if (m_state == State.CLICKED) {
                expandToFullscreen();
            }
        }

        @Override
        public void onReceivedError(WebView view, int errorCode,
                                    String description, String failingUrl) {
            super.onReceivedError(view, errorCode, description, failingUrl);

            if (m_listener != null) {
                String error = description + "(" + failingUrl + ")";
                m_listener.onError(TVASTAdView.this, error);
            }
            m_state = State.DONE;
        }
    }

    private boolean isGooglePlayUrl(String url) {
        return url.startsWith("market://details?") ||
                url.startsWith("http://market.android.com/details?") ||
                url.startsWith("https://market.android.com/details?") ||
                url.startsWith("http://play.google.com/store/apps/details?") ||
                url.startsWith("https://play.google.com/store/apps/details?");
    }

    private void openUrlInExternalBrowser(Context context, String url) {
        if (m_listener != null) {
            m_listener.willLeaveApplication(this);
        }
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        if (context instanceof SnakkAdActivity) {
            ((Activity)context).startActivityForResult(intent, 3);
        }
        else {
            context.startActivity (intent);
        }
    }

    public void setAdViewListener(AdViewListener listener) {
        m_listener = listener;
    }

    public interface AdViewListener {
        public void onLoaded(TVASTAdView adView);

        public void onError(TVASTAdView adView, String error);

        public void onClicked(TVASTAdView adview);

        public void willLeaveApplication(TVASTAdView adview);
    }
}




Java Source Code List

com.snakk.advertising.Config.java
com.snakk.advertising.SnakkAdPrompt.java
com.snakk.advertising.SnakkAdRequest.java
com.snakk.advertising.SnakkAdvertising.java
com.snakk.advertising.SnakkBannerAdView.java
com.snakk.advertising.SnakkInterstitialAd.java
com.snakk.advertising.SnakkVideoInterstitialAd.java
com.snakk.advertising.internal.AbstractStatefulAd.java
com.snakk.advertising.internal.AdActivityContentWrapper.java
com.snakk.advertising.internal.AdPromptImpl.java
com.snakk.advertising.internal.AdRequestImpl.java
com.snakk.advertising.internal.BasicWebView.java
com.snakk.advertising.internal.DeviceCapabilities.java
com.snakk.advertising.internal.InterstitialAdImpl.java
com.snakk.advertising.internal.InterstitialBaseView.java
com.snakk.advertising.internal.Sharable.java
com.snakk.advertising.internal.SnakkAdActivity.java
com.snakk.advertising.internal.VastPlayerView.java
com.snakk.advertising.internal.VideoInterstitialAdImpl.java
com.snakk.advertising.package-info.java
com.snakk.adview.AdFullscreenView.java
com.snakk.adview.AdInterstitialBaseView.java
com.snakk.adview.AdInterstitialView.java
com.snakk.adview.AdLog.java
com.snakk.adview.AdOfferWallView.java
com.snakk.adview.AdPrompt.java
com.snakk.adview.AdRequest.java
com.snakk.adview.AdVideoUnitView.java
com.snakk.adview.AdViewCore.java
com.snakk.adview.AdView.java
com.snakk.adview.AutoDetectedParametersSet.java
com.snakk.adview.Base64.java
com.snakk.adview.DeprecatedInterstitialBaseView.java
com.snakk.adview.IMraidCommand.java
com.snakk.adview.LockedOrientationDialog.java
com.snakk.adview.MraidCommand.java
com.snakk.adview.Mraid.java
com.snakk.adview.OldAdActivity.java
com.snakk.adview.Rotate3dAnimation.java
com.snakk.adview.Utils.java
com.snakk.adview.track.EventTracker.java
com.snakk.adview.track.InstallTracker.java
com.snakk.adview.package-info.java
com.snakk.core.SnakkLog.java
com.snakk.vastsdk.TVASTAdErrorEvent.java
com.snakk.vastsdk.TVASTAdErrorListener.java
com.snakk.vastsdk.TVASTAdError.java
com.snakk.vastsdk.TVASTAdType.java
com.snakk.vastsdk.TVASTAdView.java
com.snakk.vastsdk.TVASTAd.java
com.snakk.vastsdk.TVASTAdsLoader.java
com.snakk.vastsdk.TVASTAdsRequest.java
com.snakk.vastsdk.TVASTCompanionAdSlot.java
com.snakk.vastsdk.TVASTCompanionAd.java
com.snakk.vastsdk.TVASTCreative.java
com.snakk.vastsdk.TVASTLinearAd.java
com.snakk.vastsdk.TVASTLinearIcon.java
com.snakk.vastsdk.TVASTMediaFile.java
com.snakk.vastsdk.TVASTNonlinearAd.java
com.snakk.vastsdk.TVASTPostbackTask.java
com.snakk.vastsdk.TVASTUtils.java
com.snakk.vastsdk.TVASTVideoAdsManager.java
com.snakk.vastsdk.player.TVASTPlayer.java
com.snakk.vastsdk.player.TVASTSharable.java
com.snakk.vastsdk.player.TVASTTrackingVideoView.java
com.snakk.vastsdk.player.TVASTVideoProgressThread.java
com.snakkads.tabsswipe.BannerAdPromptFragment.java
com.snakkads.tabsswipe.BannerInterstitialFragment.java
com.snakkads.tabsswipe.BannerRichMediaFragment.java
com.snakkads.tabsswipe.BannerStandardMediaFragment.java
com.snakkads.tabsswipe.BannerVideoFragment.java
com.snakkads.tabsswipe.MainActivity.java
com.snakkads.tabsswipe.SplashScreen.java
com.snakkads.tabsswipe.adapter.TabsPagerAdapter.java
com.yourcompany.AdMobActivity.java
com.yourcompany.SampleActivity.java