Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.google.android.material.bottomnavigation.BottomNavigationView;

public class MainActivity extends AppCompatActivity {

public static OptableSDK OPTABLE;

@Override
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -15,123 +15,147 @@
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<String, List<String>> 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<String, List<String>> data = result.getData();
changeStatusText("Loading GAM ad with targeting data", data);

if (data != null) {
for (String key : data.keySet()) {
List<String> 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<String, List<String>> data = MainActivity.OPTABLE.targetingFromCache();

if (data != null) {
changeStatusText("Loading GAM ad with cached targeting data", data);
for (String key : data.keySet()) {
List<String> 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<String,Object> traits = new HashMap<String,Object>();
HashMap<String, Object> traits = new HashMap<>();
traits.put("gender", "F");
traits.put("age", 38);
traits.put("hasAccount", true);

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<String,Object> eventProperties = new HashMap<String,Object>();
HashMap<String, Object> eventProperties = new HashMap<>();
eventProperties.put("exampleKey", "exampleValue");
eventProperties.put("exampleKey2", 123);
eventProperties.put("exampleKey3", false);

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<String, List<String>> optableResponse) {
StringBuilder formattedMessage = new StringBuilder(message);
if (optableResponse != null) {
formattedMessage.append("\n\nTargeting data: ");
for (Map.Entry<String, ? extends Collection<?>> 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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
});
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
app:layout_constraintStart_toStartOf="parent" />

<Button
android:id="@+id/loadAdButton"
android:id="@+id/btnLoadBanner"
android:layout_width="120dp"
android:layout_height="50dp"
android:layout_marginTop="8dp"
Expand All @@ -33,7 +33,7 @@
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/loadAdButton2"
android:id="@+id/btnCachedBanner"
android:layout_width="120dp"
android:layout_height="50dp"
android:layout_marginTop="8dp"
Expand All @@ -45,7 +45,7 @@
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/loadAdButton3"
android:id="@+id/btnClearCache"
android:layout_width="120dp"
android:layout_height="50dp"
android:layout_marginTop="8dp"
Expand All @@ -63,6 +63,6 @@
android:layout_marginTop="100dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/loadAdButton" />
app:layout_constraintTop_toBottomOf="@+id/btnLoadBanner" />

</androidx.constraintlayout.widget.ConstraintLayout>
Loading