2017-07-13 06:09:44 +03:00
|
|
|
|
using System;
|
|
|
|
|
using Android.Content;
|
|
|
|
|
using Bit.App.Abstractions;
|
|
|
|
|
using Xamarin.Forms;
|
|
|
|
|
using Android.Webkit;
|
|
|
|
|
using Plugin.CurrentActivity;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using Android.Support.V4.Content;
|
|
|
|
|
|
|
|
|
|
namespace Bit.Android.Services
|
|
|
|
|
{
|
|
|
|
|
public class DeviceActionService : IDeviceActionService
|
|
|
|
|
{
|
2017-07-13 18:11:04 +03:00
|
|
|
|
private readonly IAppSettingsService _appSettingsService;
|
|
|
|
|
|
|
|
|
|
public DeviceActionService(IAppSettingsService appSettingsService)
|
|
|
|
|
{
|
|
|
|
|
_appSettingsService = appSettingsService;
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-13 06:09:44 +03:00
|
|
|
|
public void CopyToClipboard(string text)
|
|
|
|
|
{
|
|
|
|
|
var clipboardManager = (ClipboardManager)Forms.Context.GetSystemService(Context.ClipboardService);
|
|
|
|
|
clipboardManager.Text = text;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool OpenFile(byte[] fileData, string id, string fileName)
|
|
|
|
|
{
|
2017-07-13 16:01:00 +03:00
|
|
|
|
if(!CanOpenFile(fileName))
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-13 06:09:44 +03:00
|
|
|
|
var extension = MimeTypeMap.GetFileExtensionFromUrl(fileName);
|
|
|
|
|
if(extension == null)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var mimeType = MimeTypeMap.Singleton.GetMimeTypeFromExtension(extension.ToLower());
|
|
|
|
|
if(mimeType == null)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var cachePath = CrossCurrentActivity.Current.Activity.CacheDir;
|
|
|
|
|
var filePath = Path.Combine(cachePath.Path, fileName);
|
2017-07-13 16:01:00 +03:00
|
|
|
|
File.WriteAllBytes(filePath, fileData);
|
2017-07-13 06:09:44 +03:00
|
|
|
|
var file = new Java.IO.File(cachePath, fileName);
|
2017-07-13 16:01:00 +03:00
|
|
|
|
if(!file.IsFile)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-13 06:09:44 +03:00
|
|
|
|
try
|
|
|
|
|
{
|
2017-07-13 16:01:00 +03:00
|
|
|
|
var intent = new Intent(Intent.ActionView);
|
|
|
|
|
var uri = FileProvider.GetUriForFile(CrossCurrentActivity.Current.Activity.ApplicationContext,
|
|
|
|
|
"com.x8bit.bitwarden.fileprovider", file);
|
|
|
|
|
intent.SetDataAndType(uri, mimeType);
|
|
|
|
|
intent.SetFlags(ActivityFlags.GrantReadUriPermission);
|
|
|
|
|
CrossCurrentActivity.Current.Activity.StartActivity(intent);
|
|
|
|
|
return true;
|
2017-07-13 06:09:44 +03:00
|
|
|
|
}
|
|
|
|
|
catch { }
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2017-07-13 16:01:00 +03:00
|
|
|
|
|
|
|
|
|
public bool CanOpenFile(string fileName)
|
|
|
|
|
{
|
|
|
|
|
var extension = MimeTypeMap.GetFileExtensionFromUrl(fileName);
|
|
|
|
|
if(extension == null)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var mimeType = MimeTypeMap.Singleton.GetMimeTypeFromExtension(extension.ToLower());
|
|
|
|
|
if(mimeType == null)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var pm = CrossCurrentActivity.Current.Activity.PackageManager;
|
|
|
|
|
var intent = new Intent(Intent.ActionView);
|
|
|
|
|
intent.SetType(mimeType);
|
|
|
|
|
var activities = pm.QueryIntentActivities(intent, global::Android.Content.PM.PackageInfoFlags.MatchDefaultOnly);
|
|
|
|
|
return (activities?.Count ?? 0) > 0;
|
|
|
|
|
}
|
2017-07-13 17:51:45 +03:00
|
|
|
|
|
|
|
|
|
public void ClearCache()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
DeleteDir(CrossCurrentActivity.Current.Activity.CacheDir);
|
2017-07-13 18:11:04 +03:00
|
|
|
|
_appSettingsService.LastCacheClear = DateTime.UtcNow;
|
2017-07-13 17:51:45 +03:00
|
|
|
|
}
|
|
|
|
|
catch(Exception) { }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool DeleteDir(Java.IO.File dir)
|
|
|
|
|
{
|
|
|
|
|
if(dir != null && dir.IsDirectory)
|
|
|
|
|
{
|
|
|
|
|
var children = dir.List();
|
|
|
|
|
for(int i = 0; i < children.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
var success = DeleteDir(new Java.IO.File(dir, children[i]));
|
|
|
|
|
if(!success)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return dir.Delete();
|
|
|
|
|
}
|
|
|
|
|
else if(dir != null && dir.IsFile)
|
|
|
|
|
{
|
|
|
|
|
return dir.Delete();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-07-13 06:09:44 +03:00
|
|
|
|
}
|
|
|
|
|
}
|