|
| 1 | +package com.featurevisor.example.android; |
| 2 | + |
| 3 | +import android.app.Activity; |
| 4 | +import android.os.Bundle; |
| 5 | +import android.view.View; |
| 6 | +import android.widget.TextView; |
| 7 | + |
| 8 | +import com.featurevisor.sdk.DatafileContent; |
| 9 | +import com.featurevisor.sdk.Featurevisor; |
| 10 | + |
| 11 | +import java.io.BufferedReader; |
| 12 | +import java.io.IOException; |
| 13 | +import java.io.InputStreamReader; |
| 14 | +import java.net.HttpURLConnection; |
| 15 | +import java.net.URL; |
| 16 | +import java.nio.charset.StandardCharsets; |
| 17 | +import java.util.HashMap; |
| 18 | +import java.util.Map; |
| 19 | +import java.util.concurrent.ExecutorService; |
| 20 | +import java.util.concurrent.Executors; |
| 21 | + |
| 22 | +public final class MainActivity extends Activity { |
| 23 | + private static final String DATAFILE_URL = |
| 24 | + "https://featurevisor-example-cloudflare.pages.dev/production/featurevisor-mobile.json"; |
| 25 | + |
| 26 | + private final ExecutorService executor = Executors.newSingleThreadExecutor(); |
| 27 | + |
| 28 | + private Featurevisor f; |
| 29 | + private View loadingView; |
| 30 | + private View errorView; |
| 31 | + private View resultsView; |
| 32 | + private TextView errorMessage; |
| 33 | + private TextView flagValue; |
| 34 | + private TextView variationValue; |
| 35 | + private TextView variableValue; |
| 36 | + |
| 37 | + @Override |
| 38 | + protected void onCreate(Bundle savedInstanceState) { |
| 39 | + super.onCreate(savedInstanceState); |
| 40 | + setContentView(R.layout.activity_main); |
| 41 | + |
| 42 | + loadingView = findViewById(R.id.loading_view); |
| 43 | + errorView = findViewById(R.id.error_view); |
| 44 | + resultsView = findViewById(R.id.results_view); |
| 45 | + errorMessage = findViewById(R.id.error_message); |
| 46 | + flagValue = findViewById(R.id.flag_value); |
| 47 | + variationValue = findViewById(R.id.variation_value); |
| 48 | + variableValue = findViewById(R.id.variable_value); |
| 49 | + |
| 50 | + findViewById(R.id.retry_button).setOnClickListener(view -> loadDatafile()); |
| 51 | + |
| 52 | + loadDatafile(); |
| 53 | + } |
| 54 | + |
| 55 | + private void loadDatafile() { |
| 56 | + showLoading(); |
| 57 | + |
| 58 | + executor.execute(() -> { |
| 59 | + try { |
| 60 | + String datafileJson = fetchDatafile(); |
| 61 | + DatafileContent datafile = DatafileContent.fromJson(datafileJson); |
| 62 | + Featurevisor loadedF = Featurevisor.createFeaturevisor( |
| 63 | + new Featurevisor.FeaturevisorOptions().datafile(datafile) |
| 64 | + ); |
| 65 | + |
| 66 | + Map<String, Object> context = new HashMap<>(); |
| 67 | + context.put("userId", "mobile-user"); |
| 68 | + context.put("country", "nl"); |
| 69 | + |
| 70 | + boolean enabled = loadedF.isEnabled("mobile_experience", context); |
| 71 | + String variation = loadedF.getVariation("mobile_experience", context); |
| 72 | + String welcomeMessage = loadedF.getVariableString( |
| 73 | + "mobile_experience", |
| 74 | + "welcome_message", |
| 75 | + context |
| 76 | + ); |
| 77 | + |
| 78 | + runOnUiThread(() -> showResults( |
| 79 | + loadedF, |
| 80 | + enabled, |
| 81 | + variation, |
| 82 | + welcomeMessage |
| 83 | + )); |
| 84 | + } catch (Exception exception) { |
| 85 | + runOnUiThread(() -> showError(exception.getMessage())); |
| 86 | + } |
| 87 | + }); |
| 88 | + } |
| 89 | + |
| 90 | + private String fetchDatafile() throws IOException { |
| 91 | + HttpURLConnection connection = (HttpURLConnection) new URL(DATAFILE_URL).openConnection(); |
| 92 | + connection.setConnectTimeout(10_000); |
| 93 | + connection.setReadTimeout(10_000); |
| 94 | + connection.setRequestProperty("Accept", "application/json"); |
| 95 | + |
| 96 | + try { |
| 97 | + int statusCode = connection.getResponseCode(); |
| 98 | + |
| 99 | + if (statusCode < 200 || statusCode >= 300) { |
| 100 | + throw new IOException("Datafile request failed with HTTP " + statusCode + "."); |
| 101 | + } |
| 102 | + |
| 103 | + try (BufferedReader reader = new BufferedReader(new InputStreamReader( |
| 104 | + connection.getInputStream(), |
| 105 | + StandardCharsets.UTF_8 |
| 106 | + ))) { |
| 107 | + StringBuilder content = new StringBuilder(); |
| 108 | + String line; |
| 109 | + |
| 110 | + while ((line = reader.readLine()) != null) { |
| 111 | + content.append(line); |
| 112 | + } |
| 113 | + |
| 114 | + return content.toString(); |
| 115 | + } |
| 116 | + } finally { |
| 117 | + connection.disconnect(); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + private void showLoading() { |
| 122 | + loadingView.setVisibility(View.VISIBLE); |
| 123 | + errorView.setVisibility(View.GONE); |
| 124 | + resultsView.setVisibility(View.GONE); |
| 125 | + } |
| 126 | + |
| 127 | + private void showResults( |
| 128 | + Featurevisor loadedF, |
| 129 | + boolean enabled, |
| 130 | + String variation, |
| 131 | + String welcomeMessage |
| 132 | + ) { |
| 133 | + if (isFinishing() || isDestroyed()) { |
| 134 | + loadedF.close(); |
| 135 | + return; |
| 136 | + } |
| 137 | + |
| 138 | + if (f != null) { |
| 139 | + f.close(); |
| 140 | + } |
| 141 | + |
| 142 | + f = loadedF; |
| 143 | + flagValue.setText(enabled ? R.string.enabled : R.string.disabled); |
| 144 | + variationValue.setText(valueOrFallback(variation)); |
| 145 | + variableValue.setText(valueOrFallback(welcomeMessage)); |
| 146 | + loadingView.setVisibility(View.GONE); |
| 147 | + errorView.setVisibility(View.GONE); |
| 148 | + resultsView.setVisibility(View.VISIBLE); |
| 149 | + } |
| 150 | + |
| 151 | + private void showError(String message) { |
| 152 | + if (isFinishing() || isDestroyed()) { |
| 153 | + return; |
| 154 | + } |
| 155 | + |
| 156 | + loadingView.setVisibility(View.GONE); |
| 157 | + errorView.setVisibility(View.VISIBLE); |
| 158 | + resultsView.setVisibility(View.GONE); |
| 159 | + errorMessage.setText(message != null ? message : getString(R.string.unknown_error)); |
| 160 | + } |
| 161 | + |
| 162 | + private String valueOrFallback(String value) { |
| 163 | + return value != null ? value : getString(R.string.no_value); |
| 164 | + } |
| 165 | + |
| 166 | + @Override |
| 167 | + protected void onDestroy() { |
| 168 | + executor.shutdownNow(); |
| 169 | + |
| 170 | + if (f != null) { |
| 171 | + f.close(); |
| 172 | + } |
| 173 | + |
| 174 | + super.onDestroy(); |
| 175 | + } |
| 176 | +} |
0 commit comments