2018-10-30 02:55:43 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2020-12-04 11:15:29 +00:00
|
|
|
// Copyright © 2018-2020 WireGuard LLC. All Rights Reserved.
|
2018-08-21 16:00:41 +00:00
|
|
|
|
|
|
|
import AVFoundation
|
|
|
|
import UIKit
|
|
|
|
|
|
|
|
protocol QRScanViewControllerDelegate: class {
|
2018-11-03 18:35:25 +00:00
|
|
|
func addScannedQRCode(tunnelConfiguration: TunnelConfiguration, qrScanViewController: QRScanViewController, completionHandler: (() -> Void)?)
|
2018-08-21 16:00:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class QRScanViewController: UIViewController {
|
2018-10-28 16:52:27 +00:00
|
|
|
weak var delegate: QRScanViewControllerDelegate?
|
2018-08-21 16:00:41 +00:00
|
|
|
var captureSession: AVCaptureSession? = AVCaptureSession()
|
|
|
|
let metadataOutput = AVCaptureMetadataOutput()
|
2018-10-31 20:44:30 +00:00
|
|
|
var previewLayer: AVCaptureVideoPreviewLayer?
|
2018-08-21 16:00:41 +00:00
|
|
|
|
|
|
|
override func viewDidLoad() {
|
|
|
|
super.viewDidLoad()
|
|
|
|
|
2018-12-18 11:00:16 +00:00
|
|
|
title = tr("scanQRCodeViewTitle")
|
2018-12-14 23:12:59 +00:00
|
|
|
navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .cancel, target: self, action: #selector(cancelTapped))
|
2018-10-28 16:52:27 +00:00
|
|
|
|
2018-10-30 13:34:10 +00:00
|
|
|
let tipLabel = UILabel()
|
2018-12-18 11:00:16 +00:00
|
|
|
tipLabel.text = tr("scanQRCodeTipText")
|
2018-10-30 13:34:10 +00:00
|
|
|
tipLabel.adjustsFontSizeToFitWidth = true
|
2018-12-13 18:58:50 +00:00
|
|
|
tipLabel.textColor = .lightGray
|
2018-10-30 13:34:10 +00:00
|
|
|
tipLabel.textAlignment = .center
|
|
|
|
|
|
|
|
view.addSubview(tipLabel)
|
|
|
|
tipLabel.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
NSLayoutConstraint.activate([
|
2018-12-22 02:37:22 +00:00
|
|
|
tipLabel.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor),
|
|
|
|
tipLabel.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor),
|
2018-12-12 17:40:57 +00:00
|
|
|
tipLabel.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: -32)
|
2018-12-12 21:33:14 +00:00
|
|
|
])
|
2018-10-30 13:34:10 +00:00
|
|
|
|
2018-08-21 16:00:41 +00:00
|
|
|
guard let videoCaptureDevice = AVCaptureDevice.default(for: .video),
|
|
|
|
let videoInput = try? AVCaptureDeviceInput(device: videoCaptureDevice),
|
|
|
|
let captureSession = captureSession,
|
|
|
|
captureSession.canAddInput(videoInput),
|
|
|
|
captureSession.canAddOutput(metadataOutput) else {
|
2018-12-18 11:00:16 +00:00
|
|
|
scanDidEncounterError(title: tr("alertScanQRCodeCameraUnsupportedTitle"), message: tr("alertScanQRCodeCameraUnsupportedMessage"))
|
2018-08-21 16:00:41 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
captureSession.addInput(videoInput)
|
|
|
|
captureSession.addOutput(metadataOutput)
|
|
|
|
|
|
|
|
metadataOutput.setMetadataObjectsDelegate(self, queue: .main)
|
|
|
|
metadataOutput.metadataObjectTypes = [.qr]
|
|
|
|
|
2018-10-31 20:44:30 +00:00
|
|
|
let previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
|
2018-08-21 16:00:41 +00:00
|
|
|
previewLayer.frame = view.layer.bounds
|
|
|
|
previewLayer.videoGravity = .resizeAspectFill
|
2018-09-19 22:27:49 +00:00
|
|
|
view.layer.insertSublayer(previewLayer, at: 0)
|
2018-10-31 20:44:30 +00:00
|
|
|
self.previewLayer = previewLayer
|
2018-08-21 16:00:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
override func viewWillAppear(_ animated: Bool) {
|
|
|
|
super.viewWillAppear(animated)
|
|
|
|
|
|
|
|
if captureSession?.isRunning == false {
|
|
|
|
captureSession?.startRunning()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override func viewWillDisappear(_ animated: Bool) {
|
|
|
|
super.viewWillDisappear(animated)
|
|
|
|
|
|
|
|
if captureSession?.isRunning == true {
|
|
|
|
captureSession?.stopRunning()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-19 22:28:07 +00:00
|
|
|
override func viewDidLayoutSubviews() {
|
|
|
|
super.viewDidLayoutSubviews()
|
|
|
|
|
2018-10-31 20:44:30 +00:00
|
|
|
if let connection = previewLayer?.connection {
|
2018-12-20 17:22:37 +00:00
|
|
|
let currentDevice = UIDevice.current
|
|
|
|
let orientation = currentDevice.orientation
|
|
|
|
let previewLayerConnection = connection
|
2018-09-19 22:28:07 +00:00
|
|
|
|
|
|
|
if previewLayerConnection.isVideoOrientationSupported {
|
|
|
|
switch orientation {
|
|
|
|
case .portrait:
|
|
|
|
previewLayerConnection.videoOrientation = .portrait
|
|
|
|
case .landscapeRight:
|
|
|
|
previewLayerConnection.videoOrientation = .landscapeLeft
|
|
|
|
case .landscapeLeft:
|
|
|
|
previewLayerConnection.videoOrientation = .landscapeRight
|
|
|
|
case .portraitUpsideDown:
|
|
|
|
previewLayerConnection.videoOrientation = .portraitUpsideDown
|
|
|
|
default:
|
|
|
|
previewLayerConnection.videoOrientation = .portrait
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-10-31 20:44:30 +00:00
|
|
|
|
2018-12-14 23:12:59 +00:00
|
|
|
previewLayer?.frame = view.bounds
|
2018-09-19 22:28:07 +00:00
|
|
|
}
|
|
|
|
|
2018-08-21 16:00:41 +00:00
|
|
|
func scanDidComplete(withCode code: String) {
|
2018-12-21 23:28:18 +00:00
|
|
|
let scannedTunnelConfiguration = try? TunnelConfiguration(fromWgQuickConfig: code, called: "Scanned")
|
2018-10-28 17:27:58 +00:00
|
|
|
guard let tunnelConfiguration = scannedTunnelConfiguration else {
|
2018-12-18 11:00:16 +00:00
|
|
|
scanDidEncounterError(title: tr("alertScanQRCodeInvalidQRCodeTitle"), message: tr("alertScanQRCodeInvalidQRCodeMessage"))
|
2018-10-28 17:27:58 +00:00
|
|
|
return
|
2018-08-21 16:00:41 +00:00
|
|
|
}
|
2018-09-25 18:59:15 +00:00
|
|
|
|
2018-12-18 11:00:16 +00:00
|
|
|
let alert = UIAlertController(title: tr("alertScanQRCodeNamePromptTitle"), message: nil, preferredStyle: .alert)
|
2018-09-25 18:59:15 +00:00
|
|
|
alert.addTextField(configurationHandler: nil)
|
2018-12-18 11:00:16 +00:00
|
|
|
alert.addAction(UIAlertAction(title: tr("actionCancel"), style: .cancel) { [weak self] _ in
|
2018-11-05 18:08:41 +00:00
|
|
|
self?.dismiss(animated: true, completion: nil)
|
2018-12-12 21:33:14 +00:00
|
|
|
})
|
2018-12-18 11:00:16 +00:00
|
|
|
alert.addAction(UIAlertAction(title: tr("actionSave"), style: .default) { [weak self] _ in
|
2018-12-12 17:40:57 +00:00
|
|
|
guard let title = alert.textFields?[0].text?.trimmingCharacters(in: .whitespacesAndNewlines), !title.isEmpty else { return }
|
2018-12-21 23:28:18 +00:00
|
|
|
tunnelConfiguration.name = title
|
2018-12-12 17:40:57 +00:00
|
|
|
if let self = self {
|
|
|
|
self.delegate?.addScannedQRCode(tunnelConfiguration: tunnelConfiguration, qrScanViewController: self) {
|
|
|
|
self.dismiss(animated: true, completion: nil)
|
2018-10-31 20:06:28 +00:00
|
|
|
}
|
2018-09-25 18:59:15 +00:00
|
|
|
}
|
2018-12-12 21:33:14 +00:00
|
|
|
})
|
2018-09-25 18:59:15 +00:00
|
|
|
present(alert, animated: true)
|
2018-08-21 16:00:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func scanDidEncounterError(title: String, message: String) {
|
|
|
|
let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
|
2018-12-18 11:00:16 +00:00
|
|
|
alertController.addAction(UIAlertAction(title: tr("actionOK"), style: .default) { [weak self] _ in
|
2018-11-01 13:29:14 +00:00
|
|
|
self?.dismiss(animated: true, completion: nil)
|
2018-12-12 21:33:14 +00:00
|
|
|
})
|
2018-08-21 16:00:41 +00:00
|
|
|
present(alertController, animated: true)
|
|
|
|
captureSession = nil
|
|
|
|
}
|
|
|
|
|
2018-10-28 16:52:27 +00:00
|
|
|
@objc func cancelTapped() {
|
|
|
|
dismiss(animated: true, completion: nil)
|
|
|
|
}
|
2018-08-21 16:00:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
extension QRScanViewController: AVCaptureMetadataOutputObjectsDelegate {
|
|
|
|
func metadataOutput(_ output: AVCaptureMetadataOutput, didOutput metadataObjects: [AVMetadataObject], from connection: AVCaptureConnection) {
|
|
|
|
captureSession?.stopRunning()
|
|
|
|
|
|
|
|
guard let metadataObject = metadataObjects.first,
|
|
|
|
let readableObject = metadataObject as? AVMetadataMachineReadableCodeObject,
|
|
|
|
let stringValue = readableObject.stringValue else {
|
2018-12-18 11:00:16 +00:00
|
|
|
scanDidEncounterError(title: tr("alertScanQRCodeUnreadableQRCodeTitle"), message: tr("alertScanQRCodeUnreadableQRCodeMessage"))
|
2018-08-21 16:00:41 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
AudioServicesPlaySystemSound(SystemSoundID(kSystemSoundID_Vibrate))
|
|
|
|
scanDidComplete(withCode: stringValue)
|
|
|
|
}
|
|
|
|
}
|