|
| 1 | +// Copyright 2025 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package com.google.android.gms.snippets; |
| 16 | + |
| 17 | +import android.content.Context; |
| 18 | +import android.util.Log; |
| 19 | +import com.google.android.gms.ads.mediation.InitializationCompleteCallback; |
| 20 | +import com.google.android.gms.ads.mediation.MediationConfiguration; |
| 21 | +import com.google.android.gms.ads.mediation.VersionInfo; |
| 22 | +import com.google.android.gms.ads.mediation.rtb.RtbAdapter; |
| 23 | +import com.google.android.gms.ads.mediation.rtb.RtbSignalData; |
| 24 | +import com.google.android.gms.ads.mediation.rtb.SignalCallbacks; |
| 25 | +import java.util.List; |
| 26 | + |
| 27 | +/** Java code snippets for the developer guide. */ |
| 28 | +public class SecureSignalSampleAdapterSnippets extends RtbAdapter { |
| 29 | + |
| 30 | + private static final String TAG = "SecureSignalSampleAdapterSnippets"; |
| 31 | + |
| 32 | + // [START initialize_adapter] |
| 33 | + @Override |
| 34 | + public void initialize(Context context, InitializationCompleteCallback initializationCompleteCallback, List<MediationConfiguration> configurations) { |
| 35 | + // Initialize your ad network's SDK here. |
| 36 | + YourSdk.initialize(); |
| 37 | + |
| 38 | + // Invoke the InitializationCompleteCallback once initialization completes. |
| 39 | + initializationCompleteCallback.onInitializationSucceeded(); |
| 40 | + } |
| 41 | + |
| 42 | + // [END initialize_adapter] |
| 43 | + |
| 44 | + // [START get_sdk_version_info] |
| 45 | + @Override |
| 46 | + public VersionInfo getSDKVersionInfo() { |
| 47 | + // Get your SDK's version as a string. E.g. "1.2.3" |
| 48 | + String versionString = YourSdk.getVersion(); |
| 49 | + String[] splits = versionString.split("\\."); |
| 50 | + if (splits.length >= 3) { |
| 51 | + int major = Integer.parseInt(splits[0]); |
| 52 | + int minor = Integer.parseInt(splits[1]); |
| 53 | + int micro = Integer.parseInt(splits[2]); |
| 54 | + return new VersionInfo(major, minor, micro); |
| 55 | + } |
| 56 | + |
| 57 | + Log.w( |
| 58 | + TAG, |
| 59 | + String.format( |
| 60 | + "Unexpected SDK version format: %s. Returning 0.0.0 for SDK version.", versionString)); |
| 61 | + return new VersionInfo(0, 0, 0); |
| 62 | + } |
| 63 | + |
| 64 | + // [END get_sdk_version_info] |
| 65 | + |
| 66 | + // [START get_adapter_version_info] |
| 67 | + @Override |
| 68 | + public VersionInfo getVersionInfo() { |
| 69 | + // If your secure signals SDK implements this adapter in the same binary, return the same |
| 70 | + // version as your SDK: |
| 71 | + // return getSDKVersionInfo(); |
| 72 | + |
| 73 | + // If you built a separate binary for this secure signals adapter, return the adapter's version: |
| 74 | + // Get your adapters's version as a string. E.g. "4.5.6" |
| 75 | + String versionString = "4.5.6"; // Replace with your adapter's version. |
| 76 | + String[] splits = versionString.split("\\."); |
| 77 | + if (splits.length >= 3) { |
| 78 | + int major = Integer.parseInt(splits[0]); |
| 79 | + int minor = Integer.parseInt(splits[1]); |
| 80 | + int micro = Integer.parseInt(splits[2]); |
| 81 | + return new VersionInfo(major, minor, micro); |
| 82 | + } |
| 83 | + |
| 84 | + Log.w( |
| 85 | + TAG, |
| 86 | + String.format( |
| 87 | + "Unexpected adapter version format: %s. Returning 0.0.0 for adapter version.", |
| 88 | + versionString)); |
| 89 | + return new VersionInfo(0, 0, 0); |
| 90 | + } |
| 91 | + |
| 92 | + // [END get_adapter_version_info] |
| 93 | + |
| 94 | + // [START collect_signals] |
| 95 | + @Override |
| 96 | + public void collectSignals(RtbSignalData rtbSignalData, SignalCallbacks signalCallbacks) { |
| 97 | + |
| 98 | + YourSdk.getSignals( |
| 99 | + new YourSdk.GetSignalsCallback() { |
| 100 | + @Override |
| 101 | + public void onSuccess(String signals) { |
| 102 | + signalCallbacks.onSuccess(signals); |
| 103 | + } |
| 104 | + |
| 105 | + @Override |
| 106 | + public void onFailure(String error) { |
| 107 | + // Handle the error. |
| 108 | + } |
| 109 | + }); |
| 110 | + } |
| 111 | + |
| 112 | + // [END collect_signals] |
| 113 | + |
| 114 | + /** |
| 115 | + * A mock SDK class used for demonstration purposes in code snippets. In a real adapter |
| 116 | + * implementation, you would replace calls to this class with calls to your actual SDK. |
| 117 | + */ |
| 118 | + public static final class YourSdk { |
| 119 | + |
| 120 | + private static final String SDK_VERSION = "1.2.3"; |
| 121 | + private static final String MOCK_SIGNAL = "sample_signal"; |
| 122 | + |
| 123 | + // Private constructor to prevent instantiation. |
| 124 | + private YourSdk() {} |
| 125 | + |
| 126 | + /** Mock SDK initialization method. */ |
| 127 | + public static void initialize() { |
| 128 | + // No-op for mock. |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * Mock SDK version retrieval method. |
| 133 | + * |
| 134 | + * @return The mock SDK version string. |
| 135 | + */ |
| 136 | + public static String getVersion() { |
| 137 | + return SDK_VERSION; |
| 138 | + } |
| 139 | + |
| 140 | + /** An interface for receiving signals asynchronously. */ |
| 141 | + public interface GetSignalsCallback { |
| 142 | + void onSuccess(String signals); |
| 143 | + |
| 144 | + void onFailure(String error); |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * Mock SDK signal collection method. |
| 149 | + * |
| 150 | + * @param callback The callback to be invoked when signal collection completes. |
| 151 | + */ |
| 152 | + public static void getSignals(GetSignalsCallback callback) { |
| 153 | + // If your SDK provides signals asynchronously, it might look like this: |
| 154 | + callback.onSuccess(MOCK_SIGNAL); |
| 155 | + } |
| 156 | + } |
| 157 | +} |
0 commit comments