44import android .view .LayoutInflater ;
55import android .view .View ;
66import android .view .ViewGroup ;
7- import android .widget .Button ;
87import android .widget .TextView ;
98import androidx .annotation .NonNull ;
9+ import androidx .annotation .Nullable ;
1010import androidx .fragment .app .Fragment ;
1111import co .optable .android_sdk .OptableSDK ;
1212import co .optable .demoappjava .MainActivity ;
1515import com .google .android .gms .ads .admanager .AdManagerAdRequest ;
1616import com .google .android .gms .ads .admanager .AdManagerAdView ;
1717
18+ import java .util .Collection ;
1819import java .util .HashMap ;
1920import java .util .List ;
21+ import java .util .Map ;
2022
2123public class GAMBannerFragment extends Fragment {
2224
2325 private AdManagerAdView mAdView ;
24- private TextView targetingDataView ;
26+ private TextView statusTextView ;
2527
2628 @ Override
27- public View onCreateView (@ NonNull LayoutInflater inflater ,
28- ViewGroup container , Bundle savedInstanceState ) {
29+ public View onCreateView (@ NonNull LayoutInflater inflater , ViewGroup container , Bundle savedInstanceState ) {
2930 View root = inflater .inflate (R .layout .fragment_gambanner , container , false );
31+ initUi (root );
32+ return root ;
33+ }
34+
35+ private void initUi (View root ) {
3036 mAdView = root .findViewById (R .id .publisherAdView );
31- targetingDataView = root .findViewById (R .id .targetingDataView );
32-
33- // loadAdButton loads targeting data and then the GAM banner:
34- Button btn = root .findViewById (R .id .loadAdButton );
35- btn .setOnClickListener (view -> {
36- targetingDataView .setText ("" );
37-
38- MainActivity .OPTABLE .targeting ().observe (getViewLifecycleOwner (), result -> {
39- AdManagerAdRequest .Builder adRequest = new AdManagerAdRequest .Builder ();
40- final StringBuilder msg = new StringBuilder ();
41- msg .append (targetingDataView .getText ().toString ());
42-
43- if (result .getStatus () == OptableSDK .Status .SUCCESS ) {
44- msg .append ("Loading GAM ad with targeting data:\n \n " );
45- result .getData ().forEach ((key , values ) -> {
46- adRequest .addCustomTargeting (key , values );
47- msg .append (key .toString () + " = " + values .toString ());
48- });
49- } else {
50- msg .append ("OptableSDK Error: " + result .getMessage ());
51- }
52-
53- targetingDataView .setText (msg .toString ());
54- mAdView .loadAd (adRequest .build ());
55- profile ();
56- witness ();
57- });
58- });
59-
60- // loadAdButton2 loads targeting data from cache, and then the GAM banner:
61- btn = root .findViewById (R .id .loadAdButton2 );
62- btn .setOnClickListener (view -> {
63- targetingDataView .setText ("" );
64- AdRequest .Builder adRequest = new AdRequest .Builder ();
65- final StringBuilder msg = new StringBuilder ();
66- HashMap <String , List <String >> data = MainActivity .OPTABLE .targetingFromCache ();
67-
68- if (data != null ) {
69- msg .append ("Loading GAM ad with cached targeting data:\n \n " );
70- data .forEach ((key , values ) -> {
71- adRequest .addCustomTargeting (key , values );
72- msg .append (key .toString () + " = " + values .toString ());
37+ statusTextView = root .findViewById (R .id .targetingDataView );
38+
39+ root .findViewById (R .id .btnLoadBanner ).setOnClickListener (view -> onClickLoadAd ());
40+ root .findViewById (R .id .btnCachedBanner ).setOnClickListener (view -> onClickCachedBanner ());
41+ root .findViewById (R .id .btnClearCache ).setOnClickListener (view -> onClickClearCache ());
42+ }
43+
44+ /**
45+ * Loads targeting data and then the GAM banner
46+ */
47+ private void onClickLoadAd () {
48+ statusTextView .setText ("" );
49+
50+ MainActivity .OPTABLE
51+ .targeting ()
52+ .observe (getViewLifecycleOwner (), result -> {
53+ AdManagerAdRequest .Builder adRequest = new AdManagerAdRequest .Builder ();
54+
55+ if (result .getStatus () == OptableSDK .Status .SUCCESS ) {
56+ HashMap <String , List <String >> data = result .getData ();
57+ changeStatusText ("Loading GAM ad with targeting data" , data );
58+
59+ if (data != null ) {
60+ for (String key : data .keySet ()) {
61+ List <String > values = data .get (key );
62+ if (values == null ) continue ;
63+ adRequest .addCustomTargeting (key , values );
64+ }
65+ }
66+ } else {
67+ changeStatusText ("Error getting targeting data: " + result .getMessage (), null );
68+ }
69+
70+ mAdView .loadAd (adRequest .build ());
71+ profile ();
72+ witness ();
7373 });
74- } else {
75- msg .append ("Targeting data cache empty." );
76- }
74+ }
7775
78- targetingDataView .setText (msg .toString ());
79- mAdView .loadAd (adRequest .build ());
80- profile ();
81- witness ();
82- });
76+ /**
77+ * Loads targeting data from cache and then the GAM banner
78+ */
79+ private void onClickCachedBanner () {
80+ statusTextView .setText ("" );
81+
82+ AdRequest .Builder adRequest = new AdRequest .Builder ();
83+ HashMap <String , List <String >> data = MainActivity .OPTABLE .targetingFromCache ();
84+
85+ if (data != null ) {
86+ changeStatusText ("Loading GAM ad with cached targeting data" , data );
87+ for (String key : data .keySet ()) {
88+ List <String > values = data .get (key );
89+ if (values == null ) continue ;
90+ adRequest .addCustomTargeting (key , values );
91+ }
92+ } else {
93+ changeStatusText ("Targeting data cache empty." , null );
94+ }
8395
84- // loadAdButton3 clears targeting data cache:
85- btn = root .findViewById (R .id .loadAdButton3 );
86- btn .setOnClickListener (view -> {
87- targetingDataView .setText ("Clearing targeting data cache.\n \n " );
88- MainActivity .OPTABLE .targetingClearCache ();
89- });
96+ mAdView .loadAd (adRequest .build ());
97+ profile ();
98+ witness ();
99+ }
90100
91- return root ;
101+ /**
102+ * Clears targeting data cache.
103+ */
104+ private void onClickClearCache () {
105+ statusTextView .setText ("Clearing targeting data cache.\n \n " );
106+ MainActivity .OPTABLE .targetingClearCache ();
92107 }
93108
94109 private void profile () {
95- HashMap <String ,Object > traits = new HashMap <String , Object >();
110+ HashMap <String , Object > traits = new HashMap <>();
96111 traits .put ("gender" , "F" );
97112 traits .put ("age" , 38 );
98113 traits .put ("hasAccount" , true );
99114
100115 MainActivity .OPTABLE
101116 .profile (traits )
102117 .observe (getViewLifecycleOwner (), result -> {
103- final StringBuilder msg = new StringBuilder ();
104- msg .append (targetingDataView .getText ().toString ());
105-
106118 if (result .getStatus () == OptableSDK .Status .SUCCESS ) {
107- msg . append ( " \n \n Success calling profile API to set user traits. \n \n " );
119+ appendStatusText ( "Success calling profile API to set traits on user. " );
108120 } else {
109- msg . append ( " \n \n OptableSDK Error: " + result .getMessage () + " \n \n " );
121+ appendStatusText ( " Error during sending profile : " + result .getMessage ());
110122 }
111-
112- targetingDataView .setText (msg .toString ());
113123 });
114124 }
115125
116126 private void witness () {
117- HashMap <String ,Object > eventProperties = new HashMap <String , Object >();
127+ HashMap <String , Object > eventProperties = new HashMap <>();
118128 eventProperties .put ("exampleKey" , "exampleValue" );
119129 eventProperties .put ("exampleKey2" , 123 );
120130 eventProperties .put ("exampleKey3" , false );
121131
122132 MainActivity .OPTABLE
123133 .witness ("GAMBannerFragment.loadAdButtonClicked" , eventProperties )
124134 .observe (getViewLifecycleOwner (), result -> {
125- final StringBuilder msg = new StringBuilder ();
126- msg .append (targetingDataView .getText ().toString ());
127-
128135 if (result .getStatus () == OptableSDK .Status .SUCCESS ) {
129- msg . append ( " \n \n Success calling witness API to log loadAdButtonClicked event.\n \n " );
136+ appendStatusText ( "Success calling witness API to log loadAdButtonClicked event." );
130137 } else {
131- msg . append ( " \n \n OptableSDK Error: " + result .getMessage () + " \n \n " );
138+ appendStatusText ( " Error during sending witness : " + result .getMessage ());
132139 }
133-
134- targetingDataView .setText (msg .toString ());
135140 });
136141 }
142+
143+ private void changeStatusText (@ NonNull String message , @ Nullable HashMap <String , List <String >> optableResponse ) {
144+ StringBuilder formattedMessage = new StringBuilder (message );
145+ if (optableResponse != null ) {
146+ formattedMessage .append ("\n \n Targeting data: " );
147+ for (Map .Entry <String , ? extends Collection <?>> entry : optableResponse .entrySet ()) {
148+ formattedMessage .append (entry .getKey ())
149+ .append (" = " )
150+ .append (entry .getValue ())
151+ .append ("\n " );
152+ }
153+ }
154+ statusTextView .setText (formattedMessage .toString ());
155+ }
156+
157+ private void appendStatusText (@ NonNull String message ) {
158+ statusTextView .append ("\n \n " + message );
159+ }
160+
137161}
0 commit comments