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.advertising.internal;
/*fromwww.java2s.com*/import com.snakk.core.SnakkLog;
/**
* logic for tracking ad state, such as ignoring load requests to ads that are
* currently loading, and handling automatic "load and show" functionality when
* end user calls ad.show() before ad.load().
*
* This class is not thread safe.
*/publicabstractclass AbstractStatefulAd {
protectedstaticfinal String TAG = "Snakk";
protectedstaticenum State { NEW, LOADING, LOADED, SHOWN, DONE }
protectedboolean showImmediately = false;
protected State state = State.NEW;
/**
* moves to the next state, if particular state transition is allowed. If
* transition is not allowed, state is not changed.
* @param newState the state to update to
* @return true if state change was successful, false if it was blocked.
*/protectedboolean ratchetState(State newState) {
if (newState.compareTo(state) > 0) {
state = newState;
return true;
}
SnakkLog.d(TAG, "Invalid state transition: " + state + " -> " + newState);
return false;
}
/**
* fill this in w/ the actual ad loading behavior, such as making a request
* to the ad server
*/publicabstractvoid doLoad();
publicvoid load() {
if (ratchetState(State.LOADING)) {
doLoad();
}
elseif (state == State.LOADING) {
// currently loading... do nothing
SnakkLog.d(TAG, "Ignoring attempt to load interstitial... already loading!");
}
else {
// already been loaded... don't reuse interstitials!
SnakkLog.w(TAG, "Ignoring attempt to re-load interstitial.");
}
}
publicboolean isLoaded() {
return (state == State.LOADED);
}
/**
* fill this in w/ the actual ad show behavior, such as displaying ad view,
* or popping up an activity.
*/publicabstractvoid doShow();
publicvoid show() {
switch (state) {
case NEW:
SnakkLog.v(TAG, "Loading ad asynchronously before showing");
load();
// fall through
case LOADING:
// mark to display as soon as interstitial is rdy
showImmediately = true;
break;
case LOADED:
doShow();
ratchetState(State.SHOWN);
break;
case SHOWN:
case DONE:
default:
SnakkLog.w(TAG, "Ignoring attempt to re-use interstitial.");
break;
}
}
}