resolve qr code permssion issue

This commit is contained in:
Hiddify
2024-03-17 13:40:13 +01:00
parent b672c904e3
commit c43d381554
9 changed files with 152 additions and 219 deletions

View File

@@ -149,13 +149,13 @@ jobs:
[IO.File]::WriteAllBytes("windows\sign.pfx", [Convert]::FromBase64String("${{ secrets.WINDOWS_SIGNING_KEY }}")) [IO.File]::WriteAllBytes("windows\sign.pfx", [Convert]::FromBase64String("${{ secrets.WINDOWS_SIGNING_KEY }}"))
(Get-Content "windows\packaging\msix\make_config.yaml") -replace '^certificate_password:.*$', 'certificate_password: ${{ secrets.WINDOWS_SIGNING_PASSWORD }}' | Set-Content "windows\packaging\msix\make_config.yaml" (Get-Content "windows\packaging\msix\make_config.yaml") -replace '^certificate_password:.*$', 'certificate_password: ${{ secrets.WINDOWS_SIGNING_PASSWORD }}' | Set-Content "windows\packaging\msix\make_config.yaml"
- name: Temporary disable Permission Handler for windows due to its issue in permission # - name: Temporary disable Permission Handler for windows due to its issue in permission
if: ${{ startsWith(matrix.platform,'windows') }} # if: ${{ startsWith(matrix.platform,'windows') }}
run: | # run: |
(Get-Content -Path "pubspec.yaml") -notmatch "permission_handler" | Set-Content -Path "pubspec.yaml" # (Get-Content -Path "pubspec.yaml") -notmatch "permission_handler" | Set-Content -Path "pubspec.yaml"
(Get-Content -Path "lib\features\profile\add\add_profile_modal.dart") -notmatch "qr_code_scanner_screen" | Set-Content -Path "lib\features\profile\add\add_profile_modal.dart" # (Get-Content -Path "lib\features\profile\add\add_profile_modal.dart") -notmatch "qr_code_scanner_screen" | Set-Content -Path "lib\features\profile\add\add_profile_modal.dart"
(Get-Content -Path lib\features\profile\add\add_profile_modal.dart) -replace 'await QRCodeScannerScreen\(\).open\(context\);', 'null;' | Set-Content -Path lib\features\profile\add\add_profile_modal.dart # (Get-Content -Path lib\features\profile\add\add_profile_modal.dart) -replace 'await QRCodeScannerScreen\(\).open\(context\);', 'null;' | Set-Content -Path lib\features\profile\add\add_profile_modal.dart
Remove-Item -Path "lib\features\common\qr_code_scanner_screen.dart" # Remove-Item -Path "lib\features\common\qr_code_scanner_screen.dart"

View File

@@ -74,7 +74,8 @@
"permissionDeniedError": "Permission Denied", "permissionDeniedError": "Permission Denied",
"unexpectedError": "Something Went Wrong", "unexpectedError": "Something Went Wrong",
"torchSemanticLabel": "Flash Light", "torchSemanticLabel": "Flash Light",
"facingSemanticLabel": "Camera Facing" "facingSemanticLabel": "Camera Facing",
"permissionRequest":"Permission to camera to scan QR Code"
}, },
"manually": "Manual Entry", "manually": "Manual Entry",
"addingProfileMsg": "Adding Profile", "addingProfileMsg": "Adding Profile",

View File

@@ -32,6 +32,8 @@ target 'Runner' do
use_modular_headers! use_modular_headers!
flutter_install_all_ios_pods File.dirname(File.realpath(__FILE__)) flutter_install_all_ios_pods File.dirname(File.realpath(__FILE__))
pod 'EasyPermissionX/Camera'
target 'RunnerTests' do target 'RunnerTests' do
inherit! :search_paths inherit! :search_paths
end end

View File

@@ -1,14 +1,15 @@
import 'package:dartx/dartx.dart'; import 'package:dartx/dartx.dart';
import 'package:fluentui_system_icons/fluentui_system_icons.dart'; import 'package:fluentui_system_icons/fluentui_system_icons.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_easy_permission/easy_permissions.dart';
import 'package:hiddify/core/localization/translations.dart'; import 'package:hiddify/core/localization/translations.dart';
import 'package:hiddify/utils/utils.dart'; import 'package:hiddify/utils/utils.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart'; import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:mobile_scanner/mobile_scanner.dart'; import 'package:mobile_scanner/mobile_scanner.dart';
import 'package:permission_handler/permission_handler.dart'; // import 'package:permission_handler/permission_handler.dart';
final _cameraPermissionProvider = const permissions = [Permissions.CAMERA];
FutureProvider.autoDispose((ref) => Permission.camera.request()); const permissionGroup = [PermissionGroup.Camera];
class QRCodeScannerScreen extends StatefulHookConsumerWidget { class QRCodeScannerScreen extends StatefulHookConsumerWidget {
const QRCodeScannerScreen({super.key}); const QRCodeScannerScreen({super.key});
@@ -32,185 +33,157 @@ class _QRCodeScannerScreenState extends ConsumerState<QRCodeScannerScreen>
final controller = final controller =
MobileScannerController(detectionTimeoutMs: 500, autoStart: false); MobileScannerController(detectionTimeoutMs: 500, autoStart: false);
bool started = false; bool started = false;
bool settingsOpened = false;
late FlutterEasyPermission _easyPermission;
@override @override
void initState() { void initState() {
super.initState(); super.initState();
WidgetsBinding.instance.addObserver(this); WidgetsBinding.instance.addObserver(this);
_easyPermission = FlutterEasyPermission()
..addPermissionCallback(onGranted: (requestCode, androidPerms, iosPerm) {
debugPrint("android:$androidPerms");
debugPrint("iOS:$iosPerm");
startQrScannerIfPermissionGranted();
}, onDenied: (requestCode, androidPerms, iosPerm, isPermanent) {
if (isPermanent) {
FlutterEasyPermission.showAppSettingsDialog(title: "Camera");
} else {
debugPrint("android:$androidPerms");
debugPrint("iOS:$iosPerm");
}
}, onSettingsReturned: () {
startQrScannerIfPermissionGranted();
});
} }
@override @override
void dispose() { void dispose() {
controller.stop(); controller.stop();
_easyPermission.dispose();
WidgetsBinding.instance.removeObserver(this); WidgetsBinding.instance.removeObserver(this);
super.dispose(); super.dispose();
} }
@override void startQrScannerIfPermissionGranted() {
void didChangeAppLifecycleState(AppLifecycleState state) { FlutterEasyPermission.has(perms: permissions, permsGroup: permissionGroup)
if (state == AppLifecycleState.resumed && settingsOpened) { .then((value) {
loggy.debug("resumed"); if (value) {
ref.invalidate(_cameraPermissionProvider); controller.start().then((result) {
settingsOpened = false; if (result != null) {
} setState(() {
started = true;
});
}
}).catchError((error) {
loggy.warning("Error starting scanner: $error");
});
} else {}
});
} }
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
final t = ref.watch(translationsProvider); final t = ref.watch(translationsProvider);
ref.listen( startQrScannerIfPermissionGranted();
_cameraPermissionProvider,
(previous, next) async {
if (next case AsyncData(:final value)
when value == PermissionStatus.granted) {
try {
final result = await controller.start();
if (result != null) {
setState(() {
started = true;
});
}
} catch (error) {
loggy.warning("error starting scanner: $error", error);
}
}
},
);
switch (ref.watch(_cameraPermissionProvider)) { final size = MediaQuery.sizeOf(context);
case AsyncData(value: final status) final overlaySize = (size.shortestSide - 12).coerceAtMost(248);
when status == PermissionStatus.granted:
final size = MediaQuery.sizeOf(context);
final overlaySize = (size.shortestSide - 12).coerceAtMost(248);
return Scaffold( return Scaffold(
extendBodyBehindAppBar: true, extendBodyBehindAppBar: true,
appBar: AppBar( appBar: AppBar(
backgroundColor: Colors.transparent, backgroundColor: Colors.transparent,
iconTheme: Theme.of(context).iconTheme.copyWith( iconTheme: Theme.of(context).iconTheme.copyWith(
color: Colors.white, color: Colors.white,
size: 32, size: 32,
),
actions: [
IconButton(
icon: ValueListenableBuilder(
valueListenable: controller.torchState,
builder: (context, state, child) {
switch (state) {
case TorchState.off:
return const Icon(
FluentIcons.flash_off_24_regular,
color: Colors.grey,
);
case TorchState.on:
return const Icon(
FluentIcons.flash_24_regular,
color: Colors.yellow,
);
}
},
),
tooltip: t.profile.add.qrScanner.torchSemanticLabel,
onPressed: () => controller.toggleTorch(),
),
IconButton(
icon: const Icon(FluentIcons.camera_switch_24_regular),
tooltip: t.profile.add.qrScanner.facingSemanticLabel,
onPressed: () => controller.switchCamera(),
),
],
),
body: Stack(
children: [
MobileScanner(
controller: controller,
onDetect: (capture) {
final rawData = capture.barcodes.first.rawValue;
loggy.debug('captured raw: [$rawData]');
if (rawData != null) {
final uri = Uri.tryParse(rawData);
if (context.mounted && uri != null) {
loggy.debug('captured url: [$uri]');
Navigator.of(context, rootNavigator: true)
.pop(uri.toString());
}
} else {
loggy.warning("unable to capture");
}
},
errorBuilder: (_, error, __) {
final message = switch (error.errorCode) {
MobileScannerErrorCode.permissionDenied =>
t.profile.add.qrScanner.permissionDeniedError,
_ => t.profile.add.qrScanner.unexpectedError,
};
return Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
const Padding(
padding: EdgeInsets.only(bottom: 8),
child: Icon(
FluentIcons.error_circle_24_regular,
color: Colors.white,
),
),
Text(message),
Text(error.errorDetails?.message ?? ''),
],
),
);
},
),
if (started)
CustomPaint(
painter: ScannerOverlay(
Rect.fromCenter(
center: size.center(Offset.zero),
width: overlaySize,
height: overlaySize,
),
),
),
],
),
);
case AsyncData(value: final status):
return Scaffold(
appBar: AppBar(),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(t.profile.add.qrScanner.permissionDeniedError),
if (status == PermissionStatus.permanentlyDenied)
TextButton(
onPressed: () async {
settingsOpened = await openAppSettings();
},
child: Text(t.general.openAppSettings),
)
else
TextButton(
onPressed: () {
ref.invalidate(_cameraPermissionProvider);
},
child: Text(t.general.grantPermission),
),
],
), ),
actions: [
IconButton(
icon: ValueListenableBuilder(
valueListenable: controller.torchState,
builder: (context, state, child) {
switch (state) {
case TorchState.off:
return const Icon(
FluentIcons.flash_off_24_regular,
color: Colors.grey,
);
case TorchState.on:
return const Icon(
FluentIcons.flash_24_regular,
color: Colors.yellow,
);
}
},
),
tooltip: t.profile.add.qrScanner.torchSemanticLabel,
onPressed: () => controller.toggleTorch(),
), ),
); IconButton(
icon: const Icon(FluentIcons.camera_switch_24_regular),
tooltip: t.profile.add.qrScanner.facingSemanticLabel,
onPressed: () => controller.switchCamera(),
),
],
),
body: Stack(
children: [
MobileScanner(
controller: controller,
onDetect: (capture) {
final rawData = capture.barcodes.first.rawValue;
loggy.debug('captured raw: [$rawData]');
if (rawData != null) {
final uri = Uri.tryParse(rawData);
if (context.mounted && uri != null) {
loggy.debug('captured url: [$uri]');
Navigator.of(context, rootNavigator: true)
.pop(uri.toString());
}
} else {
loggy.warning("unable to capture");
}
},
errorBuilder: (_, error, __) {
final message = switch (error.errorCode) {
MobileScannerErrorCode.permissionDenied =>
t.profile.add.qrScanner.permissionDeniedError,
_ => t.profile.add.qrScanner.unexpectedError,
};
default: return Center(
return Scaffold( child: Column(
appBar: AppBar(), mainAxisSize: MainAxisSize.min,
); children: [
} const Padding(
padding: EdgeInsets.only(bottom: 8),
child: Icon(
FluentIcons.error_circle_24_regular,
color: Colors.white,
),
),
Text(message),
Text(error.errorDetails?.message ?? ''),
],
),
);
},
),
if (started)
CustomPaint(
painter: ScannerOverlay(
Rect.fromCenter(
center: size.center(Offset.zero),
width: overlaySize,
height: overlaySize,
),
),
),
],
),
);
} }
} }

Submodule libcore updated: ec9efac57c...84e5933d9d

View File

@@ -503,6 +503,14 @@ packages:
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "0.6.0" version: "0.6.0"
flutter_easy_permission:
dependency: "direct main"
description:
name: flutter_easy_permission
sha256: "05eb1b561c894adef28b3ae38d8087fc2635f1047c5e18cf2698fb42b6ccc132"
url: "https://pub.dev"
source: hosted
version: "1.1.2"
flutter_gen_core: flutter_gen_core:
dependency: transitive dependency: transitive
description: description:
@@ -1102,54 +1110,6 @@ packages:
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "4.2.3" version: "4.2.3"
permission_handler:
dependency: "direct main"
description:
name: permission_handler
sha256: "74e962b7fad7ff75959161bb2c0ad8fe7f2568ee82621c9c2660b751146bfe44"
url: "https://pub.dev"
source: hosted
version: "11.3.0"
permission_handler_android:
dependency: transitive
description:
name: permission_handler_android
sha256: "1acac6bae58144b442f11e66621c062aead9c99841093c38f5bcdcc24c1c3474"
url: "https://pub.dev"
source: hosted
version: "12.0.5"
permission_handler_apple:
dependency: transitive
description:
name: permission_handler_apple
sha256: bdafc6db74253abb63907f4e357302e6bb786ab41465e8635f362ee71fd8707b
url: "https://pub.dev"
source: hosted
version: "9.4.0"
permission_handler_html:
dependency: transitive
description:
name: permission_handler_html
sha256: "54bf176b90f6eddd4ece307e2c06cf977fb3973719c35a93b85cc7093eb6070d"
url: "https://pub.dev"
source: hosted
version: "0.1.1"
permission_handler_platform_interface:
dependency: transitive
description:
name: permission_handler_platform_interface
sha256: "23dfba8447c076ab5be3dee9ceb66aad345c4a648f0cac292c77b1eb0e800b78"
url: "https://pub.dev"
source: hosted
version: "4.2.0"
permission_handler_windows:
dependency: transitive
description:
name: permission_handler_windows
sha256: "1a790728016f79a41216d88672dbc5df30e686e811ad4e698bfc51f76ad91f1e"
url: "https://pub.dev"
source: hosted
version: "0.2.1"
petitparser: petitparser:
dependency: transitive dependency: transitive
description: description:

View File

@@ -76,7 +76,8 @@ dependencies:
http: ^1.2.0 http: ^1.2.0
timezone_to_country: ^2.1.0 timezone_to_country: ^2.1.0
json_path: ^0.7.1 json_path: ^0.7.1
permission_handler: ^11.3.0 # permission_handler: ^11.3.0 # is not compatible with windows
flutter_easy_permission: ^1.1.2
# circle_flags: ^4.0.2 # circle_flags: ^4.0.2
circle_flags: circle_flags:
git: https://github.com/hiddify-com/flutter_circle_flags.git git: https://github.com/hiddify-com/flutter_circle_flags.git

View File

@@ -6,7 +6,6 @@
#include "generated_plugin_registrant.h" #include "generated_plugin_registrant.h"
#include <permission_handler_windows/permission_handler_windows_plugin.h>
#include <protocol_handler_windows/protocol_handler_windows_plugin_c_api.h> #include <protocol_handler_windows/protocol_handler_windows_plugin_c_api.h>
#include <screen_retriever/screen_retriever_plugin.h> #include <screen_retriever/screen_retriever_plugin.h>
#include <sentry_flutter/sentry_flutter_plugin.h> #include <sentry_flutter/sentry_flutter_plugin.h>
@@ -18,8 +17,6 @@
#include <window_manager/window_manager_plugin.h> #include <window_manager/window_manager_plugin.h>
void RegisterPlugins(flutter::PluginRegistry* registry) { void RegisterPlugins(flutter::PluginRegistry* registry) {
PermissionHandlerWindowsPluginRegisterWithRegistrar(
registry->GetRegistrarForPlugin("PermissionHandlerWindowsPlugin"));
ProtocolHandlerWindowsPluginCApiRegisterWithRegistrar( ProtocolHandlerWindowsPluginCApiRegisterWithRegistrar(
registry->GetRegistrarForPlugin("ProtocolHandlerWindowsPluginCApi")); registry->GetRegistrarForPlugin("ProtocolHandlerWindowsPluginCApi"));
ScreenRetrieverPluginRegisterWithRegistrar( ScreenRetrieverPluginRegisterWithRegistrar(

View File

@@ -3,7 +3,6 @@
# #
list(APPEND FLUTTER_PLUGIN_LIST list(APPEND FLUTTER_PLUGIN_LIST
permission_handler_windows
protocol_handler_windows protocol_handler_windows
screen_retriever screen_retriever
sentry_flutter sentry_flutter