|
| 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.os.Bundle; |
| 19 | +import android.util.Log; |
| 20 | +import androidx.annotation.NonNull; |
| 21 | +import com.google.ads.mediation.admob.AdMobAdapter; |
| 22 | +import com.google.android.gms.ads.AdFormat; |
| 23 | +import com.google.android.gms.ads.AdSize; |
| 24 | +import com.google.android.gms.ads.admanager.AdManagerAdRequest; |
| 25 | +import com.google.android.gms.ads.query.QueryInfo; |
| 26 | +import com.google.android.gms.ads.query.QueryInfoGenerationCallback; |
| 27 | + |
| 28 | +/** Java code snippets for the developer guide. */ |
| 29 | +public class AdManagerSCARSnippets { |
| 30 | + |
| 31 | + private static final String TAG = "AdManagerSCARSnippets"; |
| 32 | + |
| 33 | + public void loadNative(Context applicationContext, String adUnitID) { |
| 34 | + // [START signal_request_native] |
| 35 | + // Specify the "query_info_type" as "requester_type_8" to |
| 36 | + // denote that the usage of QueryInfo is for Ad Manager S2S. |
| 37 | + Bundle extras = new Bundle(); |
| 38 | + extras.putString("query_info_type", "requester_type_8"); |
| 39 | + |
| 40 | + // Create a signal request for an ad. |
| 41 | + AdManagerAdRequest signalRequest = |
| 42 | + new AdManagerAdRequest.Builder() |
| 43 | + .addNetworkExtrasBundle(AdMobAdapter.class, extras) |
| 44 | + .setRequestAgent("REQUEST_AGENT") |
| 45 | + .build(); |
| 46 | + |
| 47 | + // Generate and send the signal request. |
| 48 | + QueryInfo.generate( |
| 49 | + applicationContext, |
| 50 | + AdFormat.NATIVE, |
| 51 | + signalRequest, |
| 52 | + adUnitID, |
| 53 | + new QueryInfoGenerationCallback() { |
| 54 | + @Override |
| 55 | + public void onSuccess(@NonNull final QueryInfo queryInfo) { |
| 56 | + Log.d(TAG, "QueryInfo string: " + queryInfo.getQuery()); |
| 57 | + // TODO: Fetch the ad response using your generated query info. |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public void onFailure(@NonNull String error) { |
| 62 | + Log.d(TAG, "QueryInfo failed with error: " + error); |
| 63 | + // TODO: Handle error. |
| 64 | + } |
| 65 | + }); |
| 66 | + // [END signal_request_native] |
| 67 | + } |
| 68 | + |
| 69 | + public void loadBanner(Context applicationContext, String adUnitID) { |
| 70 | + // [START signal_request_banner] |
| 71 | + // Specify the "query_info_type" as "requester_type_8" to |
| 72 | + // denote that the usage of QueryInfo is for Ad Manager S2S. |
| 73 | + Bundle extras = new Bundle(); |
| 74 | + extras.putString("query_info_type", "requester_type_8"); |
| 75 | + |
| 76 | + // Set the adaptive banner size. |
| 77 | + // Refer to the AdSize class for available ad sizes. |
| 78 | + AdSize size = AdSize.getCurrentOrientationInlineAdaptiveBannerAdSize(applicationContext, 320); |
| 79 | + extras.putInt("adaptive_banner_w", size.getWidth()); |
| 80 | + extras.putInt("adaptive_banner_h", size.getHeight()); |
| 81 | + |
| 82 | + // Create a signal request for an ad. |
| 83 | + AdManagerAdRequest signalRequest = |
| 84 | + new AdManagerAdRequest.Builder() |
| 85 | + .addNetworkExtrasBundle(AdMobAdapter.class, extras) |
| 86 | + .setRequestAgent("REQUEST_AGENT") |
| 87 | + .build(); |
| 88 | + |
| 89 | + // Generate and send the signal request. |
| 90 | + QueryInfo.generate( |
| 91 | + applicationContext, |
| 92 | + AdFormat.BANNER, |
| 93 | + signalRequest, |
| 94 | + adUnitID, |
| 95 | + new QueryInfoGenerationCallback() { |
| 96 | + @Override |
| 97 | + public void onSuccess(@NonNull final QueryInfo queryInfo) { |
| 98 | + Log.d(TAG, "QueryInfo string: " + queryInfo.getQuery()); |
| 99 | + // TODO: Fetch the ad response using your generated query info. |
| 100 | + } |
| 101 | + |
| 102 | + @Override |
| 103 | + public void onFailure(@NonNull String error) { |
| 104 | + Log.d(TAG, "QueryInfo failed with error: " + error); |
| 105 | + // TODO: Handle error. |
| 106 | + } |
| 107 | + }); |
| 108 | + // [END signal_request_banner] |
| 109 | + } |
| 110 | + |
| 111 | + public void loadNativePlusBanner(Context applicationContext, String adUnitID) { |
| 112 | + // [START signal_request_native_plus_banner] |
| 113 | + // Specify the "query_info_type" as "requester_type_8" to |
| 114 | + // denote that the usage of QueryInfo is for Ad Manager S2S. |
| 115 | + Bundle extras = new Bundle(); |
| 116 | + extras.putString("query_info_type", "requester_type_8"); |
| 117 | + |
| 118 | + // Set the adaptive banner size. |
| 119 | + // Refer to the AdSize class for available ad sizes. |
| 120 | + AdSize size = AdSize.getCurrentOrientationInlineAdaptiveBannerAdSize(applicationContext, 320); |
| 121 | + extras.putInt("adaptive_banner_w", size.getWidth()); |
| 122 | + extras.putInt("adaptive_banner_h", size.getHeight()); |
| 123 | + |
| 124 | + // Create a signal request for an ad. |
| 125 | + AdManagerAdRequest signalRequest = |
| 126 | + new AdManagerAdRequest.Builder() |
| 127 | + .addNetworkExtrasBundle(AdMobAdapter.class, extras) |
| 128 | + .setRequestAgent("REQUEST_AGENT") |
| 129 | + .build(); |
| 130 | + |
| 131 | + // Generate and send the signal request. |
| 132 | + QueryInfo.generate( |
| 133 | + applicationContext, |
| 134 | + AdFormat.NATIVE, |
| 135 | + signalRequest, |
| 136 | + adUnitID, |
| 137 | + new QueryInfoGenerationCallback() { |
| 138 | + @Override |
| 139 | + public void onSuccess(@NonNull final QueryInfo queryInfo) { |
| 140 | + Log.d(TAG, "QueryInfo string: " + queryInfo.getQuery()); |
| 141 | + // TODO: Fetch the ad response using your generated query info. |
| 142 | + } |
| 143 | + |
| 144 | + @Override |
| 145 | + public void onFailure(@NonNull String error) { |
| 146 | + Log.d(TAG, "QueryInfo failed with error: " + error); |
| 147 | + // TODO: Handle error. |
| 148 | + } |
| 149 | + }); |
| 150 | + // [END signal_request_native_plus_banner] |
| 151 | + } |
| 152 | +} |
0 commit comments