1515 */
1616package devrel .firebase .google .com .functions ;
1717
18+ import android .os .Bundle ;
19+
20+ import androidx .annotation .NonNull ;
21+ import androidx .annotation .Nullable ;
1822import androidx .appcompat .app .AppCompatActivity ;
1923
24+ import com .google .android .gms .tasks .Continuation ;
25+ import com .google .android .gms .tasks .OnCompleteListener ;
26+ import com .google .android .gms .tasks .Task ;
2027import com .google .firebase .functions .FirebaseFunctions ;
28+ import com .google .firebase .functions .FirebaseFunctionsException ;
29+ import com .google .firebase .functions .HttpsCallableResult ;
30+
31+ import java .util .HashMap ;
32+ import java .util .Map ;
2133
2234public class MainActivity extends AppCompatActivity {
2335
36+ // [START define_functions_instance]
37+ private FirebaseFunctions mFunctions ;
38+ // [END define_functions_instance]
39+
40+
41+ @ Override
42+ protected void onCreate (@ Nullable Bundle savedInstanceState ) {
43+ super .onCreate (savedInstanceState );
44+ // [START initialize_functions_instance]
45+ mFunctions = FirebaseFunctions .getInstance ();
46+ // [END initialize_functions_instance]
47+ }
48+
2449 public void emulatorSettings () {
2550 // [START functions_emulator_connect]
2651 // 10.0.2.2 is the special IP address to connect to the 'localhost' of
@@ -30,4 +55,94 @@ public void emulatorSettings() {
3055 // [END functions_emulator_connect]
3156 }
3257
58+ // [START function_add_numbers]
59+ private Task <Integer > addNumbers (int a , int b ) {
60+ // Create the arguments to the callable function, which are two integers
61+ Map <String , Object > data = new HashMap <>();
62+ data .put ("firstNumber" , a );
63+ data .put ("secondNumber" , b );
64+
65+ // Call the function and extract the operation from the result
66+ return mFunctions
67+ .getHttpsCallable ("addNumbers" )
68+ .call (data )
69+ .continueWith (new Continuation <HttpsCallableResult , Integer >() {
70+ @ Override
71+ public Integer then (@ NonNull Task <HttpsCallableResult > task ) throws Exception {
72+ // This continuation runs on either success or failure, but if the task
73+ // has failed then getResult() will throw an Exception which will be
74+ // propagated down.
75+ Map <String , Object > result = (Map <String , Object >) task .getResult ().getData ();
76+ return (Integer ) result .get ("operationResult" );
77+ }
78+ });
79+ }
80+ // [END function_add_numbers]
81+
82+ // [START function_add_message]
83+ private Task <String > addMessage (String text ) {
84+ // Create the arguments to the callable function.
85+ Map <String , Object > data = new HashMap <>();
86+ data .put ("text" , text );
87+ data .put ("push" , true );
88+
89+ return mFunctions
90+ .getHttpsCallable ("addMessage" )
91+ .call (data )
92+ .continueWith (new Continuation <HttpsCallableResult , String >() {
93+ @ Override
94+ public String then (@ NonNull Task <HttpsCallableResult > task ) throws Exception {
95+ // This continuation runs on either success or failure, but if the task
96+ // has failed then getResult() will throw an Exception which will be
97+ // propagated down.
98+ String result = (String ) task .getResult ().getData ();
99+ return result ;
100+ }
101+ });
102+ }
103+ // [END function_add_message]
104+
105+ private void callAddNumbers (int firstNumber , int secondNumber ) {
106+ // [START call_add_numbers]
107+ addNumbers (firstNumber , secondNumber )
108+ .addOnCompleteListener (new OnCompleteListener <Integer >() {
109+ @ Override
110+ public void onComplete (@ NonNull Task <Integer > task ) {
111+ if (!task .isSuccessful ()) {
112+ Exception e = task .getException ();
113+ if (e instanceof FirebaseFunctionsException ) {
114+ FirebaseFunctionsException ffe = (FirebaseFunctionsException ) e ;
115+
116+ // Function error code, will be INTERNAL if the failure
117+ // was not handled properly in the function call.
118+ FirebaseFunctionsException .Code code = ffe .getCode ();
119+
120+ // Arbitrary error details passed back from the function,
121+ // usually a Map<String, Object>.
122+ Object details = ffe .getDetails ();
123+ }
124+ }
125+ }
126+ });
127+ // [END call_add_numbers]
128+ }
129+
130+ private void callAddMessage (String inputMessage ) {
131+ // [START call_add_message]
132+ addMessage (inputMessage )
133+ .addOnCompleteListener (new OnCompleteListener <String >() {
134+ @ Override
135+ public void onComplete (@ NonNull Task <String > task ) {
136+ if (!task .isSuccessful ()) {
137+ Exception e = task .getException ();
138+ if (e instanceof FirebaseFunctionsException ) {
139+ FirebaseFunctionsException ffe = (FirebaseFunctionsException ) e ;
140+ FirebaseFunctionsException .Code code = ffe .getCode ();
141+ Object details = ffe .getDetails ();
142+ }
143+ }
144+ }
145+ });
146+ // [END call_add_message]
147+ }
33148}
0 commit comments