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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
use crossbow_android::{error::*, jni::JavaVM, plugin::*};
use std::sync::Arc;

pub struct AdMobPlugin {
    singleton: Arc<JniSingleton>,
    vm: Arc<JavaVM>,
}

impl CrossbowPlugin for AdMobPlugin {
    fn from_java_vm(vm: Arc<JavaVM>) -> Result<Self>
    where
        Self: Sized,
    {
        let singleton = get_jni_singleton(Self::get_plugin_name()).ok_or_else(|| {
            AndroidError::SingletonNotRegistered(Self::get_plugin_name().to_owned())
        })?;
        Ok(Self { singleton, vm })
    }

    fn get_plugin_name() -> &'static str {
        "CrossbowAdMob"
    }

    fn get_receiver(&self) -> &Receiver<Signal> {
        self.singleton.get_receiver()
    }
}

impl AdMobPlugin {
    // TODO: Make async API
    // pub async fn initialize_async<S>(
    //     &self,
    //     is_for_child_directed_treatment: bool,
    //     max_ad_content_rating: S,
    //     is_real: bool,
    //     is_test_europe_user_consent: bool,
    // ) -> Result<()>
    // where
    //     S: AsRef<str>,
    // {
    //     self.initialize(
    //         is_for_child_directed_treatment,
    //         max_ad_content_rating,
    //         is_real,
    //         is_test_europe_user_consent,
    //     )?;
    //     // loop {
    //     //     self.get_receiver().recv().await?;
    //     // }
    //     Ok(())
    // }

    // TODO: Fix initialization_complete Signal not being sent
    pub fn initialize<S>(
        &self,
        is_for_child_directed_treatment: bool,
        max_ad_content_rating: S,
        is_real: bool,
        is_test_europe_user_consent: bool,
    ) -> Result<()>
    where
        S: AsRef<str>,
    {
        let jnienv = self.vm.attach_current_thread_as_daemon()?;
        let g_str = jnienv.new_string(max_ad_content_rating)?;
        self.singleton.call_method(
            &jnienv,
            "initialize",
            &[
                is_for_child_directed_treatment.into(),
                g_str.into(),
                is_real.into(),
                is_test_europe_user_consent.into(),
            ],
        )?;
        jnienv.exception_check()?;
        Ok(())
    }

    pub fn is_initialized(&self) -> Result<bool> {
        let jnienv = self.vm.attach_current_thread_as_daemon()?;
        let val = self
            .singleton
            .call_method(&jnienv, "getIsInitialized", &[])?;
        Ok(val.z()?)
    }

    pub fn load_interstitial(&self, ad_id: &str) -> Result<()> {
        let jnienv = self.vm.attach_current_thread_as_daemon()?;
        let ad_id = jnienv.new_string(ad_id.to_string())?;
        self.singleton
            .call_method(&jnienv, "loadInterstitial", &[ad_id.into()])?;
        jnienv.exception_check()?;
        Ok(())
    }

    pub fn is_interstitial_loaded(&self) -> Result<bool> {
        let jnienv = self.vm.attach_current_thread_as_daemon()?;
        let val = self
            .singleton
            .call_method(&jnienv, "getIsInterstitialLoaded", &[])?;
        Ok(val.z()?)
    }

    pub fn show_interstitial(&self) -> Result<()> {
        let jnienv = self.vm.attach_current_thread_as_daemon()?;
        self.singleton
            .call_method(&jnienv, "showInterstitial", &[])?;
        jnienv.exception_check()?;
        Ok(())
    }

    pub fn request_user_consent(&self) -> Result<()> {
        let jnienv = self.vm.attach_current_thread_as_daemon()?;
        self.singleton
            .call_method(&jnienv, "requestUserConsent", &[])?;
        jnienv.exception_check()?;
        Ok(())
    }

    pub fn reset_consent_state(&self) -> Result<()> {
        let jnienv = self.vm.attach_current_thread_as_daemon()?;
        self.singleton
            .call_method(&jnienv, "resetConsentState", &[])?;
        jnienv.exception_check()?;
        Ok(())
    }

    pub fn load_banner<S>(
        &self,
        ad_unit_id: S,
        position: i32,
        size: BannerSize,
        show_instantly: bool,
        respect_safe_area: bool,
    ) -> Result<()>
    where
        S: AsRef<str>,
    {
        let jnienv = self.vm.attach_current_thread_as_daemon()?;
        let ad_unit_id = jnienv.new_string(ad_unit_id)?;
        let size = jnienv.new_string(size.to_string())?;
        self.singleton.call_method(
            &jnienv,
            "loadBanner",
            &[
                ad_unit_id.into(),
                position.into(),
                size.into(),
                show_instantly.into(),
                respect_safe_area.into(),
            ],
        )?;
        jnienv.exception_check()?;
        Ok(())
    }

    pub fn is_banner_loaded(&self) -> Result<bool> {
        let jnienv = self.vm.attach_current_thread_as_daemon()?;
        let val = self
            .singleton
            .call_method(&jnienv, "getIsBannerLoaded", &[])?;
        Ok(val.z()?)
    }

    pub fn destroy_banner(&self) -> Result<()> {
        let jnienv = self.vm.attach_current_thread_as_daemon()?;
        self.singleton.call_method(&jnienv, "destroyBanner", &[])?;
        jnienv.exception_check()?;
        Ok(())
    }

    pub fn show_banner(&self) -> Result<()> {
        let jnienv = self.vm.attach_current_thread_as_daemon()?;
        self.singleton.call_method(&jnienv, "showBanner", &[])?;
        jnienv.exception_check()?;
        Ok(())
    }

    pub fn hide_banner(&self) -> Result<()> {
        let jnienv = self.vm.attach_current_thread_as_daemon()?;
        self.singleton.call_method(&jnienv, "hideBanner", &[])?;
        jnienv.exception_check()?;
        Ok(())
    }

    pub fn banner_width(&self) -> Result<i32> {
        let jnienv = self.vm.attach_current_thread_as_daemon()?;
        let val = self.singleton.call_method(&jnienv, "getBannerWidth", &[])?;
        Ok(val.i()?)
    }

    pub fn banner_height(&self) -> Result<i32> {
        let jnienv = self.vm.attach_current_thread_as_daemon()?;
        let val = self
            .singleton
            .call_method(&jnienv, "getBannerHeight", &[])?;
        Ok(val.i()?)
    }

    pub fn banner_width_in_pixels(&self) -> Result<i32> {
        let jnienv = self.vm.attach_current_thread_as_daemon()?;
        let val = self
            .singleton
            .call_method(&jnienv, "getBannerWidthInPixels", &[])?;
        Ok(val.i()?)
    }

    pub fn banner_height_in_pixels(&self) -> Result<i32> {
        let jnienv = self.vm.attach_current_thread_as_daemon()?;
        let val = self
            .singleton
            .call_method(&jnienv, "getBannerHeightInPixels", &[])?;
        Ok(val.i()?)
    }

    pub fn load_rewarded<S>(&self, ad_unit_id: S) -> Result<()>
    where
        S: AsRef<str>,
    {
        let jnienv = self.vm.attach_current_thread_as_daemon()?;
        let ad_unit_id = jnienv.new_string(ad_unit_id)?;
        self.singleton
            .call_method(&jnienv, "loadRewarded", &[ad_unit_id.into()])?;
        jnienv.exception_check()?;
        Ok(())
    }

    pub fn is_rewarded_loaded(&self) -> Result<bool> {
        let jnienv = self.vm.attach_current_thread_as_daemon()?;
        let val = self
            .singleton
            .call_method(&jnienv, "getIsRewardedLoaded", &[])?;
        Ok(val.z()?)
    }

    pub fn show_rewarded(&self) -> Result<()> {
        let jnienv = self.vm.attach_current_thread_as_daemon()?;
        self.singleton.call_method(&jnienv, "showRewarded", &[])?;
        jnienv.exception_check()?;
        Ok(())
    }

    pub fn load_rewarded_interstitial<S>(&self, ad_unit_id: S) -> Result<()>
    where
        S: AsRef<str>,
    {
        let jnienv = self.vm.attach_current_thread_as_daemon()?;
        let ad_unit_id = jnienv.new_string(ad_unit_id)?;
        self.singleton
            .call_method(&jnienv, "loadRewardedInterstitial", &[ad_unit_id.into()])?;
        jnienv.exception_check()?;
        Ok(())
    }

    pub fn is_rewarded_interstitial_loaded(&self) -> Result<bool> {
        let jnienv = self.vm.attach_current_thread_as_daemon()?;
        let val = self
            .singleton
            .call_method(&jnienv, "getIsRewardedInterstitialLoaded", &[])?;
        Ok(val.z()?)
    }

    pub fn show_rewarded_interstitial(&self) -> Result<()> {
        let jnienv = self.vm.attach_current_thread_as_daemon()?;
        self.singleton
            .call_method(&jnienv, "showRewardedInterstitial", &[])?;
        jnienv.exception_check()?;
        Ok(())
    }
}

#[derive(Clone, Copy, Default)]
pub enum BannerSize {
    Banner,
    LargeBanner,
    MediumRectangle,
    FullBanner,
    Leaderboard,
    Adaptive,
    #[default]
    SmartBanner,
}

impl ToString for BannerSize {
    fn to_string(&self) -> String {
        match self {
            Self::Banner => "BANNER".to_string(),
            Self::LargeBanner => "LARGE_BANNER".to_string(),
            Self::MediumRectangle => "MEDIUM_RECTANGLE".to_string(),
            Self::FullBanner => "FULL_BANNER".to_string(),
            Self::Leaderboard => "LEADERBOARD".to_string(),
            Self::Adaptive => "ADAPTIVE".to_string(),
            Self::SmartBanner => "SMART_BANNER".to_string(),
        }
    }
}