mirror of
https://github.com/bitwarden/mobile
synced 2026-01-01 08:03:37 +00:00
* [EC-426] Add watchOS PoC app (#2054) * EC-426 Added watchOS app, configured iOS.csproj to bundle the output of XCode build into the Xamarin iOS app and added some custom logic to use WCSession to communicate between the iOS and the watchOS apps * EC-426 Removed Info.plist from iOS.Core project given that it's not needed * [EC-426] Added new encrypted watch app profiles * EC-426 added configuration for building watchApp and bundle it up on the iOS one * EC-426 Fix build for watchOS * EC-426 Fix build for watchOS applied shell bash * EC-426 Fix build for watchOS echo * EC-426 Fix build for watchOS simplify * EC-426 Fix build for watchOS added workspace path * EC-426 Changed code sign identity of watchOS project to Apple Distribution * EC-426 added manual code sign style and specified the provisioning profile for the targets on the watch xcode project * EC-426 updated path to watchOS on release on iOS.csproj and disabled android and f-.droid * EC-426 fix build * EC-426 fix path and check listing of directory of watchOS output just in case * EC-426 Fix Apple Watch build to list the folder recursively just in case we need to change the path for the watch bundle * EC-426 TEMP Change texts on input on login and lock to show that the app is for the Watch PoC testing * EC-426 Fix WatchApp build path * EC-426 Added WatchOS AppIcons * EC-426 added gitignore for XCode project removed files supposed to be ignored * EC-426 Cleaned the code a bit to avoid misbehavior * EC-426 Code cleanup Co-authored-by: Joseph Flinn <joseph.s.flinn@gmail.com> * [EC-585] Added data, encryption and some helpers and structure to the Watch app (#2164) * [EC-585] Added foundation classes on the watch to handle CoreData and some fixes on the communication of the ciphers, also some helper classes to store in keychain and encrypt data * EC-585 Added keychain helper, encryption helpers and added data storage using CoreData configuring it appropiately. View and ViewModel are here only to test that the fetching/saving works but it's not the actual UI of the watch app. Also removed all the places where the automatic file signature was added by XCode * EC-585 Fixed CipherServiceMock to implement protocol * EC-585 Fixed DeviceActionService duplicated services * [EC-614] Apple Watch MVP Cipher list UI (#2175) * [EC-585] Added foundation classes on the watch to handle CoreData and some fixes on the communication of the ciphers, also some helper classes to store in keychain and encrypt data * EC-585 Added keychain helper, encryption helpers and added data storage using CoreData configuring it appropiately. View and ViewModel are here only to test that the fetching/saving works but it's not the actual UI of the watch app. Also removed all the places where the automatic file signature was added by XCode * EC-585 Fixed CipherServiceMock to implement protocol * EC-585 Fixed DeviceActionService duplicated services * EC-614 Implemented watch ciphers list UI * [EC-615] Apple Watch MVP Cipher details UI (#2192) * [EC-585] Added foundation classes on the watch to handle CoreData and some fixes on the communication of the ciphers, also some helper classes to store in keychain and encrypt data * EC-585 Added keychain helper, encryption helpers and added data storage using CoreData configuring it appropiately. View and ViewModel are here only to test that the fetching/saving works but it's not the actual UI of the watch app. Also removed all the places where the automatic file signature was added by XCode * EC-585 Fixed CipherServiceMock to implement protocol * EC-585 Fixed DeviceActionService duplicated services * EC-614 Implemented watch ciphers list UI * EC-615 Added cipher details UI to watch and also implemented logic and helpers to generate the TOTPs * EC-615 Added value transformer to login uris on the cipher entity * EC-617 Added state view on watch app and some state helpers and wired it on the CipherListView. Also added some images (#2195) * [EC-581] Implement Apple Watch MVP Sync (#2206) * EC-581 Implemented sync iPhone -> watchOS, fix some issues with the watch database and sync flows for login/locks/multiple accounts * EC-581 Added watch sync on unlocking and need setup state when no user is synced and the session is not active * EC-581 Removed unused method * EC-581 Fix format * EC-759 Added avatar row on cipher list header to display avatar icon and email (#2213) * [EC-786] Apple Watch MVP Sync fixes (#2214) * EC-786 Commented things that are not going to be included on the MVP and fixed issue on the dictionary sent on the applicationContext to have a changing key based on time * EC-786 Commented need unlock state * EC-579 Added logic for Connect To Watch on iOS settings and moved it to the correct place. Also improved the synchronization and watch session activation logic (#2218) * EC-616 Added search header for ciphers and polished the code (#2226) Co-authored-by: Federico Maccaroni <fedemkr@gmail.com> Co-authored-by: Joseph Flinn <joseph.s.flinn@gmail.com>
535 lines
19 KiB
C#
535 lines
19 KiB
C#
using System;
|
|
using System.Threading.Tasks;
|
|
using Android.App;
|
|
using Android.Content;
|
|
using Android.Content.PM;
|
|
using Android.Nfc;
|
|
using Android.OS;
|
|
using Android.Provider;
|
|
using Android.Text;
|
|
using Android.Text.Method;
|
|
using Android.Views;
|
|
using Android.Views.InputMethods;
|
|
using Android.Widget;
|
|
using Bit.App.Abstractions;
|
|
using Bit.App.Resources;
|
|
using Bit.Core.Abstractions;
|
|
using Bit.Core.Enums;
|
|
using Bit.Core.Utilities;
|
|
using Bit.Droid.Utilities;
|
|
using Plugin.CurrentActivity;
|
|
using static Bit.App.Pages.SettingsPageViewModel;
|
|
|
|
namespace Bit.Droid.Services
|
|
{
|
|
public class DeviceActionService : IDeviceActionService
|
|
{
|
|
private readonly IStateService _stateService;
|
|
private readonly IMessagingService _messagingService;
|
|
private AlertDialog _progressDialog;
|
|
object _progressDialogLock = new object();
|
|
|
|
private Toast _toast;
|
|
private string _userAgent;
|
|
|
|
public DeviceActionService(
|
|
IStateService stateService,
|
|
IMessagingService messagingService)
|
|
{
|
|
_stateService = stateService;
|
|
_messagingService = messagingService;
|
|
}
|
|
|
|
public string DeviceUserAgent
|
|
{
|
|
get
|
|
{
|
|
if (string.IsNullOrWhiteSpace(_userAgent))
|
|
{
|
|
_userAgent = $"Bitwarden_Mobile/{Xamarin.Essentials.AppInfo.VersionString} " +
|
|
$"(Android {Build.VERSION.Release}; SDK {Build.VERSION.Sdk}; Model {Build.Model})";
|
|
}
|
|
return _userAgent;
|
|
}
|
|
}
|
|
|
|
public DeviceType DeviceType => DeviceType.Android;
|
|
|
|
public void Toast(string text, bool longDuration = false)
|
|
{
|
|
if (_toast != null)
|
|
{
|
|
_toast.Cancel();
|
|
_toast.Dispose();
|
|
_toast = null;
|
|
}
|
|
_toast = Android.Widget.Toast.MakeText(CrossCurrentActivity.Current.Activity, text,
|
|
longDuration ? ToastLength.Long : ToastLength.Short);
|
|
_toast.Show();
|
|
}
|
|
|
|
public bool LaunchApp(string appName)
|
|
{
|
|
if ((int)Build.VERSION.SdkInt < 33)
|
|
{
|
|
// API 33 required to avoid using wildcard app visibility or dangerous permissions
|
|
// https://developer.android.com/reference/android/content/pm/PackageManager#getLaunchIntentSenderForPackage(java.lang.String)
|
|
return false;
|
|
}
|
|
var activity = CrossCurrentActivity.Current.Activity;
|
|
appName = appName.Replace("androidapp://", string.Empty);
|
|
var launchIntentSender = activity?.PackageManager?.GetLaunchIntentSenderForPackage(appName);
|
|
launchIntentSender?.SendIntent(activity, Result.Ok, null, null, null);
|
|
return launchIntentSender != null;
|
|
}
|
|
|
|
public async Task ShowLoadingAsync(string text)
|
|
{
|
|
if (_progressDialog != null)
|
|
{
|
|
await HideLoadingAsync();
|
|
}
|
|
|
|
var activity = CrossCurrentActivity.Current.Activity;
|
|
var inflater = (LayoutInflater)activity.GetSystemService(Context.LayoutInflaterService);
|
|
var dialogView = inflater.Inflate(Resource.Layout.progress_dialog_layout, null);
|
|
|
|
var txtLoading = dialogView.FindViewById<TextView>(Resource.Id.txtLoading);
|
|
txtLoading.Text = text;
|
|
txtLoading.SetTextColor(ThemeHelpers.TextColor);
|
|
|
|
_progressDialog = new AlertDialog.Builder(activity)
|
|
.SetView(dialogView)
|
|
.SetCancelable(false)
|
|
.Create();
|
|
_progressDialog.Show();
|
|
}
|
|
|
|
public Task HideLoadingAsync()
|
|
{
|
|
// Based on https://github.com/redth-org/AndHUD/blob/master/AndHUD/AndHUD.cs
|
|
lock (_progressDialogLock)
|
|
{
|
|
if (_progressDialog is null)
|
|
{
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
void actionDismiss()
|
|
{
|
|
try
|
|
{
|
|
if (IsAlive(_progressDialog) && IsAlive(_progressDialog.Window))
|
|
{
|
|
_progressDialog.Hide();
|
|
_progressDialog.Dismiss();
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
// ignore
|
|
}
|
|
|
|
_progressDialog = null;
|
|
}
|
|
|
|
// First try the SynchronizationContext
|
|
if (Application.SynchronizationContext != null)
|
|
{
|
|
Application.SynchronizationContext.Send(state => actionDismiss(), null);
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
// Otherwise try OwnerActivity on dialog
|
|
var ownerActivity = _progressDialog?.OwnerActivity;
|
|
if (IsAlive(ownerActivity))
|
|
{
|
|
ownerActivity.RunOnUiThread(actionDismiss);
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
// Otherwise try get it from the Window Context
|
|
if (_progressDialog?.Window?.Context is Activity windowActivity && IsAlive(windowActivity))
|
|
{
|
|
windowActivity.RunOnUiThread(actionDismiss);
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
// Finally if all else fails, let's see if current activity is MainActivity
|
|
if (CrossCurrentActivity.Current.Activity is MainActivity activity && IsAlive(activity))
|
|
{
|
|
activity.RunOnUiThread(actionDismiss);
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
return Task.CompletedTask;
|
|
}
|
|
}
|
|
|
|
bool IsAlive(Java.Lang.Object @object)
|
|
{
|
|
if (@object == null)
|
|
return false;
|
|
|
|
if (@object.Handle == IntPtr.Zero)
|
|
return false;
|
|
|
|
if (@object is Activity activity)
|
|
{
|
|
if (activity.IsFinishing)
|
|
return false;
|
|
|
|
if (activity.IsDestroyed)
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
public Task<string> DisplayPromptAync(string title = null, string description = null,
|
|
string text = null, string okButtonText = null, string cancelButtonText = null,
|
|
bool numericKeyboard = false, bool autofocus = true, bool password = false)
|
|
{
|
|
var activity = (MainActivity)CrossCurrentActivity.Current.Activity;
|
|
if (activity == null)
|
|
{
|
|
return Task.FromResult<string>(null);
|
|
}
|
|
|
|
var alertBuilder = new AlertDialog.Builder(activity);
|
|
alertBuilder.SetTitle(title);
|
|
alertBuilder.SetMessage(description);
|
|
var input = new EditText(activity)
|
|
{
|
|
InputType = InputTypes.ClassText
|
|
};
|
|
if (text == null)
|
|
{
|
|
text = string.Empty;
|
|
}
|
|
if (numericKeyboard)
|
|
{
|
|
input.InputType = InputTypes.ClassNumber | InputTypes.NumberFlagDecimal | InputTypes.NumberFlagSigned;
|
|
#pragma warning disable CS0618 // Type or member is obsolete
|
|
input.KeyListener = DigitsKeyListener.GetInstance(false, false);
|
|
#pragma warning restore CS0618 // Type or member is obsolete
|
|
}
|
|
if (password)
|
|
{
|
|
input.InputType = InputTypes.TextVariationPassword | InputTypes.ClassText;
|
|
}
|
|
|
|
input.ImeOptions = input.ImeOptions | (ImeAction)ImeFlags.NoPersonalizedLearning |
|
|
(ImeAction)ImeFlags.NoExtractUi;
|
|
input.Text = text;
|
|
input.SetSelection(text.Length);
|
|
var container = new FrameLayout(activity);
|
|
var lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MatchParent,
|
|
LinearLayout.LayoutParams.MatchParent);
|
|
lp.SetMargins(25, 0, 25, 0);
|
|
input.LayoutParameters = lp;
|
|
container.AddView(input);
|
|
alertBuilder.SetView(container);
|
|
|
|
okButtonText = okButtonText ?? AppResources.Ok;
|
|
cancelButtonText = cancelButtonText ?? AppResources.Cancel;
|
|
var result = new TaskCompletionSource<string>();
|
|
alertBuilder.SetPositiveButton(okButtonText,
|
|
(sender, args) => result.TrySetResult(input.Text ?? string.Empty));
|
|
alertBuilder.SetNegativeButton(cancelButtonText, (sender, args) => result.TrySetResult(null));
|
|
|
|
var alert = alertBuilder.Create();
|
|
alert.Window.SetSoftInputMode(Android.Views.SoftInput.StateVisible);
|
|
alert.Show();
|
|
if (autofocus)
|
|
{
|
|
input.RequestFocus();
|
|
}
|
|
return result.Task;
|
|
}
|
|
|
|
public void RateApp()
|
|
{
|
|
var activity = (MainActivity)CrossCurrentActivity.Current.Activity;
|
|
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 string GetBuildNumber()
|
|
{
|
|
return Application.Context.ApplicationContext.PackageManager.GetPackageInfo(
|
|
Application.Context.PackageName, 0).VersionCode.ToString();
|
|
}
|
|
|
|
public bool SupportsFaceBiometric()
|
|
{
|
|
// only used by iOS
|
|
return false;
|
|
}
|
|
|
|
public Task<bool> SupportsFaceBiometricAsync()
|
|
{
|
|
// only used by iOS
|
|
return Task.FromResult(SupportsFaceBiometric());
|
|
}
|
|
|
|
public bool SupportsNfc()
|
|
{
|
|
var activity = (MainActivity)CrossCurrentActivity.Current.Activity;
|
|
var manager = activity.GetSystemService(Context.NfcService) as NfcManager;
|
|
return manager.DefaultAdapter?.IsEnabled ?? false;
|
|
}
|
|
|
|
public bool SupportsCamera()
|
|
{
|
|
var activity = (MainActivity)CrossCurrentActivity.Current.Activity;
|
|
return activity.PackageManager.HasSystemFeature(PackageManager.FeatureCamera);
|
|
}
|
|
|
|
public int SystemMajorVersion()
|
|
{
|
|
return (int)Build.VERSION.SdkInt;
|
|
}
|
|
|
|
public string SystemModel()
|
|
{
|
|
return Build.Model;
|
|
}
|
|
|
|
public Task<string> DisplayAlertAsync(string title, string message, string cancel, params string[] buttons)
|
|
{
|
|
var activity = (MainActivity)CrossCurrentActivity.Current.Activity;
|
|
if (activity == null)
|
|
{
|
|
return Task.FromResult<string>(null);
|
|
}
|
|
|
|
var result = new TaskCompletionSource<string>();
|
|
var alertBuilder = new AlertDialog.Builder(activity);
|
|
alertBuilder.SetTitle(title);
|
|
|
|
if (!string.IsNullOrWhiteSpace(message))
|
|
{
|
|
if (buttons != null && buttons.Length > 2)
|
|
{
|
|
if (!string.IsNullOrWhiteSpace(title))
|
|
{
|
|
alertBuilder.SetTitle($"{title}: {message}");
|
|
}
|
|
else
|
|
{
|
|
alertBuilder.SetTitle(message);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
alertBuilder.SetMessage(message);
|
|
}
|
|
}
|
|
|
|
if (buttons != null)
|
|
{
|
|
if (buttons.Length > 2)
|
|
{
|
|
alertBuilder.SetItems(buttons, (sender, args) =>
|
|
{
|
|
result.TrySetResult(buttons[args.Which]);
|
|
});
|
|
}
|
|
else
|
|
{
|
|
if (buttons.Length > 0)
|
|
{
|
|
alertBuilder.SetPositiveButton(buttons[0], (sender, args) =>
|
|
{
|
|
result.TrySetResult(buttons[0]);
|
|
});
|
|
}
|
|
if (buttons.Length > 1)
|
|
{
|
|
alertBuilder.SetNeutralButton(buttons[1], (sender, args) =>
|
|
{
|
|
result.TrySetResult(buttons[1]);
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
if (!string.IsNullOrWhiteSpace(cancel))
|
|
{
|
|
alertBuilder.SetNegativeButton(cancel, (sender, args) =>
|
|
{
|
|
result.TrySetResult(cancel);
|
|
});
|
|
}
|
|
|
|
var alert = alertBuilder.Create();
|
|
alert.CancelEvent += (o, args) => { result.TrySetResult(null); };
|
|
alert.Show();
|
|
return result.Task;
|
|
}
|
|
|
|
public async Task<string> DisplayActionSheetAsync(string title, string cancel, string destruction,
|
|
params string[] buttons)
|
|
{
|
|
return await Xamarin.Forms.Application.Current.MainPage.DisplayActionSheet(
|
|
title, cancel, destruction, buttons);
|
|
}
|
|
|
|
|
|
public void OpenAccessibilityOverlayPermissionSettings()
|
|
{
|
|
var activity = (MainActivity)CrossCurrentActivity.Current.Activity;
|
|
try
|
|
{
|
|
var intent = new Intent(Settings.ActionManageOverlayPermission);
|
|
intent.SetData(Android.Net.Uri.Parse("package:com.x8bit.bitwarden"));
|
|
activity.StartActivity(intent);
|
|
}
|
|
catch (ActivityNotFoundException)
|
|
{
|
|
// can't open overlay permission management, fall back to app settings
|
|
var intent = new Intent(Settings.ActionApplicationDetailsSettings);
|
|
intent.SetData(Android.Net.Uri.Parse("package:com.x8bit.bitwarden"));
|
|
activity.StartActivity(intent);
|
|
}
|
|
catch
|
|
{
|
|
var alertBuilder = new AlertDialog.Builder(activity);
|
|
alertBuilder.SetMessage(AppResources.BitwardenAutofillGoToSettings);
|
|
alertBuilder.SetCancelable(true);
|
|
alertBuilder.SetPositiveButton(AppResources.Ok, (sender, args) =>
|
|
{
|
|
(sender as AlertDialog)?.Cancel();
|
|
});
|
|
alertBuilder.Create().Show();
|
|
}
|
|
}
|
|
|
|
public void OpenAccessibilitySettings()
|
|
{
|
|
try
|
|
{
|
|
var activity = (MainActivity)CrossCurrentActivity.Current.Activity;
|
|
var intent = new Intent(Settings.ActionAccessibilitySettings);
|
|
activity.StartActivity(intent);
|
|
}
|
|
catch { }
|
|
}
|
|
|
|
public void OpenAutofillSettings()
|
|
{
|
|
var activity = (MainActivity)CrossCurrentActivity.Current.Activity;
|
|
try
|
|
{
|
|
var intent = new Intent(Settings.ActionRequestSetAutofillService);
|
|
intent.SetData(Android.Net.Uri.Parse("package:com.x8bit.bitwarden"));
|
|
activity.StartActivity(intent);
|
|
}
|
|
catch (ActivityNotFoundException)
|
|
{
|
|
var alertBuilder = new AlertDialog.Builder(activity);
|
|
alertBuilder.SetMessage(AppResources.BitwardenAutofillGoToSettings);
|
|
alertBuilder.SetCancelable(true);
|
|
alertBuilder.SetPositiveButton(AppResources.Ok, (sender, args) =>
|
|
{
|
|
(sender as AlertDialog)?.Cancel();
|
|
});
|
|
alertBuilder.Create().Show();
|
|
}
|
|
}
|
|
|
|
public long GetActiveTime()
|
|
{
|
|
// Returns milliseconds since the system was booted, and includes deep sleep. This clock is guaranteed to
|
|
// be monotonic, and continues to tick even when the CPU is in power saving modes, so is the recommend
|
|
// basis for general purpose interval timing.
|
|
// ref: https://developer.android.com/reference/android/os/SystemClock#elapsedRealtime()
|
|
return SystemClock.ElapsedRealtime();
|
|
}
|
|
|
|
public void CloseMainApp()
|
|
{
|
|
var activity = (MainActivity)CrossCurrentActivity.Current.Activity;
|
|
if (activity == null)
|
|
{
|
|
return;
|
|
}
|
|
activity.Finish();
|
|
_messagingService.Send("finishMainActivity");
|
|
}
|
|
|
|
public bool SupportsFido2()
|
|
{
|
|
return true;
|
|
}
|
|
|
|
private Intent RateIntentForUrl(string url, Activity activity)
|
|
{
|
|
var intent = new Intent(Intent.ActionView, 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;
|
|
}
|
|
|
|
public float GetSystemFontSizeScale()
|
|
{
|
|
var activity = CrossCurrentActivity.Current?.Activity as MainActivity;
|
|
return activity?.Resources?.Configuration?.FontScale ?? 1;
|
|
}
|
|
|
|
public async Task OnAccountSwitchCompleteAsync()
|
|
{
|
|
// for any Android-specific cleanup required after switching accounts
|
|
}
|
|
|
|
public async Task SetScreenCaptureAllowedAsync()
|
|
{
|
|
if (CoreHelpers.ForceScreenCaptureEnabled())
|
|
{
|
|
return;
|
|
}
|
|
|
|
var activity = CrossCurrentActivity.Current?.Activity;
|
|
if (await _stateService.GetScreenCaptureAllowedAsync())
|
|
{
|
|
activity.RunOnUiThread(() => activity.Window.ClearFlags(WindowManagerFlags.Secure));
|
|
return;
|
|
}
|
|
activity.RunOnUiThread(() => activity.Window.AddFlags(WindowManagerFlags.Secure));
|
|
}
|
|
|
|
public void OpenAppSettings()
|
|
{
|
|
var intent = new Intent(Android.Provider.Settings.ActionApplicationDetailsSettings);
|
|
intent.AddFlags(ActivityFlags.NewTask);
|
|
var uri = Android.Net.Uri.FromParts("package", Application.Context.PackageName, null);
|
|
intent.SetData(uri);
|
|
Application.Context.StartActivity(intent);
|
|
}
|
|
|
|
public void CloseExtensionPopUp()
|
|
{
|
|
// only used by iOS
|
|
throw new NotImplementedException();
|
|
}
|
|
}
|
|
}
|