Class DynamicAds

java.lang.Object
com.pranavpandey.android.dynamic.ads.DynamicAds

public class DynamicAds extends Object
Helper class to handle the ads related operations including the consent information.

It must be initialized once before accessing its methods.

  • Method Details

    • initializeInstance

      public static void initializeInstance(@Nullable Context context)
      Initialize ads when application starts.

      Must be initialized once.

      Parameters:
      context - The context to retrieve resources.
    • getInstance

      @NonNull public static DynamicAds getInstance()
      Retrieves the singleton instance of DynamicAds.

      Must be called before accessing the public methods.

      Returns:
      The singleton instance of DynamicAds.
    • isAdVisible

      public static boolean isAdVisible(@Nullable DynamicAd dynamicAd)
      Returns whether the ad is visible to the user.
      Parameters:
      dynamicAd - The dynamic ad to be used.
      Returns:
      true if the ad is visible to the user.
      See Also:
    • onAdResume

      public static void onAdResume(@Nullable DynamicAd dynamicAd)
      Try to resume the dynamic ad.
      Parameters:
      dynamicAd - The dynamic ad to be used.
      See Also:
    • onAdPause

      public static void onAdPause(@Nullable DynamicAd dynamicAd)
      Try to pause the dynamic ad.
      Parameters:
      dynamicAd - The dynamic ad to be used.
      See Also:
    • onAdDestroy

      public static void onAdDestroy(@Nullable DynamicAd dynamicAd)
      Try to destroy the dynamic ad.
      Parameters:
      dynamicAd - The dynamic ad to be used.
      See Also:
    • getAdEventCount

      public static long getAdEventCount()
      Returns the dynamic ad event count from the shared preferences.
      Returns:
      The dynamic ad event count from the shared preferences.
      See Also:
    • setAdEventCount

      public static void setAdEventCount(long eventCount)
      Save the dynamic ad event count into the shared preferences.
      Parameters:
      eventCount - The ad event count to be set.
      See Also:
    • resetAdEventCount

      public static void resetAdEventCount()
      Reset the dynamic ad event count.
      See Also:
    • onAdEvent

      public static void onAdEvent()
      Increment the saved ad event count by one.
      See Also:
    • getContext

      @NonNull public Context getContext()
      Returns the context used by this instance.
      Returns:
      The context to retrieve the resources.
    • getHandler

      @NonNull public Handler getHandler()
      Returns the main thread handler to publish results.
      Returns:
      The main thread handler to publish results.
    • isInitialized

      public boolean isInitialized()
      Returns whether the mobile ads have been initialized.
      Returns:
      true if the mobile ads have been initialized.
    • getConsentInformation

      @Nullable public com.google.android.ump.ConsentInformation getConsentInformation()
      Get the consent information if available.
      Returns:
      The consent information if available.
    • getConsentForm

      @Nullable public com.google.android.ump.ConsentForm getConsentForm()
      Get the consent form if available.
      Returns:
      The consent form if available.
    • isConsentInformationAvailable

      public boolean isConsentInformationAvailable()
      Checks whether a consent information is available.
      Returns:
      true if a consent information is available.
    • isConsentFormAvailable

      public boolean isConsentFormAvailable()
      Checks whether a consent form is available.
      Returns:
      true if a consent form is available.
    • isConsentFormVisible

      public boolean isConsentFormVisible()
      Checks whether a consent form is visible to the user.
      Returns:
      true if a consent form is visible to the user.
    • getConsentRequestParameters

      @NonNull public com.google.android.ump.ConsentRequestParameters getConsentRequestParameters()
      Returns the consent request parameters required to load the consent information.
      Returns:
      The consent request parameters required to load the consent information.
    • setConsentRequestParameters

      public void setConsentRequestParameters(@Nullable com.google.android.ump.ConsentRequestParameters consentRequestParameters)
      Set the consent request parameters required to load the consent information.
      Parameters:
      consentRequestParameters - The parameters to be set.

      Use null to set the default parameters.

    • getConsentStatus

      public int getConsentStatus()
      Returns the status of the consent information.
      Returns:
      The status of the consent information.
    • canRequestAds

      public boolean canRequestAds()
      Returns whether the app has completed the necessary steps for gathering updated user consent.
      Returns:
      true if the app has completed the necessary steps for gathering updated user consent.
    • isConsentRequired

      public boolean isConsentRequired()
      Checks whether a consent is required from the user.
      Returns:
      true if a consent is required from the user.
    • loadConsentInformation

      public void loadConsentInformation(@Nullable BaseAdListener adListener, boolean force)
      Try to request the consent information update.
      Parameters:
      adListener - The dynamic ad lister to be used.
      force - true to always show the consent form.
    • loadConsentForm

      public void loadConsentForm(@Nullable BaseAdListener adListener, boolean force)
      Try to load the consent form.
      Parameters:
      adListener - The dynamic ad lister to be used.
      force - true to always show the consent form.
    • showConsentForm

      public void showConsentForm(@Nullable BaseAdListener adListener)
      Try to show the consent form.
      Parameters:
      adListener - The dynamic ad lister to be used.
    • initializeAd

      public void initializeAd(@Nullable DynamicAd dynamicAd, @Nullable BaseAdListener adListener)
      Try to initialize the mobile ads.
      Parameters:
      dynamicAd - The dynamic ad to be initialized.
      adListener - The dynamic ad lister to be used.
    • postAd

      public void postAd(@Nullable DynamicAd dynamicAd)
      Try to create a dynamic ad on main thread.
      Parameters:
      dynamicAd - The dynamic ad to be created.
      See Also:
    • createAd

      public void createAd(@Nullable DynamicAd dynamicAd)
      Try to create a dynamic ad.
      Parameters:
      dynamicAd - The dynamic ad to be created.
      See Also:
    • queueAd

      public void queueAd(@Nullable DynamicAd dynamicAd)
      Queue a dynamic ad to be reloaded later.
      Parameters:
      dynamicAd - The ad to be queued.
    • recreateAds

      public void recreateAds()
      Try to recreate all the queued ads.