admin
2022-03-12 222df7b655c51992580b832f5e06c6772d27d9d6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package com.demo.library_flutter.nativeview;
 
import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
 
import com.bytedance.sdk.openadsdk.AdSlot;
import com.bytedance.sdk.openadsdk.TTAdConstant;
import com.bytedance.sdk.openadsdk.TTAdDislike;
import com.bytedance.sdk.openadsdk.TTAdManager;
import com.bytedance.sdk.openadsdk.TTAdNative;
import com.bytedance.sdk.openadsdk.TTNativeExpressAd;
import com.demo.lib.common.util.ui.MyActivityManager;
import com.demo.library_ad.TTAdManagerHolder;
import com.idlefish.flutterboost.FlutterBoost;
 
import java.util.List;
import java.util.Map;
 
import androidx.annotation.NonNull;
import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.common.MessageCodec;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.platform.PlatformView;
import io.flutter.plugin.platform.PlatformViewFactory;
 
class CSJExpressNativeView implements PlatformView {
    private Map<String, Object> params;
    private TTAdNative mTTAdNative;
    private MethodChannel methodChannel;
 
    public CSJExpressNativeView(int viewId, Map<String, Object> params, Context context, BinaryMessenger binaryMessenger) {
        this.params = params;
        try {
            TTAdManager ttAdManager = TTAdManagerHolder.get();
            mTTAdNative = ttAdManager.createAdNative(context);
        } catch (Exception e) {
            e.printStackTrace();
        }
        methodChannel = new MethodChannel(binaryMessenger, "ad-csj-express-view-" + viewId);
        methodChannel.setMethodCallHandler(new MethodChannel.MethodCallHandler() {
            @Override
            public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result result) {
                if ("refresh".equalsIgnoreCase(call.method)) {
                    //刷新
                    if (fl_container != null && adSlot != null) {
                        loadAd(adSlot, fl_container);
                    }
 
                }
            }
        });
    }
 
    private AdSlot adSlot = null;
    private FrameLayout fl_container;
 
    @Override
    public View getView() {
        if (fl_container == null) {
            fl_container = new FrameLayout(getView().getContext());
        }
        if (adSlot == null) {
            float width = Float.parseFloat(params.get("width") + "");
            float height = Float.parseFloat(params.get("height") + "");
            String pid = params.get("pid") + "";
            adSlot = new AdSlot.Builder()
                    .setCodeId(pid)
                    .setSupportDeepLink(true)
                    .setExpressViewAcceptedSize(width, height)
                    .setAdCount(1) //请求广告数量为1到3条
                    .build();
        }
        loadAd(adSlot, fl_container);
        return fl_container;
    }
 
    private void loadAd(AdSlot adSlot, FrameLayout container) {
        mTTAdNative.loadNativeExpressAd(adSlot, new TTAdNative.NativeExpressAdListener() {
            @Override
            public void onError(int i, String s) {
                //加载失败
                methodChannel.invokeMethod("loadFail", s);
            }
 
            @Override
            public void onNativeExpressAdLoad(List<TTNativeExpressAd> list) {
 
                if (list != null && list.size() > 0) {
 
                    if (FlutterBoost.instance().currentActivity() != null) {
                        list.get(0).setDislikeCallback(FlutterBoost.instance().currentActivity(), new TTAdDislike.DislikeInteractionCallback() {
                            @Override
                            public void onShow() {
 
                            }
 
                            @Override
                            public void onSelected(int i, String s, boolean b) {
                                //关闭广告
                                methodChannel.invokeMethod("close", null);
                            }
 
                            @Override
                            public void onCancel() {
 
                            }
                        });
                    }
                    list.get(0).render();
                    fl_container.removeAllViews();
                    if (list.get(0).getExpressAdView().getParent() != null) {
                        ((ViewGroup) list.get(0).getExpressAdView().getParent()).removeAllViews();
                    }
                    fl_container.addView(list.get(0).getExpressAdView());
                }
            }
        });
 
    }
 
    @Override
    public void dispose() {
 
    }
}