From 922dc683af807063f04d89894629a6267bb1c409 Mon Sep 17 00:00:00 2001 From: Dinis Vieira Date: Wed, 6 Dec 2023 17:27:17 +0000 Subject: [PATCH] PM-3349 Replaced CrossCurrentActivity plugin with MAUI internal CurrentActivity --- src/App/App.csproj | 1 - src/App/Platforms/Android/MainApplication.cs | 4 +-- .../Android/Services/AutofillHandler.cs | 9 +++-- .../Android/Services/DeviceActionService.cs | 33 +++++++++---------- .../Platforms/Android/Services/FileService.cs | 33 ++++++++++++------- src/Core/Core.csproj | 1 - 6 files changed, 42 insertions(+), 39 deletions(-) diff --git a/src/App/App.csproj b/src/App/App.csproj index 0f9779275..f6cc08850 100644 --- a/src/App/App.csproj +++ b/src/App/App.csproj @@ -113,7 +113,6 @@ - diff --git a/src/App/Platforms/Android/MainApplication.cs b/src/App/Platforms/Android/MainApplication.cs index 3b9e37381..d38aa2ed1 100644 --- a/src/App/Platforms/Android/MainApplication.cs +++ b/src/App/Platforms/Android/MainApplication.cs @@ -12,7 +12,6 @@ using Bit.Core.Abstractions; using Bit.Core.Services; using Bit.Core.Utilities; using Bit.Droid.Services; -using Plugin.CurrentActivity; using Plugin.Fingerprint; using Xamarin.Android.Net; using System.Net.Http; @@ -101,7 +100,6 @@ namespace Bit.Droid { base.OnCreate(); Bootstrap(); - CrossCurrentActivity.Current.Init(this); } public void OnProviderInstallFailed(int errorCode, Intent recoveryIntent) @@ -136,7 +134,7 @@ namespace Bit.Droid // }); // ZXing.Net.Mobile.Forms.Android.Platform.Init(); //}); - CrossFingerprint.SetCurrentActivityResolver(() => CrossCurrentActivity.Current.Activity); + CrossFingerprint.SetCurrentActivityResolver(() => Microsoft.Maui.ApplicationModel.Platform.CurrentActivity); var preferencesStorage = new PreferencesStorageService(null); var localAppDataFolderPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.LocalApplicationData); diff --git a/src/App/Platforms/Android/Services/AutofillHandler.cs b/src/App/Platforms/Android/Services/AutofillHandler.cs index bb188cb37..12429b841 100644 --- a/src/App/Platforms/Android/Services/AutofillHandler.cs +++ b/src/App/Platforms/Android/Services/AutofillHandler.cs @@ -12,7 +12,6 @@ using Bit.Core.Enums; using Bit.Core.Models.View; using Bit.Core.Utilities; using Bit.Droid.Autofill; -using Plugin.CurrentActivity; using Application = Android.App.Application; namespace Bit.Droid.Services @@ -46,7 +45,7 @@ namespace Bit.Droid.Services } try { - var activity = (MainActivity)CrossCurrentActivity.Current.Activity; + var activity = (MainActivity)Microsoft.Maui.ApplicationModel.Platform.CurrentActivity; var afm = (AutofillManager)activity.GetSystemService( Java.Lang.Class.FromType(typeof(AutofillManager))); return afm.IsEnabled && afm.HasEnabledAutofillServices; @@ -65,7 +64,7 @@ namespace Bit.Droid.Services } try { - var activity = (MainActivity)CrossCurrentActivity.Current.Activity; + var activity = (MainActivity)Microsoft.Maui.ApplicationModel.Platform.CurrentActivity; var type = Java.Lang.Class.FromType(typeof(AutofillManager)); var manager = activity.GetSystemService(type) as AutofillManager; return manager.IsAutofillSupported; @@ -78,7 +77,7 @@ namespace Bit.Droid.Services public void Autofill(CipherView cipher) { - var activity = CrossCurrentActivity.Current.Activity as MauiAppCompatActivity; + var activity = Microsoft.Maui.ApplicationModel.Platform.CurrentActivity as MauiAppCompatActivity; if (activity == null) { return; @@ -170,7 +169,7 @@ namespace Bit.Droid.Services { try { - var activity = (MainActivity)CrossCurrentActivity.Current.Activity; + var activity = (MainActivity)Microsoft.Maui.ApplicationModel.Platform.CurrentActivity; var type = Java.Lang.Class.FromType(typeof(AutofillManager)); var manager = activity.GetSystemService(type) as AutofillManager; manager.DisableAutofillServices(); diff --git a/src/App/Platforms/Android/Services/DeviceActionService.cs b/src/App/Platforms/Android/Services/DeviceActionService.cs index 26356eed6..102f1de80 100644 --- a/src/App/Platforms/Android/Services/DeviceActionService.cs +++ b/src/App/Platforms/Android/Services/DeviceActionService.cs @@ -18,7 +18,6 @@ using Bit.App.Utilities.Prompts; using Bit.Core.Abstractions; using Bit.Core.Enums; using Bit.App.Droid.Utilities; -using Plugin.CurrentActivity; using Microsoft.Maui.Controls.Compatibility.Platform.Android; using Resource = Bit.Core.Resource; using Application = Android.App.Application; @@ -66,7 +65,7 @@ namespace Bit.Droid.Services _toast.Dispose(); _toast = null; } - _toast = Android.Widget.Toast.MakeText(CrossCurrentActivity.Current.Activity, text, + _toast = Android.Widget.Toast.MakeText(Microsoft.Maui.ApplicationModel.Platform.CurrentActivity, text, longDuration ? ToastLength.Long : ToastLength.Short); _toast.Show(); } @@ -79,7 +78,7 @@ namespace Bit.Droid.Services // https://developer.android.com/reference/android/content/pm/PackageManager#getLaunchIntentSenderForPackage(java.lang.String) return false; } - var activity = CrossCurrentActivity.Current.Activity; + var activity = Microsoft.Maui.ApplicationModel.Platform.CurrentActivity; appName = appName.Replace("androidapp://", string.Empty); var launchIntentSender = activity?.PackageManager?.GetLaunchIntentSenderForPackage(appName); launchIntentSender?.SendIntent(activity, Result.Ok, null, null, null); @@ -93,7 +92,7 @@ namespace Bit.Droid.Services await HideLoadingAsync(); } - var activity = CrossCurrentActivity.Current.Activity; + var activity = Microsoft.Maui.ApplicationModel.Platform.CurrentActivity; var inflater = (LayoutInflater)activity.GetSystemService(Context.LayoutInflaterService); var dialogView = inflater.Inflate(Resource.Layout.progress_dialog_layout, null); @@ -159,7 +158,7 @@ namespace Bit.Droid.Services } // Finally if all else fails, let's see if current activity is MainActivity - if (CrossCurrentActivity.Current.Activity is MainActivity activity && IsAlive(activity)) + if (Microsoft.Maui.ApplicationModel.Platform.CurrentActivity is MainActivity activity && IsAlive(activity)) { activity.RunOnUiThread(actionDismiss); return Task.CompletedTask; @@ -193,7 +192,7 @@ namespace Bit.Droid.Services string text = null, string okButtonText = null, string cancelButtonText = null, bool numericKeyboard = false, bool autofocus = true, bool password = false) { - var activity = (MainActivity)CrossCurrentActivity.Current.Activity; + var activity = (MainActivity)Microsoft.Maui.ApplicationModel.Platform.CurrentActivity; if (activity == null) { return Task.FromResult(null); @@ -250,7 +249,7 @@ namespace Bit.Droid.Services public Task DisplayValidatablePromptAsync(ValidatablePromptConfig config) { - var activity = (MainActivity)CrossCurrentActivity.Current.Activity; + var activity = (MainActivity)Microsoft.Maui.ApplicationModel.Platform.CurrentActivity; if (activity == null) { return Task.FromResult(null); @@ -327,7 +326,7 @@ namespace Bit.Droid.Services public void RateApp() { - var activity = (MainActivity)CrossCurrentActivity.Current.Activity; + var activity = (MainActivity)Microsoft.Maui.ApplicationModel.Platform.CurrentActivity; try { var rateIntent = RateIntentForUrl("market://details", activity); @@ -360,14 +359,14 @@ namespace Bit.Droid.Services public bool SupportsNfc() { - var activity = (MainActivity)CrossCurrentActivity.Current.Activity; + var activity = (MainActivity)Microsoft.Maui.ApplicationModel.Platform.CurrentActivity; var manager = activity.GetSystemService(Context.NfcService) as NfcManager; return manager.DefaultAdapter?.IsEnabled ?? false; } public bool SupportsCamera() { - var activity = (MainActivity)CrossCurrentActivity.Current.Activity; + var activity = (MainActivity)Microsoft.Maui.ApplicationModel.Platform.CurrentActivity; return activity.PackageManager.HasSystemFeature(PackageManager.FeatureCamera); } @@ -383,7 +382,7 @@ namespace Bit.Droid.Services public Task DisplayAlertAsync(string title, string message, string cancel, params string[] buttons) { - var activity = (MainActivity)CrossCurrentActivity.Current.Activity; + var activity = (MainActivity)Microsoft.Maui.ApplicationModel.Platform.CurrentActivity; if (activity == null) { return Task.FromResult(null); @@ -464,7 +463,7 @@ namespace Bit.Droid.Services public void OpenAccessibilityOverlayPermissionSettings() { - var activity = (MainActivity)CrossCurrentActivity.Current.Activity; + var activity = (MainActivity)Microsoft.Maui.ApplicationModel.Platform.CurrentActivity; try { var intent = new Intent(Settings.ActionManageOverlayPermission); @@ -495,7 +494,7 @@ namespace Bit.Droid.Services { try { - var activity = (MainActivity)CrossCurrentActivity.Current.Activity; + var activity = (MainActivity)Microsoft.Maui.ApplicationModel.Platform.CurrentActivity; var intent = new Intent(Settings.ActionAccessibilitySettings); activity.StartActivity(intent); } @@ -504,7 +503,7 @@ namespace Bit.Droid.Services public void OpenAutofillSettings() { - var activity = (MainActivity)CrossCurrentActivity.Current.Activity; + var activity = (MainActivity)Microsoft.Maui.ApplicationModel.Platform.CurrentActivity; try { var intent = new Intent(Settings.ActionRequestSetAutofillService); @@ -535,7 +534,7 @@ namespace Bit.Droid.Services public void CloseMainApp() { - var activity = (MainActivity)CrossCurrentActivity.Current.Activity; + var activity = (MainActivity)Microsoft.Maui.ApplicationModel.Platform.CurrentActivity; if (activity == null) { return; @@ -574,7 +573,7 @@ namespace Bit.Droid.Services public float GetSystemFontSizeScale() { - var activity = CrossCurrentActivity.Current?.Activity as MainActivity; + var activity = Microsoft.Maui.ApplicationModel.Platform.CurrentActivity as MainActivity; return activity?.Resources?.Configuration?.FontScale ?? 1; } @@ -585,7 +584,7 @@ namespace Bit.Droid.Services public async Task SetScreenCaptureAllowedAsync() { - var activity = CrossCurrentActivity.Current?.Activity; + var activity = Microsoft.Maui.ApplicationModel.Platform.CurrentActivity; if (await _stateService.GetScreenCaptureAllowedAsync()) { activity.RunOnUiThread(() => activity.Window.ClearFlags(WindowManagerFlags.Secure)); diff --git a/src/App/Platforms/Android/Services/FileService.cs b/src/App/Platforms/Android/Services/FileService.cs index 42dd9ec5a..25635e58c 100644 --- a/src/App/Platforms/Android/Services/FileService.cs +++ b/src/App/Platforms/Android/Services/FileService.cs @@ -13,7 +13,6 @@ using AndroidX.Core.Content; using Bit.Core.Resources.Localization; using Bit.Core; using Bit.Core.Abstractions; -using Plugin.CurrentActivity; using FileProvider = AndroidX.Core.Content.FileProvider; namespace Bit.Droid.Services @@ -43,7 +42,7 @@ namespace Bit.Droid.Services { try { - var activity = (MainActivity)CrossCurrentActivity.Current.Activity; + var activity = (MainActivity)Microsoft.Maui.ApplicationModel.Platform.CurrentActivity; var intent = BuildOpenFileIntent(fileData, fileName); if (intent == null) { @@ -60,7 +59,7 @@ namespace Bit.Droid.Services { try { - var activity = (MainActivity)CrossCurrentActivity.Current.Activity; + var activity = (MainActivity)Microsoft.Maui.ApplicationModel.Platform.CurrentActivity; var intent = BuildOpenFileIntent(new byte[0], string.Concat("opentest_", fileName)); if (intent == null) { @@ -87,7 +86,7 @@ namespace Bit.Droid.Services return null; } - var activity = (MainActivity)CrossCurrentActivity.Current.Activity; + var activity = (MainActivity)Microsoft.Maui.ApplicationModel.Platform.CurrentActivity; var cachePath = activity.CacheDir; var filePath = Path.Combine(cachePath.Path, fileName); File.WriteAllBytes(filePath, fileData); @@ -114,7 +113,7 @@ namespace Bit.Droid.Services { try { - var activity = (MainActivity)CrossCurrentActivity.Current.Activity; + var activity = (MainActivity)Microsoft.Maui.ApplicationModel.Platform.CurrentActivity; if (contentUri != null) { @@ -162,7 +161,7 @@ namespace Bit.Droid.Services { try { - DeleteDir(CrossCurrentActivity.Current.Activity.CacheDir); + DeleteDir(Microsoft.Maui.ApplicationModel.Platform.CurrentActivity?.CacheDir); await _stateService.SetLastFileCacheClearAsync(DateTime.UtcNow); } catch (Exception) { } @@ -170,7 +169,7 @@ namespace Bit.Droid.Services public Task SelectFileAsync() { - var activity = (MainActivity)CrossCurrentActivity.Current.Activity; + var activity = (MainActivity)Microsoft.Maui.ApplicationModel.Platform.CurrentActivity; var hasStorageWritePermission = !_cameraPermissionsDenied && HasPermission(Manifest.Permission.WriteExternalStorage); var additionalIntents = new List(); @@ -249,20 +248,30 @@ namespace Bit.Droid.Services private bool HasPermission(string permission) { - return ContextCompat.CheckSelfPermission( - CrossCurrentActivity.Current.Activity, permission) == Permission.Granted; + var activity = Microsoft.Maui.ApplicationModel.Platform.CurrentActivity; + if (activity != null) + { + return ContextCompat.CheckSelfPermission(activity, permission) == Permission.Granted; + } + else + { + return false; + } } private void AskPermission(string permission) { - ActivityCompat.RequestPermissions(CrossCurrentActivity.Current.Activity, new string[] { permission }, - Core.Constants.SelectFilePermissionRequestCode); + var activity = Microsoft.Maui.ApplicationModel.Platform.CurrentActivity; + if (activity != null) + { + ActivityCompat.RequestPermissions(activity, new string[] { permission }, Core.Constants.SelectFilePermissionRequestCode); + } } private List GetCameraIntents(Android.Net.Uri outputUri) { var intents = new List(); - var pm = CrossCurrentActivity.Current.Activity.PackageManager; + var pm = Microsoft.Maui.ApplicationModel.Platform.CurrentActivity?.PackageManager; var captureIntent = new Intent(MediaStore.ActionImageCapture); var listCam = pm.QueryIntentActivities(captureIntent, 0); foreach (var res in listCam) diff --git a/src/Core/Core.csproj b/src/Core/Core.csproj index dcac20dc2..3471f8e48 100644 --- a/src/Core/Core.csproj +++ b/src/Core/Core.csproj @@ -47,7 +47,6 @@ -