-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathsetup_app.dart
More file actions
225 lines (193 loc) · 8.54 KB
/
setup_app.dart
File metadata and controls
225 lines (193 loc) · 8.54 KB
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
import 'dart:isolate';
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_crashlytics/firebase_crashlytics.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_app/app/app.dart';
import 'package:flutter_app/app/configuration/configuration.dart';
import 'package:flutter_app/app/setup/app_platform.dart';
import 'package:flutter_app/app/setup/flavor.dart';
import 'package:flutter_app/app/setup/web_setup.dart';
import 'package:flutter_app/app/theme/custom_system_bars_theme.dart';
import 'package:flutter_app/common/provider/theme_mode_provider.dart';
import 'package:flutter_app/core/analytics/crashlytics_manager.dart';
import 'package:flutter_app/core/flogger.dart';
import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'package:flutter_native_splash/flutter_native_splash.dart';
import 'package:flutter_web_plugins/url_strategy.dart';
import 'package:freerasp/freerasp.dart';
import 'package:google_sign_in/google_sign_in.dart';
import 'package:window_manager/window_manager.dart';
Future<void> setupApp({required Flavor flavor}) async {
final widgetsBinding = WidgetsFlutterBinding.ensureInitialized();
if (!kIsWeb) {
FlutterNativeSplash.preserve(widgetsBinding: widgetsBinding);
}
// Force app orientation to portrait and landscape.
// This must be also forced in info.plist file for iOS, under [UISupportedInterfaceOrientations].
await SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitUp,
DeviceOrientation.portraitDown,
DeviceOrientation.landscapeLeft,
DeviceOrientation.landscapeRight,
]);
// Configure Flavor
await Configuration.setup(flavor: flavor);
// Load secrets from .env file and override with specific flavor file
await dotenv.load(
overrideWithFiles: switch (flavor) {
Flavor.staging => ['.env-staging'],
Flavor.develop => ['.env-development'],
Flavor.production => ['.env-production'],
},
);
// Setup Firebase
await _setupFirebase(flavor: flavor);
await _setupFirebaseCrashlytics();
await _setupFirebaseRemoteConfig();
await _setupFirebaseMessaging();
await _setupLocalNotificationsService();
// Setup Google Sign In
await GoogleSignIn.instance.initialize();
// Setup FreeRASP Security
await _setupRASP(flavor: flavor);
// Web support
await _setupWebPlatform(flavor: flavor);
// Desktop support
await _setupDesktopPlatform();
// Setup Images Cache size
PaintingBinding.instance.imageCache.maximumSize = 100; // Number of images to hold in cache
PaintingBinding.instance.imageCache.maximumSizeBytes = 200 << 20; // Equals to 250MB of cache
// Load Theme Mode from DB before starting app
await providerContainer.read(themeModeProvider.future);
// Setup reactive Edge-to-Edge support across all platforms
await CustomSystemBarsTheme.setupSystemBarsTheme(providerContainer: providerContainer);
}
// TODO(strv): Support it or remove it!
Future<void> _setupFirebase({required Flavor flavor}) async {
if (AppPlatform.isMobile || AppPlatform.isMacOS) {
await Firebase.initializeApp();
} else if (AppPlatform.isWeb) {
await Firebase.initializeApp(
// For WebApp we need to specify the FirebaseOptions here!
// TODO(strv): Replace with actual values (use secrets!). Can be found inside Firebase -> Project Settings, under Web App
options: FirebaseOptions(
apiKey: dotenv.get('FIREBASE_WEB_API_KEY'),
authDomain: 'strv-flutter-template.firebaseapp.com',
projectId: 'strv-flutter-template',
storageBucket: 'strv-flutter-template.appspot.com',
messagingSenderId: dotenv.get('FIREBASE_WEB_MESSAGING_SENDER_ID'),
appId: dotenv.get('FIREBASE_WEB_APP_ID'),
measurementId: dotenv.get('FIREBASE_WEB_MEASUREMENT_ID'),
),
);
}
}
// TODO(strv): Support it or remove it!
/// Setup Firebase crashlytics according [docs](https://firebase.google.com/docs/crashlytics/customize-crash-reports?platform=flutter).
Future<void> _setupFirebaseCrashlytics() async {
if (!AppPlatform.isMobile) return;
await FirebaseCrashlytics.instance.setCrashlyticsCollectionEnabled(!kDebugMode);
// Handles errors thrown by Flutter framework and records them as fatal errors
FlutterError.onError = FirebaseCrashlytics.instance.recordFlutterFatalError;
// Handles async errors that can't be handled by Flutter framework
PlatformDispatcher.instance.onError = (error, stack) {
CrashlyticsManager.logCritical(
error,
stack: stack,
);
return true;
};
// Handles errors that happen outside of the Flutter context
Isolate.current.addErrorListener(
RawReceivePort((dynamic pair) async {
if (pair is List && pair.length == 2 && pair[1] is StackTrace) {
final dynamic error = pair[0];
final stack = pair[1] as StackTrace;
await CrashlyticsManager.logCritical(error, stack: stack);
} else {
// Optionally log or handle unexpected message format
Flogger.e('Unexpected error message format from isolate: $pair');
}
}).sendPort,
);
}
// TODO(strv): Support it or remove it!
Future<void> _setupFirebaseRemoteConfig() async {
if (!AppPlatform.isMobile) return;
// TODO(strv): Uncomment once secrets are filled with proper values
// await providerContainer.read(firebaseRemoteConfigServiceProvider.future);
}
// TODO(strv): Support it or remove it!
Future<void> _setupFirebaseMessaging() async {
if (!AppPlatform.isLinux && !AppPlatform.isWindows) {
// TODO(strv): Uncomment once secrets are filled with proper values
// await providerContainer.read(firebaseMessagingServiceProvider.future);
}
}
// TODO(strv): Support it or remove it!
Future<void> _setupLocalNotificationsService() async {
if (!AppPlatform.isLinux && !AppPlatform.isWindows) {
// TODO(strv): Uncomment once secrets are filled with proper values
// await providerContainer.read(notificationsServiceProvider.future);
}
}
// TODO(strv): [FreeRASP] Configure correct package names and other values
Future<void> _setupRASP({required Flavor flavor}) async {
// Only Mobile platforms are supported!
if (!AppPlatform.isMobile) return;
final config = TalsecConfig(
isProd: flavor == Flavor.production && !kDebugMode,
watcherMail: 'michal.urbanek@strv.com',
/// For Android
androidConfig: AndroidConfig(
packageName: dotenv.get('APP_ID'),
// signingCertHashes: list of hashes of the certificates of the keys which were used to sign the application.
// At least one hash value must be provided. Hashes which are passed here must be encoded in Base64 form
// https://github.com/talsec/Free-RASP-Community/wiki/Getting-your-signing-certificate-hash-of-app
signingCertHashes: [
dotenv.get('SIGNING_CERT_DEBUG_BASE64'), // Debug
dotenv.get('SIGNING_CERT_RELEASE_BASE64'), // Release
dotenv.get('SIGNING_CERT_GOOGLE_PLAY_BASE64'), // Google Play
],
),
/// For iOS
iosConfig: IOSConfig(
bundleIds: [dotenv.get('APP_ID')],
teamId: dotenv.get('APPLE_TEAM_ID'),
),
);
// Setting up callbacks
final callback = ThreatCallback(
onAppIntegrity: () => CrashlyticsManager.logNonCritical('App integrity'),
onObfuscationIssues: () => CrashlyticsManager.logNonCritical('Obfuscation issues'),
onDebug: () => CrashlyticsManager.logNonCritical('Debugging'),
onDeviceBinding: () => CrashlyticsManager.logNonCritical('Device binding'),
onDeviceID: () => CrashlyticsManager.logNonCritical('Device ID'),
onHooks: () => CrashlyticsManager.logNonCritical('Hooks'),
onPasscode: () => CrashlyticsManager.logNonCritical('Phone has no passcode set'),
onPrivilegedAccess: () => CrashlyticsManager.logNonCritical('Privileged access'),
onSecureHardwareNotAvailable: () => CrashlyticsManager.logNonCritical('Secure hardware not available'),
onSimulator: () => CrashlyticsManager.logNonCritical('Simulator'),
onUnofficialStore: () => CrashlyticsManager.logNonCritical('Unofficial store'),
);
// Attach listener and Start freeRASP
await Talsec.instance.attachListener(callback);
await Talsec.instance.start(config);
}
Future<void> _setupWebPlatform({required Flavor flavor}) async {
if (AppPlatform.isWeb) {
// Use paths without /#/
usePathUrlStrategy();
// Setup the Web
WebSetup.setup(flavor: flavor);
}
}
Future<void> _setupDesktopPlatform() async {
if (AppPlatform.isDesktop) {
await windowManager.ensureInitialized();
await windowManager.setMinimumSize(const Size(400, 400));
await windowManager.setSize(const Size(500, 950));
}
}