-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPrecomputedActivity.java
More file actions
277 lines (248 loc) · 9.78 KB
/
Copy pathPrecomputedActivity.java
File metadata and controls
277 lines (248 loc) · 9.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
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)");
} 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();
}
}
}