-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #294 from naz013/feature/REM-928_Add_ads_rotation_…
…for_free_build REM-928 - Add the rotation to Ads provider
- Loading branch information
Showing
7 changed files
with
278 additions
and
133 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
app/src/free/java/com/elementary/tasks/OnAdsFailureCallback.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package com.elementary.tasks | ||
|
||
interface OnAdsFailureCallback { | ||
fun onAdsFailure() | ||
} |
85 changes: 85 additions & 0 deletions
85
app/src/free/java/com/elementary/tasks/RotatingBannerAdsProvider.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package com.elementary.tasks | ||
|
||
import android.os.Handler | ||
import android.os.Looper | ||
import android.view.ViewGroup | ||
import androidx.core.view.doOnDetach | ||
import com.github.naz013.logging.Logger | ||
import com.github.naz013.ui.common.view.gone | ||
import com.github.naz013.ui.common.view.visible | ||
import com.google.android.gms.ads.AdListener | ||
import com.google.android.gms.ads.AdRequest | ||
import com.google.android.gms.ads.AdSize | ||
import com.google.android.gms.ads.AdView | ||
import com.google.android.gms.ads.LoadAdError | ||
import java.lang.ref.WeakReference | ||
|
||
class RotatingBannerAdsProvider( | ||
private val bannerId: String, | ||
viewGroup: ViewGroup, | ||
onAdsFailureCallback: OnAdsFailureCallback | ||
) { | ||
|
||
private val parent = WeakReference(viewGroup) | ||
private val callback = WeakReference(onAdsFailureCallback) | ||
|
||
private val handler = Handler(Looper.getMainLooper()) | ||
private val runnable = Runnable { scheduleAds() } | ||
|
||
init { | ||
scheduleAds() | ||
listenParent() | ||
} | ||
|
||
private fun listenParent() { | ||
parent.get()?.doOnDetach { | ||
Logger.d(TAG, "Parent view is detached, will not show ADS") | ||
handler.removeCallbacks(runnable) | ||
callback.clear() | ||
parent.clear() | ||
} | ||
} | ||
|
||
private fun scheduleAds() { | ||
if (safeLoadAds()) { | ||
Logger.d(TAG, "Scheduled ads") | ||
handler.postDelayed(runnable, ADS_DURATION) | ||
} | ||
} | ||
|
||
private fun safeLoadAds(): Boolean { | ||
return runCatching { loadAds() }.getOrNull() ?: false | ||
} | ||
|
||
private fun loadAds(): Boolean { | ||
val viewGroup = parent.get() ?: return false.also { | ||
Logger.e(TAG, "Will not show ADS, Parent view is null") | ||
} | ||
val adView = AdView(viewGroup.context) | ||
adView.setAdSize(AdSize.LARGE_BANNER) | ||
adView.adUnitId = bannerId | ||
|
||
viewGroup.removeAllViews() | ||
viewGroup.addView(adView) | ||
|
||
val adRequest = AdRequest.Builder().build() | ||
adView.loadAd(adRequest) | ||
|
||
adView.adListener = object : AdListener() { | ||
override fun onAdFailedToLoad(adError: LoadAdError) { | ||
adView.gone() | ||
callback.get()?.onAdsFailure() | ||
} | ||
|
||
override fun onAdLoaded() { | ||
adView.visible() | ||
} | ||
} | ||
return true | ||
} | ||
|
||
companion object { | ||
private const val TAG = "RotatingBannerAdsProvider" | ||
private const val ADS_DURATION = 1 * 60 * 1000L // 1 minute | ||
} | ||
} |
Oops, something went wrong.