Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(android,core): allow initializing default Firebase apps via FirebaseOptions.fromResource on Android #8566

Merged
merged 1 commit into from May 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -73,39 +73,43 @@ public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
applicationContext = null;
}

private Task<Map<String, Object>> firebaseAppToMap(FirebaseApp firebaseApp) {
return Tasks.call(
cachedThreadPool,
() -> {
Map<String, Object> appMap = new HashMap<>();
Map<String, String> optionsMap = new HashMap<>();
FirebaseOptions options = firebaseApp.getOptions();
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());
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.getGcmSenderId() != null) {
optionsMap.put(KEY_MESSAGING_SENDER_ID, options.getGcmSenderId());
}

if (options.getProjectId() != null) {
optionsMap.put(KEY_PROJECT_ID, options.getProjectId());
}
if (options.getProjectId() != null) {
optionsMap.put(KEY_PROJECT_ID, options.getProjectId());
}

if (options.getDatabaseUrl() != null) {
optionsMap.put(KEY_DATABASE_URL, options.getDatabaseUrl());
}
if (options.getDatabaseUrl() != null) {
optionsMap.put(KEY_DATABASE_URL, options.getDatabaseUrl());
}

if (options.getStorageBucket() != null) {
optionsMap.put(KEY_STORAGE_BUCKET, options.getStorageBucket());
}
if (options.getStorageBucket() != null) {
optionsMap.put(KEY_STORAGE_BUCKET, options.getStorageBucket());
}

if (options.getGaTrackingId() != null) {
optionsMap.put(KEY_TRACKING_ID, options.getGaTrackingId());
}
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, optionsMap);
appMap.put(KEY_OPTIONS, firebaseOptionsToMap(firebaseApp.getOptions()));

appMap.put(
KEY_IS_AUTOMATIC_DATA_COLLECTION_ENABLED,
Expand Down Expand Up @@ -173,6 +177,16 @@ private Task<List<Map<String, Object>>> initializeCore() {
});
}

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,
Expand Down Expand Up @@ -224,6 +238,9 @@ public void onMethodCall(MethodCall call, @NonNull final MethodChannel.Result re
case "Firebase#initializeCore":
methodCallTask = initializeCore();
break;
case "Firebase#optionsFromResource":
methodCallTask = firebaseOptionsFromResource();
break;
case "FirebaseApp#setAutomaticDataCollectionEnabled":
methodCallTask = setAutomaticDataCollectionEnabled(call.arguments());
break;
Expand Down
@@ -0,0 +1,87 @@
// File generated by FlutterFire CLI.
// ignore_for_file: lines_longer_than_80_chars, avoid_classes_with_only_static_members
import 'package:firebase_core/firebase_core.dart' show FirebaseOptions;
import 'package:flutter/foundation.dart'
show defaultTargetPlatform, kIsWeb, TargetPlatform;

/// Default [FirebaseOptions] for use with your Firebase apps.
///
/// Example:
/// ```dart
/// import 'firebase_options.dart';
/// // ...
/// await Firebase.initializeApp(
/// options: DefaultFirebaseOptions.currentPlatform,
/// );
/// ```
class DefaultFirebaseOptions {
static FirebaseOptions get currentPlatform {
if (kIsWeb) {
return web;
}
switch (defaultTargetPlatform) {
case TargetPlatform.android:
return android;
case TargetPlatform.iOS:
return ios;
case TargetPlatform.macOS:
return macos;
default:
throw UnsupportedError(
'DefaultFirebaseOptions are not supported for this platform.',
);
}
}

static const FirebaseOptions web = FirebaseOptions(
apiKey: 'AIzaSyB7wZb2tO1-Fs6GbDADUSTs2Qs3w08Hovw',
appId: '1:406099696497:web:87e25e51afe982cd3574d0',
messagingSenderId: '406099696497',
projectId: 'flutterfire-e2e-tests',
authDomain: 'flutterfire-e2e-tests.firebaseapp.com',
databaseURL:
'https://flutterfire-e2e-tests-default-rtdb.europe-west1.firebasedatabase.app',
storageBucket: 'flutterfire-e2e-tests.appspot.com',
measurementId: 'G-JN95N1JV2E',
);

static const FirebaseOptions android = FirebaseOptions(
apiKey: 'AIzaSyCdRjCVZlhrq72RuEklEyyxYlBRCYhI2Sw',
appId: '1:406099696497:android:0d4ed619c031c0ac3574d0',
messagingSenderId: '406099696497',
projectId: 'flutterfire-e2e-tests',
databaseURL:
'https://flutterfire-e2e-tests-default-rtdb.europe-west1.firebasedatabase.app',
storageBucket: 'flutterfire-e2e-tests.appspot.com',
);

static const FirebaseOptions ios = FirebaseOptions(
apiKey: 'AIzaSyDooSUGSf63Ghq02_iIhtnmwMDs4HlWS6c',
appId: '1:406099696497:ios:acd9c8e17b5e620e3574d0',
messagingSenderId: '406099696497',
projectId: 'flutterfire-e2e-tests',
databaseURL:
'https://flutterfire-e2e-tests-default-rtdb.europe-west1.firebasedatabase.app',
storageBucket: 'flutterfire-e2e-tests.appspot.com',
androidClientId:
'406099696497-tvtvuiqogct1gs1s6lh114jeps7hpjm5.apps.googleusercontent.com',
iosClientId:
'406099696497-taeapvle10rf355ljcvq5dt134mkghmp.apps.googleusercontent.com',
iosBundleId: 'io.flutter.plugins.firebase.tests',
);

static const FirebaseOptions macos = FirebaseOptions(
apiKey: 'AIzaSyDooSUGSf63Ghq02_iIhtnmwMDs4HlWS6c',
appId: '1:406099696497:ios:acd9c8e17b5e620e3574d0',
messagingSenderId: '406099696497',
projectId: 'flutterfire-e2e-tests',
databaseURL:
'https://flutterfire-e2e-tests-default-rtdb.europe-west1.firebasedatabase.app',
storageBucket: 'flutterfire-e2e-tests.appspot.com',
androidClientId:
'406099696497-tvtvuiqogct1gs1s6lh114jeps7hpjm5.apps.googleusercontent.com',
iosClientId:
'406099696497-taeapvle10rf355ljcvq5dt134mkghmp.apps.googleusercontent.com',
iosBundleId: 'io.flutter.plugins.firebase.tests',
);
}