bitwarden-android/src/Android/MainActivity.cs

103 lines
3.8 KiB
C#
Raw Normal View History

2019-03-28 06:44:54 +03:00
using Android.App;
2019-03-28 03:12:44 +03:00
using Android.Content.PM;
using Android.Runtime;
using Android.OS;
2019-05-11 06:43:35 +03:00
using Bit.Core;
using System.Linq;
using Bit.App.Abstractions;
using Bit.Core.Utilities;
using Bit.Core.Abstractions;
using System.IO;
using System;
using Android.Content;
using Bit.Droid.Utilities;
2019-03-28 03:12:44 +03:00
namespace Bit.Droid
{
[Activity(
Label = "Bitwarden",
Icon = "@mipmap/ic_launcher",
Theme = "@style/MainTheme",
2019-04-26 17:07:42 +03:00
Exported = false,
2019-03-28 03:12:44 +03:00
ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
[Register("com.x8bit.bitwarden.MainActivity")]
2019-03-28 06:44:54 +03:00
public class MainActivity : Xamarin.Forms.Platform.Android.FormsAppCompatActivity
2019-03-28 03:12:44 +03:00
{
2019-05-11 06:43:35 +03:00
private IDeviceActionService _deviceActionService;
private IMessagingService _messagingService;
2019-03-28 03:12:44 +03:00
protected override void OnCreate(Bundle savedInstanceState)
{
2019-05-11 06:43:35 +03:00
_deviceActionService = ServiceContainer.Resolve<IDeviceActionService>("deviceActionService");
_messagingService = ServiceContainer.Resolve<IMessagingService>("messagingService");
2019-03-28 03:12:44 +03:00
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
base.OnCreate(savedInstanceState);
2019-03-28 06:44:54 +03:00
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
Xamarin.Forms.Forms.Init(this, savedInstanceState);
2019-03-28 03:12:44 +03:00
LoadApplication(new App.App());
}
2019-03-28 06:44:54 +03:00
2019-05-11 06:43:35 +03:00
public async override void OnRequestPermissionsResult(int requestCode, string[] permissions,
2019-03-28 06:44:54 +03:00
[GeneratedEnum] Permission[] grantResults)
{
2019-05-11 06:43:35 +03:00
if(requestCode == Constants.SelectFilePermissionRequestCode)
{
if(grantResults.Any(r => r != Permission.Granted))
{
_messagingService.Send("selectFileCameraPermissionDenied");
}
await _deviceActionService.SelectFileAsync();
}
else
{
Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}
2019-03-28 06:44:54 +03:00
base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}
2019-05-11 06:43:35 +03:00
protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
if(requestCode == Constants.SelectFileRequestCode && resultCode == Result.Ok)
{
Android.Net.Uri uri = null;
string fileName = null;
if(data != null && data.Data != null)
{
uri = data.Data;
fileName = AndroidHelpers.GetFileName(ApplicationContext, uri);
}
else
{
// camera
var root = new Java.IO.File(Android.OS.Environment.ExternalStorageDirectory, "bitwarden");
var file = new Java.IO.File(root, "temp_camera_photo.jpg");
uri = Android.Net.Uri.FromFile(file);
fileName = $"photo_{DateTime.UtcNow.ToString("yyyyMMddHHmmss")}.jpg";
}
if(uri == null)
{
return;
}
try
{
using(var stream = ContentResolver.OpenInputStream(uri))
using(var memoryStream = new MemoryStream())
{
stream.CopyTo(memoryStream);
_messagingService.Send("selectFileResult",
new Tuple<byte[], string>(memoryStream.ToArray(), fileName ?? "unknown_file_name"));
}
}
catch(Java.IO.FileNotFoundException)
{
return;
}
}
}
2019-03-28 03:12:44 +03:00
}
2019-03-28 06:44:54 +03:00
}