001package com.intentsoftware.addapptr;
002
003import android.util.Log;
004
005import com.intentsoftware.addapptr.module.Logger;
006
007/**
008 * Used to set up {@link BannerCache}.
009 */
010public class BannerCacheConfiguration {
011    private static final int MAX_CACHE_SIZE = 5;
012
013    private final String placementName;
014    private final int size;
015    private BannerCache.CacheDelegate delegate;
016    private boolean shouldCacheAdditionalAdAtStart;
017    private BannerRequest requestConfiguration = new BannerRequest(null); //prepare default if user does not need extra configuration
018    private int minDelayMillis = 1000;
019
020    /**
021     * Creates the configuration object to be used when creating BannerCache
022     *
023     * @param placementName The name of the banner placement that will be created. <b>The placement will be created by the cache and should not be created manually.</b>
024     * @param size          Defines how many preloaded banners should be available in the cache. Should be smaller than {@value MAX_CACHE_SIZE}.
025     */
026    public BannerCacheConfiguration(String placementName, int size) {
027        this.placementName = placementName;
028        if (size <= 0) {
029            if (Logger.isLoggable(Log.ERROR)) {
030                Logger.e(this, "Cache size cannot be lower than 1! Size = 1 will be used.");
031            }
032            size = 1;
033        } else if (size > MAX_CACHE_SIZE) {
034            if (Logger.isLoggable(Log.ERROR)) {
035                Logger.e(this, "Cache size cannot be greater than " + MAX_CACHE_SIZE + ". Size = " + MAX_CACHE_SIZE + " will be used.");
036            }
037            size = MAX_CACHE_SIZE;
038        }
039        this.size = size;
040    }
041
042    BannerCacheConfiguration(BannerCacheConfiguration configuration) {
043        this.placementName = configuration.placementName;
044        this.size = configuration.size;
045        this.delegate = configuration.delegate;
046        this.shouldCacheAdditionalAdAtStart = configuration.shouldCacheAdditionalAdAtStart;
047        this.requestConfiguration = configuration.requestConfiguration;
048        this.minDelayMillis = configuration.minDelayMillis;
049    }
050
051    /**
052     * Sets optional cache delegate.
053     *
054     * @param delegate {@link BannerCache.CacheDelegate} to be used.
055     */
056    public void setDelegate(BannerCache.CacheDelegate delegate) {
057        this.delegate = delegate;
058    }
059
060    /**
061     * Defines if the cache should load additional ad at the beginning.
062     *
063     * @param shouldCacheAdditionalAdAtStart Boolean indicating if additional ad should be cached.
064     */
065    public void setShouldCacheAdditionalAdAtStart(boolean shouldCacheAdditionalAdAtStart) {
066        this.shouldCacheAdditionalAdAtStart = shouldCacheAdditionalAdAtStart;
067    }
068
069    /**
070     * The configuration that will be used when requesting new banners.
071     *
072     * @param requestConfiguration The {@link BannerRequest} instance.
073     */
074    public void setRequestConfiguration(BannerRequest requestConfiguration) {
075        this.requestConfiguration = requestConfiguration;
076    }
077
078    /**
079     * Sets the minimum delay between two banner consumptions. 1s by default.
080     *
081     * @param minDelay The minimum delay in seconds between two banner consumptions.
082     */
083    public void setMinimumDelay(int minDelay) {
084        this.minDelayMillis = minDelay * 1000;
085    }
086
087    boolean shouldCacheAdditionalAdAtStart() {
088        return shouldCacheAdditionalAdAtStart;
089    }
090
091    BannerCache.CacheDelegate getDelegate() {
092        return delegate;
093    }
094
095    int getSize() {
096        return size;
097    }
098
099    String getPlacementName() {
100        return placementName;
101    }
102
103    BannerRequest getRequestConfiguration() {
104        return requestConfiguration;
105    }
106
107    int getMinDelayMillis() {
108        return minDelayMillis;
109    }
110
111    @Override
112    public String toString() {
113        return "BannerCacheConfiguration{" +
114                "placementName='" + placementName + '\'' +
115                ", size=" + size +
116                ", delegate=" + delegate +
117                ", shouldCacheAdditionalAdAtStart=" + shouldCacheAdditionalAdAtStart +
118                ", requestConfiguration=" + requestConfiguration +
119                ", minDelayMillis=" + minDelayMillis +
120                '}';
121    }
122}