From 4790de0f78d7bddfea8bd35972f12596786f726b Mon Sep 17 00:00:00 2001 From: TheJoKlLa Date: Sat, 11 Feb 2023 17:14:17 +0100 Subject: [PATCH] Added: NFC Intent for Machine --- Borepin/Borepin.Android/MainActivity.cs | 32 +++++++---- Borepin/Borepin.Android/MainApplication.cs | 4 +- Borepin/Borepin/App.xaml.cs | 63 ++++++++++++++++++++-- 3 files changed, 83 insertions(+), 16 deletions(-) diff --git a/Borepin/Borepin.Android/MainActivity.cs b/Borepin/Borepin.Android/MainActivity.cs index dffd46d..26a6f98 100644 --- a/Borepin/Borepin.Android/MainActivity.cs +++ b/Borepin/Borepin.Android/MainActivity.cs @@ -4,30 +4,28 @@ using Android.Content.PM; using Android.OS; using AndroidX.AppCompat.App; using Java.Interop; +using System; namespace Borepin.Droid { - [Activity(MainLauncher = true, Exported = true, Theme = "@style/MainTheme", ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)] + [Activity(MainLauncher = true, Exported = true, Theme = "@style/MainTheme", LaunchMode = LaunchMode.SingleTask, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)] [IntentFilter( new[] { - "android.nfc.action.NDEF_DISCOVERED" + "android.nfc.action.NDEF_DISCOVERED", + "android.intent.action.VIEW", }, - Categories = new[] + Categories = new[] { - Intent.CategoryDefault + Intent.CategoryDefault, + Intent.CategoryBrowsable }, - DataScheme = "fabaccess", - DataHost = "innovisionlab.de" + DataScheme = "fabaccess" )] public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity { protected override void OnCreate(Bundle savedInstanceState) { - Android.Net.Uri test = Intent.Data; - string text = Intent.GetStringExtra("MyData") ?? "Data not available"; - - AppCompatDelegate.DefaultNightMode = AppCompatDelegate.ModeNightNo; TabLayoutResource = Resource.Layout.Tabbar; @@ -39,10 +37,22 @@ namespace Borepin.Droid Xamarin.Essentials.Platform.Init(this, savedInstanceState); Xamarin.Forms.Forms.Init(this, savedInstanceState); - LoadApplication(new App(new PlatformInitializer())); + + Android.Net.Uri intentdata = Intent.Data; + Uri uri = null; + if (intentdata != null) + { + uri = new Uri(intentdata.ToString()); + } + LoadApplication(new App(new PlatformInitializer(), uri)); } protected override void OnNewIntent(Intent intent) { + if(intent.Action == "android.nfc.action.NDEF_DISCOVERED") + { + intent.SetAction(Intent.ActionView); + } + base.OnNewIntent(intent); } diff --git a/Borepin/Borepin.Android/MainApplication.cs b/Borepin/Borepin.Android/MainApplication.cs index 3b9e7a7..29d747f 100644 --- a/Borepin/Borepin.Android/MainApplication.cs +++ b/Borepin/Borepin.Android/MainApplication.cs @@ -7,9 +7,9 @@ namespace Borepin.Droid [Application(Label = "FabAccess", Icon = "@mipmap/ic_launcher")] public class MainApplication : Application { - public MainApplication(IntPtr javaReference, JniHandleOwnership transfer) - : base(javaReference, transfer) + public MainApplication(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer) { + } } } \ No newline at end of file diff --git a/Borepin/Borepin/App.xaml.cs b/Borepin/Borepin/App.xaml.cs index b3effeb..2ce7da7 100644 --- a/Borepin/Borepin/App.xaml.cs +++ b/Borepin/Borepin/App.xaml.cs @@ -13,26 +13,83 @@ using System; using Borepin.Service.Storage; using NLog; using Borepin.Service.ErrorMessage; +using Prism.Navigation.Xaml; +using Prism.Navigation; +using Borepin.Service; +using FabAccessAPI; +using FabAccessAPI.Schema; +using System.Threading.Tasks; namespace Borepin { public partial class App { - public App(IPlatformInitializer platformInitializer) : base(platformInitializer) + private readonly Uri _Intent; + public App(IPlatformInitializer platformInitializer, Uri intent = null) : base(platformInitializer) { - var config = new NLog.Config.LoggingConfiguration(); - var logconsole = new NLog.Targets.ConsoleTarget("logconsole"); + NLog.Config.LoggingConfiguration config = new NLog.Config.LoggingConfiguration(); + NLog.Targets.ConsoleTarget logconsole = new NLog.Targets.ConsoleTarget("logconsole"); config.AddRule(LogLevel.Trace, LogLevel.Fatal, logconsole); LogManager.Configuration = config; + + _Intent = intent; } protected override async void OnInitialized() { InitializeComponent(); + if(_Intent!= null) + { + Prism.Navigation.NavigationParameters parameters = new Prism.Navigation.NavigationParameters + { + { "intent", _Intent }, + }; + + await NavigationService.NavigateAsync(new Uri("https://borepin.fab-access.org/StartPage"), parameters).ConfigureAwait(false); + return; + } + await NavigationService.NavigateAsync(new Uri("https://borepin.fab-access.org/StartPage")).ConfigureAwait(false); } + + protected override async void OnAppLinkRequestReceived(Uri uri) + { + if(uri.LocalPath.StartsWith("/resource/", StringComparison.OrdinalIgnoreCase)) + { + string resource_id = uri.LocalPath.Remove(0, "/resource/".Length); + + if (Container.IsRegistered()) + { + IAPIService apiService = Container.Resolve(); + IAPI api = apiService.GetAPI(); + if (api.IsConnected) + { + if (string.Equals(uri.Host, api.ConnectionData.Host.Host, StringComparison.OrdinalIgnoreCase)) + { + Optional optional = await api.Session.MachineSystem.Info.GetMachine(resource_id).ConfigureAwait(false); + + if (optional.Just == null) + { + return; + } + + Prism.Navigation.NavigationParameters parameters = new Prism.Navigation.NavigationParameters + { + { "instance", optional.Just.Id }, + }; + + Device.BeginInvokeOnMainThread(async () => + { + INavigationResult result = await Container.Resolve().NavigateAsync("/MainPage/NavigationPage/MachineListPage/MachinePage", parameters).ConfigureAwait(false); + }); + } + } + } + } + } + protected override void RegisterTypes(IContainerRegistry containerRegistry) { #region Register Basic Navigation