-
Notifications
You must be signed in to change notification settings - Fork 3
feat(example): add precomputed client demo #241
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
leoromanovsky
wants to merge
15
commits into
main
Choose a base branch
from
precomputed/5-example-app
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
8a45491
feat: add obfuscation utilities and precomputed DTOs
leoromanovsky a933cd2
perf: optimize MD5 hex conversion with lookup table
leoromanovsky 5970e9f
perf: add md5HexPrefix for efficient partial hash
leoromanovsky 5d2f360
refactor: extract BaseCacheFile for reuse
leoromanovsky 5ff6524
feat: add precomputed configuration storage
leoromanovsky dd5309f
style: apply spotless formatting
leoromanovsky 4741881
fix: address PR #239 feedback
leoromanovsky 75dcdc4
Merge branch 'main' into precomputed/3-storage-layer
leoromanovsky c8c0fe8
feat: add EppoPrecomputedClient
leoromanovsky 590804e
perf: use md5HexPrefix for cache file naming
leoromanovsky 78c90ac
fix: address PR review feedback for EppoPrecomputedClient
leoromanovsky 4239fa0
feat: derive precomputed client base URL from SDK key
leoromanovsky dd5c86d
feat(example): add precomputed client demo
leoromanovsky f81109a
chore: remove self-evident comments in UtilsTest
leoromanovsky 801950c
Merge branch 'main' into precomputed/5-example-app
leoromanovsky File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
277 changes: 277 additions & 0 deletions
277
example/src/main/java/cloud/eppo/androidexample/PrecomputedActivity.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,277 @@ | ||
| package cloud.eppo.androidexample; | ||
|
|
||
| import android.os.Bundle; | ||
| import android.text.TextUtils; | ||
| import android.util.Log; | ||
| import android.view.MenuItem; | ||
| import android.view.View; | ||
| import android.widget.Button; | ||
| import android.widget.EditText; | ||
| import android.widget.LinearLayout; | ||
| import android.widget.RadioGroup; | ||
| import android.widget.ScrollView; | ||
| import android.widget.TextView; | ||
| import androidx.appcompat.app.AppCompatActivity; | ||
| import cloud.eppo.android.EppoPrecomputedClient; | ||
| import cloud.eppo.api.Attributes; | ||
| import cloud.eppo.api.EppoValue; | ||
| import com.geteppo.androidexample.BuildConfig; | ||
| import com.geteppo.androidexample.R; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| /** | ||
| * Example activity demonstrating the EppoPrecomputedClient. The precomputed client computes all | ||
| * flag assignments server-side for a specific subject, providing instant lookups. | ||
| */ | ||
| public class PrecomputedActivity extends AppCompatActivity { | ||
| private static final String TAG = PrecomputedActivity.class.getSimpleName(); | ||
| private static final String API_KEY = BuildConfig.API_KEY; | ||
|
|
||
| private EditText subjectInput; | ||
| private EditText flagKeyInput; | ||
| private RadioGroup flagTypeGroup; | ||
| private TextView assignmentLog; | ||
| private ScrollView assignmentLogScrollView; | ||
| private TextView statusText; | ||
| private LinearLayout attributesContainer; | ||
| private Button getAssignmentButton; | ||
| private List<View> attributeRows = new ArrayList<>(); | ||
|
|
||
| private EppoPrecomputedClient precomputedClient; | ||
|
|
||
| @Override | ||
| protected void onCreate(Bundle savedInstanceState) { | ||
| super.onCreate(savedInstanceState); | ||
| setContentView(R.layout.activity_precomputed); | ||
|
|
||
| // Enable the action bar back button | ||
| if (getSupportActionBar() != null) { | ||
| getSupportActionBar().setDisplayHomeAsUpEnabled(true); | ||
| getSupportActionBar().setTitle("Precomputed Client"); | ||
| } | ||
|
|
||
| subjectInput = findViewById(R.id.precomputed_subject); | ||
| flagKeyInput = findViewById(R.id.precomputed_flag_key); | ||
| flagTypeGroup = findViewById(R.id.flag_type_group); | ||
| assignmentLog = findViewById(R.id.precomputed_assignment_log); | ||
| assignmentLogScrollView = findViewById(R.id.precomputed_assignment_log_scrollview); | ||
| statusText = findViewById(R.id.precomputed_status); | ||
| attributesContainer = findViewById(R.id.attributes_container); | ||
|
|
||
| findViewById(R.id.btn_init_server).setOnClickListener(view -> initializeClient(false)); | ||
| findViewById(R.id.btn_init_disk).setOnClickListener(view -> initializeClient(true)); | ||
| getAssignmentButton = findViewById(R.id.btn_get_assignment); | ||
| getAssignmentButton.setEnabled(false); | ||
| getAssignmentButton.setOnClickListener(view -> getAssignment()); | ||
| findViewById(R.id.btn_add_attribute).setOnClickListener(view -> addAttributeRow("", "")); | ||
|
|
||
| // Add default attributes | ||
| addAttributeRow("platform", "android"); | ||
| addAttributeRow("appVersion", BuildConfig.VERSION_NAME); | ||
| } | ||
|
|
||
| private void addAttributeRow(String key, String value) { | ||
| LinearLayout row = new LinearLayout(this); | ||
| row.setOrientation(LinearLayout.HORIZONTAL); | ||
| row.setLayoutParams( | ||
| new LinearLayout.LayoutParams( | ||
| LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)); | ||
|
|
||
| EditText keyInput = new EditText(this); | ||
| keyInput.setHint("Key"); | ||
| keyInput.setText(key); | ||
| LinearLayout.LayoutParams keyParams = | ||
| new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 1f); | ||
| keyInput.setLayoutParams(keyParams); | ||
| keyInput.setTag("key"); | ||
|
|
||
| EditText valueInput = new EditText(this); | ||
| valueInput.setHint("Value"); | ||
| valueInput.setText(value); | ||
| LinearLayout.LayoutParams valueParams = | ||
| new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 1f); | ||
| valueParams.setMarginStart(8); | ||
| valueInput.setLayoutParams(valueParams); | ||
| valueInput.setTag("value"); | ||
|
|
||
| Button removeButton = new Button(this); | ||
| removeButton.setText("X"); | ||
| removeButton.setMinWidth(0); | ||
| removeButton.setMinHeight(0); | ||
| removeButton.setMinimumWidth(0); | ||
| removeButton.setMinimumHeight(0); | ||
| removeButton.setPadding(16, 8, 16, 8); | ||
| LinearLayout.LayoutParams removeParams = | ||
| new LinearLayout.LayoutParams( | ||
| LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); | ||
| removeParams.setMarginStart(8); | ||
| removeButton.setLayoutParams(removeParams); | ||
| removeButton.setOnClickListener( | ||
| v -> { | ||
| attributesContainer.removeView(row); | ||
| attributeRows.remove(row); | ||
| }); | ||
|
|
||
| row.addView(keyInput); | ||
| row.addView(valueInput); | ||
| row.addView(removeButton); | ||
|
|
||
| attributesContainer.addView(row); | ||
| attributeRows.add(row); | ||
| } | ||
|
|
||
| private Attributes collectAttributes() { | ||
| Attributes attributes = new Attributes(); | ||
| for (View row : attributeRows) { | ||
| EditText keyInput = row.findViewWithTag("key"); | ||
| EditText valueInput = row.findViewWithTag("value"); | ||
| if (keyInput != null && valueInput != null) { | ||
| String key = keyInput.getText().toString().trim(); | ||
| String value = valueInput.getText().toString().trim(); | ||
| if (!key.isEmpty()) { | ||
| // Try to parse as number first | ||
| try { | ||
| double numValue = Double.parseDouble(value); | ||
| attributes.put(key, EppoValue.valueOf(numValue)); | ||
| } catch (NumberFormatException e) { | ||
| // Use as string | ||
| attributes.put(key, EppoValue.valueOf(value)); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| return attributes; | ||
| } | ||
|
|
||
| private void initializeClient(boolean offlineMode) { | ||
| String subjectKey = subjectInput.getText().toString(); | ||
| if (TextUtils.isEmpty(subjectKey)) { | ||
| appendToLog("Subject ID is required"); | ||
| return; | ||
| } | ||
|
|
||
| String source = offlineMode ? "disk" : "server"; | ||
| statusText.setText("Initializing from " + source + "..."); | ||
| appendToLog( | ||
| "Initializing precomputed client for subject: " + subjectKey + " (from " + source + ")"); | ||
|
|
||
| // Collect subject attributes from the UI | ||
| Attributes subjectAttributes = collectAttributes(); | ||
| appendToLog("Subject attributes: " + subjectAttributes.size() + " attributes"); | ||
|
|
||
| new EppoPrecomputedClient.Builder(API_KEY, getApplication()) | ||
| .subjectKey(subjectKey) | ||
| .subjectAttributes(subjectAttributes) | ||
| .isGracefulMode(true) | ||
| .forceReinitialize(true) | ||
| .offlineMode(offlineMode) | ||
| .assignmentLogger( | ||
| assignment -> { | ||
| Log.d( | ||
| TAG, | ||
| "Assignment logged: " | ||
| + assignment.getFeatureFlag() | ||
| + " -> " | ||
| + assignment.getVariation()); | ||
| }) | ||
| .buildAndInitAsync() | ||
| .thenAccept( | ||
| client -> { | ||
| precomputedClient = client; | ||
| runOnUiThread( | ||
| () -> { | ||
| statusText.setText("Initialized for: " + subjectKey + " (from " + source + ")"); | ||
| appendToLog("Client initialized successfully from " + source + "!"); | ||
| getAssignmentButton.setEnabled(true); | ||
| }); | ||
| }) | ||
| .exceptionally( | ||
| error -> { | ||
| Log.e(TAG, "Failed to initialize", error); | ||
| runOnUiThread( | ||
| () -> { | ||
| statusText.setText("Initialization failed"); | ||
| appendToLog("Error: " + error.getMessage()); | ||
| }); | ||
| return null; | ||
| }); | ||
| } | ||
|
|
||
| private void getAssignment() { | ||
| if (precomputedClient == null) { | ||
| appendToLog("Client not initialized. Click 'From Server' or 'From Disk' first."); | ||
| return; | ||
| } | ||
|
|
||
| String flagKey = flagKeyInput.getText().toString(); | ||
| if (TextUtils.isEmpty(flagKey)) { | ||
| appendToLog("Flag key is required"); | ||
| return; | ||
| } | ||
|
|
||
| int selectedTypeId = flagTypeGroup.getCheckedRadioButtonId(); | ||
| String result; | ||
|
|
||
| try { | ||
| if (selectedTypeId == R.id.type_string) { | ||
| result = precomputedClient.getStringAssignment(flagKey, "(default)"); | ||
| appendToLog("String assignment for '" + flagKey + "': " + result); | ||
| } else if (selectedTypeId == R.id.type_boolean) { | ||
| boolean boolResult = precomputedClient.getBooleanAssignment(flagKey, false); | ||
| appendToLog("Boolean assignment for '" + flagKey + "': " + boolResult); | ||
| } else if (selectedTypeId == R.id.type_integer) { | ||
| int intResult = precomputedClient.getIntegerAssignment(flagKey, 0); | ||
| appendToLog("Integer assignment for '" + flagKey + "': " + intResult); | ||
| } else if (selectedTypeId == R.id.type_numeric) { | ||
| double numericResult = precomputedClient.getNumericAssignment(flagKey, 0.0); | ||
| appendToLog("Numeric assignment for '" + flagKey + "': " + numericResult); | ||
| } else if (selectedTypeId == R.id.type_json) { | ||
| // JSON assignments return JsonNode - for simplicity, we show as string | ||
| appendToLog("JSON assignment for '" + flagKey + "': (use getJSONAssignment() API)"); | ||
|
Comment on lines
+230
to
+231
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you do and stringify it with |
||
| } else { | ||
| appendToLog("Please select a flag type"); | ||
| } | ||
| } catch (Exception e) { | ||
| appendToLog("Error getting assignment: " + e.getMessage()); | ||
| } | ||
| } | ||
|
|
||
| private void appendToLog(String message) { | ||
| assignmentLog.append(message + "\n\n"); | ||
| assignmentLogScrollView.post(() -> assignmentLogScrollView.fullScroll(View.FOCUS_DOWN)); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean onOptionsItemSelected(MenuItem item) { | ||
| if (item.getItemId() == android.R.id.home) { | ||
| finish(); | ||
| return true; | ||
| } | ||
| return super.onOptionsItemSelected(item); | ||
| } | ||
|
|
||
| @Override | ||
| public void onPause() { | ||
| super.onPause(); | ||
| if (precomputedClient != null) { | ||
| precomputedClient.pausePolling(); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void onResume() { | ||
| super.onResume(); | ||
| if (precomputedClient != null) { | ||
| precomputedClient.resumePolling(); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void onDestroy() { | ||
| super.onDestroy(); | ||
| if (precomputedClient != null) { | ||
| precomputedClient.stopPolling(); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it common to create UI components programmatically like this in Android? 😮
Also
.setMinWidthand.setMinimumWidthare different?