001package com.intentsoftware.addapptr;
002
003import android.app.Activity;
004import android.content.Context;
005import android.content.pm.PackageManager;
006import android.graphics.Bitmap;
007import android.util.Log;
008import android.util.Pair;
009import android.view.Gravity;
010import android.view.View;
011import android.view.ViewGroup;
012
013import com.intentsoftware.addapptr.ad.NativeAd;
014import com.intentsoftware.addapptr.ad.NativeAdData;
015import com.intentsoftware.addapptr.ad.VASTAdData;
016import com.intentsoftware.addapptr.module.Logger;
017import com.intentsoftware.addapptr.module.ServerLogger;
018import com.intentsoftware.addapptr.module.Utils;
019
020import java.util.List;
021import java.util.Map;
022import java.util.Set;
023
024import androidx.annotation.Nullable;
025
026@SuppressWarnings({"unused", "WeakerAccess", "UnusedReturnValue"})
027public class AATKit {
028
029    // Used only in Javadoc.
030    public static final int BANNER_DEFAULT_RELOAD_INTERVAL_IN_SECONDS = (int) (BannerReloader.AUTORELOAD_INTERVAL_DEFAULT / 1000);
031    public static final int BANNER_MIN_RELOAD_INTERVAL_IN_SECONDS = (int) (BannerReloader.AUTORELOAD_INTERVAL_MIN / 1000);
032    public static final int BANNER_MAX_RELOAD_INTERVAL_IN_SECONDS = (int) (BannerReloader.AUTORELOAD_INTERVAL_MAX / 1000);
033
034    /**
035     * Used for passing of detailed GDPR consent. Possible implementations: {@link ManagedConsent}, {@link SimpleConsent}, {@link VendorConsent}.
036     */
037    public interface Consent {
038        /**
039         * Sets to set the networks that will not be requested if they do not have the TCF2 consent. Only works if TCF2 compliant CMP is used.
040         *
041         * @param stopSet Set of networks not to be requested without TCF2 consent.
042         */
043        void setNoConsentNetworkStopSet(Set<AdNetwork> stopSet);
044    }
045
046    /**
047     * Notifies about AATKit events.
048     */
049    public interface Delegate {
050        /**
051         * Notifies that placement has finished loading an ad successfully.
052         *
053         * @param placementId Placement identifier obtained from {@link #createPlacement(String, PlacementSize)}.
054         */
055        void aatkitHaveAd(int placementId);
056
057        /**
058         * Notifies that placement has failed to load an ad.
059         *
060         * @param placementId Placement identifier obtained from {@link #createPlacement(String, PlacementSize)}.
061         */
062        void aatkitNoAd(int placementId);
063
064        /**
065         * Notifies that ad went fullscreen and that application should pause.
066         *
067         * @param placementId Placement identifier obtained from {@link #createPlacement(String, PlacementSize)}.
068         */
069        void aatkitPauseForAd(int placementId);
070
071        /**
072         * Notifies that ad came back from fullscreen and that application should resume.
073         *
074         * @param placementId Placement identifier obtained from {@link #createPlacement(String, PlacementSize)}.
075         */
076        void aatkitResumeAfterAd(int placementId);
077
078        /**
079         * Notifies that placement has loaded an Empty ad.
080         *
081         * @param placementId Placement identifier obtained from {@link #createPlacement(String, PlacementSize)}.
082         */
083        void aatkitShowingEmpty(int placementId);
084
085        /**
086         * Notifies that placement has earned incentive (by rewarded ads).
087         * <p>
088         * Note: This callback might also be invoked for regular fullscreen
089         * placements, not just the ones configured for rewarded videos. Please
090         * make sure placementId is your rewarded video placement before rewarding
091         * the user.
092         *
093         * @param placementId  Placement identifier obtained from {@link #createPlacement(String, PlacementSize)}.
094         * @param aatKitReward Incentive object. Can be null for networks not supporting reward information.
095         */
096        void aatkitUserEarnedIncentive(int placementId, @Nullable AATKitReward aatKitReward);
097
098        /**
099         * Notifies that the AATKit has obtained ad rules.
100         *
101         * @param fromTheServer Indicates if the rules came from the server. It will return false if the currently used rules come from the {@link #setInitialRules(String)} method or the cached rules are used.
102         */
103        void aatkitObtainedAdRules(boolean fromTheServer);
104
105        /**
106         * Notifies that application's bundle ID was not recognized by the AddApptr server.
107         */
108        void aatkitUnknownBundleId();
109
110        /**
111         * Notifies that AATKit has new banner view ready for placement. Used only for MultiSizeBanner.
112         *
113         * @param placementId Placement identifier obtained from {@link #createPlacement(String, PlacementSize)}.
114         * @param bannerView  Loaded banner view
115         */
116        void aatkitHaveAdForPlacementWithBannerView(int placementId, BannerPlacementLayout bannerView);
117
118        /**
119         * Notifies that placement has finished loading an VAST ad successfully. Used only for VAST.
120         *
121         * @param placementId Placement identifier obtained from {@link #createPlacement(String, PlacementSize)}.
122         * @param data        Loaded VAST ad data.
123         */
124        void aatkitHaveVASTAd(int placementId, VASTAdData data);
125    }
126
127    private static AdController adController;
128
129    /**
130     * @return AATKit version.
131     */
132    public static String getVersion() {
133        logAATKitCall("CMD: getVersion()");
134        return Version.NAME;
135    }
136
137    /**
138     * @return AATKit detailed build version.
139     */
140    public static String getFullVersion() {
141        logAATKitCall("CMD: getFullVersion()");
142        return Version.FULL_NAME;
143    }
144
145    /**
146     * Initializes the AATKit library. Should be called once during application initialization before any other calls to AATKit.
147     *
148     * @param configuration Configuration for AATKit.
149     * @see AATKitConfiguration
150     */
151    public static void init(AATKitConfiguration configuration) {
152        logAATKitCall("CMD: init(" + configuration + ")");
153
154        if (configuration == null) {
155            if (Logger.isLoggable(Log.ERROR)) {
156                Logger.e(AATKit.class, "Configuration cannot be null");
157            }
158            return;
159        }
160        if (adController != null) {
161            if (Logger.isLoggable(Log.ERROR)) {
162                Logger.e(AATKit.class, "AdController is already initialized");
163            }
164            return;
165        }
166
167        String version = "?";
168        String packageName = configuration.getApplication().getApplicationContext().getPackageName();
169        try {
170            version = configuration.getApplication().getPackageManager().getPackageInfo(packageName, 0).versionName;
171        } catch (PackageManager.NameNotFoundException e) {
172            if (Logger.isLoggable(Log.WARN)) {
173                Logger.w(AATKit.class, "Failed to check version of application", e);
174            }
175        }
176
177        Logger.d("Init", Version.FULL_NAME
178                + ", application: " + packageName
179                + " (version: " + version + ")"
180                + ", configuration: " + configuration);
181        adController = new AdController(configuration);
182    }
183
184    /**
185     * Enables debug screen that will show after shaking the device. It is already enabled by default.
186     */
187    public static void enableDebugScreen() {
188        logAATKitCall("CMD: enableDebugScreen()");
189        adController.enableDebugScreen();
190    }
191
192    /**
193     * Disables the debug screen appearing after shaking the device. It is enabled by default.
194     */
195    public static void disableDebugScreen() {
196        logAATKitCall("CMD: disableDebugScreen()");
197        adController.disableDebugScreen();
198    }
199
200    /**
201     * Used for obtaining debug information (the same that would be presented in dialog after shaking the device if debug screen is enabled)
202     *
203     * @return String with debug information
204     */
205    public static String getDebugInfo() {
206        logAATKitCall("CMD: getDebugInfo()");
207        return adController.getDebugInfo();
208    }
209
210    /**
211     * Allows to reconfigure the options for GDPR consent.
212     *
213     * @param configuration New configuration.
214     */
215    public static void reconfigure(AATKitRuntimeConfiguration configuration) {
216        logAATKitCall("CMD: reconfigure(" + configuration + ")");
217        adController.reconfigure(configuration);
218    }
219
220    /**
221     * Checks if AATKit recognizes given device as tablet.
222     *
223     * @param context The {@link Context} of your application.
224     * @return True if device is recognized as tablet, false otherwise.
225     */
226    public static boolean isTablet(Context context) {
227        logAATKitCall("CMD: isTablet(" + context + ")");
228        return Utils.isTablet(context);
229    }
230
231    /**
232     * Returns the {@link PlacementSize} with maximum width that will fit on a given device in portrait screen orientation.
233     *
234     * @param context The {@link Context} of your application.
235     * @return {@link PlacementSize} best fitting current device
236     */
237    public static PlacementSize maximumBannerSizePortrait(Context context) {
238        logAATKitCall("CMD: maximumBannerSizePortrait(" + context + ")");
239        return Utils.maxPlacementSize(Utils.screenWidthPortrait(context));
240    }
241
242    /**
243     * Returns the {@link PlacementSize} with maximum width that will fit on a given device in landscape screen orientation.
244     *
245     * @param context The {@link Context} of your application.
246     * @return {@link PlacementSize} best fitting current device
247     */
248    public static PlacementSize maximumBannerSizeLandscape(Context context) {
249        logAATKitCall("CMD: maximumBannerSizeLandscape(" + context + ")");
250        return Utils.maxPlacementSize(Utils.screenWidthLandscape(context));
251    }
252
253    /**
254     * Returns the set of {@link BannerSize} that will fit on a given device in portrait screen orientation.
255     *
256     * @param context The {@link Context} of your application.
257     * @return Set of {@link BannerSize} fitting current device
258     */
259    public static Set<BannerSize> fittingBannerSizesPortrait(Context context) {
260        logAATKitCall("CMD: fittingBannerSizesPortrait(" + context + ")");
261        return Utils.fittingBannerSizes(Utils.screenWidthPortrait(context));
262    }
263
264    /**
265     * Returns the set of {@link BannerSize} that will fit on a given device in landscape screen orientation.
266     *
267     * @param context The {@link Context} of your application.
268     * @return Set of {@link BannerSize} fitting current device
269     */
270    public static Set<BannerSize> fittingBannerSizesLandscape(Context context) {
271        logAATKitCall("CMD: fittingBannerSizesLandscape(" + context + ")");
272        return Utils.fittingBannerSizes(Utils.screenWidthLandscape(context));
273    }
274
275    /**
276     * Notifies AATKit about activity resume. Invoke this method in every activity that uses AATKit.
277     *
278     * @param activity Reference to {@link Activity}.
279     */
280    public static void onActivityResume(Activity activity) {
281        logAATKitCall("CMD: onActivityResume(" + activity + ")");
282        adController.onActivityResume(activity);
283    }
284
285    /**
286     * Notifies AATKit about activity pause. Invoke this method in every activity that uses AATKit.
287     *
288     * @param activity Reference to {@link Activity}.
289     */
290    public static void onActivityPause(Activity activity) {
291        logAATKitCall("CMD: onActivityPause(" + activity + ")");
292        adController.onActivityPause();
293    }
294
295    /**
296     * Creates placement with given name and size. If the placement of given name and size already exists, it will be returned.
297     *
298     * @param name Unique name of placement. The same name will be used in addapptr.com account.
299     * @param size Size of placement. Use {@link PlacementSize#Fullscreen} for interstitials, {@link PlacementSize#Native} for native ads and other sizes for banner ads.
300     * @return Placement identifier, or -1 if placement cannot be created.
301     */
302    public static int createPlacement(String name, PlacementSize size) {
303        logAATKitCall("CMD: createPlacement(" + name + "," + size + ")");
304        return adController.createPlacement(name, size);
305    }
306
307    /**
308     * Creates a new native ad placement. If the native ad placement of given name already exists, it will be returned.
309     *
310     * @param name              Unique name of placement. The same name will be used in addapptr.com account.
311     * @param supportsMainImage True if the native ads returned should have main image asset. Keep in mind that if main image is used, it has to be displayed.
312     * @return Placement identifier, or -1 if placement cannot be created.
313     */
314    public static int createNativeAdPlacement(String name, boolean supportsMainImage) {
315        logAATKitCall("CMD: createNativeAdPlacement(" + name + "," + supportsMainImage + ")");
316        return adController.createNativeAdPlacement(name, supportsMainImage);
317    }
318
319    /**
320     * Creates a new banner placement. If the banner placement of given name already exists, it will be returned.
321     * <p>
322     * <b>The placement will create a copy of the
323     * configuration.</b> Any changes made to the configuration after placement is created will be ignored.
324     *
325     * @param name          Unique name of placement. The same name will be used in addapptr.com account.
326     * @param configuration The configuration for this placement. The placement will ignore any changes made to configuration after it was created.
327     * @return Banner placement instance, or null if the placement cannot be created.
328     */
329    public static BannerPlacement createBannerPlacement(String name, BannerConfiguration configuration) {
330        logAATKitCall("CMD: createInFeedBannerPlacement(" + name + "," + configuration + ")");
331        return adController.createBannerPlacement(name, configuration);
332    }
333
334    /**
335     * Creates a new rewarded video placement. If the rewarded video ad placement of given name already exists, it will be returned.
336     * <b>Only one Rewarded Video placement can be used within the app.</b>
337     *
338     * @param name Unique name of placement. The same name will be used in addapptr.com account.
339     * @return Placement identifier, or -1 if placement cannot be created.
340     */
341    public static int createRewardedVideoPlacement(String name) {
342        logAATKitCall("CMD: createRewardedVideoPlacement(" + name + ")");
343        return adController.createRewardedVideoPlacement(name);
344    }
345
346    /**
347     * Enables automatic reloading of placement. Autoreloader will use reload time configured on addapptr.com account or fallback to default {@value #BANNER_DEFAULT_RELOAD_INTERVAL_IN_SECONDS} seconds.
348     *
349     * @param placementId Placement identifier obtained from {@link #createPlacement(String, PlacementSize)}.
350     */
351    public static void startPlacementAutoReload(int placementId) {
352        logAATKitCall("CMD: startPlacementAutoReload(" + placementId + ")");
353        adController.startPlacementAutoReload(placementId);
354    }
355
356    /**
357     * Enables automatic reloading of placement and sets custom reload time. This reload time will be used instead of time configured on addapptr.com account.
358     *
359     * @param placementId Placement identifier obtained from {@link #createPlacement(String, PlacementSize)}.
360     * @param seconds     Interval between ad reloads. Should be a value from {@value #BANNER_MIN_RELOAD_INTERVAL_IN_SECONDS} to {@value #BANNER_MAX_RELOAD_INTERVAL_IN_SECONDS} seconds.
361     */
362    public static void startPlacementAutoReload(int placementId, int seconds) {
363        logAATKitCall("CMD: startPlacementAutoReload(" + placementId + "," + seconds + ")");
364        if (seconds < BANNER_MIN_RELOAD_INTERVAL_IN_SECONDS || seconds > BANNER_MAX_RELOAD_INTERVAL_IN_SECONDS) {
365            if (Logger.isLoggable(Log.WARN)) {
366                Logger.w(AATKit.class, "Passed autoreload interval: " + seconds + " is not within " + BANNER_MIN_RELOAD_INTERVAL_IN_SECONDS + " to " + BANNER_MAX_RELOAD_INTERVAL_IN_SECONDS + " bounds and will be ignored.");
367            }
368        }
369        adController.startPlacementAutoReload(placementId, seconds);
370    }
371
372    /**
373     * Disables automatic reloading of placement.
374     *
375     * @param placementId Placement identifier obtained from {@link #createPlacement(String, PlacementSize)}.
376     */
377    public static void stopPlacementAutoReload(int placementId) {
378        logAATKitCall("CMD: stopPlacementAutoReload(" + placementId + ")");
379        adController.stopPlacementAutoReload(placementId);
380    }
381
382    /**
383     * Sets reload time that will be used instead of time configured on addapptr.com account.
384     *
385     * @param placementId Placement identifier obtained from {@link #createPlacement(String, PlacementSize)}.
386     * @param seconds     Interval between ad reloads. Should be a value from {@value #BANNER_MIN_RELOAD_INTERVAL_IN_SECONDS} to {@value #BANNER_MAX_RELOAD_INTERVAL_IN_SECONDS} seconds.
387     */
388    public static void setPlacementAutoreloadInterval(int placementId, int seconds) {
389        logAATKitCall("CMD: setPlacementAutoreloadInterval(" + placementId + "," + seconds + ")");
390        adController.setPlacementAutoreloadInterval(placementId, seconds);
391    }
392
393    /**
394     * Requests placement reload. Works only if automatic reloading is disabled. For native ads, it might return false if current limit for native ads loading in parallel is reached.
395     * <p>
396     * For banners it will not cause new banner to show more often than set banner refresh interval (default: 30s) - instead, it will set a timer to reload and show next ad when the refresh interval passes. To force ad reload and display before this delay, use the method {@link #reloadPlacement(int, boolean)} instead.
397     *
398     * @param placementId Placement identifier obtained from {@link #createPlacement(String, PlacementSize)}.
399     * @return True if call ends in causing a placement to reload, false otherwise.
400     * @see #reloadPlacement(int, boolean)
401     */
402    public static boolean reloadPlacement(int placementId) {
403        logAATKitCall("CMD: reloadPlacement(" + placementId + ")");
404        return adController.reloadPlacement(placementId, false);
405    }
406
407    /**
408     * Requests placement reload. Works only if automatic reloading is disabled. For native ads, it might return false if current limit for native ads loading in parallel is reached.
409     *
410     * @param placementId Placement identifier obtained from {@link #createPlacement(String, PlacementSize)}.
411     * @param forceLoad   True if you want to be able to reload and show new banner before set banner reload interval (default: 30s) between reloads, false otherwise.
412     * @return True if call ends in causing a placement to reload, false otherwise.
413     * @see #reloadPlacement(int)
414     */
415    public static boolean reloadPlacement(int placementId, boolean forceLoad) {
416        logAATKitCall("CMD: reloadPlacement(" + placementId + "," + forceLoad + ")");
417        return adController.reloadPlacement(placementId, forceLoad);
418    }
419
420    /**
421     * Returns placement view. Works only for banner placements.
422     *
423     * @param placementId Placement identifier obtained from {@link #createPlacement(String, PlacementSize)}.
424     * @return Placement view or null if {@code placementId} is not valid.
425     */
426    public static View getPlacementView(int placementId) {
427        logAATKitCall("CMD: getPlacementView(" + placementId + ")");
428        return adController.getPlacementView(placementId);
429    }
430
431    /**
432     * Binds the native ad instance with given ViewGroup. Needed for click handling and tracking.
433     *
434     * @param nativeAd      Native ad instance.
435     * @param layout        ViewGroup used to render the native ad.
436     * @param mainImageView View used to show the main image of the ad. Can be null.
437     * @param iconView      View used to show the icon of the native ad.
438     * @deprecated This method is deprecated and will be removed in the future. Please use {@link #attachNativeAdToLayout(NativeAdData, ViewGroup, View, View, View)} instead.
439     */
440    @Deprecated
441    public static void attachNativeAdToLayout(NativeAdData nativeAd, ViewGroup layout, View mainImageView, View iconView) {
442        String mainImageClass = mainImageView == null ? "null" : mainImageView.getClass().getSimpleName();
443        String iconViewClass = iconView == null ? "null" : iconView.getClass().getSimpleName();
444        logAATKitCall("CMD: attachNativeAdToLayout(" + nativeAd + ", " + layout.getClass().getSimpleName() + ", " + mainImageClass + ", " + iconViewClass + ")");
445        adController.attachNativeAdToLayout(nativeAd, layout, mainImageView, iconView, null);
446    }
447
448    /**
449     * Binds the native ad instance with given ViewGroup. Needed for click handling and tracking.
450     *
451     * @param nativeAd      Native ad instance.
452     * @param layout        ViewGroup used to render the native ad.
453     * @param mainImageView View used to show the main image of the ad. Can be null.
454     * @param iconView      View used to show the icon of the native ad.
455     * @param CTAView       View used to show the Call To Action of the native ad. Can be null.
456     */
457    public static void attachNativeAdToLayout(NativeAdData nativeAd, ViewGroup layout, View mainImageView, View iconView, View CTAView) {
458        String mainImageClass = mainImageView == null ? "null" : mainImageView.getClass().getSimpleName();
459        String iconViewClass = iconView == null ? "null" : iconView.getClass().getSimpleName();
460        String ctaViewClass = CTAView == null ? "null" : CTAView.getClass().getSimpleName();
461        logAATKitCall("CMD: attachNativeAdToLayout(" + nativeAd + ", " + layout.getClass().getSimpleName() + ", " + mainImageClass + ", " + iconViewClass + ", " + ctaViewClass + ")");
462        adController.attachNativeAdToLayout(nativeAd, layout, mainImageView, iconView, CTAView);
463    }
464
465    /**
466     * Removes the binding between native ad and ViewGroup. Should be called when the native ad will no longer be presented and should be destroyed.
467     *
468     * @param nativeAd Native ad instance.
469     */
470    public static void detachNativeAdFromLayout(NativeAdData nativeAd) {
471        logAATKitCall("CMD: detachNativeAdFromLayout(" + nativeAd + ")");
472        adController.detachNativeAdFromLayout(nativeAd);
473    }
474
475    /**
476     * Returns the title of native ad.
477     *
478     * @param nativeAd Native ad instance.
479     * @return String with title asset of the ad, or null if it is not available.
480     */
481    public static String getNativeAdTitle(NativeAdData nativeAd) {
482        logAATKitCall("CMD: getNativeAdTitle(" + nativeAd + ")");
483        return adController.getNativeAdTitle(nativeAd);
484    }
485
486    /**
487     * Returns the description of native ad.
488     *
489     * @param nativeAd Native ad instance.
490     * @return String with description asset of the ad, or null if it is not available.
491     */
492    public static String getNativeAdDescription(NativeAdData nativeAd) {
493        logAATKitCall("CMD: getNativeAdDescription(" + nativeAd + ")");
494        return adController.getNativeAdDescription(nativeAd);
495    }
496
497    /**
498     * Returns the call to action of native ad.
499     *
500     * @param nativeAd Native ad instance.
501     * @return String with call to action asset of the ad, or null if it is not available.
502     */
503    public static String getNativeAdCallToAction(NativeAdData nativeAd) {
504        logAATKitCall("CMD: getNativeAdCallToAction(" + nativeAd + ")");
505        return adController.getNativeAdCallToAction(nativeAd);
506    }
507
508    /**
509     * Returns the URL of the image asset of native ad.
510     *
511     * @param nativeAd Native ad instance.
512     * @return String with URL of the image asset of the ad, or null if it is not available.
513     */
514    public static String getNativeAdImageUrl(NativeAdData nativeAd) {
515        logAATKitCall("CMD: getNativeAdImageUrl(" + nativeAd + ")");
516        return adController.getNativeAdImageUrl(nativeAd);
517    }
518
519    /**
520     * Returns the URL of the icon asset of native ad.
521     *
522     * @param nativeAd Native ad instance.
523     * @return String with URL of the icon asset of the ad, or null if it is not available.
524     */
525    public static String getNativeAdIconUrl(NativeAdData nativeAd) {
526        logAATKitCall("CMD: getNativeAdIconUrl(" + nativeAd + ")");
527        return adController.getNativeAdIconUrl(nativeAd);
528    }
529
530    /**
531     * Returns the rating asset of native ad.
532     *
533     * @param nativeAd Native ad instance.
534     * @return NativeAdRating instance containing both value and scale of rating, or null if it is not available.
535     */
536    public static NativeAd.NativeAdRating getNativeAdRating(NativeAdData nativeAd) {
537        logAATKitCall("CMD: getNativeAdRating(" + nativeAd + ")");
538        return adController.getNativeAdRating(nativeAd);
539    }
540
541    /**
542     * Returns the view with branding logo or ad information related to the ad network providing the native ad. Some networks like Facebook Audience Network require this special view to be visible on native ads.
543     *
544     * @param nativeAd Native ad insance.
545     * @return View that should be added to native ad layout, or null if it is not available.
546     */
547    public static View getNativeAdBrandingLogo(NativeAdData nativeAd) {
548        logAATKitCall("CMD: getNativeAdBrandingLogo(" + nativeAd + ")");
549        return adController.getNativeAdBrandingLogo(nativeAd);
550    }
551
552    /**
553     * Returns the advertiser asset of native ad (not the same as ad network providing it).
554     *
555     * @param nativeAd Native ad instance.
556     * @return String with advertiser asset of the ad, or null if it is not available.
557     */
558    public static String getNativeAdAdvertiser(NativeAdData nativeAd) {
559        logAATKitCall("CMD: getNativeAdAdvertiser(" + nativeAd + ")");
560        return adController.getNativeAdAdvertiser(nativeAd);
561    }
562
563    /**
564     * Returns the ad network providing given native ad
565     *
566     * @param nativeAd Native ad instance.
567     * @return Enum value representing the ad network providing the ad.
568     */
569    public static AdNetwork getNativeAdNetwork(NativeAdData nativeAd) {
570        logAATKitCall("CMD: getNativeAdNetwork(" + nativeAd + ")");
571        return adController.getNativeAdNetwork(nativeAd);
572    }
573
574    /**
575     * Returns if the native ad has expired and shall no longer be used.
576     *
577     * @param nativeAd Native ad instance.
578     * @return True if native ad has expired, false otherwise.
579     */
580    public static boolean isNativeAdExpired(NativeAdData nativeAd) {
581        logAATKitCall("CMD: isNativeAdExpired(" + nativeAd + ")");
582        return adController.isNativeAdExpired(nativeAd);
583    }
584
585    /**
586     * Returns if the native ad is ready to be displayed.
587     *
588     * @param nativeAd Native ad instance.
589     * @return True if native ad is ready, false otherwise.
590     */
591    public static boolean isNativeAdReady(NativeAdData nativeAd) {
592        logAATKitCall("CMD: isNativeAdReady(" + nativeAd + ")");
593        return adController.isNativeAdReady(nativeAd);
594    }
595
596    /**
597     * Indicates that the application wants to display a native or VAST ad for given placement. Used in reporting.
598     *
599     * @param placementId Placement identifier obtained from
600     *                    {@link #createPlacement(String, PlacementSize)}.
601     */
602    public static void reportAdSpaceForPlacement(int placementId) {
603        logAATKitCall("CMD: reportAdSpaceForPlacement(" + placementId + ")");
604        adController.reportAdSpaceForPlacement(placementId);
605    }
606
607    /**
608     * Returns how many ads are currently loading for given native ad placement.
609     *
610     * @param placementId Placement identifier obtained from
611     *                    {@link #createPlacement(String, PlacementSize)}.
612     * @return Number of ads that are currently loading for given placement.
613     */
614    public static int currentlyLoadingNativeAdsOnPlacement(int placementId) {
615        logAATKitCall("CMD: currentlyLoadingNativeAdsOnPlacement(" + placementId + ")");
616        return adController.currentlyLoadingNativeAdsOnPlacement(placementId);
617    }
618
619    /**
620     * Checks if desired impression cap (set on AddApptr Dashboard) is reached for given native ad, fullscreen, rewarded video or VAST placement.
621     * Note: this method will ALWAYS return "false" for other placement types.
622     *
623     * @param placementId Placement identifier obtained from
624     *                    {@link #createPlacement(String, PlacementSize)}.
625     * @return True if impression cap is reached for given placement, false otherwise.
626     */
627    public static boolean isFrequencyCapReachedForPlacement(int placementId) {
628        logAATKitCall("CMD: isFrequencyCapReachedForPlacement(" + placementId + ")");
629        return adController.isFrequencyCapReachedForPlacement(placementId);
630    }
631
632    /**
633     * Returns the instance of native ad for given native ad placement.
634     *
635     * @param placementId Placement identifier obtained from
636     *                    {@link #createPlacement(String, PlacementSize)}.
637     * @return Native ad instance if it is loaded for given placement, null otherwise.
638     */
639    public static NativeAdData getNativeAd(int placementId) {
640        logAATKitCall("CMD: getNativeAd(" + placementId + ")");
641        return adController.getNativeAd(placementId);
642    }
643
644    /**
645     * Returns identifier of placement with given name, or -1 if placement with given name does not exist.
646     *
647     * @param placementName Placent name, used when creating placement with {@link #createPlacement(String, PlacementSize)} method.
648     * @return Placement identifier, or -1 if placement does not exist.
649     */
650    public static int getPlacementIdForName(String placementName) {
651        logAATKitCall("CMD: getPlacementIdForName(" + placementName + ")");
652        return adController.findPlacementIdByRealName(placementName);
653    }
654
655    /**
656     * Returns true if there is an ad loaded for given placementId.
657     *
658     * @param placementId Placement identifier obtained from
659     *                    {@link #createPlacement(String, PlacementSize)}.
660     * @return True if there is an ad loaded for given placementId.
661     */
662    public static boolean hasAdForPlacement(int placementId) {
663        logAATKitCall("CMD: hasAdForPlacement(" + placementId + ")");
664        return adController.hasAdForPlacement(placementId);
665    }
666
667    /**
668     * Shows interstitial ad if ad is ready.
669     *
670     * @param placementId Placement identifier obtained from {@link #createPlacement(String, PlacementSize)}.
671     * @return True if showing interstitial was successful, false otherwise.
672     */
673    public static boolean showPlacement(int placementId) {
674        logAATKitCall("CMD: showPlacement(" + placementId + ")");
675        return adController.showPlacement(placementId);
676    }
677
678    /**
679     * Allows to enable or disable selected ad networks. By default all networks are enabled.
680     *
681     * @param network Ad network.
682     * @param enabled True to enable, false to disable.
683     */
684    public static void setNetworkEnabled(AdNetwork network, boolean enabled) {
685        logAATKitCall("CMD: setNetworkEnabled(" + network + "," + enabled + ")");
686        SupportedNetworks.setNetworkEnabled(network, enabled);
687    }
688
689    /**
690     * Returns true if ad network is enabled, false otherwise.
691     *
692     * @param network Ad network.
693     * @return True if ad network is enabled, false otherwise.
694     */
695    public static boolean isNetworkEnabled(AdNetwork network) {
696        logAATKitCall("CMD: isNetworkEnabled(" + network + ")");
697        return SupportedNetworks.isNetworkEnabled(network);
698    }
699
700    /**
701     * Sets placement default image. This image will be shown in placement when no ad is available.
702     *
703     * @param placementId Placement identifier obtained from {@link #createPlacement(String, PlacementSize)}.
704     * @param image       The bitmap to set.
705     */
706    public static void setPlacementDefaultImageBitmap(int placementId, Bitmap image) {
707        logAATKitCall("CMD: setPlacementDefaultImageBitmap(" + placementId + "," + image + ")");
708        adController.setPlacementDefaultImage(placementId, image);
709    }
710
711    /**
712     * Sets placement default image. This image will be shown in placement when no ad is available.
713     *
714     * @param placementId Placement identifier obtained from {@link #createPlacement(String, PlacementSize)}.
715     * @param resId       The identifier of the resource.
716     */
717    public static void setPlacementDefaultImageResource(int placementId, int resId) {
718        logAATKitCall("CMD: setPlacementDefaultImageResource(" + placementId + "," + resId + ")");
719        adController.setPlacementDefaultImageResource(placementId, resId);
720    }
721
722    /**
723     * Get option from AATKit. Options can be obtained from the server or set using the {@link #setOption(String, String)} method.
724     *
725     * @param optionName The name of the option to be checked.
726     * @return Value of the option or null if it is not set.
727     */
728    public static String getOption(String optionName) {
729        logAATKitCall("CMD: getOption(" + optionName + ")");
730        return AdController.getOption(optionName);
731    }
732
733    /**
734     * Convenience method for checking if option is enabled in AATKit. Options can be obtained from the server or set using the {@link #setOption(String, String)} method.
735     *
736     * @param optionName The name of the option to be checked.
737     * @return True if option value is "Yes", false otherwise.
738     */
739    public static boolean isOptionEnabled(String optionName) {
740        logAATKitCall("CMD: isOptionEnabled(" + optionName + ")");
741        return AdController.isOptionEnabled(optionName);
742    }
743
744    /**
745     * Set option in AATKit. Options can also be obtained from the server.
746     *
747     * @param optionName  The name of the option to be set.
748     * @param optionValue The value of the option to be set.
749     */
750    public static void setOption(String optionName, String optionValue) {
751        logAATKitCall("CMD: setOption(" + optionName + "," + optionName + ")");
752        adController.setOption(optionName, optionValue);
753    }
754
755    /**
756     * Sets the targeting information for the application. This information will be used only if no placement-specific targeting is available.
757     *
758     * @param info Map with targeting information
759     * @see #setTargetingInfo(int, Map)
760     */
761    public static void setTargetingInfo(Map<String, List<String>> info) {
762        logAATKitCall("CMD: setTargetingInfo(" + info + ")");
763        adController.setTargetingInfo(null, info);
764    }
765
766    /**
767     * Sets the targeting information for the given placement. Information provided for placement overrides targeting information for application set by the {@link #setTargetingInfo(Map)}.
768     *
769     * @param placementId Placement identifier obtained from {@link #createPlacement(String, PlacementSize)}.
770     * @param info        Map with targeting information
771     */
772    public static void setTargetingInfo(int placementId, Map<String, List<String>> info) {
773        logAATKitCall("CMD: setTargetingInfo(" + placementId + ", " + info + ")");
774        adController.setTargetingInfo(placementId, info);
775    }
776
777    /**
778     * Sets the content targeting url for the application. This information will be used only if no placement-specific targeting is available.
779     *
780     * @param targetingUrl The targeting url
781     * @see #setContentTargetingUrl(int, String)
782     */
783    public static void setContentTargetingUrl(String targetingUrl) {
784        logAATKitCall("CMD: setContentTargetingUrl(" + targetingUrl + ")");
785        adController.setContentTargetingUrl(null, targetingUrl);
786    }
787
788    /**
789     * Sets the content targeting url for the given placement. Information provided for placement overrides targeting information for application set by the {@link #setContentTargetingUrl(String)}.
790     *
791     * @param placementId  Placement identifier obtained from {@link #createPlacement(String, PlacementSize)}.
792     * @param targetingUrl The targeting url
793     */
794    public static void setContentTargetingUrl(int placementId, String targetingUrl) {
795        logAATKitCall("CMD: setContentTargetingUrl(" + placementId + ", " + targetingUrl + ")");
796        adController.setContentTargetingUrl(placementId, targetingUrl);
797    }
798
799    /**
800     * Adds an ad network to the list of ad networks that receive targeting keywords (if any set).
801     * If no ad networks are added, any set keywords will be delivered to all ad networks supporting keyword targeting.
802     *
803     * @param network Chosen ad network.
804     */
805    public static void addAdNetworkForKeywordTargeting(AdNetwork network) {
806        logAATKitCall("CMD: addAdNetworkForKeywordTargeting(" + network + ")");
807        adController.addAdNetworkForKeywordTargeting(network);
808    }
809
810    /**
811     * Removes an ad network from the list of ad networks that receive targeting keywords (if any set).
812     * If no ad networks are added to the list, any set keywords will be delivered to all ad networks supporting keyword targeting.
813     *
814     * @param network Chosen ad network.
815     */
816    public static void removeAdNetworkForKeywordTargeting(AdNetwork network) {
817        logAATKitCall("CMD: removeAdNetworkForKeywordTargeting(" + network + ")");
818        adController.removeAdNetworkForKeywordTargeting(network);
819    }
820
821    /**
822     * Sets gravity for ads that don't fill entire placement area. Works only for banner placements.
823     *
824     * @param placementId Placement identifier obtained from {@link #createPlacement(String, PlacementSize)}.
825     * @param gravity     The {@link Gravity} to set.
826     */
827    public static void setPlacementContentGravity(int placementId, int gravity) {
828        logAATKitCall("CMD: setPlacementContentGravity(" + placementId + "," + gravity + ")");
829        adController.setPlacementContentGravity(placementId, gravity);
830    }
831
832    /**
833     * Allows setting of ad rules that will be used before real rules from the server are downloaded.
834     *
835     * @param rules String containing the rules to be used.
836     */
837    public static void setInitialRules(String rules) {
838        logAATKitCall("CMD: setInitialRules(" + rules + ")");
839        adController.setInitialRules(rules);
840    }
841
842    /**
843     * Allows the AATKit to preserve last downloaded ad rules when the application is closed. Such rules will be re-used next time the application is started, before new ones get downloaded.
844     *
845     * @param enabled True to enable, false to disable.
846     */
847    public static void setRuleCachingEnabled(boolean enabled) {
848        logAATKitCall("CMD: setRuleCachingEnabled(" + enabled + ")");
849        adController.setRuleCachingEnabled(enabled);
850    }
851
852    /**
853     * Allows to set log level from code.
854     *
855     * @param logLevel Desired log level, as in {@link android.util.Log} class.
856     */
857    public static void setLogLevel(int logLevel) {
858        ServerLogger.log("CMD: setLogLevel(" + logLevel + ")");
859        adController.setLogLevel(logLevel);
860    }
861
862    /**
863     * Reports the VAST ad impression.
864     *
865     * @param data The data for a VAST ad which has been impressed.
866     */
867    public static void reportVASTImpression(VASTAdData data) {
868        logAATKitCall("CMD: reportVASTImpression(" + data + ")");
869        adController.reportVASTImpression(data);
870    }
871
872    /**
873     * Reports the VAST ad viewable impression.
874     *
875     * @param data The VAST ad data for which the viewable impression should be counted.
876     */
877    public static void reportVASTViewableImpression(VASTAdData data) {
878        logAATKitCall("CMD: reportVASTViewableImpression(" + data + ")");
879        adController.reportVASTViewableImpression(data);
880    }
881
882    /**
883     * Reports the VAST ad click.
884     *
885     * @param data The data for a VAST ad which has been clicked.
886     */
887    public static void reportVASTClick(VASTAdData data) {
888        logAATKitCall("CMD: reportVASTClick(" + data + ")");
889        adController.reportVASTClick(data);
890    }
891
892    /**
893     * Sets VAST ad request parameters.
894     *
895     * @param placementId Placement identifier obtained from {@link #createPlacement(String, PlacementSize)}.
896     * @param parameters  Parameters to set for a given placement identifier.
897     */
898    public static void setVASTRequestParameters(int placementId, VASTRequestParameters parameters) {
899        logAATKitCall("CMD: setVASTRequestParameters(" + placementId + ", " + parameters + ")");
900        adController.setVASTRequestParameters(placementId, parameters);
901    }
902
903    //region non-public methods
904
905    private static void logAATKitCall(String call) {
906        if (adController != null && adController.shouldLogAATKitCalls()) {
907            ServerLogger.log(call);
908        }
909        if (Logger.isLoggable(Log.VERBOSE)) {
910            String command = call.replaceFirst("^CMD:\\s*", "");
911            Logger.v(AATKit.class, command);
912        }
913    }
914
915    static Pair<BannerPlacement, Boolean> createBannerPlacementForCache(String placementName, BannerConfiguration configuration, BannerCache cache) {
916        return adController.createBannerPlacementForCache(placementName, configuration, cache);
917    }
918
919    static void destroyBannerCache(BannerCache cache) {
920        adController.destroyBannerCache(cache);
921    }
922
923    // methods below are intended only for use by the plugins.
924
925    static void destroy() {
926        adController.destroy();
927        adController = null;
928    }
929
930    static boolean isInitialized() {
931        return adController != null;
932    }
933
934    /**
935     * Shows AATKit debug screen.
936     */
937    static void showDebugDialog() {
938        logAATKitCall("CMD: showDebugDialog()");
939        adController.showDebugDialog();
940    }
941
942    //endregion
943
944}