forked from vpnhood/VpnHood
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AdMobUtil.cs
49 lines (41 loc) · 1.76 KB
/
AdMobUtil.cs
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
using Android.Content;
using Android.Gms.Ads;
using Android.Gms.Ads.Initialization;
using VpnHood.Common.Exceptions;
using VpnHood.Common.Utils;
namespace VpnHood.Client.App.Droid.Ads.VhAdMob;
public class AdMobUtil
{
private static readonly AsyncLock InitLock = new();
public static bool IsInitialized { get; private set; }
public static TimeSpan DefaultAdTimeSpan { get; } = TimeSpan.FromMinutes(45);
public static async Task Initialize(Context context, CancellationToken cancellationToken)
{
using var lockAsync = await InitLock.LockAsync(cancellationToken);
if (IsInitialized)
return;
var initializeListener = new MyOnInitializationCompleteListener();
MobileAds.Initialize(context, initializeListener);
await initializeListener.Task;
IsInitialized = true;
}
private class MyOnInitializationCompleteListener : Java.Lang.Object, IOnInitializationCompleteListener
{
private readonly TaskCompletionSource _loadedCompletionSource = new();
public Task Task => _loadedCompletionSource.Task;
public void OnInitializationComplete(IInitializationStatus initializationStatus)
{
// no adapter
if (initializationStatus.AdapterStatusMap.Keys.Count == 0)
throw new AdException("Could not find any ad adapter.");
// at-least one ok
if (initializationStatus.AdapterStatusMap.Values.Any(value =>
value.InitializationState == AdapterStatusState.Ready)) {
_loadedCompletionSource.TrySetResult();
return;
}
// not success
_loadedCompletionSource.TrySetException(new AdException("Could not initialize any ad adapter."));
}
}
}