Kaynağa Gözat

【BeiZi】放在主线程中处理

hubowen 1 yıl önce
ebeveyn
işleme
55b2c876a2

+ 5 - 4
app/build.gradle

@@ -126,10 +126,11 @@ dependencies {
     implementation project(':lib_ad_scope')
     implementation project(':lib_pdf_base')
 
-    /*GroMore 百度*/
-    implementation "by.gm_mediation.com:baidu-adapter:9.26.0"
-    /*GroMore 优量汇*/
-    implementation "by.gm_mediation.com:gdt-adapter:4.510.1380.0"
+    implementation "com.pangle.cn:mediation-gdt-adapter:4.520.1390.0"
+    implementation "com.pangle.cn:mediation-baidu-adapter:9.271.1"
+    implementation "com.pangle.cn:mediation-klevin-adapter:2.11.0.3.5"//游可赢 adapter
+    implementation "com.pangle.cn:mediation-mintegral-adapter:16.4.17.2"//mintegral adapter
+    implementation "com.pangle.cn:mediation-sigmob-adapter:4.10.1.0"
 
     implementation 'com.pangle.cn:mediation-sdk:5.1.6.1'
     implementation 'com.pangle.cn:mediation-test-tools:5.1.6.1'//测试工具

+ 1 - 1
app/src/main/java/com/convenient/android/lib/ui/sample/ad/AdMainActivity.kt

@@ -24,7 +24,7 @@ class AdMainActivity : AppCompatActivity() {
         }
         initAd()
     }
-    
+
     private fun initAd() {
         AdManager.instance.init(applicationContext, true)
         AdManager.instance.initAdvertisersSDK(applicationContext, AdmobInitialize(), GroMoreInitialize("5089223"))

+ 1 - 4
lib_ad_gromore/build.gradle

@@ -52,13 +52,10 @@ android {
     kotlinOptions {
         jvmTarget = '1.8'
     }
-
 }
 
-
 dependencies {
-
-    implementation fileTree(include: ['*.jar', '*.aar'], dir: 'libs')
+    compileOnly fileTree(include: ['*.jar', '*.aar'], dir: 'libs')
 
     implementation project(':lib_ad_core')
 

+ 3 - 2
lib_ad_gromore/src/main/java/com/composition/android/ad/gromore/impl/custom/BeiZiInitialize.kt

@@ -4,6 +4,7 @@ import android.content.Context
 import com.beizi.fusion.BeiZis
 import com.bytedance.sdk.openadsdk.mediation.bridge.custom.MediationCustomInitLoader
 import com.bytedance.sdk.openadsdk.mediation.custom.MediationCustomInitConfig
+import com.composition.android.ad.gromore.util.ThreadUtils
 
 /**
  * @author: HuBoWen
@@ -13,10 +14,10 @@ import com.bytedance.sdk.openadsdk.mediation.custom.MediationCustomInitConfig
 class BeiZiInitialize : MediationCustomInitLoader() {
     override fun initializeADN(context: Context?, customInitConfig: MediationCustomInitConfig?, localExtra: MutableMap<String, Any>?) {
         if(!isInit){
-            Thread{
+            ThreadUtils.runOnUIThread{
                 BeiZis.init(context, customInitConfig?.appId)
                 callInitSuccess()
-            }.start()
+            }
         }
     }
 

+ 68 - 41
lib_ad_gromore/src/main/java/com/composition/android/ad/gromore/load/request/custom/CustomBeiZiAppOpenRequestImpl.kt

@@ -2,18 +2,22 @@ package com.composition.android.ad.gromore.load.request.custom
 
 import android.content.Context
 import android.view.ViewGroup
-import android.view.ViewParent
 import com.beizi.fusion.AdListener
 import com.beizi.fusion.SplashAd
 import com.bytedance.sdk.openadsdk.AdSlot
 import com.bytedance.sdk.openadsdk.mediation.MediationConstant
+import com.bytedance.sdk.openadsdk.mediation.MediationConstant.AdIsReadyStatus
 import com.bytedance.sdk.openadsdk.mediation.bridge.custom.splash.MediationCustomSplashLoader
 import com.bytedance.sdk.openadsdk.mediation.custom.MediationCustomServiceConfig
+import com.composition.android.ad.gromore.util.ThreadUtils
 import com.composition.android.lib.ad.util.adLogWithFrame
 import com.composition.android.lib.ad.util.getScreenHeight
 import com.composition.android.lib.ad.util.getScreenHeightDp
 import com.composition.android.lib.ad.util.getScreenWidth
 import com.composition.android.lib.ad.util.getScreenWidthDp
+import java.util.concurrent.Callable
+import java.util.concurrent.Future
+import java.util.concurrent.TimeUnit
 
 /**
  * @author: HuBoWen
@@ -25,61 +29,84 @@ class CustomBeiZiAppOpenRequestImpl : MediationCustomSplashLoader() {
     private var splash: SplashAd? = null
 
     override fun load(context: Context?, adSlot: AdSlot?, serviceConfig: MediationCustomServiceConfig?) {
+        ThreadUtils.runOnUIThread {
+            adLogWithFrame(msg = "scope onAdLoaded")
+            //step3:创建开屏广告请求参数AdSlot,具体参数含义参考文档
+            context?.apply {
+                val splashWidthDp: Float = getScreenWidthDp(context)
+                val splashWidthPx: Int = getScreenWidth(context)
+                val screenHeightPx: Int = getScreenHeight(context)
+                val screenHeightDp: Float = getScreenHeightDp(context)
 
-        adLogWithFrame(msg = "scope onAdLoaded")
-        //step3:创建开屏广告请求参数AdSlot,具体参数含义参考文档
-        context?.apply {
-            val splashWidthDp: Float = getScreenWidthDp(context)
-            val splashWidthPx: Int = getScreenWidth(context)
-            val screenHeightPx: Int = getScreenHeight(context)
-            val screenHeightDp: Float = getScreenHeightDp(context)
+                // 开屏高度 = 屏幕高度 - 下方预留的高度,demo中是预留了屏幕高度的1/5,因此开屏高度传入 屏幕高度*4/5
+                val splashHeightDp: Float = screenHeightDp * 4 / 4f
+                val splashHeightPx: Int = (screenHeightPx * 4 / 4f).toInt()
 
-            // 开屏高度 = 屏幕高度 - 下方预留的高度,demo中是预留了屏幕高度的1/5,因此开屏高度传入 屏幕高度*4/5
-            val splashHeightDp: Float = screenHeightDp * 4 / 4f
-            val splashHeightPx: Int = (screenHeightPx * 4 / 4f).toInt()
-
-            splash = SplashAd(context, null, serviceConfig?.adnNetworkSlotId, object : AdListener {
-                override fun onAdLoaded() {
-                    adLogWithFrame(msg = "scope onAdLoaded")
-                    if (biddingType == MediationConstant.AD_TYPE_CLIENT_BIDING) {
-                        var epcm = splash?.ecpm?.toDouble()
-                        epcm?.apply {
-                            if (this < 0) {
-                                epcm = 0.0
+                splash = SplashAd(context, null, serviceConfig?.adnNetworkSlotId, object : AdListener {
+                    override fun onAdLoaded() {
+                        adLogWithFrame(msg = "scope onAdLoaded")
+                        if (biddingType == MediationConstant.AD_TYPE_CLIENT_BIDING) {
+                            var epcm = splash?.ecpm?.toDouble()
+                            epcm?.apply {
+                                if (this < 0) {
+                                    epcm = 0.0
+                                }
+                                callLoadSuccess(epcm!!)
                             }
-                            callLoadSuccess(epcm!!)
+                        } else {
+                            callLoadSuccess()
                         }
-                    } else {
-                        callLoadSuccess()
                     }
-                }
 
-                override fun onAdShown() {
-                    adLogWithFrame(msg = "scope onAdShown")
-                }
+                    override fun onAdShown() {
+                        adLogWithFrame(msg = "scope onAdShown")
+                    }
 
-                override fun onAdFailedToLoad(p0: Int) {
-                    adLogWithFrame(msg = "scope onAdFailedToLoad")
+                    override fun onAdFailedToLoad(p0: Int) {
+                        adLogWithFrame(msg = "scope onAdFailedToLoad")
 //                    callLoadFail(p0, s)
-                }
+                    }
 
-                override fun onAdClosed() {
-                    adLogWithFrame(msg = "scope onAdClosed")
-                }
+                    override fun onAdClosed() {
+                        adLogWithFrame(msg = "scope onAdClosed")
+                        callSplashAdSkip()
+                    }
 
-                override fun onAdTick(p0: Long) {
-                    adLogWithFrame(msg = "scope onAdTick")
-                }
+                    override fun onAdTick(p0: Long) {
+                        adLogWithFrame(msg = "scope onAdTick")
+                    }
 
-                override fun onAdClicked() {
-                    adLogWithFrame(msg = "scope onAdClicked")
-                }
-            }, 5000)
-            splash?.loadAd(splashWidthPx, (screenHeightPx - 100))
+                    override fun onAdClicked() {
+                        adLogWithFrame(msg = "scope onAdClicked")
+                        callSplashAdClicked()
+                    }
+                }, 5000)
+                splash?.loadAd(splashWidthPx, (screenHeightPx - 100))
+            }
         }
     }
 
     override fun showAd(container: ViewGroup?) {
         splash?.show(container)
     }
+
+    override fun isReadyCondition(): AdIsReadyStatus {
+        /**
+         * 在子线程中进行广告是否可用的判断
+         */
+        val future: Future<AdIsReadyStatus> = ThreadUtils.runOnThreadPool(Callable {
+            if (splash != null) {
+                AdIsReadyStatus.AD_IS_READY
+            } else {
+                AdIsReadyStatus.AD_IS_NOT_READY
+            }
+        })
+        try {
+            val result = future[500, TimeUnit.MILLISECONDS] //设置500毫秒的总超时,避免线程阻塞
+            return result ?: AdIsReadyStatus.AD_IS_NOT_READY
+        } catch (e: Exception) {
+            e.printStackTrace()
+        }
+        return AdIsReadyStatus.AD_IS_NOT_READY
+    }
 }

+ 72 - 0
lib_ad_gromore/src/main/java/com/composition/android/ad/gromore/util/ThreadUtils.java

@@ -0,0 +1,72 @@
+package com.composition.android.ad.gromore.util;
+
+import android.os.Handler;
+import android.os.Looper;
+
+import java.util.concurrent.Callable;
+import java.util.concurrent.Future;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.ThreadPoolExecutor;
+import java.util.concurrent.TimeUnit;
+
+public class ThreadUtils {
+    private static final int CPU_COUNT = Runtime.getRuntime().availableProcessors();
+    private static final int CORE_POOL_SIZE = Math.max(CPU_COUNT, 5); // 至少允许5个线程
+    private static final int THREAD_KEEP_LIVE_TIME = 30; // 线程如果30秒不用,允许超时
+    private static final int TASK_QUEUE_MAX_COUNT = 128;
+
+    private static ThreadPoolExecutor mThreadPoolExecutor = new ThreadPoolExecutor(CORE_POOL_SIZE,
+            CORE_POOL_SIZE,
+            THREAD_KEEP_LIVE_TIME,
+            TimeUnit.SECONDS,
+            new LinkedBlockingQueue<Runnable>(TASK_QUEUE_MAX_COUNT));
+
+    public static Handler mMainHandler = new Handler(Looper.getMainLooper());
+
+
+    /**
+     * 在子线程中运行任务
+     *
+     * @param runnable
+     */
+    public static void runOnThreadPool(Runnable runnable) {
+        mThreadPoolExecutor.execute(runnable);
+    }
+
+    /**
+     * 在主线程中运行任务
+     *
+     * @param runnable
+     */
+    public static void runOnUIThread(Runnable runnable) {
+        if (Thread.currentThread() == Looper.getMainLooper().getThread()) {
+            runnable.run();
+        } else {
+            mMainHandler.post(runnable);
+        }
+    }
+
+    /**
+     * 在子线程中切主线程运行任务(切两次线程,先切子线程,再在子线程中切主线程)
+     *
+     * @param runnable
+     */
+    public static void runOnUIThreadByThreadPool(Runnable runnable) {
+        mThreadPoolExecutor.execute(new Runnable() {
+            @Override
+            public void run() {
+                mMainHandler.post(runnable);
+            }
+        });
+    }
+
+    /**
+     * 在子线程中运行任务
+     *
+     * @param callable
+     * @return
+     */
+    public static<T> Future<T> runOnThreadPool(Callable<T> callable) {
+        return mThreadPoolExecutor.submit(callable);
+    }
+}