/
FlutterFirebaseCorePlugin.java
268 lines (232 loc) 路 9.76 KB
/
FlutterFirebaseCorePlugin.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package io.flutter.plugins.firebase.core;
import static io.flutter.plugins.firebase.core.FlutterFirebasePlugin.cachedThreadPool;
import android.content.Context;
import android.os.Looper;
import androidx.annotation.NonNull;
import com.google.android.gms.tasks.Task;
import com.google.android.gms.tasks.Tasks;
import com.google.firebase.FirebaseApp;
import com.google.firebase.FirebaseOptions;
import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
/**
* Flutter plugin implementation controlling the entrypoint for the Firebase SDK.
*
* <p>Instantiate this in an add to app scenario to gracefully handle activity and context changes.
*/
public class FlutterFirebaseCorePlugin implements FlutterPlugin, MethodChannel.MethodCallHandler {
private static final String KEY_API_KEY = "apiKey";
private static final String KEY_APP_NAME = "appName";
private static final String KEY_APP_ID = "appId";
private static final String KEY_MESSAGING_SENDER_ID = "messagingSenderId";
private static final String KEY_PROJECT_ID = "projectId";
private static final String KEY_DATABASE_URL = "databaseURL";
private static final String KEY_STORAGE_BUCKET = "storageBucket";
private static final String KEY_OPTIONS = "options";
private static final String KEY_NAME = "name";
private static final String KEY_TRACKING_ID = "trackingId";
private static final String KEY_ENABLED = "enabled";
private static final String KEY_IS_AUTOMATIC_DATA_COLLECTION_ENABLED =
"isAutomaticDataCollectionEnabled";
private static final String KEY_PLUGIN_CONSTANTS = "pluginConstants";
private static final String CHANNEL_NAME = "plugins.flutter.io/firebase_core";
private MethodChannel channel;
private Context applicationContext;
private boolean coreInitialized = false;
/**
* Default Constructor.
*
* <p>Use this constructor in an add to app scenario to gracefully handle activity and context
* changes.
*/
public FlutterFirebaseCorePlugin() {}
private FlutterFirebaseCorePlugin(Context applicationContext) {
this.applicationContext = applicationContext;
}
@Override
public void onAttachedToEngine(FlutterPluginBinding binding) {
applicationContext = binding.getApplicationContext();
channel = new MethodChannel(binding.getBinaryMessenger(), CHANNEL_NAME);
channel.setMethodCallHandler(this);
}
@Override
public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
channel.setMethodCallHandler(null);
applicationContext = null;
}
private Map<String, String> firebaseOptionsToMap(FirebaseOptions options) {
Map<String, String> optionsMap = new HashMap<>();
optionsMap.put(KEY_API_KEY, options.getApiKey());
optionsMap.put(KEY_APP_ID, options.getApplicationId());
if (options.getGcmSenderId() != null) {
optionsMap.put(KEY_MESSAGING_SENDER_ID, options.getGcmSenderId());
}
if (options.getProjectId() != null) {
optionsMap.put(KEY_PROJECT_ID, options.getProjectId());
}
if (options.getDatabaseUrl() != null) {
optionsMap.put(KEY_DATABASE_URL, options.getDatabaseUrl());
}
if (options.getStorageBucket() != null) {
optionsMap.put(KEY_STORAGE_BUCKET, options.getStorageBucket());
}
if (options.getGaTrackingId() != null) {
optionsMap.put(KEY_TRACKING_ID, options.getGaTrackingId());
}
return optionsMap;
}
private Task<Map<String, Object>> firebaseAppToMap(FirebaseApp firebaseApp) {
return Tasks.call(
cachedThreadPool,
() -> {
Map<String, Object> appMap = new HashMap<>();
appMap.put(KEY_NAME, firebaseApp.getName());
appMap.put(KEY_OPTIONS, firebaseOptionsToMap(firebaseApp.getOptions()));
appMap.put(
KEY_IS_AUTOMATIC_DATA_COLLECTION_ENABLED,
firebaseApp.isDataCollectionDefaultEnabled());
appMap.put(
KEY_PLUGIN_CONSTANTS,
Tasks.await(
FlutterFirebasePluginRegistry.getPluginConstantsForFirebaseApp(firebaseApp)));
return appMap;
});
}
private Task<Map<String, Object>> initializeApp(Map<String, Object> arguments) {
return Tasks.call(
cachedThreadPool,
() -> {
String name = (String) Objects.requireNonNull(arguments.get(KEY_APP_NAME));
@SuppressWarnings("unchecked")
Map<String, String> optionsMap =
(Map<String, String>) Objects.requireNonNull(arguments.get(KEY_OPTIONS));
FirebaseOptions options =
new FirebaseOptions.Builder()
.setApiKey(Objects.requireNonNull(optionsMap.get(KEY_API_KEY)))
.setApplicationId(Objects.requireNonNull(optionsMap.get(KEY_APP_ID)))
.setDatabaseUrl(optionsMap.get(KEY_DATABASE_URL))
.setGcmSenderId(optionsMap.get(KEY_MESSAGING_SENDER_ID))
.setProjectId(optionsMap.get(KEY_PROJECT_ID))
.setStorageBucket(optionsMap.get(KEY_STORAGE_BUCKET))
.setGaTrackingId(optionsMap.get(KEY_TRACKING_ID))
.build();
// TODO(Salakar) hacky workaround a bug with FirebaseInAppMessaging causing the error:
// Can't create handler inside thread Thread[pool-3-thread-1,5,main] that has not called Looper.prepare()
// at com.google.firebase.inappmessaging.internal.ForegroundNotifier.<init>(ForegroundNotifier.java:61)
try {
Looper.prepare();
} catch (Exception e) {
// do nothing
}
FirebaseApp firebaseApp = FirebaseApp.initializeApp(applicationContext, options, name);
return Tasks.await(firebaseAppToMap(firebaseApp));
});
}
private Task<List<Map<String, Object>>> initializeCore() {
return Tasks.call(
cachedThreadPool,
() -> {
if (!coreInitialized) {
coreInitialized = true;
} else {
Tasks.await(FlutterFirebasePluginRegistry.didReinitializeFirebaseCore());
}
List<FirebaseApp> firebaseApps = FirebaseApp.getApps(applicationContext);
List<Map<String, Object>> firebaseAppsList = new ArrayList<>(firebaseApps.size());
for (FirebaseApp firebaseApp : firebaseApps) {
firebaseAppsList.add(Tasks.await(firebaseAppToMap(firebaseApp)));
}
return firebaseAppsList;
});
}
private Task<Map<String, String>> firebaseOptionsFromResource() {
return Tasks.call(
cachedThreadPool,
() -> {
final FirebaseOptions options = FirebaseOptions.fromResource(applicationContext);
if (options == null) return null;
return firebaseOptionsToMap(options);
});
}
private Task<Void> setAutomaticDataCollectionEnabled(Map<String, Object> arguments) {
return Tasks.call(
cachedThreadPool,
() -> {
String appName = (String) Objects.requireNonNull(arguments.get(KEY_APP_NAME));
Boolean enabled = (Boolean) Objects.requireNonNull(arguments.get(KEY_ENABLED));
FirebaseApp firebaseApp = FirebaseApp.getInstance(appName);
firebaseApp.setDataCollectionDefaultEnabled(enabled);
return null;
});
}
private Task<Void> setAutomaticResourceManagementEnabled(Map<String, Object> arguments) {
return Tasks.call(
cachedThreadPool,
() -> {
String appName = (String) Objects.requireNonNull(arguments.get(KEY_APP_NAME));
boolean enabled = (boolean) Objects.requireNonNull(arguments.get(KEY_ENABLED));
FirebaseApp firebaseApp = FirebaseApp.getInstance(appName);
firebaseApp.setAutomaticResourceManagementEnabled(enabled);
return null;
});
}
private Task<Void> deleteApp(Map<String, Object> arguments) {
return Tasks.call(
cachedThreadPool,
() -> {
String appName = (String) Objects.requireNonNull(arguments.get(KEY_APP_NAME));
FirebaseApp firebaseApp = FirebaseApp.getInstance(appName);
try {
firebaseApp.delete();
} catch (IllegalStateException appNotFoundException) {
// Ignore app not found exceptions.
}
return null;
});
}
@Override
public void onMethodCall(MethodCall call, @NonNull final MethodChannel.Result result) {
Task<?> methodCallTask;
switch (call.method) {
case "Firebase#initializeApp":
methodCallTask = initializeApp(call.arguments());
break;
case "Firebase#initializeCore":
methodCallTask = initializeCore();
break;
case "Firebase#optionsFromResource":
methodCallTask = firebaseOptionsFromResource();
break;
case "FirebaseApp#setAutomaticDataCollectionEnabled":
methodCallTask = setAutomaticDataCollectionEnabled(call.arguments());
break;
case "FirebaseApp#setAutomaticResourceManagementEnabled":
methodCallTask = setAutomaticResourceManagementEnabled(call.arguments());
break;
case "FirebaseApp#delete":
methodCallTask = deleteApp(call.arguments());
break;
default:
result.notImplemented();
return;
}
methodCallTask.addOnCompleteListener(
task -> {
if (task.isSuccessful()) {
result.success(task.getResult());
} else {
Exception exception = task.getException();
result.error("firebase_core", exception != null ? exception.getMessage() : null, null);
}
});
}
}