| 1 | package com.plugin.admob.core |
| 2 | |
| 3 | import android.R |
| 4 | import android.app.Activity |
| 5 | import android.view.ViewGroup |
| 6 | import com.google.android.gms.ads.AdError |
| 7 | import com.google.android.gms.ads.rewarded.RewardItem |
| 8 | import com.plugin.admob.AdmobPlugin |
| 9 | import java.util.Objects |
| 10 | |
| 11 | abstract class Ad(val id: Int, val adUnitId: String) { |
| 12 | init { |
| 13 | Helper.ads.put(id, this) |
| 14 | } |
| 15 | |
| 16 | constructor(ctx: Context) : this( |
| 17 | Objects.requireNonNull<Int?>(ctx.optId()), |
| 18 | Objects.requireNonNull<String?>(ctx.optAdUnitID()) |
| 19 | ) |
| 20 | |
| 21 | open fun destroy() { |
| 22 | Helper.ads.remove(id) |
| 23 | } |
| 24 | |
| 25 | protected abstract val plugin: AdmobPlugin |
| 26 | val activity: Activity |
| 27 | get() = plugin.activity |
| 28 | val contentView: ViewGroup? |
| 29 | get() = activity.findViewById(R.id.content) |
| 30 | |
| 31 | protected fun emit(eventName: String?, error: AdError) { |
| 32 | this.emit(eventName, object : HashMap<String?, Any?>() { |
| 33 | init { |
| 34 | put("code", error.code) |
| 35 | put("message", error.message) |
| 36 | put("cause", error.cause) |
| 37 | } |
| 38 | }) |
| 39 | } |
| 40 | |
| 41 | protected fun emit(eventName: String?, rewardItem: RewardItem) { |
| 42 | this.emit(eventName, object : HashMap<String?, Any?>() { |
| 43 | init { |
| 44 | put("reward", object : HashMap<String?, Any?>() { |
| 45 | init { |
| 46 | put("amount", rewardItem.amount) |
| 47 | put("type", rewardItem.type) |
| 48 | } |
| 49 | }) |
| 50 | } |
| 51 | }) |
| 52 | } |
| 53 | |
| 54 | protected fun emit(eventName: String?, data: Map<String?, Any?>? = HashMap()) { |
| 55 | plugin.emit(eventName, object : HashMap<String?, Any?>(data) { |
| 56 | init { |
| 57 | put("adId", id) |
| 58 | } |
| 59 | }) |
| 60 | } |
| 61 | } |