com.sponsorpay.sdk.android.publisher
Class InterstitialLoader

java.lang.Object
  extended by com.sponsorpay.sdk.android.publisher.InterstitialLoader

public class InterstitialLoader
extends java.lang.Object

Performs the request for the interstitial in the background, notifies a registered listener of the results and shows the InterstitialActivity if and ad is returned.

Shows a progress dialog on top of the calling activity while it contacts the SponsorPayÕs servers to determine whether thereÕs an interstitial available.

If an ad is available, it will continue showing the loading progress dialog while the ad contents are loaded in the background. The entire process must happen in less than 5 seconds (or a custom amount of time that can be configured, see setLoadingTimeoutSecs(int)). Otherwise a timeout error will be triggered and the loading progress dialog will disappear without showing the ad.

When the contents of the ad finish loading, the loading progress view dialog will disappear and the interstitial will be shown on the screen (by showing the InterstitialActivity).

If no ad is available for this request, the loading progress dialog will disappear too and the process will be completed.

An InterstitialLoader.InterstitialLoadingStatusListener can be defined which will be notified of interesting events in the life of the interstitial.


Nested Class Summary
 class InterstitialLoader.InterstitialLoadingAsyncTask
           Requests and loads an interstitial ad in the background.
private  class InterstitialLoader.InterstitialLoadingResults
          Encloses the interesting data from the server's response to the interstitial request.
static interface InterstitialLoader.InterstitialLoadingStatusListener
          Interface to be implemented by parties interested in being notified of interesting events in the life of the interstitial.
 
Field Summary
private static java.lang.String INTERSTITIAL_PRODUCTION_BASE_URL
           
private static java.lang.String INTERSTITIAL_PRODUCTION_DOMAIN
           
private static java.lang.String INTERSTITIAL_STAGING_BASE_URL
           
private static java.lang.String INTERSTITIAL_STAGING_DOMAIN
           
private static int LOADING_TIMEOUT_SECONDS_DEFAULT
           
private  java.lang.String mBackgroundUrl
           
private  Activity mCallingActivity
          The activity which created this InterstitialLoader instance.
private  java.lang.Runnable mCancelLoadingOnTimeOut
           
private  Handler mHandler
           
private  HostInfo mHostInfo
           
private static int MILLISECONDS_IN_SECOND
           
private  InterstitialLoader.InterstitialLoadingAsyncTask mInterstitialLoadingAsyncTask
           
private  InterstitialLoader.InterstitialLoadingStatusListener mLoadingStatusListener
           
private  int mLoadingTimeoutSecs
           
private  ProgressDialog mProgressDialog
          Loading progress dialog.
private  boolean mShouldStayOpen
           
private  java.lang.String mSkinName
           
private  java.lang.String mUserId
           
private static boolean SHOULD_INTERSTITIAL_REMAIN_OPEN_DEFAULT
           
private static int sInterstitialAvailableResponseCount
          Static counter which gets incremented every time an interstitial ad is returned.
private static java.lang.String SKIN_NAME_DEFAULT
           
private static java.lang.String URL_PARAM_ALLOW_CAMPAIGN_KEY
           
private static java.lang.String URL_PARAM_ALLOW_CAMPAIGN_VALUE_ON
           
private static java.lang.String URL_PARAM_BACKGROUND_KEY
           
private static java.lang.String URL_PARAM_INTERSTITIAL_KEY
           
private static java.lang.String URL_PARAM_INTERSTITIAL_VALUE_ON
           
private static java.lang.String URL_PARAM_OFFSET_KEY
           
private static java.lang.String URL_PARAM_SKIN_KEY
           
private static java.lang.String USER_AGENT_HEADER_NAME
           
private static java.lang.String USER_AGENT_HEADER_VALUE
           
 
Constructor Summary
InterstitialLoader(Activity callingActivity, java.lang.String userId, HostInfo hostInfo, InterstitialLoader.InterstitialLoadingStatusListener loadingStatusListener)
          Initializes a new IntestitialLoader instance.
 
Method Summary
private  void cancelInterstitialLoading()
          Cancels the request and loading of the interstitial if the InterstitialActivity has still not been launched.
static boolean isInterstitialAvailableAccordingToStatusCode(int statusCode)
          Takes an HTTP status code and returns whether it means that an interstitial ad is available.
private  void launchInterstitialActivity(InterstitialLoader.InterstitialLoadingResults result)
          Launchs the InterstitialActivity with the initial contents of the interstitial ad, the initial base URL for images, scripts and other dependencies, and the desired behavior for staying open after user redirection.
protected  void onInterstitialLoadResultsAvailable(InterstitialLoader.InterstitialLoadingResults result)
          Called when the results of the interstitial request are available, i.e.
 void setBackgroundUrl(java.lang.String backgroundUrl)
          Can be set to the absolute URL of an image to use as background graphic for the interstitial.
 void setLoadingTimeoutSecs(int loadingTimeoutSecs)
          Sets the maximum amount of time the interstitial should take to load.
 void setShouldStayOpen(boolean shouldStayOpen)
          Used to specify the behavior of the interstitial once the user clicks on the presented ad and is redirected outside the host publisher app.
 void setSkinName(java.lang.String skinName)
          Used to specify the name of a custom skin or template for the requested interstitial.
 void startLoading()
           Triggers the loading of the interstitial with the configured parameters.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

SHOULD_INTERSTITIAL_REMAIN_OPEN_DEFAULT

private static final boolean SHOULD_INTERSTITIAL_REMAIN_OPEN_DEFAULT
See Also:
Constant Field Values

LOADING_TIMEOUT_SECONDS_DEFAULT

private static final int LOADING_TIMEOUT_SECONDS_DEFAULT
See Also:
Constant Field Values

SKIN_NAME_DEFAULT

private static final java.lang.String SKIN_NAME_DEFAULT
See Also:
Constant Field Values

INTERSTITIAL_PRODUCTION_BASE_URL

private static final java.lang.String INTERSTITIAL_PRODUCTION_BASE_URL
See Also:
Constant Field Values

INTERSTITIAL_STAGING_BASE_URL

private static final java.lang.String INTERSTITIAL_STAGING_BASE_URL
See Also:
Constant Field Values

INTERSTITIAL_PRODUCTION_DOMAIN

private static final java.lang.String INTERSTITIAL_PRODUCTION_DOMAIN
See Also:
Constant Field Values

INTERSTITIAL_STAGING_DOMAIN

private static final java.lang.String INTERSTITIAL_STAGING_DOMAIN
See Also:
Constant Field Values

URL_PARAM_INTERSTITIAL_KEY

private static final java.lang.String URL_PARAM_INTERSTITIAL_KEY
See Also:
Constant Field Values

URL_PARAM_INTERSTITIAL_VALUE_ON

private static final java.lang.String URL_PARAM_INTERSTITIAL_VALUE_ON
See Also:
Constant Field Values

URL_PARAM_ALLOW_CAMPAIGN_KEY

private static final java.lang.String URL_PARAM_ALLOW_CAMPAIGN_KEY
See Also:
Constant Field Values

URL_PARAM_ALLOW_CAMPAIGN_VALUE_ON

private static final java.lang.String URL_PARAM_ALLOW_CAMPAIGN_VALUE_ON
See Also:
Constant Field Values

URL_PARAM_SKIN_KEY

private static final java.lang.String URL_PARAM_SKIN_KEY
See Also:
Constant Field Values

URL_PARAM_OFFSET_KEY

private static final java.lang.String URL_PARAM_OFFSET_KEY
See Also:
Constant Field Values

URL_PARAM_BACKGROUND_KEY

private static final java.lang.String URL_PARAM_BACKGROUND_KEY
See Also:
Constant Field Values

USER_AGENT_HEADER_NAME

private static java.lang.String USER_AGENT_HEADER_NAME

USER_AGENT_HEADER_VALUE

private static java.lang.String USER_AGENT_HEADER_VALUE

MILLISECONDS_IN_SECOND

private static final int MILLISECONDS_IN_SECOND
See Also:
Constant Field Values

sInterstitialAvailableResponseCount

private static int sInterstitialAvailableResponseCount
Static counter which gets incremented every time an interstitial ad is returned.


mCallingActivity

private Activity mCallingActivity
The activity which created this InterstitialLoader instance. Used to launch the InterstitialActivity and to attach the loading progress dialog to.


mUserId

private java.lang.String mUserId

mHostInfo

private HostInfo mHostInfo

mLoadingStatusListener

private InterstitialLoader.InterstitialLoadingStatusListener mLoadingStatusListener

mBackgroundUrl

private java.lang.String mBackgroundUrl

mSkinName

private java.lang.String mSkinName

mShouldStayOpen

private boolean mShouldStayOpen

mLoadingTimeoutSecs

private int mLoadingTimeoutSecs

mInterstitialLoadingAsyncTask

private InterstitialLoader.InterstitialLoadingAsyncTask mInterstitialLoadingAsyncTask

mCancelLoadingOnTimeOut

private java.lang.Runnable mCancelLoadingOnTimeOut

mHandler

private Handler mHandler

mProgressDialog

private ProgressDialog mProgressDialog
Loading progress dialog.

Constructor Detail

InterstitialLoader

public InterstitialLoader(Activity callingActivity,
                          java.lang.String userId,
                          HostInfo hostInfo,
                          InterstitialLoader.InterstitialLoadingStatusListener loadingStatusListener)
Initializes a new IntestitialLoader instance.

Parameters:
callingActivity - The activity from which the loading of the interstitial is requested.
userId - The current user of the host application.
hostInfo - HostInfo with information from the host device and publisher application.
loadingStatusListener - InterstitialLoader.InterstitialLoadingStatusListener to register to be notified of events in the interstitial lifecycle.
Method Detail

setBackgroundUrl

public void setBackgroundUrl(java.lang.String backgroundUrl)
Can be set to the absolute URL of an image to use as background graphic for the interstitial. Must include the protocol scheme (http:// or https://) at the beginning of the URL. Leave it null for no custom background.

Parameters:
backgroundUrl -

setShouldStayOpen

public void setShouldStayOpen(boolean shouldStayOpen)
Used to specify the behavior of the interstitial once the user clicks on the presented ad and is redirected outside the host publisher app. The default behavior is to close the interstitial and let the user go back to the activity that called the interstitial when they come back to the app. If you want the interstitial not to close until the user does it explicitly, set this parameter to true.

Parameters:
shouldStayOpen -

setSkinName

public void setSkinName(java.lang.String skinName)
Used to specify the name of a custom skin or template for the requested interstitial. Leaving it null will make the interstitial fall back to the DEFAULT template.

Parameters:
skinName -

setLoadingTimeoutSecs

public void setLoadingTimeoutSecs(int loadingTimeoutSecs)
Sets the maximum amount of time the interstitial should take to load. If you set it to 0 or a negative number, it will fall back to the default value of 5 seconds.

Parameters:
loadingTimeoutSecs -

startLoading

public void startLoading()

Triggers the loading of the interstitial with the configured parameters.

The process will be performed in a background thread. The invocation of the InterstitialLoader.InterstitialLoadingStatusListener registered in InterstitialLoader(Activity, String, HostInfo, InterstitialLoadingStatusListener) will be done in the calling thread.


cancelInterstitialLoading

private void cancelInterstitialLoading()
Cancels the request and loading of the interstitial if the InterstitialActivity has still not been launched. Will dismiss the loading progress dialog if it is currently being shown.


onInterstitialLoadResultsAvailable

protected void onInterstitialLoadResultsAvailable(InterstitialLoader.InterstitialLoadingResults result)
Called when the results of the interstitial request are available, i.e. a response has been received from the server. Notifies the InterstitialLoader.InterstitialLoadingStatusListener registered by the host publisher application of the events "Will Show Interstitial", "Interstitial Request Error" and "No Interstitial Available"

Parameters:
result - a InterstitialLoader.InterstitialLoadingResults containing the status code and the contents of the response.

launchInterstitialActivity

private void launchInterstitialActivity(InterstitialLoader.InterstitialLoadingResults result)
Launchs the InterstitialActivity with the initial contents of the interstitial ad, the initial base URL for images, scripts and other dependencies, and the desired behavior for staying open after user redirection.

Parameters:
result -

isInterstitialAvailableAccordingToStatusCode

public static boolean isInterstitialAvailableAccordingToStatusCode(int statusCode)
Takes an HTTP status code and returns whether it means that an interstitial ad is available.

Parameters:
statusCode - The HTTP status code as int.
Returns:
True for interstitial available, false otherwise.