diff --git a/DemoApp/DemoAppJava/app/src/main/java/co/optable/demoappjava/MainActivity.java b/DemoApp/DemoAppJava/app/src/main/java/co/optable/demoappjava/MainActivity.java index 4f71878..ef7ce31 100644 --- a/DemoApp/DemoAppJava/app/src/main/java/co/optable/demoappjava/MainActivity.java +++ b/DemoApp/DemoAppJava/app/src/main/java/co/optable/demoappjava/MainActivity.java @@ -10,6 +10,7 @@ import com.google.android.material.bottomnavigation.BottomNavigationView; public class MainActivity extends AppCompatActivity { + public static OptableSDK OPTABLE; @Override @@ -19,13 +20,13 @@ protected void onCreate(Bundle savedInstanceState) { MainActivity.OPTABLE = new OptableSDK(this.getApplicationContext(), "sandbox.optable.co", "ios-sdk-demo"); + initUi(); + } + + private void initUi() { BottomNavigationView navView = findViewById(R.id.nav_view); NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment); - // Passing each menu ID as a set of Ids because each - // menu should be considered as top level destinations. - AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder( - R.id.navigation_identify, R.id.navigation_gambanner) - .build(); + AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(R.id.navigation_identify, R.id.navigation_gambanner).build(); NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration); NavigationUI.setupWithNavController(navView, navController); } diff --git a/DemoApp/DemoAppJava/app/src/main/java/co/optable/demoappjava/ui/GAMBanner/GAMBannerFragment.java b/DemoApp/DemoAppJava/app/src/main/java/co/optable/demoappjava/ui/GAMBanner/GAMBannerFragment.java index 90d3a27..d717c4f 100644 --- a/DemoApp/DemoAppJava/app/src/main/java/co/optable/demoappjava/ui/GAMBanner/GAMBannerFragment.java +++ b/DemoApp/DemoAppJava/app/src/main/java/co/optable/demoappjava/ui/GAMBanner/GAMBannerFragment.java @@ -4,9 +4,9 @@ import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; -import android.widget.Button; import android.widget.TextView; import androidx.annotation.NonNull; +import androidx.annotation.Nullable; import androidx.fragment.app.Fragment; import co.optable.android_sdk.OptableSDK; import co.optable.demoappjava.MainActivity; @@ -15,84 +15,99 @@ import com.google.android.gms.ads.admanager.AdManagerAdRequest; import com.google.android.gms.ads.admanager.AdManagerAdView; +import java.util.Collection; import java.util.HashMap; import java.util.List; +import java.util.Map; public class GAMBannerFragment extends Fragment { private AdManagerAdView mAdView; - private TextView targetingDataView; + private TextView statusTextView; @Override - public View onCreateView(@NonNull LayoutInflater inflater, - ViewGroup container, Bundle savedInstanceState) { + public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View root = inflater.inflate(R.layout.fragment_gambanner, container, false); + initUi(root); + return root; + } + + private void initUi(View root) { mAdView = root.findViewById(R.id.publisherAdView); - targetingDataView = root.findViewById(R.id.targetingDataView); - - // loadAdButton loads targeting data and then the GAM banner: - Button btn = root.findViewById(R.id.loadAdButton); - btn.setOnClickListener(view -> { - targetingDataView.setText(""); - - MainActivity.OPTABLE.targeting().observe(getViewLifecycleOwner(), result -> { - AdManagerAdRequest.Builder adRequest = new AdManagerAdRequest.Builder(); - final StringBuilder msg = new StringBuilder(); - msg.append(targetingDataView.getText().toString()); - - if (result.getStatus() == OptableSDK.Status.SUCCESS) { - msg.append("Loading GAM ad with targeting data:\n\n"); - result.getData().forEach((key, values) -> { - adRequest.addCustomTargeting(key, values); - msg.append(key.toString() + " = " + values.toString()); - }); - } else { - msg.append("OptableSDK Error: " + result.getMessage()); - } - - targetingDataView.setText(msg.toString()); - mAdView.loadAd(adRequest.build()); - profile(); - witness(); - }); - }); - - // loadAdButton2 loads targeting data from cache, and then the GAM banner: - btn = root.findViewById(R.id.loadAdButton2); - btn.setOnClickListener(view -> { - targetingDataView.setText(""); - AdRequest.Builder adRequest = new AdRequest.Builder(); - final StringBuilder msg = new StringBuilder(); - HashMap> data = MainActivity.OPTABLE.targetingFromCache(); - - if (data != null) { - msg.append("Loading GAM ad with cached targeting data:\n\n"); - data.forEach((key, values) -> { - adRequest.addCustomTargeting(key, values); - msg.append(key.toString() + " = " + values.toString()); + statusTextView = root.findViewById(R.id.targetingDataView); + + root.findViewById(R.id.btnLoadBanner).setOnClickListener(view -> onClickLoadAd()); + root.findViewById(R.id.btnCachedBanner).setOnClickListener(view -> onClickCachedBanner()); + root.findViewById(R.id.btnClearCache).setOnClickListener(view -> onClickClearCache()); + } + + /** + * Loads targeting data and then the GAM banner + */ + private void onClickLoadAd() { + statusTextView.setText(""); + + MainActivity.OPTABLE + .targeting() + .observe(getViewLifecycleOwner(), result -> { + AdManagerAdRequest.Builder adRequest = new AdManagerAdRequest.Builder(); + + if (result.getStatus() == OptableSDK.Status.SUCCESS) { + HashMap> data = result.getData(); + changeStatusText("Loading GAM ad with targeting data", data); + + if (data != null) { + for (String key : data.keySet()) { + List values = data.get(key); + if (values == null) continue; + adRequest.addCustomTargeting(key, values); + } + } + } else { + changeStatusText("Error getting targeting data: " + result.getMessage(), null); + } + + mAdView.loadAd(adRequest.build()); + profile(); + witness(); }); - } else { - msg.append("Targeting data cache empty."); - } + } - targetingDataView.setText(msg.toString()); - mAdView.loadAd(adRequest.build()); - profile(); - witness(); - }); + /** + * Loads targeting data from cache and then the GAM banner + */ + private void onClickCachedBanner() { + statusTextView.setText(""); + + AdRequest.Builder adRequest = new AdRequest.Builder(); + HashMap> data = MainActivity.OPTABLE.targetingFromCache(); + + if (data != null) { + changeStatusText("Loading GAM ad with cached targeting data", data); + for (String key : data.keySet()) { + List values = data.get(key); + if (values == null) continue; + adRequest.addCustomTargeting(key, values); + } + } else { + changeStatusText("Targeting data cache empty.", null); + } - // loadAdButton3 clears targeting data cache: - btn = root.findViewById(R.id.loadAdButton3); - btn.setOnClickListener(view -> { - targetingDataView.setText("Clearing targeting data cache.\n\n"); - MainActivity.OPTABLE.targetingClearCache(); - }); + mAdView.loadAd(adRequest.build()); + profile(); + witness(); + } - return root; + /** + * Clears targeting data cache. + */ + private void onClickClearCache() { + statusTextView.setText("Clearing targeting data cache.\n\n"); + MainActivity.OPTABLE.targetingClearCache(); } private void profile() { - HashMap traits = new HashMap(); + HashMap traits = new HashMap<>(); traits.put("gender", "F"); traits.put("age", 38); traits.put("hasAccount", true); @@ -100,21 +115,16 @@ private void profile() { MainActivity.OPTABLE .profile(traits) .observe(getViewLifecycleOwner(), result -> { - final StringBuilder msg = new StringBuilder(); - msg.append(targetingDataView.getText().toString()); - if (result.getStatus() == OptableSDK.Status.SUCCESS) { - msg.append("\n\nSuccess calling profile API to set user traits.\n\n"); + appendStatusText("Success calling profile API to set traits on user."); } else { - msg.append("\n\nOptableSDK Error: " + result.getMessage() + "\n\n"); + appendStatusText("Error during sending profile: " + result.getMessage()); } - - targetingDataView.setText(msg.toString()); }); } private void witness() { - HashMap eventProperties = new HashMap(); + HashMap eventProperties = new HashMap<>(); eventProperties.put("exampleKey", "exampleValue"); eventProperties.put("exampleKey2", 123); eventProperties.put("exampleKey3", false); @@ -122,16 +132,30 @@ private void witness() { MainActivity.OPTABLE .witness("GAMBannerFragment.loadAdButtonClicked", eventProperties) .observe(getViewLifecycleOwner(), result -> { - final StringBuilder msg = new StringBuilder(); - msg.append(targetingDataView.getText().toString()); - if (result.getStatus() == OptableSDK.Status.SUCCESS) { - msg.append("\n\nSuccess calling witness API to log loadAdButtonClicked event.\n\n"); + appendStatusText("Success calling witness API to log loadAdButtonClicked event."); } else { - msg.append("\n\nOptableSDK Error: " + result.getMessage() + "\n\n"); + appendStatusText("Error during sending witness: " + result.getMessage()); } - - targetingDataView.setText(msg.toString()); }); } + + private void changeStatusText(@NonNull String message, @Nullable HashMap> optableResponse) { + StringBuilder formattedMessage = new StringBuilder(message); + if (optableResponse != null) { + formattedMessage.append("\n\nTargeting data: "); + for (Map.Entry> entry : optableResponse.entrySet()) { + formattedMessage.append(entry.getKey()) + .append(" = ") + .append(entry.getValue()) + .append("\n"); + } + } + statusTextView.setText(formattedMessage.toString()); + } + + private void appendStatusText(@NonNull String message) { + statusTextView.append("\n\n" + message); + } + } \ No newline at end of file diff --git a/DemoApp/DemoAppJava/app/src/main/java/co/optable/demoappjava/ui/Identify/IdentifyFragment.java b/DemoApp/DemoAppJava/app/src/main/java/co/optable/demoappjava/ui/Identify/IdentifyFragment.java index 94293a8..b73e02c 100644 --- a/DemoApp/DemoAppJava/app/src/main/java/co/optable/demoappjava/ui/Identify/IdentifyFragment.java +++ b/DemoApp/DemoAppJava/app/src/main/java/co/optable/demoappjava/ui/Identify/IdentifyFragment.java @@ -4,7 +4,6 @@ import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; -import android.widget.Button; import android.widget.EditText; import android.widget.Switch; import android.widget.TextView; @@ -15,37 +14,42 @@ import co.optable.demoappjava.R; public class IdentifyFragment extends Fragment { + private TextView identifyView; private EditText emailText; private Switch gaidSwitch; @Override - public View onCreateView(@NonNull LayoutInflater inflater, - ViewGroup container, Bundle savedInstanceState) { + public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View root = inflater.inflate(R.layout.fragment_identify, container, false); + initUi(root); + return root; + } + + private void initUi(View root) { identifyView = root.findViewById(R.id.identifyView); emailText = root.findViewById(R.id.editTextTextEmailAddress); gaidSwitch = root.findViewById(R.id.gaidSwitch); - Button btn = root.findViewById(R.id.identifyButton); - btn.setOnClickListener(view -> { - identifyView.setText(""); - MainActivity.OPTABLE - .identify(emailText.getText().toString(), gaidSwitch.isChecked()) - .observe(getViewLifecycleOwner(), result -> { - String msg = "Calling identify API... "; - - if (result.getStatus() == OptableSDK.Status.SUCCESS) { - msg += "Success"; - } else { - msg += "\n\nOptableSDK Error: " + result.getMessage(); - } - - identifyView.setText(msg); - }); - } - ); + root.findViewById(R.id.identifyButton).setOnClickListener(view -> onClickIdentify()); + } + + private void onClickIdentify() { + identifyView.setText(""); - return root; + MainActivity.OPTABLE + .identify(emailText.getText().toString(), gaidSwitch.isChecked()) + .observe(getViewLifecycleOwner(), result -> { + String msg = "Calling identify API... "; + + if (result.getStatus() == OptableSDK.Status.SUCCESS) { + msg += "Success"; + } else { + msg += "\n\nOptableSDK Error: " + result.getMessage(); + } + + identifyView.setText(msg); + }); } + } diff --git a/DemoApp/DemoAppJava/app/src/main/res/layout/fragment_gambanner.xml b/DemoApp/DemoAppJava/app/src/main/res/layout/fragment_gambanner.xml index d533674..2fe5cbc 100644 --- a/DemoApp/DemoAppJava/app/src/main/res/layout/fragment_gambanner.xml +++ b/DemoApp/DemoAppJava/app/src/main/res/layout/fragment_gambanner.xml @@ -21,7 +21,7 @@ app:layout_constraintStart_toStartOf="parent" />