Deprecate XF.Context and give context to renderers

This commit is contained in:
Kyle Spearrin 2017-12-22 11:23:03 -05:00
parent d0257df134
commit 7f431dbd01
16 changed files with 731 additions and 653 deletions

View file

@ -1,5 +1,6 @@
using System; using System;
using System.ComponentModel; using System.ComponentModel;
using Android.Content;
using Bit.Android.Controls; using Bit.Android.Controls;
using Xamarin.Forms; using Xamarin.Forms;
using Xamarin.Forms.Platform.Android; using Xamarin.Forms.Platform.Android;
@ -9,6 +10,10 @@ namespace Bit.Android.Controls
{ {
public class CustomButtonRenderer : ButtonRenderer public class CustomButtonRenderer : ButtonRenderer
{ {
public CustomButtonRenderer(Context context)
: base(context)
{ }
protected override void OnElementChanged(ElementChangedEventArgs<Button> e) protected override void OnElementChanged(ElementChangedEventArgs<Button> e)
{ {
base.OnElementChanged(e); base.OnElementChanged(e);

View file

@ -1,4 +1,5 @@
using Bit.Android.Controls; using Android.Content;
using Bit.Android.Controls;
using Xamarin.Forms; using Xamarin.Forms;
using Xamarin.Forms.Platform.Android; using Xamarin.Forms.Platform.Android;
@ -7,6 +8,10 @@ namespace Bit.Android.Controls
{ {
public class CustomLabelRenderer : LabelRenderer public class CustomLabelRenderer : LabelRenderer
{ {
public CustomLabelRenderer(Context context)
: base(context)
{ }
protected override void OnElementChanged(ElementChangedEventArgs<Label> e) protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
{ {
base.OnElementChanged(e); base.OnElementChanged(e);

View file

@ -1,5 +1,6 @@
using System; using System;
using System.ComponentModel; using System.ComponentModel;
using Android.Content;
using Bit.Android.Controls; using Bit.Android.Controls;
using Xamarin.Forms; using Xamarin.Forms;
using Xamarin.Forms.Platform.Android; using Xamarin.Forms.Platform.Android;
@ -9,6 +10,10 @@ namespace Bit.Android.Controls
{ {
public class CustomSearchBarRenderer : SearchBarRenderer public class CustomSearchBarRenderer : SearchBarRenderer
{ {
public CustomSearchBarRenderer(Context context)
: base(context)
{ }
protected override void OnElementChanged(ElementChangedEventArgs<SearchBar> e) protected override void OnElementChanged(ElementChangedEventArgs<SearchBar> e)
{ {
base.OnElementChanged(e); base.OnElementChanged(e);

View file

@ -1,5 +1,6 @@
using System; using System;
using System.ComponentModel; using System.ComponentModel;
using Android.Content;
using Bit.Android.Controls; using Bit.Android.Controls;
using Bit.App.Controls; using Bit.App.Controls;
using Xamarin.Forms; using Xamarin.Forms;
@ -10,6 +11,10 @@ namespace Bit.Android.Controls
{ {
public class ExtendedButtonRenderer : CustomButtonRenderer public class ExtendedButtonRenderer : CustomButtonRenderer
{ {
public ExtendedButtonRenderer(Context context)
: base(context)
{ }
protected override void OnElementChanged(ElementChangedEventArgs<Button> e) protected override void OnElementChanged(ElementChangedEventArgs<Button> e)
{ {
base.OnElementChanged(e); base.OnElementChanged(e);

View file

@ -6,12 +6,17 @@ using Xamarin.Forms;
using Xamarin.Forms.Platform.Android; using Xamarin.Forms.Platform.Android;
using Android.Text.Method; using Android.Text.Method;
using Android.Views; using Android.Views;
using Android.Content;
[assembly: ExportRenderer(typeof(ExtendedEditor), typeof(ExtendedEditorRenderer))] [assembly: ExportRenderer(typeof(ExtendedEditor), typeof(ExtendedEditorRenderer))]
namespace Bit.Android.Controls namespace Bit.Android.Controls
{ {
public class ExtendedEditorRenderer : EditorRenderer public class ExtendedEditorRenderer : EditorRenderer
{ {
public ExtendedEditorRenderer(Context context)
: base(context)
{ }
protected override void OnElementChanged(ElementChangedEventArgs<Editor> e) protected override void OnElementChanged(ElementChangedEventArgs<Editor> e)
{ {
base.OnElementChanged(e); base.OnElementChanged(e);

View file

@ -1,5 +1,6 @@
using System; using System;
using System.ComponentModel; using System.ComponentModel;
using Android.Content;
using Android.Graphics; using Android.Graphics;
using Android.Text; using Android.Text;
using Android.Text.Method; using Android.Text.Method;
@ -8,6 +9,7 @@ using Android.Widget;
using Bit.Android.Controls; using Bit.Android.Controls;
using Bit.App.Controls; using Bit.App.Controls;
using Bit.App.Enums; using Bit.App.Enums;
using Plugin.CurrentActivity;
using Xamarin.Forms; using Xamarin.Forms;
using Xamarin.Forms.Platform.Android; using Xamarin.Forms.Platform.Android;
@ -16,6 +18,10 @@ namespace Bit.Android.Controls
{ {
public class ExtendedEntryRenderer : EntryRenderer public class ExtendedEntryRenderer : EntryRenderer
{ {
public ExtendedEntryRenderer(Context context)
: base(context)
{ }
private bool _isPassword; private bool _isPassword;
private bool _toggledPassword; private bool _toggledPassword;
private bool _isDisposed; private bool _isDisposed;
@ -96,7 +102,7 @@ namespace Bit.Android.Controls
} }
// show keyboard // show keyboard
var imm = Forms.Context.GetSystemService(global::Android.Content.Context.InputMethodService) as InputMethodManager; var imm = CrossCurrentActivity.Current.Activity.GetSystemService(Context.InputMethodService) as InputMethodManager;
imm.ShowSoftInput(Control, ShowFlags.Forced); imm.ShowSoftInput(Control, ShowFlags.Forced);
imm.ToggleSoftInput(ShowFlags.Forced, HideSoftInputFlags.ImplicitOnly); imm.ToggleSoftInput(ShowFlags.Forced, HideSoftInputFlags.ImplicitOnly);

View file

@ -1,5 +1,6 @@
using System; using System;
using System.ComponentModel; using System.ComponentModel;
using Android.Content;
using Bit.Android.Controls; using Bit.Android.Controls;
using Bit.App.Controls; using Bit.App.Controls;
using Xamarin.Forms; using Xamarin.Forms;
@ -10,6 +11,10 @@ namespace Bit.Android.Controls
{ {
public class ExtendedPickerRenderer : PickerRenderer public class ExtendedPickerRenderer : PickerRenderer
{ {
public ExtendedPickerRenderer(Context context)
: base(context)
{ }
protected override void OnElementChanged(ElementChangedEventArgs<Picker> e) protected override void OnElementChanged(ElementChangedEventArgs<Picker> e)
{ {
base.OnElementChanged(e); base.OnElementChanged(e);

View file

@ -7,6 +7,7 @@ using Android.Support.Design.Widget;
using Xamarin.Forms.Platform.Android.AppCompat; using Xamarin.Forms.Platform.Android.AppCompat;
using System.Reflection; using System.Reflection;
using System.Linq; using System.Linq;
using Android.Content;
[assembly: ExportRenderer(typeof(ExtendedTabbedPage), typeof(ExtendedTabbedPageRenderer))] [assembly: ExportRenderer(typeof(ExtendedTabbedPage), typeof(ExtendedTabbedPageRenderer))]
namespace Bit.Android.Controls namespace Bit.Android.Controls
@ -15,6 +16,10 @@ namespace Bit.Android.Controls
{ {
private TabLayout _tabLayout; private TabLayout _tabLayout;
public ExtendedTabbedPageRenderer(Context context)
: base(context)
{ }
protected override void OnElementChanged(ElementChangedEventArgs<TabbedPage> e) protected override void OnElementChanged(ElementChangedEventArgs<TabbedPage> e)
{ {
base.OnElementChanged(e); base.OnElementChanged(e);

View file

@ -14,6 +14,10 @@ namespace Bit.Android.Controls
{ {
public class ExtendedTableViewRenderer : TableViewRenderer public class ExtendedTableViewRenderer : TableViewRenderer
{ {
public ExtendedTableViewRenderer(Context context)
: base(context)
{ }
protected override void OnElementChanged(ElementChangedEventArgs<TableView> e) protected override void OnElementChanged(ElementChangedEventArgs<TableView> e)
{ {
base.OnElementChanged(e); base.OnElementChanged(e);

View file

@ -6,6 +6,8 @@ using Xamarin.Forms.Platform.Android;
using Android.Webkit; using Android.Webkit;
using AWebkit = Android.Webkit; using AWebkit = Android.Webkit;
using Java.Interop; using Java.Interop;
using Android.Content;
using Plugin.CurrentActivity;
[assembly: ExportRenderer(typeof(HybridWebView), typeof(HybridWebViewRenderer))] [assembly: ExportRenderer(typeof(HybridWebView), typeof(HybridWebViewRenderer))]
namespace Bit.Android.Controls namespace Bit.Android.Controls
@ -14,13 +16,17 @@ namespace Bit.Android.Controls
{ {
private const string JSFunction = "function invokeCSharpAction(data){jsBridge.invokeAction(data);}"; private const string JSFunction = "function invokeCSharpAction(data){jsBridge.invokeAction(data);}";
public HybridWebViewRenderer(Context context)
: base(context)
{ }
protected override void OnElementChanged(ElementChangedEventArgs<HybridWebView> e) protected override void OnElementChanged(ElementChangedEventArgs<HybridWebView> e)
{ {
base.OnElementChanged(e); base.OnElementChanged(e);
if(Control == null) if(Control == null)
{ {
var webView = new AWebkit.WebView(Forms.Context); var webView = new AWebkit.WebView(CrossCurrentActivity.Current.Activity);
webView.Settings.JavaScriptEnabled = true; webView.Settings.JavaScriptEnabled = true;
SetNativeControl(webView); SetNativeControl(webView);
} }

File diff suppressed because it is too large Load diff

View file

@ -89,7 +89,7 @@ namespace Bit.Android.Services
{ {
return App.Utilities.Crypto.AesCbcDecrypt(new App.Models.CipherString(cs), aesKey); return App.Utilities.Crypto.AesCbcDecrypt(new App.Models.CipherString(cs), aesKey);
} }
catch(Exception e) catch
{ {
Console.WriteLine("Failed to decrypt from secure storage."); Console.WriteLine("Failed to decrypt from secure storage.");
_settings.Remove(formattedKey); _settings.Remove(formattedKey);
@ -120,7 +120,7 @@ namespace Bit.Android.Services
var cipherString = App.Utilities.Crypto.AesCbcEncrypt(dataBytes, aesKey); var cipherString = App.Utilities.Crypto.AesCbcEncrypt(dataBytes, aesKey);
_settings.AddOrUpdateValue(formattedKey, cipherString.EncryptedString); _settings.AddOrUpdateValue(formattedKey, cipherString.EncryptedString);
} }
catch(Exception e) catch
{ {
Console.WriteLine("Failed to encrypt to secure storage."); Console.WriteLine("Failed to encrypt to secure storage.");
//Utilities.SendCrashEmail(e); //Utilities.SendCrashEmail(e);
@ -224,7 +224,7 @@ namespace Bit.Android.Services
return new App.Models.SymmetricCryptoKey(key); return new App.Models.SymmetricCryptoKey(key);
} }
} }
catch(Exception e) catch
{ {
Console.WriteLine("Cannot get AesKey."); Console.WriteLine("Cannot get AesKey.");
_keyStore.DeleteEntry(KeyAlias); _keyStore.DeleteEntry(KeyAlias);

View file

@ -1,6 +1,7 @@
using Android.App; using Android.App;
using Android.Views.Autofill; using Android.Views.Autofill;
using Bit.App.Abstractions; using Bit.App.Abstractions;
using Plugin.CurrentActivity;
using System.Linq; using System.Linq;
using AndroidApp = Android.App.Application; using AndroidApp = Android.App.Application;
@ -19,7 +20,7 @@ namespace Bit.Android.Services
private bool AutofillAccessibilityRunning() private bool AutofillAccessibilityRunning()
{ {
var manager = ((ActivityManager)Xamarin.Forms.Forms.Context.GetSystemService("activity")); var manager = ((ActivityManager)CrossCurrentActivity.Current.Activity.GetSystemService("activity"));
var services = manager.GetRunningServices(int.MaxValue); var services = manager.GetRunningServices(int.MaxValue);
return services.Any(s => s.Process.ToLowerInvariant().Contains("bitwarden") && return services.Any(s => s.Process.ToLowerInvariant().Contains("bitwarden") &&
s.Service.ClassName.ToLowerInvariant().Contains("autofill")); s.Service.ClassName.ToLowerInvariant().Contains("autofill"));
@ -32,7 +33,7 @@ namespace Bit.Android.Services
return false; return false;
} }
var activity = (MainActivity)Xamarin.Forms.Forms.Context; 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; return afm.IsEnabled;
} }

View file

@ -43,9 +43,11 @@ namespace Bit.Android.Services
_userDialogs = userDialogs; _userDialogs = userDialogs;
} }
private Context CurrentContext => CrossCurrentActivity.Current.Activity;
public void CopyToClipboard(string text) public void CopyToClipboard(string text)
{ {
var clipboardManager = (ClipboardManager)Forms.Context.GetSystemService(Context.ClipboardService); var clipboardManager = (ClipboardManager)CurrentContext.GetSystemService(Context.ClipboardService);
clipboardManager.Text = text; clipboardManager.Text = text;
} }
@ -131,7 +133,7 @@ namespace Bit.Android.Services
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(Forms.Context.PackageManager.HasSystemFeature(PackageManager.FeatureCamera)) if(CurrentContext.PackageManager.HasSystemFeature(PackageManager.FeatureCamera))
{ {
var hasCameraPermission = !_cameraPermissionsDenied && HasPermission(Manifest.Permission.Camera); var hasCameraPermission = !_cameraPermissionsDenied && HasPermission(Manifest.Permission.Camera);
@ -181,7 +183,7 @@ namespace Bit.Android.Services
public void Autofill(VaultListPageModel.Cipher cipher) public void Autofill(VaultListPageModel.Cipher cipher)
{ {
var activity = (MainActivity)Forms.Context; var activity = (MainActivity)CurrentContext;
if(activity.Intent.GetBooleanExtra("autofillFramework", false)) if(activity.Intent.GetBooleanExtra("autofillFramework", false))
{ {
if(cipher == null) if(cipher == null)
@ -259,7 +261,7 @@ namespace Bit.Android.Services
public void Background() public void Background()
{ {
var activity = (MainActivity)Forms.Context; var activity = (MainActivity)CurrentContext;
if(activity.Intent.GetBooleanExtra("autofillFramework", false)) if(activity.Intent.GetBooleanExtra("autofillFramework", false))
{ {
activity.SetResult(Result.Canceled); activity.SetResult(Result.Canceled);
@ -273,7 +275,7 @@ namespace Bit.Android.Services
public void RateApp() public void RateApp()
{ {
var activity = (MainActivity)Forms.Context; var activity = (MainActivity)CurrentContext;
try try
{ {
var rateIntent = RateIntentForUrl("market://details", activity); var rateIntent = RateIntentForUrl("market://details", activity);
@ -288,7 +290,7 @@ namespace Bit.Android.Services
public void DismissKeyboard() public void DismissKeyboard()
{ {
var activity = (MainActivity)Forms.Context; var activity = (MainActivity)CurrentContext;
try try
{ {
var imm = (InputMethodManager)activity.GetSystemService(Context.InputMethodService); var imm = (InputMethodManager)activity.GetSystemService(Context.InputMethodService);
@ -299,14 +301,14 @@ namespace Bit.Android.Services
public void OpenAccessibilitySettings() public void OpenAccessibilitySettings()
{ {
var activity = (MainActivity)Forms.Context; var activity = (MainActivity)CurrentContext;
var intent = new Intent(Settings.ActionAccessibilitySettings); var intent = new Intent(Settings.ActionAccessibilitySettings);
activity.StartActivity(intent); activity.StartActivity(intent);
} }
public void LaunchApp(string appName) public void LaunchApp(string appName)
{ {
var activity = (MainActivity)Forms.Context; var activity = (MainActivity)CurrentContext;
if(_lastAction.LastActionWasRecent()) if(_lastAction.LastActionWasRecent())
{ {
return; return;
@ -410,7 +412,7 @@ namespace Bit.Android.Services
public void OpenAutofillSettings() public void OpenAutofillSettings()
{ {
var activity = (MainActivity)Forms.Context; var activity = (MainActivity)CurrentContext;
var intent = new Intent(Settings.ActionRequestSetAutofillService); var intent = new Intent(Settings.ActionRequestSetAutofillService);
intent.SetData(global::Android.Net.Uri.Parse("package:com.x8bit.bitwarden")); intent.SetData(global::Android.Net.Uri.Parse("package:com.x8bit.bitwarden"));
activity.StartActivity(intent); activity.StartActivity(intent);

View file

@ -3,6 +3,7 @@ using Android.Content.PM;
using Android.OS; using Android.OS;
using Android.Views.Autofill; using Android.Views.Autofill;
using Bit.App.Abstractions; using Bit.App.Abstractions;
using Plugin.CurrentActivity;
namespace Bit.Android.Services namespace Bit.Android.Services
{ {
@ -45,7 +46,8 @@ namespace Bit.Android.Services
} }
} }
public bool NfcEnabled => Utilities.NfcEnabled(); public bool NfcEnabled => Utilities.NfcEnabled();
public bool HasCamera => Xamarin.Forms.Forms.Context.PackageManager.HasSystemFeature(PackageManager.FeatureCamera); public bool HasCamera => CrossCurrentActivity.Current.Activity.PackageManager.HasSystemFeature(
PackageManager.FeatureCamera);
public bool AutofillServiceSupported => AutofillSupported(); public bool AutofillServiceSupported => AutofillSupported();
public bool HasFaceIdSupport => false; public bool HasFaceIdSupport => false;
private bool AutofillSupported() private bool AutofillSupported()
@ -55,8 +57,8 @@ namespace Bit.Android.Services
return false; return false;
} }
var activity = (MainActivity)Xamarin.Forms.Forms.Context; var afm = (AutofillManager)CrossCurrentActivity.Current.Activity.GetSystemService(
var afm = (AutofillManager)activity.GetSystemService(Java.Lang.Class.FromType(typeof(AutofillManager))); Java.Lang.Class.FromType(typeof(AutofillManager)));
return afm.IsAutofillSupported; return afm.IsAutofillSupported;
} }
} }

View file

@ -1,6 +1,7 @@
using System; using System;
using Android.Content; using Android.Content;
using Bit.App.Abstractions; using Bit.App.Abstractions;
using Plugin.CurrentActivity;
using Xamarin.Forms; using Xamarin.Forms;
namespace Bit.Android.Services namespace Bit.Android.Services
@ -9,12 +10,12 @@ namespace Bit.Android.Services
{ {
public MemoryInfo GetInfo() public MemoryInfo GetInfo()
{ {
return MemoryHelper.GetMemoryInfo(Forms.Context); return MemoryHelper.GetMemoryInfo(CrossCurrentActivity.Current.Activity);
} }
public void Check() public void Check()
{ {
MemoryHelper.MemoryCheck(Forms.Context); MemoryHelper.MemoryCheck(CrossCurrentActivity.Current.Activity);
} }
public static class MemoryHelper public static class MemoryHelper