Files
smooth-app/packages/smooth_app/lib/helpers/network_config.dart
monsieurtanuki ad46236bdf feat: 3585 - upgrade to flutter 3.7 (#3666)
Impacted files:
* `account_deletion_webview.dart`: minor refactoring; could not test, unfortunately
* `background_task_badge.dart`: minor refactoring
* `camera_scan_page.dart`: minor refactoring
* `country_selector.dart`: minor refactoring
* `generated_plugin_registrant.cc`: wtf
* `generated_plugins.cmake`: wtf
* `GeneratedPluginRegistrant.swift`: wtf
* `Info.plist`: wtf
* `knowledge_panel_world_map_card.dart`: minor refactoring
* `network_config.dart`: minor refactoring
* `Podfile.lock`: wtf
* `product_query_page.dart`: minor refactoring
* `project.pbxproj`: wtf
* `pubspec.lock`: wtf
* `apple_app_store/pubspec.yaml`: to dart 2.19 without restriction for flutter
* `google_play/pubspec.yaml`: to dart 2.19 without restriction for flutter
* `app_store/shared/pubspec.yaml`: to dart 2.19 without restriction for flutter
* `uri_store/pubspec.yaml`: to dart 2.19 without restriction for flutter
* `data_importer/pubspec.yaml`: to dart 2.19 without restriction for flutter
* `data_importer_shared/pubspec.yaml`: to dart 2.19 without restriction for flutter
* `mlkit/pubspec.yaml`: to dart 2.19 without restriction for flutter
* `scanner/shared/pubspec.yaml`: to dart 2.19 without restriction for flutter
* `zxing/pubspec.yaml`: to dart 2.19 without restriction for flutter
* `smooth_app/pubspec.yaml`: to dart 2.19 without restriction for flutter; upgraded the versions
* `user_preferences_page_test.dart`: minor cheat - we need either to refactor with `launchUrl` or to fix `WebView`
2023-02-03 09:54:05 +01:00

93 lines
2.5 KiB
Dart

import 'dart:io';
import 'package:device_info_plus/device_info_plus.dart' deferred as dip;
import 'package:flutter/services.dart';
import 'package:openfoodfacts/openfoodfacts.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:smooth_app/helpers/app_helper.dart';
/// Initializes both the user agent && the SSL certificate
Future<void> setupAppNetworkConfig() async {
await _initUserAgent();
return _importSSLCertificate();
}
Future<void> _initUserAgent() async {
final PackageInfo packageInfo = await PackageInfo.fromPlatform();
final String name = 'Smoothie - ${packageInfo.appName}';
final String version = '${packageInfo.version}+${packageInfo.buildNumber}';
final String system =
'${Platform.operatingSystem}+${Platform.operatingSystemVersion}';
final String comment = _getAppInfoComment(
name: name,
version: version,
system: system,
);
OpenFoodAPIConfiguration.userAgent = UserAgent(
name: name,
version: version,
system: system,
url: 'https://world.openfoodfacts.org/',
comment: comment);
}
String _getAppInfoComment({
bool withName = true,
String name = '',
bool withVersion = true,
String version = '',
bool withSystem = true,
String system = '',
}) {
String appInfo = '';
const String infoDelimiter = ' - ';
if (withName) {
appInfo += infoDelimiter;
appInfo += name;
}
if (withVersion) {
appInfo += infoDelimiter;
appInfo += version;
}
if (withSystem) {
appInfo += infoDelimiter;
appInfo += system;
}
return appInfo;
}
/// Imports the OFF SSL certificate (for Android 7.1+ / iOS devices)
/// or accepts all certificates
Future<void> _importSSLCertificate() async {
if (Platform.isAndroid) {
await dip.loadLibrary();
final int sdkInt =
(await dip.DeviceInfoPlugin().androidInfo).version.sdkInt;
// API Level 25 is Android 7.1
if (sdkInt < 25) {
HttpOverrides.global = _AndroidHttpOverrides();
}
}
final ByteData data = await PlatformAssetBundle().load(
AppHelper.getAssetPath('assets/network/cert.pem'),
);
SecurityContext.defaultContext.setTrustedCertificatesBytes(
data.buffer.asUint8List(),
);
}
/// A custom Http implementation that accepts all SSL certificates
class _AndroidHttpOverrides extends HttpOverrides {
@override
HttpClient createHttpClient(SecurityContext? context) {
return super.createHttpClient(context)
..badCertificateCallback =
(X509Certificate cert, String host, int port) =>
host.contains('openfoodfacts.org');
}
}