From bdad5e4f0a39b228dc796d1bd423f3e303395417 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Fri, 30 Aug 2019 16:43:58 -0400 Subject: [PATCH] fixes to opening file types on android --- src/Android/Services/DeviceActionService.cs | 91 ++++++++++++--------- 1 file changed, 52 insertions(+), 39 deletions(-) diff --git a/src/Android/Services/DeviceActionService.cs b/src/Android/Services/DeviceActionService.cs index 859c0f12a..f07e1b5e5 100644 --- a/src/Android/Services/DeviceActionService.cs +++ b/src/Android/Services/DeviceActionService.cs @@ -114,38 +114,14 @@ namespace Bit.Droid.Services public bool OpenFile(byte[] fileData, string id, string fileName) { - if(!CanOpenFile(fileName)) - { - return false; - } - var extension = MimeTypeMap.GetFileExtensionFromUrl(fileName.Replace(' ', '_').ToLower()); - if(extension == null) - { - return false; - } - var mimeType = MimeTypeMap.Singleton.GetMimeTypeFromExtension(extension); - if(mimeType == null) - { - return false; - } - - var activity = (MainActivity)CrossCurrentActivity.Current.Activity; - var cachePath = activity.CacheDir; - var filePath = Path.Combine(cachePath.Path, fileName); - File.WriteAllBytes(filePath, fileData); - var file = new Java.IO.File(cachePath, fileName); - if(!file.IsFile) - { - return false; - } - try { - var intent = new Intent(Intent.ActionView); - var uri = FileProvider.GetUriForFile(activity.ApplicationContext, - "com.x8bit.bitwarden.fileprovider", file); - intent.SetDataAndType(uri, mimeType); - intent.SetFlags(ActivityFlags.GrantReadUriPermission); + var activity = (MainActivity)CrossCurrentActivity.Current.Activity; + var intent = BuildOpenFileIntent(fileData, fileName); + if(intent == null) + { + return false; + } activity.StartActivity(intent); return true; } @@ -154,22 +130,57 @@ namespace Bit.Droid.Services } public bool CanOpenFile(string fileName) + { + try + { + var activity = (MainActivity)CrossCurrentActivity.Current.Activity; + var intent = BuildOpenFileIntent(new byte[0], string.Concat("opentest_", fileName)); + if(intent == null) + { + return false; + } + var activities = activity.PackageManager.QueryIntentActivities(intent, + PackageInfoFlags.MatchDefaultOnly); + return (activities?.Count ?? 0) > 0; + } + catch { } + return false; + } + + private Intent BuildOpenFileIntent(byte[] fileData, string fileName) { var extension = MimeTypeMap.GetFileExtensionFromUrl(fileName.Replace(' ', '_').ToLower()); if(extension == null) { - return false; + return null; } var mimeType = MimeTypeMap.Singleton.GetMimeTypeFromExtension(extension); if(mimeType == null) { - return false; + return null; } + var activity = (MainActivity)CrossCurrentActivity.Current.Activity; - var intent = new Intent(Intent.ActionView); - intent.SetType(mimeType); - var activities = activity.PackageManager.QueryIntentActivities(intent, PackageInfoFlags.MatchDefaultOnly); - return (activities?.Count ?? 0) > 0; + var cachePath = activity.CacheDir; + var filePath = Path.Combine(cachePath.Path, fileName); + File.WriteAllBytes(filePath, fileData); + var file = new Java.IO.File(cachePath, fileName); + if(!file.IsFile) + { + return null; + } + + try + { + var intent = new Intent(Intent.ActionView); + var uri = FileProvider.GetUriForFile(activity.ApplicationContext, + "com.x8bit.bitwarden.fileprovider", file); + intent.SetDataAndType(uri, mimeType); + intent.SetFlags(ActivityFlags.GrantReadUriPermission); + return intent; + } + catch { } + return null; } public async Task ClearCacheAsync() @@ -185,7 +196,8 @@ namespace Bit.Droid.Services public Task SelectFileAsync() { var activity = (MainActivity)CrossCurrentActivity.Current.Activity; - var hasStorageWritePermission = !_cameraPermissionsDenied && HasPermission(Manifest.Permission.WriteExternalStorage); + var hasStorageWritePermission = !_cameraPermissionsDenied && + HasPermission(Manifest.Permission.WriteExternalStorage); var additionalIntents = new List(); if(activity.PackageManager.HasSystemFeature(PackageManager.FeatureCamera)) { @@ -543,7 +555,8 @@ namespace Bit.Droid.Services try { var activity = (MainActivity)CrossCurrentActivity.Current.Activity; - var afm = (AutofillManager)activity.GetSystemService(Java.Lang.Class.FromType(typeof(AutofillManager))); + var afm = (AutofillManager)activity.GetSystemService( + Java.Lang.Class.FromType(typeof(AutofillManager))); return afm.IsEnabled && afm.HasEnabledAutofillServices; } catch @@ -684,4 +697,4 @@ namespace Bit.Droid.Services clipboardManager.Text = text; } } -} \ No newline at end of file +}