using System; using System.Linq; using System.Threading.Tasks; using Android; using Android.App; using Android.Content; using Android.Content.PM; using Android.Runtime; using Android.Views; using Android.Widget; using Android.OS; using InABox.Mobile.Android; using Plugin.LocalNotification; using Xamarin.Essentials; using Xamarin.Forms; namespace PRS.Mobile.Droid { [Activity( Label = "PRS.Mobile", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation )] [IntentFilter(new[] { Android.Content.Intent.ActionView }, AutoVerify = true, Categories = new[] { Android.Content.Intent.CategoryDefault, Android.Content.Intent.CategoryBrowsable, }, DataScheme = "http", DataPathPrefix = "/open", DataHost = "www.prsmobile.com") ] public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity { private readonly string[] Permissions = { Manifest.Permission.Bluetooth, Manifest.Permission.BluetoothAdmin, Manifest.Permission.AccessCoarseLocation, Manifest.Permission.AccessFineLocation, Manifest.Permission.BluetoothScan, Manifest.Permission.BluetoothAdvertise, Manifest.Permission.BluetoothConnect, Manifest.Permission.ReadMediaVideo, Manifest.Permission.ReadMediaAudio, Manifest.Permission.ReadMediaImages, Manifest.Permission.Flashlight, }; protected override void OnCreate(Bundle savedInstanceState) { if (!string.IsNullOrWhiteSpace(Intent?.Data?.EncodedAuthority)) { string s = Intent?.Data?.Path ?? ""; App.LaunchParameters = (s.Length > 6) ? s.Remove(0, 6) : ""; } CheckPermissions(); DependencyService.Register(); TabLayoutResource = Resource.Layout.Tabbar; ToolbarResource = Resource.Layout.Toolbar; AppDomain.CurrentDomain.UnhandledException += CurrentDomainOnUnhandledException; TaskScheduler.UnobservedTaskException += TaskSchedulerOnUnobservedTaskException; base.OnCreate(savedInstanceState); global::Xamarin.Forms.Forms.Init(this, savedInstanceState); // https://github.com/Redth/ZXing.Net.Mobile ZXing.Net.Mobile.Forms.Android.Platform.Init(); // https://github.com/Baseflow/XF-Material-Library XF.Material.Droid.Material.Init(this, savedInstanceState); LocalNotificationCenter.CreateNotificationChannel(new Plugin.LocalNotification.AndroidOption.NotificationChannelRequest { Importance = Plugin.LocalNotification.AndroidOption.AndroidImportance.Max, LockScreenVisibility = Plugin.LocalNotification.AndroidOption.AndroidVisibilityType.Public, ShowBadge = true, EnableVibration = true, Sound = "requiitemadded.mp3" }); LocalNotificationCenter.NotifyNotificationTapped(Intent); // https://learn.microsoft.com/en-au/xamarin/essentials/get-started?tabs=windows%2Candroid Xamarin.Essentials.Platform.Init(this, savedInstanceState); Syncfusion.XForms.Android.PopupLayout.SfPopupLayoutRenderer.Init(); Xamarin.FormsMaps.Init(this, savedInstanceState); ZXing.Net.Mobile.Forms.Android.Platform.Init(); XF.Material.Droid.Material.Init(this, savedInstanceState); LoadApplication(new App()); // FV 23-07-07 Not sure what this does - can we strip it out? // Window.SetSoftInputMode(Android.Views.SoftInput.AdjustResize); } protected override void OnNewIntent(Intent intent) { LocalNotificationCenter.NotifyNotificationTapped(intent); base.OnNewIntent(intent); } public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Android.Content.PM.Permission[] grantResults) { //at this point in time, xamarin essentials is unable to ask for WRITE_MEDIA_STORAGE from the user, so it never gets granted //has to be set manually when asked for, in order for API 33 on Android 13 to work //https://github.com/xamarin/Essentials/issues/2041 if (permissions.Any(x => x == "WRITE_MEDIA_STORAGE")) { var grants = new Permission[] { Permission.Granted }; Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grants); } else { Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults); } base.OnRequestPermissionsResult(requestCode, permissions, grantResults); } private async void CheckPermissions() { bool minimumPermissionsGranted = true; foreach (string permission in Permissions) { if (CheckSelfPermission(permission) != Permission.Granted) { minimumPermissionsGranted = false; } } // If any of the minimum permissions aren't granted, we request them from the user if (!minimumPermissionsGranted) { RequestPermissions(Permissions, 0); } } #region Error handling private static void TaskSchedulerOnUnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs args) { LogUnhandledException("TaskScheduler", args.Exception?.GetType(), args.Exception); } private static void CurrentDomainOnUnhandledException(object sender, UnhandledExceptionEventArgs args) { LogUnhandledException("CurrentDomain", args.ExceptionObject?.GetType(), args.ExceptionObject); } private static void LogUnhandledException(String source, Type type, object exceptionobject) { if (exceptionobject is Exception exception) MobileLogging.Log(exception, source); else MobileLogging.Log($"{source}: {type?.Name ?? "NULL"} -> {exceptionobject}"); } #endregion } }