|
| 1 | +/* |
| 2 | + * Copyright (C) 2025 Google, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.unity.ads.decagon; |
| 18 | + |
| 19 | +import android.app.Activity; |
| 20 | +import android.content.pm.ApplicationInfo; |
| 21 | +import android.content.pm.PackageManager; |
| 22 | +import android.os.Bundle; |
| 23 | +import android.util.Log; |
| 24 | +import androidx.annotation.Nullable; |
| 25 | +import com.google.android.libraries.ads.mobile.sdk.MobileAds; |
| 26 | +import com.google.android.libraries.ads.mobile.sdk.common.RequestConfiguration; |
| 27 | +import com.google.android.libraries.ads.mobile.sdk.initialization.InitializationConfig; |
| 28 | +import com.google.android.libraries.ads.mobile.sdk.initialization.OnAdapterInitializationCompleteListener; |
| 29 | +import com.google.errorprone.annotations.concurrent.GuardedBy; |
| 30 | + |
| 31 | +/** Mobile Ads implementation for the Google Mobile Ads Unity plugin. */ |
| 32 | +public final class UnityMobileAds { |
| 33 | + private static final String TAG = "UnityMobileAds"; |
| 34 | + private static final String APPLICATION_ID_KEY = "com.google.android.gms.ads.APPLICATION_ID"; |
| 35 | + |
| 36 | + private static final Object stateLock = new Object(); |
| 37 | + |
| 38 | + @GuardedBy("stateLock") |
| 39 | + private static volatile boolean isMobileAdsInitialized = false; |
| 40 | + |
| 41 | + @GuardedBy("stateLock") |
| 42 | + private static volatile RequestConfiguration requestConfiguration; |
| 43 | + |
| 44 | + @GuardedBy("stateLock") |
| 45 | + private static volatile float userVolume = -1; |
| 46 | + |
| 47 | + @GuardedBy("stateLock") |
| 48 | + private static volatile boolean isMuted = false; |
| 49 | + |
| 50 | + @GuardedBy("stateLock") |
| 51 | + private static volatile boolean isPublisherFirstPartyIdEnabled = false; |
| 52 | + |
| 53 | + private UnityMobileAds() {} |
| 54 | + |
| 55 | + /** |
| 56 | + * Initializes the Google Mobile Ads SDK. |
| 57 | + * |
| 58 | + * @param activity The {@link Activity} to use for initializing Google Mobile Ads SDK. |
| 59 | + * @param callback The {@link OnAdapterInitializationCompleteListener} to be called when Google |
| 60 | + * Mobile Ads SDK is initialized. |
| 61 | + */ |
| 62 | + public static void initialize( |
| 63 | + Activity activity, OnAdapterInitializationCompleteListener callback) { |
| 64 | + String appId = getApplicationMetaData(activity, APPLICATION_ID_KEY); |
| 65 | + |
| 66 | + if (appId == null) { |
| 67 | + Log.e(TAG, "Application ID is null. Cannot initialize the Google Mobile Ads SDK."); |
| 68 | + return; |
| 69 | + } |
| 70 | + |
| 71 | + InitializationConfig.Builder configBuilder = new InitializationConfig.Builder(appId); |
| 72 | + synchronized (stateLock) { |
| 73 | + if (requestConfiguration != null) { |
| 74 | + configBuilder = configBuilder.setRequestConfiguration(requestConfiguration); |
| 75 | + } |
| 76 | + } |
| 77 | + InitializationConfig config = configBuilder.build(); |
| 78 | + // Initialize the Google Mobile Ads SDK on a background thread. |
| 79 | + new Thread( |
| 80 | + () -> |
| 81 | + MobileAds.initialize( |
| 82 | + activity, |
| 83 | + config, |
| 84 | + initializationStatus -> { |
| 85 | + synchronized (stateLock) { |
| 86 | + isMobileAdsInitialized = true; |
| 87 | + requestConfiguration = null; |
| 88 | + if (isPublisherFirstPartyIdEnabled) { |
| 89 | + var unused = MobileAds.putPublisherFirstPartyIdEnabled(true); |
| 90 | + isPublisherFirstPartyIdEnabled = false; |
| 91 | + } |
| 92 | + if (userVolume >= 0) { |
| 93 | + MobileAds.setUserControlledAppVolume(userVolume); |
| 94 | + userVolume = -1; |
| 95 | + } |
| 96 | + if (isMuted) { |
| 97 | + MobileAds.setUserMutedApp(isMuted); |
| 98 | + isMuted = false; |
| 99 | + } |
| 100 | + } |
| 101 | + callback.onAdapterInitializationComplete(initializationStatus); |
| 102 | + })) |
| 103 | + .start(); |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Sets the global {@link RequestConfiguration} that will be used for every ad request. |
| 108 | + * |
| 109 | + * @param config The {@link RequestConfiguration} to used for future ad requests. |
| 110 | + */ |
| 111 | + public static void setRequestConfiguration(RequestConfiguration config) { |
| 112 | + // Cannot set the configuration until after the MobileAds has been initialized. |
| 113 | + synchronized (stateLock) { |
| 114 | + if (!isMobileAdsInitialized) { |
| 115 | + requestConfiguration = config; |
| 116 | + return; |
| 117 | + } |
| 118 | + } |
| 119 | + MobileAds.setRequestConfiguration(config); |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * Sets whether the publisher first party ID is enabled. |
| 124 | + * |
| 125 | + * @param enabled Whether the publisher first party ID is enabled. |
| 126 | + */ |
| 127 | + public static boolean putPublisherFirstPartyIdEnabled(boolean enabled) { |
| 128 | + synchronized (stateLock) { |
| 129 | + if (!isMobileAdsInitialized) { |
| 130 | + isPublisherFirstPartyIdEnabled = enabled; |
| 131 | + return true; |
| 132 | + } |
| 133 | + } |
| 134 | + return MobileAds.putPublisherFirstPartyIdEnabled(enabled); |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * Sets the user-controlled app volume. |
| 139 | + * |
| 140 | + * @param volume The user-controlled app volume. |
| 141 | + */ |
| 142 | + public static void setUserControlledAppVolume(float volume) { |
| 143 | + synchronized (stateLock) { |
| 144 | + if (!isMobileAdsInitialized) { |
| 145 | + userVolume = volume; |
| 146 | + return; |
| 147 | + } |
| 148 | + } |
| 149 | + MobileAds.setUserControlledAppVolume(volume); |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * Sets whether the app is muted. |
| 154 | + * |
| 155 | + * @param muted Whether the app is muted. |
| 156 | + */ |
| 157 | + public static void setApplicationMuted(boolean muted) { |
| 158 | + synchronized (stateLock) { |
| 159 | + if (!isMobileAdsInitialized) { |
| 160 | + isMuted = muted; |
| 161 | + return; |
| 162 | + } |
| 163 | + } |
| 164 | + MobileAds.setUserMutedApp(muted); |
| 165 | + } |
| 166 | + |
| 167 | + /** |
| 168 | + * Retrieves the application metadata value from the given activity. |
| 169 | + * |
| 170 | + * @param activity The {@link Activity} to use for retrieving the application metadata. |
| 171 | + * @param key The key to use for retrieving the application metadata value. |
| 172 | + * @return The application metadata value, or null if not found. |
| 173 | + */ |
| 174 | + @Nullable |
| 175 | + private static String getApplicationMetaData(Activity activity, String key) { |
| 176 | + if (activity == null) { |
| 177 | + Log.e(TAG, "Unity Activity is null. Cannot read Application ID."); |
| 178 | + return null; |
| 179 | + } |
| 180 | + |
| 181 | + try { |
| 182 | + ApplicationInfo appInfo = |
| 183 | + activity |
| 184 | + .getPackageManager() |
| 185 | + .getApplicationInfo(activity.getPackageName(), PackageManager.GET_META_DATA); |
| 186 | + Bundle bundle = appInfo.metaData; |
| 187 | + if (bundle != null && bundle.containsKey(key)) { |
| 188 | + return bundle.getString(key); |
| 189 | + } else { |
| 190 | + Log.e(TAG, "Application ID not found in manifest!"); |
| 191 | + } |
| 192 | + } catch (Exception e) { |
| 193 | + Log.e(TAG, "Error reading application ID from manifest: " + e.getMessage()); |
| 194 | + } |
| 195 | + return null; |
| 196 | + } |
| 197 | +} |
0 commit comments