11package com .launchdarkly .example ;
22
33import android .os .Bundle ;
4+ import android .support .annotation .Nullable ;
45import android .support .v7 .app .AppCompatActivity ;
56import android .util .Log ;
67import android .view .View ;
78import android .widget .ArrayAdapter ;
89import android .widget .Button ;
9- import android .widget .CompoundButton ;
1010import android .widget .EditText ;
1111import android .widget .Spinner ;
1212import android .widget .Switch ;
1313import android .widget .TextView ;
1414
1515import com .google .gson .JsonNull ;
16- import com .launchdarkly .android .FeatureFlagChangeListener ;
1716import com .launchdarkly .android .LDClient ;
1817import com .launchdarkly .android .LDConfig ;
1918import com .launchdarkly .android .LDUser ;
2019
20+ import java .io .IOException ;
2121import java .util .concurrent .ExecutionException ;
2222import java .util .concurrent .Future ;
2323import java .util .concurrent .TimeUnit ;
@@ -41,6 +41,7 @@ protected void onCreate(Bundle savedInstanceState) {
4141
4242 LDConfig ldConfig = new LDConfig .Builder ()
4343 .setMobileKey ("MOBILE_KEY" )
44+ .setUseReport (false ) // change to `true` if the request is to be REPORT'ed instead of GET'ed
4445 .build ();
4546
4647 LDUser user = new LDUser .Builder ("user key" )
@@ -51,116 +52,127 @@ protected void onCreate(Bundle savedInstanceState) {
5152 try {
5253 ldClient = initFuture .get (10 , TimeUnit .SECONDS );
5354 } catch (InterruptedException | ExecutionException | TimeoutException e ) {
54- Log .e (TAG , "Exception when awaiting LaunchDarkly Client initialization" , e );
55+ Log .e (TAG , "Exception when awaiting LaunchDarkly Client initialization" , e );
5556 }
5657 }
5758
58- private void setupFlushButton () {
59- Button flushButton = (Button ) findViewById (R .id .flush_button );
60- flushButton .setOnClickListener (new View .OnClickListener () {
59+ @ Override
60+ public void onStop () {
61+ super .onStop ();
62+ try {
63+ ldClient .close ();
64+ } catch (IOException e ) {
65+ Log .e (TAG , "Exception when closing LaunchDarkly Client" , e );
66+ }
67+ }
6168
62- @ Override
63- public void onClick ( View v ) {
64- Log . i ( TAG , "flush onClick" );
65- ldClient . flush ( );
66- }
69+ private void setupFlushButton () {
70+ Button flushButton = findViewById ( R . id . flush_button );
71+ flushButton . setOnClickListener (( View v ) -> {
72+ Log . i ( TAG , "flush onClick" );
73+ doSafeClientAction (() -> ldClient . flush ());
6774 });
6875 }
6976
70- private void setupTrackButton () {
71- Button trackButton = ( Button ) findViewById ( R . id . track_button );
72- trackButton . setOnClickListener ( new View . OnClickListener () {
77+ private interface LDClientFunction {
78+ void call ( );
79+ }
7380
74- @ Override
75- public void onClick (View v ) {
76- Log .i (TAG , "track onClick" );
77- ldClient .track ("Android event name" );
78- }
79- });
81+ private interface LDClientGetFunction <V > {
82+ V get ();
8083 }
8184
82- private void setupIdentifyButton () {
83- Button identify = (Button ) findViewById (R .id .identify_button );
84- identify .setOnClickListener (new View .OnClickListener () {
85+ private void doSafeClientAction (LDClientFunction function ) {
86+ if (ldClient != null ) {
87+ function .call ();
88+ }
89+ }
8590
86- @ Override
87- public void onClick (View v ) {
88- Log .i (TAG , "identify onClick" );
89- String userKey = ((EditText ) findViewById (R .id .userKey_editText )).getText ().toString ();
91+ @ Nullable
92+ private <V > V doSafeClientGet (LDClientGetFunction <V > function ) {
93+ if (ldClient != null ) {
94+ return function .get ();
95+ }
96+ return null ;
97+ }
9098
91- LDUser updatedUser = new LDUser .Builder (userKey )
92- .build ();
99+ private void setupTrackButton () {
100+ Button trackButton = findViewById (R .id .track_button );
101+ trackButton .setOnClickListener (v -> {
102+ Log .i (TAG , "track onClick" );
103+ doSafeClientAction (() -> ldClient .track ("Android event name" ));
104+ });
105+ }
93106
94- ldClient .identify (updatedUser );
95- }
107+ private void setupIdentifyButton () {
108+ Button identify = findViewById (R .id .identify_button );
109+ identify .setOnClickListener (v -> {
110+ Log .i (TAG , "identify onClick" );
111+ String userKey = ((EditText ) findViewById (R .id .userKey_editText )).getText ().toString ();
112+ LDUser updatedUser = new LDUser .Builder (userKey ).build ();
113+ doSafeClientAction (() -> ldClient .identify (updatedUser ));
96114 });
97115 }
98116
99117 private void setupOfflineSwitch () {
100- Switch offlineSwitch = (Switch ) findViewById (R .id .offlineSwitch );
101- offlineSwitch .setOnCheckedChangeListener (new CompoundButton .OnCheckedChangeListener () {
102- @ Override
103- public void onCheckedChanged (CompoundButton compoundButton , boolean isChecked ) {
104- if (isChecked ) {
105- ldClient .setOffline ();
106- } else {
107- ldClient .setOnline ();
108- }
118+ Switch offlineSwitch = findViewById (R .id .offlineSwitch );
119+ offlineSwitch .setOnCheckedChangeListener ((compoundButton , isChecked ) -> {
120+ if (isChecked ) {
121+ doSafeClientAction (() -> ldClient .setOffline ());
122+ } else {
123+ doSafeClientAction (() -> ldClient .setOnline ());
109124 }
110125 });
111126 }
112127
113128 private void setupEval () {
114- final Spinner spinner = ( Spinner ) findViewById (R .id .type_spinner );
129+ final Spinner spinner = findViewById (R .id .type_spinner );
115130 ArrayAdapter <CharSequence > adapter = ArrayAdapter .createFromResource (this ,
116131 R .array .types_array , android .R .layout .simple_spinner_item );
117132 adapter .setDropDownViewResource (android .R .layout .simple_spinner_dropdown_item );
118133 spinner .setAdapter (adapter );
119134
120- Button evalButton = (Button ) findViewById (R .id .eval_button );
121- evalButton .setOnClickListener (new View .OnClickListener () {
122-
123- @ Override
124- public void onClick (View v ) {
125- Log .i (TAG , "eval onClick" );
126- final String flagKey = ((EditText ) findViewById (R .id .feature_flag_key )).getText ().toString ();
127-
128- String type = spinner .getSelectedItem ().toString ();
129- final String result ;
130- switch (type ) {
131- case "String" :
132- result = ldClient .stringVariation (flagKey , "default" );
133- Log .i (TAG , result );
134- ((TextView ) findViewById (R .id .result_textView )).setText (result );
135- ldClient .registerFeatureFlagListener (flagKey , new FeatureFlagChangeListener () {
136- @ Override
137- public void onFeatureFlagChange (String flagKey ) {
138- ((TextView ) findViewById (R .id .result_textView ))
139- .setText (ldClient .stringVariation (flagKey , "default" ));
140- }
141- });
142- break ;
143- case "Boolean" :
144- result = ldClient .boolVariation (flagKey , false ).toString ();
145- Log .i (TAG , result );
146- ((TextView ) findViewById (R .id .result_textView )).setText (result );
147- break ;
148- case "Integer" :
149- result = ldClient .intVariation (flagKey , 0 ).toString ();
150- Log .i (TAG , result );
151- ((TextView ) findViewById (R .id .result_textView )).setText (result );
152- break ;
153- case "Float" :
154- result = ldClient .floatVariation (flagKey , 0F ).toString ();
155- Log .i (TAG , result );
156- ((TextView ) findViewById (R .id .result_textView )).setText (result );
157- break ;
158- case "Json" :
159- result = ldClient .jsonVariation (flagKey , JsonNull .INSTANCE ).toString ();
160- Log .i (TAG , result );
161- ((TextView ) findViewById (R .id .result_textView )).setText (result );
162- break ;
163- }
135+ Button evalButton = findViewById (R .id .eval_button );
136+ evalButton .setOnClickListener ((View v ) -> {
137+ Log .i (TAG , "eval onClick" );
138+ final String flagKey = ((EditText ) findViewById (R .id .feature_flag_key )).getText ().toString ();
139+
140+ String type = spinner .getSelectedItem ().toString ();
141+ final String result ;
142+ String logResult = "no result" ;
143+ switch (type ) {
144+ case "String" :
145+ result = doSafeClientGet (() -> ldClient .stringVariation (flagKey , "default" ));
146+ logResult = result == null ? "no result" : result ;
147+ Log .i (TAG , logResult );
148+ ((TextView ) findViewById (R .id .result_textView )).setText (result );
149+ doSafeClientAction (() -> ldClient .registerFeatureFlagListener (flagKey , flagKey1 -> ((TextView ) findViewById (R .id .result_textView ))
150+ .setText (ldClient .stringVariation (flagKey1 , "default" ))));
151+ break ;
152+ case "Boolean" :
153+ result = doSafeClientGet (() -> ldClient .boolVariation (flagKey , false ).toString ());
154+ logResult = result == null ? "no result" : result ;
155+ Log .i (TAG , logResult );
156+ ((TextView ) findViewById (R .id .result_textView )).setText (result );
157+ break ;
158+ case "Integer" :
159+ result = doSafeClientGet (() -> ldClient .intVariation (flagKey , 0 ).toString ());
160+ logResult = result == null ? "no result" : result ;
161+ Log .i (TAG , logResult );
162+ ((TextView ) findViewById (R .id .result_textView )).setText (result );
163+ break ;
164+ case "Float" :
165+ result = doSafeClientGet (() -> ldClient .floatVariation (flagKey , 0F ).toString ());
166+ logResult = result == null ? "no result" : result ;
167+ Log .i (TAG , logResult );
168+ ((TextView ) findViewById (R .id .result_textView )).setText (result );
169+ break ;
170+ case "Json" :
171+ result = doSafeClientGet (() -> ldClient .jsonVariation (flagKey , JsonNull .INSTANCE ).toString ());
172+ logResult = result == null ? "no result" : result ;
173+ Log .i (TAG , logResult );
174+ ((TextView ) findViewById (R .id .result_textView )).setText (result );
175+ break ;
164176 }
165177 });
166178 }
0 commit comments