From 688ffc1a12cfaa80772b96271e29601b099e4738 Mon Sep 17 00:00:00 2001 From: Me on Windows Date: Mon, 26 Jul 2021 18:42:35 +0000 Subject: [PATCH] Implement QR code scanning --- QuickPlay/MainActivity.cs | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/QuickPlay/MainActivity.cs b/QuickPlay/MainActivity.cs index 59682d0..393829c 100644 --- a/QuickPlay/MainActivity.cs +++ b/QuickPlay/MainActivity.cs @@ -7,6 +7,7 @@ using Android.Content; using Android.Support.V7.App; using Toolbar = Android.Support.V7.Widget.Toolbar; using GridLayoutManager = Android.Support.V7.Widget.GridLayoutManager; +using Android.Runtime; namespace QuickPlay { @@ -84,7 +85,13 @@ namespace QuickPlay StartActivity(i); }); b.SetNegativeButton("Scan QR", delegate { - Toast.MakeText(this, "Not implemented :-(", ToastLength.Long).Show(); + try + { + var i = new Intent("com.google.zxing.client.android.SCAN"); + StartActivityForResult(i, 0); + } catch (ActivityNotFoundException e) { + Toast.MakeText(this, "You need ZXing Barcode scanner for this", ToastLength.Long).Show(); + } }); b.SetCancelable(true); @@ -169,5 +176,26 @@ namespace QuickPlay this.appConfig.saveConfiguration(); } + + protected override void OnActivityResult(int requestCode, [GeneratedEnum] Result resultCode, Intent data) + { + base.OnActivityResult(requestCode, resultCode, data); + if (requestCode == 0) + { + if (resultCode == Result.Ok) + { + string url = data.GetStringExtra("SCAN_RESULT"); + var cfg = new AppConfiguration { playerConfigUrl = url }; + cfg.saveConfiguration(); + Toast.MakeText(this, "Configuration saved, reloading", ToastLength.Short).Show(); + var i = new Intent(this, typeof(MainActivity)); + StartActivity(i); + } + else if (resultCode == Result.Canceled) + { + Toast.MakeText(this, "Could not load QR code", ToastLength.Short).Show(); + } + } + } } }