Skip to content

Commit db50a75

Browse files
authored
port functions snippets from quickstart-android (#350)
1 parent 2650955 commit db50a75

File tree

2 files changed

+208
-0
lines changed

2 files changed

+208
-0
lines changed

functions/app/src/main/java/devrel/firebase/google/com/functions/MainActivity.java

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,37 @@
1515
*/
1616
package devrel.firebase.google.com.functions;
1717

18+
import android.os.Bundle;
19+
20+
import androidx.annotation.NonNull;
21+
import androidx.annotation.Nullable;
1822
import 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;
2027
import 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

2234
public 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
}

functions/app/src/main/java/devrel/firebase/google/com/functions/kotlin/MainActivity.kt

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,26 @@
11
package devrel.firebase.google.com.functions.kotlin
22

3+
import android.os.Bundle
34
import androidx.appcompat.app.AppCompatActivity
5+
import com.google.android.gms.tasks.Task
46
import com.google.firebase.functions.FirebaseFunctions
7+
import com.google.firebase.functions.FirebaseFunctionsException
58
import com.google.firebase.functions.ktx.functions
69
import com.google.firebase.ktx.Firebase
710

811
class MainActivity : AppCompatActivity() {
912

13+
// [START define_functions_instance]
14+
private lateinit var functions: FirebaseFunctions
15+
// [END define_functions_instance]
16+
17+
override fun onCreate(savedInstanceState: Bundle?) {
18+
super.onCreate(savedInstanceState)
19+
// [START initialize_functions_instance]
20+
functions = Firebase.functions
21+
// [END initialize_functions_instance]
22+
}
23+
1024
fun emulatorSettings() {
1125
// [START functions_emulator_connect]
1226
// 10.0.2.2 is the special IP address to connect to the 'localhost' of
@@ -15,4 +29,83 @@ class MainActivity : AppCompatActivity() {
1529
functions.useEmulator("10.0.2.2", 5001)
1630
// [END functions_emulator_connect]
1731
}
32+
33+
// [START function_add_numbers]
34+
private fun addNumbers(a: Int, b: Int): Task<Int> {
35+
// Create the arguments to the callable function, which are two integers
36+
val data = hashMapOf(
37+
"firstNumber" to a,
38+
"secondNumber" to b
39+
)
40+
41+
// Call the function and extract the operation from the result
42+
return functions
43+
.getHttpsCallable("addNumbers")
44+
.call(data)
45+
.continueWith { task ->
46+
// This continuation runs on either success or failure, but if the task
47+
// has failed then task.result will throw an Exception which will be
48+
// propagated down.
49+
val result = task.result?.data as Map<String, Any>
50+
result["operationResult"] as Int
51+
}
52+
}
53+
// [END function_add_numbers]
54+
55+
// [START function_add_message]
56+
private fun addMessage(text: String): Task<String> {
57+
// Create the arguments to the callable function.
58+
val data = hashMapOf(
59+
"text" to text,
60+
"push" to true
61+
)
62+
63+
return functions
64+
.getHttpsCallable("addMessage")
65+
.call(data)
66+
.continueWith { task ->
67+
// This continuation runs on either success or failure, but if the task
68+
// has failed then result will throw an Exception which will be
69+
// propagated down.
70+
val result = task.result?.data as String
71+
result
72+
}
73+
}
74+
// [END function_add_message]
75+
76+
private fun callAddNumbers(firstNumber: Int, secondNumber: Int) {
77+
// [START call_add_numbers]
78+
addNumbers(firstNumber, secondNumber)
79+
.addOnCompleteListener { task ->
80+
if (!task.isSuccessful) {
81+
val e = task.exception
82+
if (e is FirebaseFunctionsException) {
83+
84+
// Function error code, will be INTERNAL if the failure
85+
// was not handled properly in the function call.
86+
val code = e.code
87+
88+
// Arbitrary error details passed back from the function,
89+
// usually a Map<String, Any>.
90+
val details = e.details
91+
}
92+
}
93+
}
94+
// [END call_add_numbers]
95+
}
96+
97+
private fun callAddMessage(inputMessage: String){
98+
// [START call_add_message]
99+
addMessage(inputMessage)
100+
.addOnCompleteListener { task ->
101+
if (!task.isSuccessful) {
102+
val e = task.exception
103+
if (e is FirebaseFunctionsException) {
104+
val code = e.code
105+
val details = e.details
106+
}
107+
}
108+
}
109+
// [END call_add_message]
110+
}
18111
}

0 commit comments

Comments
 (0)