Skip to content

Commit

Permalink
Merge pull request #258 from humhub/f-internal-63
Browse files Browse the repository at this point in the history
Add latest userAgent and define applicationNameForUserAgent
  • Loading branch information
marc-farre authored Nov 27, 2024
2 parents 0cd45a7 + 880fa0f commit bbf0400
Show file tree
Hide file tree
Showing 12 changed files with 70 additions and 52 deletions.
4 changes: 2 additions & 2 deletions lib/app_flavored.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,6 @@ class FlavoredAppState extends ConsumerState<FlavoredApp> {
}
}

final humHubFProvider = FutureProvider<HumHubF>((ref) {
return HumHubF.initialize();
final humHubFProvider = Provider<HumHubF>((ref) {
return HumHubF();
});
9 changes: 3 additions & 6 deletions lib/flavored/models/humhub.f.dart
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'package:humhub/flavored/models/manifest.f.dart';
import 'package:humhub/models/global_package_info.dart';
import 'package:humhub/models/hum_hub.dart';
import 'package:package_info_plus/package_info_plus.dart';

class HumHubF extends HumHub {
@override
ManifestF get manifest => ManifestF.fromEnv();
@override
String get manifestUrl => dotenv.env['MANIFEST_URL']!;
final String bundleId;

HumHubF({
bool isHideOpener = false,
String? randomHash,
String? appVersion,
String? pushToken,
required this.bundleId,
}) : super(
isHideOpener: isHideOpener,
randomHash: HumHub.generateHash(32),
Expand All @@ -26,14 +24,13 @@ class HumHubF extends HumHub {
Map<String, String> get customHeaders => {
'x-humhub-app-token': randomHash!,
'x-humhub-app': appVersion ?? '1.0.0',
'x-humhub-app-bundle-id': bundleId,
'x-humhub-app-bundle-id': GlobalPackageInfo.info.packageName,
'x-humhub-app-ostate': isHideOpener ? '1' : '0',
'x-humhub-app-is-ios': isIos ? '1' : '0',
'x-humhub-app-is-android': isAndroid ? '1' : '0'
};

static Future<HumHubF> initialize() async {
PackageInfo packageInfo = await PackageInfo.fromPlatform();
return HumHubF(bundleId: packageInfo.packageName);
return HumHubF();
}
}
4 changes: 2 additions & 2 deletions lib/flavored/web_view.f.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ class FlavoredWebViewState extends ConsumerState<WebViewF> {

@override
void initState() {
instance = ref.read(humHubFProvider).value!;
instance = ref.read(humHubFProvider);
_authBrowser = AuthInAppBrowser(
manifest: ref.read(humHubFProvider).value!.manifest,
manifest: ref.read(humHubFProvider).manifest,
concludeAuth: (URLRequest request) {
_concludeAuth(request);
},
Expand Down
23 changes: 1 addition & 22 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,29 +1,8 @@
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:humhub/models/hum_hub.dart';
import 'package:humhub/util/log.dart';
import 'package:humhub/util/permission_handler.dart';
import 'package:humhub/util/storage_service.dart';
import 'package:loggy/loggy.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:permission_handler/permission_handler.dart';

main() async {
Loggy.initLoggy(
logPrinter: const GlobalLog(),
);
WidgetsFlutterBinding.ensureInitialized();
await SecureStorageService.clearSecureStorageOnReinstall();
PackageInfo packageInfo = await PackageInfo.fromPlatform();
final app = await HumHub.app(packageInfo.packageName);
await PermissionHandler.requestPermissions(
[
Permission.notification,
Permission.camera,
Permission.microphone,
Permission.storage,
Permission.photos
],
);
final app = await HumHub.init();
runApp(ProviderScope(child: app));
}
9 changes: 9 additions & 0 deletions lib/models/global_package_info.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import 'package:package_info_plus/package_info_plus.dart';

class GlobalPackageInfo {
static late PackageInfo info;

static Future<void> init() async {
info = await PackageInfo.fromPlatform();
}
}
18 changes: 18 additions & 0 deletions lib/models/global_user_agent.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import 'dart:io';

class GlobalUserAgent {
static const String _ios =
"Mozilla/5.0 (iPhone; CPU iPhone OS 17_7 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) CriOS/131.0.6778.73 Mobile/15E148 Safari/604.1";
static const String _android =
"Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.6778.81 Mobile Safari/537.36";

static String get value {
if (Platform.isIOS) {
return _ios;
} else if (Platform.isAndroid) {
return _android;
} else {
return "Unknown User Agent"; // Fallback for unsupported platforms
}
}
}
19 changes: 17 additions & 2 deletions lib/models/hum_hub.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,14 @@ import 'package:flutter/material.dart';
import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'package:humhub/app_flavored.dart';
import 'package:humhub/app_opener.dart';
import 'package:humhub/models/global_package_info.dart';
import 'package:humhub/models/manifest.dart';
import 'package:humhub/util/log.dart';
import 'package:humhub/util/openers/universal_opener_controller.dart';
import 'package:humhub/util/permission_handler.dart';
import 'package:humhub/util/storage_service.dart';
import 'package:loggy/loggy.dart';
import 'package:permission_handler/permission_handler.dart';

enum RedirectAction { opener, webView }

Expand Down Expand Up @@ -73,8 +79,17 @@ class HumHub {
'x-humhub-app-is-android': isAndroid ? '1' : '0'
};

static Future<Widget> app(String bundleId) async {
switch (bundleId) {
static Future<Widget> init() async {
Loggy.initLoggy(
logPrinter: const GlobalLog(),
);
WidgetsFlutterBinding.ensureInitialized();
await SecureStorageService.clearSecureStorageOnReinstall();
await GlobalPackageInfo.init();
await PermissionHandler.requestPermissions(
[Permission.notification, Permission.camera, Permission.microphone, Permission.storage, Permission.photos],
);
switch (GlobalPackageInfo.info.packageName) {
case 'com.humhub.app':
return const OpenerApp();
default:
Expand Down
16 changes: 7 additions & 9 deletions lib/util/auth_in_app_browser.dart
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
import 'package:humhub/models/global_package_info.dart';
import 'package:humhub/models/global_user_agent.dart';
import 'package:humhub/models/manifest.dart';
import 'package:loggy/loggy.dart';

import 'extensions.dart';

class AuthInAppBrowser extends InAppBrowser {
final Manifest manifest;
late InAppBrowserClassSettings settings;
final Function concludeAuth;
static const String userAgent =
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36';

AuthInAppBrowser({required this.manifest, required this.concludeAuth}) {
settings = InAppBrowserClassSettings(
browserSettings: InAppBrowserSettings(
Expand All @@ -23,10 +21,10 @@ class AuthInAppBrowser extends InAppBrowser {
presentationStyle: ModalPresentationStyle.PAGE_SHEET,
),
webViewSettings: InAppWebViewSettings(
javaScriptEnabled: true,
useShouldOverrideUrlLoading: true,
userAgent: userAgent,
),
javaScriptEnabled: true,
useShouldOverrideUrlLoading: true,
userAgent: GlobalUserAgent.value,
applicationNameForUserAgent: GlobalPackageInfo.info.appName),
);
}

Expand All @@ -42,6 +40,6 @@ class AuthInAppBrowser extends InAppBrowser {
}

launchUrl(URLRequest urlRequest) {
openUrlRequest(urlRequest: urlRequest, settings: settings);
openUrlRequest(urlRequest: urlRequest, /*options: options,*/ settings: settings);
}
}
6 changes: 3 additions & 3 deletions lib/util/file_handler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import 'dart:convert';
import 'dart:io';

import 'package:flutter_inappwebview/flutter_inappwebview.dart';
import 'package:humhub/models/global_package_info.dart';
import 'package:humhub/util/permission_handler.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:path_provider/path_provider.dart';

class FileHandler {
Expand Down Expand Up @@ -143,6 +143,7 @@ class FileHandler {
}
}

// ignore: unused_element
Future<(File file, String filename)> _saveFile(String base64Data) async {
final decodedBytes = base64Decode(base64Data.split(",").last);

Expand All @@ -160,8 +161,7 @@ class FileHandler {
}

Future<String> _generateFilename() async {
PackageInfo packageInfo = await PackageInfo.fromPlatform();
String appName = packageInfo.appName.replaceAll(' ', '_');
String appName = GlobalPackageInfo.info.appName.replaceAll(' ', '_');
return '${appName}file';
}

Expand Down
5 changes: 2 additions & 3 deletions lib/util/notifications/channel.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:humhub/flavored/util/notifications/channel.f.dart';
import 'package:humhub/models/global_package_info.dart';
import 'package:humhub/pages/web_view.dart';
import 'package:humhub/util/const.dart';
import 'package:humhub/util/init_from_url.dart';
import 'package:humhub/util/openers/universal_opener_controller.dart';
import 'package:package_info_plus/package_info_plus.dart';

class NotificationChannel {
final String id;
Expand Down Expand Up @@ -43,8 +43,7 @@ class NotificationChannel {
}

static Future<NotificationChannel> getChannel() async {
PackageInfo packageInfo = await PackageInfo.fromPlatform(); // Replace this with the actual condition logic
switch (packageInfo.packageName) {
switch (GlobalPackageInfo.info.packageName) {
case 'com.humhub.app':
return const NotificationChannel();
default:
Expand Down
5 changes: 2 additions & 3 deletions lib/util/providers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import 'dart:convert';
import 'package:flutter/cupertino.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:humhub/models/global_package_info.dart';
import 'package:humhub/models/hum_hub.dart';
import 'package:humhub/models/manifest.dart';
import 'package:package_info_plus/package_info_plus.dart';

import 'const.dart';

Expand Down Expand Up @@ -35,11 +35,10 @@ class HumHubNotifier extends ChangeNotifier {
}

Future<void> setInstance(HumHub instance) async {
PackageInfo packageInfo = await PackageInfo.fromPlatform();
_humHubInstance.manifest = instance.manifest;
_humHubInstance.isHideOpener = instance.isHideOpener;
_humHubInstance.randomHash = instance.randomHash;
_humHubInstance.appVersion = packageInfo.version;
_humHubInstance.appVersion = GlobalPackageInfo.info.version;
_humHubInstance.manifestUrl = instance.manifestUrl;
_updateSafeStorage();
notifyListeners();
Expand Down
4 changes: 4 additions & 0 deletions lib/util/web_view_global_controller.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import 'dart:convert';
import 'package:flutter/services.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
import 'package:humhub/models/global_package_info.dart';
import 'package:humhub/models/global_user_agent.dart';
import 'package:humhub/models/manifest.dart';

class WebViewGlobalController {
Expand Down Expand Up @@ -138,6 +140,8 @@ class WebViewGlobalController {
allowsInlineMediaPlayback: true,
mediaPlaybackRequiresUserGesture: false,
supportZoom: zoom ? true : false,
userAgent: GlobalUserAgent.value,
applicationNameForUserAgent: GlobalPackageInfo.info.appName,
);
}
}

0 comments on commit bbf0400

Please sign in to comment.