mirror of
https://github.com/bitwarden/android.git
synced 2024-10-31 23:25:45 +03:00
fixes to opening file types on android
This commit is contained in:
parent
b5dcdc74d7
commit
bdad5e4f0a
1 changed files with 52 additions and 39 deletions
|
@ -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 intent = new Intent(Intent.ActionView);
|
var cachePath = activity.CacheDir;
|
||||||
intent.SetType(mimeType);
|
var filePath = Path.Combine(cachePath.Path, fileName);
|
||||||
var activities = activity.PackageManager.QueryIntentActivities(intent, PackageInfoFlags.MatchDefaultOnly);
|
File.WriteAllBytes(filePath, fileData);
|
||||||
return (activities?.Count ?? 0) > 0;
|
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()
|
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
|
||||||
|
@ -684,4 +697,4 @@ namespace Bit.Droid.Services
|
||||||
clipboardManager.Text = text;
|
clipboardManager.Text = text;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue