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;
|
2017-07-22 22:38:08 +03:00
|
|
|
|
using Bit.App;
|
2017-07-23 04:06:53 +03:00
|
|
|
|
using Bit.App.Resources;
|
|
|
|
|
using Android.Provider;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Android.OS;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using Android;
|
|
|
|
|
using Android.Content.PM;
|
|
|
|
|
using Android.Support.V4.App;
|
2017-11-22 07:08:45 +03:00
|
|
|
|
using Bit.App.Models.Page;
|
|
|
|
|
using XLabs.Ioc;
|
|
|
|
|
using Android.App;
|
|
|
|
|
using Android.Views.Autofill;
|
|
|
|
|
using Android.App.Assist;
|
|
|
|
|
using Bit.Android.Autofill;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using Plugin.Settings.Abstractions;
|
|
|
|
|
using Android.Views.InputMethods;
|
2018-03-05 23:15:20 +03:00
|
|
|
|
using Android.Widget;
|
2017-07-13 06:09:44 +03:00
|
|
|
|
|
|
|
|
|
namespace Bit.Android.Services
|
|
|
|
|
{
|
|
|
|
|
public class DeviceActionService : IDeviceActionService
|
|
|
|
|
{
|
2017-07-13 18:11:04 +03:00
|
|
|
|
private readonly IAppSettingsService _appSettingsService;
|
2017-07-23 04:06:53 +03:00
|
|
|
|
private bool _cameraPermissionsDenied;
|
2017-11-22 07:08:45 +03:00
|
|
|
|
private DateTime? _lastAction;
|
2017-12-23 07:56:45 +03:00
|
|
|
|
private ProgressDialog _progressDialog;
|
2017-12-24 07:48:47 +03:00
|
|
|
|
private global::Android.Widget.Toast _toast;
|
2017-07-13 18:11:04 +03:00
|
|
|
|
|
2017-07-23 04:06:53 +03:00
|
|
|
|
public DeviceActionService(
|
2017-12-23 06:41:48 +03:00
|
|
|
|
IAppSettingsService appSettingsService)
|
2017-07-13 18:11:04 +03:00
|
|
|
|
{
|
|
|
|
|
_appSettingsService = appSettingsService;
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-22 19:23:03 +03:00
|
|
|
|
private Context CurrentContext => CrossCurrentActivity.Current.Activity;
|
|
|
|
|
|
2017-12-22 23:00:11 +03:00
|
|
|
|
public void Toast(string text, bool longDuration = false)
|
|
|
|
|
{
|
2017-12-24 07:48:47 +03:00
|
|
|
|
if(_toast != null)
|
|
|
|
|
{
|
|
|
|
|
_toast.Cancel();
|
|
|
|
|
_toast.Dispose();
|
|
|
|
|
_toast = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_toast = global::Android.Widget.Toast.MakeText(CurrentContext, text,
|
|
|
|
|
longDuration ? global::Android.Widget.ToastLength.Long : global::Android.Widget.ToastLength.Short);
|
|
|
|
|
_toast.Show();
|
2017-12-22 23:00:11 +03:00
|
|
|
|
}
|
|
|
|
|
|
2017-07-13 06:09:44 +03:00
|
|
|
|
public void CopyToClipboard(string text)
|
|
|
|
|
{
|
2017-12-22 19:23:03 +03:00
|
|
|
|
var clipboardManager = (ClipboardManager)CurrentContext.GetSystemService(Context.ClipboardService);
|
2017-07-13 06:09:44 +03:00
|
|
|
|
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 19:08:48 +03:00
|
|
|
|
var extension = MimeTypeMap.GetFileExtensionFromUrl(fileName.Replace(' ', '_').ToLower());
|
2017-07-13 06:09:44 +03:00
|
|
|
|
if(extension == null)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-13 19:08:48 +03:00
|
|
|
|
var mimeType = MimeTypeMap.Singleton.GetMimeTypeFromExtension(extension);
|
2017-07-13 06:09:44 +03:00
|
|
|
|
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)
|
|
|
|
|
{
|
2017-07-13 19:08:48 +03:00
|
|
|
|
var extension = MimeTypeMap.GetFileExtensionFromUrl(fileName.Replace(' ', '_').ToLower());
|
2017-07-13 16:01:00 +03:00
|
|
|
|
if(extension == null)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-13 19:08:48 +03:00
|
|
|
|
var mimeType = MimeTypeMap.Singleton.GetMimeTypeFromExtension(extension);
|
2017-07-13 16:01:00 +03:00
|
|
|
|
if(mimeType == null)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var pm = CrossCurrentActivity.Current.Activity.PackageManager;
|
|
|
|
|
var intent = new Intent(Intent.ActionView);
|
|
|
|
|
intent.SetType(mimeType);
|
2017-07-29 00:21:39 +03:00
|
|
|
|
var activities = pm.QueryIntentActivities(intent, PackageInfoFlags.MatchDefaultOnly);
|
2017-07-13 16:01:00 +03:00
|
|
|
|
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) { }
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-23 04:06:53 +03:00
|
|
|
|
public Task SelectFileAsync()
|
2017-07-15 08:09:06 +03:00
|
|
|
|
{
|
2017-11-22 07:08:45 +03:00
|
|
|
|
MessagingCenter.Unsubscribe<Xamarin.Forms.Application>(Xamarin.Forms.Application.Current,
|
|
|
|
|
"SelectFileCameraPermissionDenied");
|
2017-07-23 04:06:53 +03:00
|
|
|
|
|
|
|
|
|
var hasStorageWritePermission = !_cameraPermissionsDenied && HasPermission(Manifest.Permission.WriteExternalStorage);
|
|
|
|
|
|
2017-07-25 15:51:55 +03:00
|
|
|
|
var additionalIntents = new List<IParcelable>();
|
2017-12-22 19:23:03 +03:00
|
|
|
|
if(CurrentContext.PackageManager.HasSystemFeature(PackageManager.FeatureCamera))
|
2017-09-07 07:33:19 +03:00
|
|
|
|
{
|
|
|
|
|
var hasCameraPermission = !_cameraPermissionsDenied && HasPermission(Manifest.Permission.Camera);
|
2017-07-25 15:51:55 +03:00
|
|
|
|
|
2017-09-07 07:33:19 +03:00
|
|
|
|
if(!_cameraPermissionsDenied && !hasStorageWritePermission)
|
|
|
|
|
{
|
|
|
|
|
AskCameraPermission(Manifest.Permission.WriteExternalStorage);
|
|
|
|
|
return Task.FromResult(0);
|
|
|
|
|
}
|
2017-07-23 04:06:53 +03:00
|
|
|
|
|
2017-09-07 07:33:19 +03:00
|
|
|
|
if(!_cameraPermissionsDenied && !hasCameraPermission)
|
|
|
|
|
{
|
|
|
|
|
AskCameraPermission(Manifest.Permission.Camera);
|
|
|
|
|
return Task.FromResult(0);
|
|
|
|
|
}
|
2017-07-23 04:06:53 +03:00
|
|
|
|
|
2017-09-07 07:33:19 +03:00
|
|
|
|
if(!_cameraPermissionsDenied && hasCameraPermission && hasStorageWritePermission)
|
2017-07-23 04:06:53 +03:00
|
|
|
|
{
|
2017-09-07 07:33:19 +03:00
|
|
|
|
try
|
2017-07-26 23:19:58 +03:00
|
|
|
|
{
|
2017-09-07 07:33:19 +03:00
|
|
|
|
var root = new Java.IO.File(global::Android.OS.Environment.ExternalStorageDirectory, "bitwarden");
|
|
|
|
|
var file = new Java.IO.File(root, "temp_camera_photo.jpg");
|
|
|
|
|
if(!file.Exists())
|
|
|
|
|
{
|
|
|
|
|
file.ParentFile.Mkdirs();
|
|
|
|
|
file.CreateNewFile();
|
|
|
|
|
}
|
|
|
|
|
var outputFileUri = global::Android.Net.Uri.FromFile(file);
|
|
|
|
|
additionalIntents.AddRange(GetCameraIntents(outputFileUri));
|
2017-07-26 23:19:58 +03:00
|
|
|
|
}
|
2017-09-07 07:33:19 +03:00
|
|
|
|
catch(Java.IO.IOException) { }
|
2017-07-23 04:06:53 +03:00
|
|
|
|
}
|
2017-07-25 15:51:55 +03:00
|
|
|
|
}
|
|
|
|
|
|
2017-09-07 07:33:19 +03:00
|
|
|
|
var docIntent = new Intent(Intent.ActionOpenDocument);
|
|
|
|
|
docIntent.AddCategory(Intent.CategoryOpenable);
|
|
|
|
|
docIntent.SetType("*/*");
|
|
|
|
|
|
|
|
|
|
var chooserIntent = Intent.CreateChooser(docIntent, AppResources.FileSource);
|
2017-07-25 15:51:55 +03:00
|
|
|
|
if(additionalIntents.Count > 0)
|
|
|
|
|
{
|
2017-07-23 04:06:53 +03:00
|
|
|
|
chooserIntent.PutExtra(Intent.ExtraInitialIntents, additionalIntents.ToArray());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CrossCurrentActivity.Current.Activity.StartActivityForResult(chooserIntent, Constants.SelectFileRequestCode);
|
|
|
|
|
return Task.FromResult(0);
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-22 07:08:45 +03:00
|
|
|
|
public void Autofill(VaultListPageModel.Cipher cipher)
|
|
|
|
|
{
|
2017-12-22 19:23:03 +03:00
|
|
|
|
var activity = (MainActivity)CurrentContext;
|
2017-11-22 07:08:45 +03:00
|
|
|
|
if(activity.Intent.GetBooleanExtra("autofillFramework", false))
|
|
|
|
|
{
|
|
|
|
|
if(cipher == null)
|
|
|
|
|
{
|
|
|
|
|
activity.SetResult(Result.Canceled);
|
|
|
|
|
activity.Finish();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var structure = activity.Intent.GetParcelableExtra(
|
|
|
|
|
AutofillManager.ExtraAssistStructure) as AssistStructure;
|
|
|
|
|
if(structure == null)
|
|
|
|
|
{
|
|
|
|
|
activity.SetResult(Result.Canceled);
|
|
|
|
|
activity.Finish();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var parser = new Parser(structure);
|
|
|
|
|
parser.Parse();
|
|
|
|
|
if(!parser.FieldCollection.Fields.Any() || string.IsNullOrWhiteSpace(parser.Uri))
|
|
|
|
|
{
|
|
|
|
|
activity.SetResult(Result.Canceled);
|
|
|
|
|
activity.Finish();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var dataset = AutofillHelpers.BuildDataset(activity, parser.FieldCollection,
|
|
|
|
|
new FilledItem(cipher.CipherModel));
|
|
|
|
|
var replyIntent = new Intent();
|
|
|
|
|
replyIntent.PutExtra(AutofillManager.ExtraAuthenticationResult, dataset);
|
|
|
|
|
activity.SetResult(Result.Ok, replyIntent);
|
|
|
|
|
activity.Finish();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var data = new Intent();
|
|
|
|
|
if(cipher == null)
|
|
|
|
|
{
|
|
|
|
|
data.PutExtra("canceled", "true");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var isPremium = Resolver.Resolve<ITokenService>()?.TokenPremium ?? false;
|
|
|
|
|
var settings = Resolver.Resolve<ISettings>();
|
|
|
|
|
var autoCopyEnabled = !settings.GetValueOrDefault(Constants.SettingDisableTotpCopy, false);
|
|
|
|
|
if(isPremium && autoCopyEnabled && cipher.LoginTotp?.Value != null)
|
|
|
|
|
{
|
|
|
|
|
CopyToClipboard(App.Utilities.Crypto.Totp(cipher.LoginTotp.Value));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
data.PutExtra("uri", cipher.LoginUri);
|
|
|
|
|
data.PutExtra("username", cipher.LoginUsername);
|
|
|
|
|
data.PutExtra("password", cipher.LoginPassword?.Value ?? null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(activity.Parent == null)
|
|
|
|
|
{
|
|
|
|
|
activity.SetResult(Result.Ok, data);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
activity.Parent.SetResult(Result.Ok, data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
activity.Finish();
|
|
|
|
|
MessagingCenter.Send(Xamarin.Forms.Application.Current, "FinishMainActivity");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void CloseAutofill()
|
|
|
|
|
{
|
|
|
|
|
Autofill(null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Background()
|
|
|
|
|
{
|
2017-12-22 19:23:03 +03:00
|
|
|
|
var activity = (MainActivity)CurrentContext;
|
2017-11-22 07:08:45 +03:00
|
|
|
|
if(activity.Intent.GetBooleanExtra("autofillFramework", false))
|
|
|
|
|
{
|
|
|
|
|
activity.SetResult(Result.Canceled);
|
|
|
|
|
activity.Finish();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
activity.MoveTaskToBack(true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void RateApp()
|
|
|
|
|
{
|
2017-12-22 19:23:03 +03:00
|
|
|
|
var activity = (MainActivity)CurrentContext;
|
2017-11-22 07:08:45 +03:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var rateIntent = RateIntentForUrl("market://details", activity);
|
|
|
|
|
activity.StartActivity(rateIntent);
|
|
|
|
|
}
|
|
|
|
|
catch(ActivityNotFoundException)
|
|
|
|
|
{
|
|
|
|
|
var rateIntent = RateIntentForUrl("https://play.google.com/store/apps/details", activity);
|
|
|
|
|
activity.StartActivity(rateIntent);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void DismissKeyboard()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2018-02-02 07:25:48 +03:00
|
|
|
|
var activity = (MainActivity)CurrentContext;
|
2017-11-22 07:08:45 +03:00
|
|
|
|
var imm = (InputMethodManager)activity.GetSystemService(Context.InputMethodService);
|
|
|
|
|
imm.HideSoftInputFromWindow(activity.CurrentFocus.WindowToken, 0);
|
|
|
|
|
}
|
|
|
|
|
catch { }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void OpenAccessibilitySettings()
|
|
|
|
|
{
|
2017-12-22 19:23:03 +03:00
|
|
|
|
var activity = (MainActivity)CurrentContext;
|
2017-11-22 07:08:45 +03:00
|
|
|
|
var intent = new Intent(Settings.ActionAccessibilitySettings);
|
|
|
|
|
activity.StartActivity(intent);
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-23 06:41:48 +03:00
|
|
|
|
public async Task LaunchAppAsync(string appName, Page page)
|
2017-11-22 07:08:45 +03:00
|
|
|
|
{
|
2017-12-22 19:23:03 +03:00
|
|
|
|
var activity = (MainActivity)CurrentContext;
|
2017-11-22 07:08:45 +03:00
|
|
|
|
if(_lastAction.LastActionWasRecent())
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
_lastAction = DateTime.UtcNow;
|
|
|
|
|
|
|
|
|
|
appName = appName.Replace("androidapp://", string.Empty);
|
|
|
|
|
var launchIntent = activity.PackageManager.GetLaunchIntentForPackage(appName);
|
|
|
|
|
if(launchIntent == null)
|
|
|
|
|
{
|
2017-12-23 06:41:48 +03:00
|
|
|
|
await page.DisplayAlert(null, string.Format(AppResources.CannotOpenApp, appName), AppResources.Ok);
|
2017-11-22 07:08:45 +03:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
activity.StartActivity(launchIntent);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Intent RateIntentForUrl(string url, Activity activity)
|
|
|
|
|
{
|
|
|
|
|
var intent = new Intent(Intent.ActionView, global::Android.Net.Uri.Parse($"{url}?id={activity.PackageName}"));
|
|
|
|
|
var flags = ActivityFlags.NoHistory | ActivityFlags.MultipleTask;
|
|
|
|
|
if((int)Build.VERSION.SdkInt >= 21)
|
|
|
|
|
{
|
|
|
|
|
flags |= ActivityFlags.NewDocument;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// noinspection deprecation
|
|
|
|
|
flags |= ActivityFlags.ClearWhenTaskReset;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
intent.AddFlags(flags);
|
|
|
|
|
return intent;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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-23 04:06:53 +03:00
|
|
|
|
private List<IParcelable> GetCameraIntents(global::Android.Net.Uri outputUri)
|
|
|
|
|
{
|
|
|
|
|
var intents = new List<IParcelable>();
|
|
|
|
|
var pm = CrossCurrentActivity.Current.Activity.PackageManager;
|
|
|
|
|
var captureIntent = new Intent(MediaStore.ActionImageCapture);
|
|
|
|
|
var listCam = pm.QueryIntentActivities(captureIntent, 0);
|
|
|
|
|
foreach(var res in listCam)
|
|
|
|
|
{
|
|
|
|
|
var packageName = res.ActivityInfo.PackageName;
|
|
|
|
|
var intent = new Intent(captureIntent);
|
|
|
|
|
intent.SetComponent(new ComponentName(packageName, res.ActivityInfo.Name));
|
|
|
|
|
intent.SetPackage(packageName);
|
|
|
|
|
intent.PutExtra(MediaStore.ExtraOutput, outputUri);
|
|
|
|
|
intents.Add(intent);
|
|
|
|
|
}
|
|
|
|
|
return intents;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool HasPermission(string permission)
|
|
|
|
|
{
|
|
|
|
|
return ContextCompat.CheckSelfPermission(CrossCurrentActivity.Current.Activity, permission) == Permission.Granted;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void AskCameraPermission(string permission)
|
|
|
|
|
{
|
2017-11-22 07:08:45 +03:00
|
|
|
|
MessagingCenter.Subscribe<Xamarin.Forms.Application>(Xamarin.Forms.Application.Current,
|
|
|
|
|
"SelectFileCameraPermissionDenied", (sender) =>
|
|
|
|
|
{
|
|
|
|
|
_cameraPermissionsDenied = true;
|
|
|
|
|
});
|
2017-07-23 04:06:53 +03:00
|
|
|
|
|
|
|
|
|
AskPermission(permission);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void AskPermission(string permission)
|
|
|
|
|
{
|
|
|
|
|
ActivityCompat.RequestPermissions(CrossCurrentActivity.Current.Activity, new string[] { permission },
|
|
|
|
|
Constants.SelectFilePermissionRequestCode);
|
2017-07-15 08:09:06 +03:00
|
|
|
|
}
|
2017-11-28 01:27:11 +03:00
|
|
|
|
|
|
|
|
|
public void OpenAutofillSettings()
|
|
|
|
|
{
|
2018-02-15 01:11:27 +03:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var activity = (MainActivity)CurrentContext;
|
|
|
|
|
var intent = new Intent(Settings.ActionRequestSetAutofillService);
|
|
|
|
|
intent.SetData(global::Android.Net.Uri.Parse("package:com.x8bit.bitwarden"));
|
|
|
|
|
activity.StartActivity(intent);
|
|
|
|
|
}
|
|
|
|
|
catch(ActivityNotFoundException)
|
|
|
|
|
{
|
|
|
|
|
var alertBuilder = new AlertDialog.Builder((MainActivity)CurrentContext);
|
|
|
|
|
alertBuilder.SetMessage(AppResources.BitwardenAutofillGoToSettings);
|
|
|
|
|
alertBuilder.SetCancelable(true);
|
|
|
|
|
alertBuilder.SetPositiveButton(AppResources.Ok, (sender, args) =>
|
|
|
|
|
{
|
|
|
|
|
(sender as AlertDialog)?.Cancel();
|
|
|
|
|
});
|
|
|
|
|
alertBuilder.Create().Show();
|
|
|
|
|
}
|
2017-11-28 01:27:11 +03:00
|
|
|
|
}
|
2017-12-23 07:56:45 +03:00
|
|
|
|
|
|
|
|
|
public void ShowLoading(string text)
|
|
|
|
|
{
|
|
|
|
|
if(_progressDialog != null)
|
|
|
|
|
{
|
|
|
|
|
HideLoading();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var activity = (MainActivity)CurrentContext;
|
|
|
|
|
_progressDialog = new ProgressDialog(activity);
|
|
|
|
|
_progressDialog.SetMessage(text);
|
2018-03-16 17:42:07 +03:00
|
|
|
|
_progressDialog.SetCancelable(false);
|
2017-12-23 07:56:45 +03:00
|
|
|
|
_progressDialog.Show();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void HideLoading()
|
|
|
|
|
{
|
|
|
|
|
if(_progressDialog == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_progressDialog.Dismiss();
|
|
|
|
|
_progressDialog.Dispose();
|
|
|
|
|
_progressDialog = null;
|
|
|
|
|
}
|
2018-03-05 23:15:20 +03:00
|
|
|
|
|
|
|
|
|
public Task<string> DisplayPromptAync(string title = null, string description = null, string text = null)
|
|
|
|
|
{
|
|
|
|
|
var activity = (MainActivity)CurrentContext;
|
2018-03-20 04:07:27 +03:00
|
|
|
|
if(activity == null)
|
|
|
|
|
{
|
|
|
|
|
return Task.FromResult<string>(null);
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-05 23:15:20 +03:00
|
|
|
|
var alertBuilder = new AlertDialog.Builder(activity);
|
|
|
|
|
alertBuilder.SetTitle(title);
|
|
|
|
|
alertBuilder.SetMessage(description);
|
|
|
|
|
|
|
|
|
|
var input = new EditText(activity)
|
|
|
|
|
{
|
|
|
|
|
InputType = global::Android.Text.InputTypes.ClassText
|
|
|
|
|
};
|
|
|
|
|
if(text != null)
|
|
|
|
|
{
|
|
|
|
|
input.Text = text;
|
|
|
|
|
input.SetSelection(text.Length);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
input.FocusedByDefault = true;
|
|
|
|
|
}
|
|
|
|
|
alertBuilder.SetView(input);
|
|
|
|
|
|
|
|
|
|
var result = new TaskCompletionSource<string>();
|
|
|
|
|
alertBuilder.SetPositiveButton(AppResources.Ok, (sender, args) =>
|
|
|
|
|
{
|
|
|
|
|
result.TrySetResult(input.Text ?? string.Empty);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
alertBuilder.SetNegativeButton(AppResources.Cancel, (sender, args) =>
|
|
|
|
|
{
|
|
|
|
|
result.TrySetResult(null);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
var alert = alertBuilder.Create();
|
|
|
|
|
alert.Window.SetSoftInputMode(global::Android.Views.SoftInput.StateVisible);
|
|
|
|
|
alert.Show();
|
|
|
|
|
return result.Task;
|
|
|
|
|
}
|
2017-07-13 06:09:44 +03:00
|
|
|
|
}
|
|
|
|
|
}
|