fixes to opening file types on android

This commit is contained in:
Kyle Spearrin 2019-08-30 16:43:58 -04:00
parent b5dcdc74d7
commit bdad5e4f0a

View file

@ -114,38 +114,14 @@ namespace Bit.Droid.Services
public bool OpenFile(byte[] fileData, string id, string fileName) 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 try
{ {
var intent = new Intent(Intent.ActionView); var activity = (MainActivity)CrossCurrentActivity.Current.Activity;
var uri = FileProvider.GetUriForFile(activity.ApplicationContext, var intent = BuildOpenFileIntent(fileData, fileName);
"com.x8bit.bitwarden.fileprovider", file); if(intent == null)
intent.SetDataAndType(uri, mimeType); {
intent.SetFlags(ActivityFlags.GrantReadUriPermission); return false;
}
activity.StartActivity(intent); activity.StartActivity(intent);
return true; return true;
} }
@ -154,22 +130,57 @@ namespace Bit.Droid.Services
} }
public bool CanOpenFile(string fileName) 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()); var extension = MimeTypeMap.GetFileExtensionFromUrl(fileName.Replace(' ', '_').ToLower());
if(extension == null) if(extension == null)
{ {
return false; return null;
} }
var mimeType = MimeTypeMap.Singleton.GetMimeTypeFromExtension(extension); var mimeType = MimeTypeMap.Singleton.GetMimeTypeFromExtension(extension);
if(mimeType == null) if(mimeType == null)
{ {
return false; return null;
} }
var activity = (MainActivity)CrossCurrentActivity.Current.Activity; 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 null;
}
try
{
var intent = new Intent(Intent.ActionView); var intent = new Intent(Intent.ActionView);
intent.SetType(mimeType); var uri = FileProvider.GetUriForFile(activity.ApplicationContext,
var activities = activity.PackageManager.QueryIntentActivities(intent, PackageInfoFlags.MatchDefaultOnly); "com.x8bit.bitwarden.fileprovider", file);
return (activities?.Count ?? 0) > 0; intent.SetDataAndType(uri, mimeType);
intent.SetFlags(ActivityFlags.GrantReadUriPermission);
return intent;
}
catch { }
return null;
} }
public async Task ClearCacheAsync() public async Task ClearCacheAsync()
@ -185,7 +196,8 @@ namespace Bit.Droid.Services
public Task SelectFileAsync() public Task SelectFileAsync()
{ {
var activity = (MainActivity)CrossCurrentActivity.Current.Activity; var activity = (MainActivity)CrossCurrentActivity.Current.Activity;
var hasStorageWritePermission = !_cameraPermissionsDenied && HasPermission(Manifest.Permission.WriteExternalStorage); var hasStorageWritePermission = !_cameraPermissionsDenied &&
HasPermission(Manifest.Permission.WriteExternalStorage);
var additionalIntents = new List<IParcelable>(); var additionalIntents = new List<IParcelable>();
if(activity.PackageManager.HasSystemFeature(PackageManager.FeatureCamera)) if(activity.PackageManager.HasSystemFeature(PackageManager.FeatureCamera))
{ {
@ -543,7 +555,8 @@ namespace Bit.Droid.Services
try try
{ {
var activity = (MainActivity)CrossCurrentActivity.Current.Activity; 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; return afm.IsEnabled && afm.HasEnabledAutofillServices;
} }
catch catch