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';
|
2023-10-27 11:10:23 +03:00
|
|
|
import 'package:yubico_authenticator/android/app_methods.dart';
|
2022-03-28 11:58:09 +03:00
|
|
|
import 'package:yubico_authenticator/app/state.dart';
|
2023-01-09 19:22:34 +03:00
|
|
|
import 'package:yubico_authenticator/exception/cancellation_exception.dart';
|
2022-09-15 16:06:56 +03:00
|
|
|
import 'package:yubico_authenticator/theme.dart';
|
2022-03-28 11:58:09 +03:00
|
|
|
|
2023-10-18 16:34:31 +03:00
|
|
|
import '../../app/message.dart';
|
|
|
|
import '../../oath/views/add_account_page.dart';
|
|
|
|
import '../../oath/views/utils.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__';
|
2022-04-28 16:24:03 +03:00
|
|
|
final WithContext _withContext;
|
2023-10-10 14:49:01 +03:00
|
|
|
|
2022-04-28 16:24:03 +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) {
|
2023-10-18 16:34:31 +03:00
|
|
|
var scannedCode = await _withContext((context) async =>
|
2023-10-10 14:49:01 +03:00
|
|
|
await Navigator.of(context).push(PageRouteBuilder(
|
|
|
|
pageBuilder: (_, __, ___) =>
|
|
|
|
Theme(data: AppTheme.darkTheme, child: const QrScannerView()),
|
|
|
|
settings: const RouteSettings(name: 'android_qr_scanner_view'),
|
|
|
|
transitionDuration: const Duration(seconds: 0),
|
|
|
|
reverseTransitionDuration: const Duration(seconds: 0),
|
|
|
|
)));
|
|
|
|
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));
|
2022-09-15 12:47:39 +03:00
|
|
|
}
|
2022-06-07 11:37:49 +03:00
|
|
|
}
|
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:
|
|
|
|
await withContext((context) => showBlurDialog(
|
|
|
|
context: context,
|
|
|
|
routeSettings: const RouteSettings(name: 'oath_add_account'),
|
|
|
|
builder: (context) {
|
|
|
|
return const OathAddAccountPage(
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
credentials: null,
|
|
|
|
);
|
|
|
|
},
|
|
|
|
));
|
|
|
|
case kQrScannerRequestReadFromFile:
|
2023-10-27 11:10:23 +03:00
|
|
|
await preserveConnectedDeviceWhenPaused();
|
2023-10-18 16:34:31 +03:00
|
|
|
final result = await FilePicker.platform.pickFiles(
|
|
|
|
allowedExtensions: ['png', 'jpg', 'gif', 'webp'],
|
|
|
|
type: FileType.custom,
|
|
|
|
allowMultiple: false,
|
|
|
|
lockParentWindow: true,
|
|
|
|
withData: true,
|
2023-10-31 11:03:53 +03:00
|
|
|
dialogTitle: l10n.l_qr_select_file);
|
2023-10-18 16:34:31 +03:00
|
|
|
|
|
|
|
if (result == null || !result.isSinglePick) {
|
|
|
|
// no result
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
final bytes = result.files.first.bytes;
|
2023-10-27 11:10:23 +03:00
|
|
|
if (bytes != null) {
|
2023-10-18 16:34:31 +03:00
|
|
|
final b64bytes = base64Encode(bytes);
|
2023-10-27 11:10:23 +03:00
|
|
|
final imageQrData = await qrScanner.scanQr(b64bytes);
|
|
|
|
if (imageQrData != null) {
|
2023-10-18 16:34:31 +03:00
|
|
|
await withContext((context) =>
|
2023-10-27 11:10:23 +03:00
|
|
|
handleUri(context, null, imageQrData, null, null, l10n));
|
2023-10-18 16:34:31 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// no QR code found
|
|
|
|
await withContext(
|
|
|
|
(context) async => showMessage(context, l10n.l_qr_not_found));
|
|
|
|
|
|
|
|
default:
|
|
|
|
await withContext(
|
|
|
|
(context) => handleUri(context, null, qrData, null, null, l10n));
|
|
|
|
}
|
|
|
|
}
|
2022-03-28 11:58:09 +03:00
|
|
|
}
|
|
|
|
|
2022-11-30 17:27:32 +03:00
|
|
|
QrScanner? Function(dynamic) androidQrScannerProvider(hasCamera) {
|
|
|
|
return (ref) =>
|
2023-10-18 16:34:31 +03:00
|
|
|
hasCamera ? AndroidQrScanner(ref.watch(withContextProvider)) : null;
|
2022-11-30 17:27:32 +03:00
|
|
|
}
|