master
kt 112 lines 3.53 KB
Raw
1 package com.plugin.admob.core
2
3 import android.annotation.SuppressLint
4 import android.app.Activity
5 import android.content.res.Resources
6 import android.provider.Settings
7 import android.util.DisplayMetrics
8 import android.util.SparseArray
9 import android.view.View
10 import android.view.ViewGroup
11 import com.google.android.gms.ads.MobileAds
12 import com.plugin.admob.AdmobPlugin
13 import org.json.JSONArray
14 import java.math.BigInteger
15 import java.security.MessageDigest
16 import java.security.NoSuchAlgorithmException
17 import java.util.Locale
18
19 class Helper(private val plugin: AdmobPlugin) {
20 val activity: Activity
21 get() = plugin.activity
22 val isRunningInTestLab: Boolean
23 get() {
24 val testLabSetting =
25 Settings.System.getString(activity.contentResolver, "firebase.test.lab")
26 return "true" == testLabSetting
27 }
28
29 fun configForTestLab() {
30 if (!isRunningInTestLab) {
31 return
32 }
33 val config = MobileAds.getRequestConfiguration()
34 val testDeviceIds = config.testDeviceIds
35 val deviceId = deviceId
36 if (testDeviceIds.contains(deviceId)) {
37 return
38 }
39 testDeviceIds.add(deviceId)
40 val builder = config.toBuilder()
41 builder.setTestDeviceIds(testDeviceIds)
42 MobileAds.setRequestConfiguration(builder.build())
43 }
44
45 private val deviceId: String
46 private get() {
47 // This will request test ads on the emulator and device by passing this hashed device ID.
48 @SuppressLint("HardwareIds") val ANDROID_ID = Settings.Secure.getString(
49 activity.contentResolver, Settings.Secure.ANDROID_ID
50 )
51 return md5(ANDROID_ID).uppercase(Locale.getDefault())
52 }
53
54 companion object {
55 val ads = SparseArray<Ad>()
56 fun getAd(id: Int?): Ad {
57 return ads[id!!]
58 }
59
60 fun dpToPx(dp: Double): Double {
61 return dp * Resources.getSystem().displayMetrics.density
62 }
63
64 fun pxToDp(px: Int): Int {
65 val displayMetrics =
66 Resources.getSystem().displayMetrics
67 return Math.round(px / (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT))
68 }
69
70 @JvmStatic
71 fun jsonArray2stringList(a: JSONArray?): List<String> {
72 val result: MutableList<String> = ArrayList()
73 if (a == null) {
74 return result
75 }
76 for (i in 0 until a.length()) {
77 val testDeviceId = a.optString(i)
78 if (testDeviceId != null) {
79 result.add(testDeviceId)
80 }
81 }
82 return result
83 }
84
85 @JvmStatic
86 fun getParentView(view: View?): ViewGroup? {
87 return if (view == null) null else view.parent as ViewGroup?
88 }
89
90 @JvmStatic
91 fun removeFromParentView(view: View?): ViewGroup? {
92 val viewParent = getParentView(view)
93 viewParent?.removeView(view)
94 return viewParent
95 }
96
97 fun NOT_IMPLEMENTED() {
98 throw UnsupportedOperationException("Not implemented.")
99 }
100
101 private fun md5(s: String): String {
102 try {
103 val digest = MessageDigest.getInstance("MD5")
104 digest.update(s.toByteArray())
105 val bigInt = BigInteger(1, digest.digest())
106 return String.format("%32s", bigInt.toString(16)).replace(' ', '0')
107 } catch (ignore: NoSuchAlgorithmException) {
108 }
109 return ""
110 }
111 }
112 }