-
Notifications
You must be signed in to change notification settings - Fork 400
/
MainActivity.java
148 lines (131 loc) · 6.18 KB
/
MainActivity.java
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
/*
* Copyright 2017 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package devrel.firebase.google.com.functions;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import com.google.android.gms.tasks.Continuation;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.functions.FirebaseFunctions;
import com.google.firebase.functions.FirebaseFunctionsException;
import com.google.firebase.functions.HttpsCallableResult;
import java.util.HashMap;
import java.util.Map;
public class MainActivity extends AppCompatActivity {
// [START define_functions_instance]
private FirebaseFunctions mFunctions;
// [END define_functions_instance]
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// [START initialize_functions_instance]
mFunctions = FirebaseFunctions.getInstance();
// [END initialize_functions_instance]
}
public void emulatorSettings() {
// [START functions_emulator_connect]
// 10.0.2.2 is the special IP address to connect to the 'localhost' of
// the host computer from an Android emulator.
FirebaseFunctions functions = FirebaseFunctions.getInstance();
functions.useEmulator("10.0.2.2", 5001);
// [END functions_emulator_connect]
}
// [START function_add_numbers]
private Task<Integer> addNumbers(int a, int b) {
// Create the arguments to the callable function, which are two integers
Map<String, Object> data = new HashMap<>();
data.put("firstNumber", a);
data.put("secondNumber", b);
// Call the function and extract the operation from the result
return mFunctions
.getHttpsCallable("addNumbers")
.call(data)
.continueWith(new Continuation<HttpsCallableResult, Integer>() {
@Override
public Integer then(@NonNull Task<HttpsCallableResult> task) throws Exception {
// This continuation runs on either success or failure, but if the task
// has failed then getResult() will throw an Exception which will be
// propagated down.
Map<String, Object> result = (Map<String, Object>) task.getResult().getData();
return (Integer) result.get("operationResult");
}
});
}
// [END function_add_numbers]
// [START function_add_message]
private Task<String> addMessage(String text) {
// Create the arguments to the callable function.
Map<String, Object> data = new HashMap<>();
data.put("text", text);
data.put("push", true);
return mFunctions
.getHttpsCallable("addMessage")
.call(data)
.continueWith(new Continuation<HttpsCallableResult, String>() {
@Override
public String then(@NonNull Task<HttpsCallableResult> task) throws Exception {
// This continuation runs on either success or failure, but if the task
// has failed then getResult() will throw an Exception which will be
// propagated down.
String result = (String) task.getResult().getData();
return result;
}
});
}
// [END function_add_message]
private void callAddNumbers(int firstNumber, int secondNumber) {
// [START call_add_numbers]
addNumbers(firstNumber, secondNumber)
.addOnCompleteListener(new OnCompleteListener<Integer>() {
@Override
public void onComplete(@NonNull Task<Integer> task) {
if (!task.isSuccessful()) {
Exception e = task.getException();
if (e instanceof FirebaseFunctionsException) {
FirebaseFunctionsException ffe = (FirebaseFunctionsException) e;
// Function error code, will be INTERNAL if the failure
// was not handled properly in the function call.
FirebaseFunctionsException.Code code = ffe.getCode();
// Arbitrary error details passed back from the function,
// usually a Map<String, Object>.
Object details = ffe.getDetails();
}
}
}
});
// [END call_add_numbers]
}
private void callAddMessage(String inputMessage) {
// [START call_add_message]
addMessage(inputMessage)
.addOnCompleteListener(new OnCompleteListener<String>() {
@Override
public void onComplete(@NonNull Task<String> task) {
if (!task.isSuccessful()) {
Exception e = task.getException();
if (e instanceof FirebaseFunctionsException) {
FirebaseFunctionsException ffe = (FirebaseFunctionsException) e;
FirebaseFunctionsException.Code code = ffe.getCode();
Object details = ffe.getDetails();
}
}
}
});
// [END call_add_message]
}
}