yubioath-flutter/lib/android/qr_scanner/qr_scanner_provider.dart

138 lines
4.5 KiB
Dart
Raw Normal View History

2022-10-04 13:12:54 +03:00
/*
2023-10-18 16:34:31 +03:00
* Copyright (C) 2022-2023 Yubico.
2022-10-04 13:12:54 +03:00
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
2023-10-10 14:49:01 +03:00
import 'dart:convert';
2023-10-18 16:34:31 +03:00
import 'package:file_picker/file_picker.dart';
2022-03-28 11:58:09 +03:00
import 'package:flutter/material.dart';
2023-10-18 16:34:31 +03:00
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:qrscanner_zxing/qrscanner_zxing_method_channel.dart';
2022-03-28 11:58:09 +03:00
2023-10-18 16:34:31 +03:00
import '../../app/message.dart';
import '../../app/state.dart';
import '../../exception/cancellation_exception.dart';
2023-10-18 16:34:31 +03:00
import '../../oath/views/add_account_page.dart';
import '../../oath/views/utils.dart';
import '../../theme.dart';
import '../app_methods.dart';
2022-03-28 11:58:09 +03:00
import 'qr_scanner_view.dart';
class AndroidQrScanner implements QrScanner {
2023-10-18 16:34:31 +03:00
static const String kQrScannerRequestManualEntry =
'__QR_SCANNER_ENTER_MANUALLY__';
static const String kQrScannerRequestReadFromFile =
'__QR_SCANNER_SCAN_FROM_FILE__';
final WithContext _withContext;
2023-10-10 14:49:01 +03:00
AndroidQrScanner(this._withContext);
2022-03-28 11:58:09 +03:00
@override
2023-10-10 14:49:01 +03:00
Future<String?> scanQr([String? imageData]) async {
if (imageData == null) {
2024-01-24 19:13:03 +03:00
var scannedCode = await _withContext(
(context) async => await Navigator.of(context).push(PageRouteBuilder(
pageBuilder: (_, __, ___) => Theme(
data: AppTheme.getDarkTheme(defaultPrimaryColor),
child: const QrScannerView()),
settings: const RouteSettings(name: 'android_qr_scanner_view'),
transitionDuration: const Duration(seconds: 0),
reverseTransitionDuration: const Duration(seconds: 0),
)));
2023-10-10 14:49:01 +03:00
if (scannedCode == null) {
// user has cancelled the scan
throw CancellationException();
}
if (scannedCode == '') {
return null;
}
return scannedCode;
} else {
var zxingChannel = MethodChannelQRScannerZxing();
return await zxingChannel.scanBitmap(base64Decode(imageData));
}
}
2023-10-18 16:34:31 +03:00
2023-12-05 18:50:06 +03:00
static Future<void> showAccountManualEntryDialog(
WithContext withContext, AppLocalizations l10n) async {
await withContext((context) => showBlurDialog(
context: context,
routeSettings: const RouteSettings(name: 'oath_add_account'),
builder: (context) {
return const OathAddAccountPage(
null,
null,
credentials: null,
);
},
));
}
static Future<void> readQrFromFile(WithContext withContext,
QrScanner qrScanner, AppLocalizations l10n) async {
await preserveConnectedDeviceWhenPaused();
final result = await FilePicker.platform.pickFiles(
allowedExtensions: ['png', 'jpg', 'gif', 'webp'],
type: FileType.custom,
allowMultiple: false,
lockParentWindow: true,
withData: true,
dialogTitle: l10n.l_qr_select_file);
if (result == null || !result.isSinglePick) {
// no result
return;
}
final bytes = result.files.first.bytes;
if (bytes != null) {
final b64bytes = base64Encode(bytes);
final imageQrData = await qrScanner.scanQr(b64bytes);
if (imageQrData != null) {
await withContext((context) =>
handleUri(context, null, imageQrData, null, null, l10n));
return;
}
}
// no QR code found
await withContext(
(context) async => showMessage(context, l10n.l_qr_not_found));
}
2023-10-18 16:34:31 +03:00
static Future<void> handleScannedData(
2023-10-27 11:10:23 +03:00
String? qrData,
WithContext withContext,
QrScanner qrScanner,
AppLocalizations l10n,
) async {
2023-10-18 16:34:31 +03:00
switch (qrData) {
case null:
break;
case kQrScannerRequestManualEntry:
2023-12-05 18:50:06 +03:00
await showAccountManualEntryDialog(withContext, l10n);
2023-10-18 16:34:31 +03:00
case kQrScannerRequestReadFromFile:
2023-12-05 18:50:06 +03:00
await readQrFromFile(withContext, qrScanner, l10n);
2023-10-18 16:34:31 +03:00
default:
await withContext(
(context) => handleUri(context, null, qrData, null, null, l10n));
}
}
2022-03-28 11:58:09 +03:00
}
QrScanner? Function(dynamic) androidQrScannerProvider(hasCamera) {
return (ref) =>
2023-10-18 16:34:31 +03:00
hasCamera ? AndroidQrScanner(ref.watch(withContextProvider)) : null;
}