001package com.intentsoftware.addapptr;
002
003import android.app.Application;
004import android.util.Log;
005
006import androidx.annotation.NonNull;
007
008import com.intentsoftware.addapptr.module.Logger;
009
010/**
011 * Used to set up AATKit.
012 */
013@SuppressWarnings({"WeakerAccess"})
014public final class AATKitConfiguration extends AATKitRuntimeConfiguration {
015    private String initialRules;
016    private boolean shouldCacheRules = true;
017    private boolean shouldSkipRules;
018    private String alternativeBundleId;
019    private boolean shouldReportUsingAlternativeBundleId = true;
020    private int testModeAccountId;
021    private AATKit.Delegate delegate;
022    private boolean useDebugShake = true;
023    private Platform platform = Platform.ANDROID;
024    private Application application;
025
026    public enum Platform {
027        ANDROID("android."),
028        HUAWEI("huawei.");
029
030        private final String platformPrefix;
031
032        Platform(String platformPrefix) {
033            this.platformPrefix = platformPrefix;
034        }
035
036        public String getPlatformPrefix() {
037            return platformPrefix;
038        }
039
040        @Override
041        public String toString() {
042            return "Platform{" +
043                    "platformPrefix='" + platformPrefix + '\'' +
044                    '}';
045        }
046    }
047
048    /**
049     * Creates the default Configuration object to be used when initializing AATKit.
050     *
051     * @param application Reference to {@link Application}.
052     */
053    public AATKitConfiguration(Application application) {
054        if (application == null) {
055            if (Logger.isLoggable(Log.ERROR)) {
056                Logger.e(AATKit.class, "Application cannot be null!");
057            }
058            return;
059        }
060
061        this.application = application;
062    }
063
064    @NonNull
065    @Override
066    public String toString() {
067        return "AATKitConfiguration{" +
068                "initialRules='" + initialRules + '\'' +
069                ", shouldCacheRules=" + shouldCacheRules +
070                ", alternativeBundleId='" + alternativeBundleId + '\'' +
071                ", shouldReportUsingAlternativeBundleId=" + shouldReportUsingAlternativeBundleId +
072                ", testModeAccountId=" + testModeAccountId +
073                ", delegate=" + delegate +
074                ", useDebugShake=" + useDebugShake +
075                ", application=" + application +
076                ", platform=" + platform +
077                "} " + super.toString();
078    }
079
080    String getInitialRules() {
081        return initialRules;
082    }
083
084    /**
085     * Allows setting of ad rules that will be used before real rules from the server are downloaded.
086     *
087     * @param initialRules String containing the rules to be used.
088     */
089    public void setInitialRules(String initialRules) {
090        this.initialRules = initialRules;
091    }
092
093    boolean isShouldCacheRules() {
094        return shouldCacheRules;
095    }
096
097    /**
098     * Allows the AATKit to preserve last downloaded ad rules when the application is closed.
099     * Such rules will be re-used next time the application is started, before new ones get downloaded.
100     * Enabled by default.
101     *
102     * @param shouldCacheRules True to enable, false to disable.
103     */
104    public void setShouldCacheRules(boolean shouldCacheRules) {
105        this.shouldCacheRules = shouldCacheRules;
106    }
107
108    String getAlternativeBundleId() {
109        return alternativeBundleId;
110    }
111
112    /**
113     * Sets the fake bundle ID for testing purposes. It should only be used during development.
114     * Cannot be used together with classic test mode ({@link #setTestModeAccountId(int)}).
115     *
116     * @param alternativeBundleId Bundle ID to be used during testing.
117     */
118    public void setAlternativeBundleId(String alternativeBundleId) {
119        if (this.alternativeBundleId != null) {
120            if (Logger.isLoggable(Log.WARN)) {
121                Logger.w(this, "Alternative bundle ID was already set! It will be overriden.");
122            }
123        }
124        if (testModeAccountId != 0) {
125            if (Logger.isLoggable(Log.WARN)) {
126                Logger.w(this, "Test mode was already enabled! It will be overriden by this bundle ID.");
127            }
128        }
129
130        this.alternativeBundleId = alternativeBundleId;
131    }
132
133    boolean isShouldReportUsingAlternativeBundleId() {
134        return shouldReportUsingAlternativeBundleId;
135    }
136
137    /**
138     * If used together with {@link #setAlternativeBundleId(String)} allows to set if the same fake bundle ID should be used in reporting.
139     * True by default. If set to false, real bundle ID will be used in reporting even if fake one is used for testing.
140     *
141     * @param shouldReportUsingAlternativeBundleId If the fake bundleId should be used in reporting.
142     */
143    public void setShouldReportUsingAlternativeBundleId(boolean shouldReportUsingAlternativeBundleId) {
144        this.shouldReportUsingAlternativeBundleId = shouldReportUsingAlternativeBundleId;
145    }
146
147    int getTestModeAccountId() {
148        return testModeAccountId;
149    }
150
151    /**
152     * Enables AATKit test ads that should be for testing - only during development.
153     * Cannot be used with alternative bundle ID ({@link #setAlternativeBundleId(String)}).
154     *
155     * @param testModeAccountId Test application id obtained from addapptr.com account.
156     */
157    public void setTestModeAccountId(int testModeAccountId) {
158        if (this.alternativeBundleId != null) {
159            if (Logger.isLoggable(Log.WARN)) {
160                Logger.w(this, "Alternative bundle ID was already set! The test mode account ID will be ignored.");
161            }
162        }
163        if (this.testModeAccountId != 0) {
164            if (Logger.isLoggable(Log.WARN)) {
165                Logger.w(this, "Test mode was already enabled! Old value for test account ID will be overriden.");
166            }
167        }
168
169        this.testModeAccountId = testModeAccountId;
170    }
171
172    AATKit.Delegate getDelegate() {
173        return delegate;
174    }
175
176    /**
177     * Sets the delegate notifying about AATKit events.
178     *
179     * @param delegate Delegate allowing to listen for AATKit events.
180     */
181    public void setDelegate(AATKit.Delegate delegate) {
182        this.delegate = delegate;
183    }
184
185    boolean isUseDebugShake() {
186        return useDebugShake;
187    }
188
189    /**
190     * Sets if the debug screen should be displayed after shaking the device. Enabled by default.
191     *
192     * @param useDebugShake True to enable, false to disable.
193     */
194    public void setUseDebugShake(boolean useDebugShake) {
195        this.useDebugShake = useDebugShake;
196    }
197
198    Platform getPlatform() {
199        return platform;
200    }
201
202    /**
203     * Sets the platform. Android by default
204     *
205     * @param platform Reference to {@link Platform}
206     */
207    public void setPlatform(Platform platform) {
208        this.platform = platform;
209    }
210
211    /**
212     * Sets if networks without TCF2 consent should be skipped (only works if TCF2 compliant CMP is used). False by default.
213     *
214     * @param shouldSkipRules True to enable rule skipping, false to disable.
215     */
216    public void setShouldSkipRules(boolean shouldSkipRules) {
217        this.shouldSkipRules = shouldSkipRules;
218    }
219
220    Application getApplication() {
221        return application;
222    }
223
224    boolean isShouldSkipRules() {
225        return shouldSkipRules;
226    }
227}