get rid of old refection and memory services

This commit is contained in:
Kyle Spearrin 2018-01-02 16:41:06 -05:00
parent 7261fd7ed9
commit fa9e22730a
14 changed files with 28 additions and 286 deletions

View file

@ -125,8 +125,6 @@
<Compile Include="MainActivity.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Services\LogService.cs" />
<Compile Include="Services\MemoryService.cs" />
<Compile Include="Services\ReflectionService.cs" />
<Compile Include="Services\SqlService.cs" />
<Compile Include="SplashActivity.cs" />
<Compile Include="PackageReplacedReceiver.cs" />

View file

@ -127,7 +127,6 @@ namespace Bit.Android
container.RegisterSingleton<IDeviceActionService, DeviceActionService>();
container.RegisterSingleton<IAppIdService, AppIdService>();
container.RegisterSingleton<IPasswordGenerationService, PasswordGenerationService>();
container.RegisterSingleton<IReflectionService, ReflectionService>();
container.RegisterSingleton<ILockService, LockService>();
container.RegisterSingleton<IAppInfoService, AppInfoService>();
container.RegisterSingleton<IGoogleAnalyticsService, GoogleAnalyticsService>();
@ -137,7 +136,6 @@ namespace Bit.Android
container.RegisterSingleton<IHttpService, HttpService>();
container.RegisterSingleton<ITokenService, TokenService>();
container.RegisterSingleton<ISettingsService, SettingsService>();
container.RegisterSingleton<IMemoryService, MemoryService>();
container.RegisterSingleton<IAppSettingsService, AppSettingsService>();
// Repositories

View file

@ -1,45 +0,0 @@
using System;
using Android.Content;
using Bit.App.Abstractions;
using Plugin.CurrentActivity;
using Xamarin.Forms;
namespace Bit.Android.Services
{
public class MemoryService : IMemoryService
{
public MemoryInfo GetInfo()
{
return MemoryHelper.GetMemoryInfo(CrossCurrentActivity.Current.Activity);
}
public void Check()
{
MemoryHelper.MemoryCheck(CrossCurrentActivity.Current.Activity);
}
public static class MemoryHelper
{
public static void MemoryCheck(Context context)
{
Console.WriteLine("MemoryHelper.MemoryCheck.{0} - {1}", "Start", context.ToString());
var maxMemory = Java.Lang.Runtime.GetRuntime().MaxMemory();
var freeMemory = Java.Lang.Runtime.GetRuntime().FreeMemory();
var percentUsed = (maxMemory - freeMemory) / (double)maxMemory;
Console.WriteLine("Free memory: {0:N}", freeMemory);
Console.WriteLine("Max memory: {0:N}", maxMemory);
Console.WriteLine("% used: {0:P}", percentUsed);
Console.WriteLine("MemoryHelper.MemoryCheck.{0} {3:P} {1} out of {2}", "End", freeMemory, maxMemory, percentUsed);
}
public static MemoryInfo GetMemoryInfo(Context context)
{
var retVal = new MemoryInfo();
retVal.MaxMemory = Java.Lang.Runtime.GetRuntime().MaxMemory();
retVal.FreeMemory = Java.Lang.Runtime.GetRuntime().FreeMemory();
retVal.TotalMemory = Java.Lang.Runtime.GetRuntime().TotalMemory();
return retVal;
}
}
}
}

View file

@ -1,34 +0,0 @@
using System;
using System.Reflection;
using Bit.App.Abstractions;
using Bit.App.Controls;
using Xamarin.Forms;
namespace Bit.Android.Services
{
public class ReflectionService : IReflectionService
{
public Func<double, double, SizeRequest> GetVisualElementOnSizeRequest(ExtendedTableView tableView)
{
var handle = typeof(VisualElement).GetMethod(
"OnSizeRequest",
BindingFlags.Instance | BindingFlags.NonPublic,
null,
new Type[] { typeof(double), typeof(double) },
null)?.MethodHandle;
if(!handle.HasValue)
{
throw new ArgumentNullException("handle could not be found.");
}
var pointer = handle.Value.GetFunctionPointer();
if(pointer == null)
{
throw new ArgumentNullException("pointer could not be found.");
}
return (Func<double, double, SizeRequest>)Activator.CreateInstance(typeof(Func<double, double, SizeRequest>), tableView, pointer);
}
}
}

View file

@ -1,26 +0,0 @@
namespace Bit.App.Abstractions
{
public interface IMemoryService
{
MemoryInfo GetInfo();
void Check();
}
public class MemoryInfo
{
public long FreeMemory { get; set; }
public long MaxMemory { get; set; }
public long TotalMemory { get; set; }
public long UsedMemory => TotalMemory - FreeMemory;
public double HeapUsage()
{
return UsedMemory / (double)TotalMemory;
}
public double Usage()
{
return UsedMemory / (double)MaxMemory;
}
}
}

View file

@ -1,11 +0,0 @@
using System;
using Bit.App.Controls;
using Xamarin.Forms;
namespace Bit.App.Abstractions
{
public interface IReflectionService
{
Func<double, double, SizeRequest> GetVisualElementOnSizeRequest(ExtendedTableView tableView);
}
}

View file

@ -1,6 +1,8 @@
using Xamarin.Forms;
using XLabs.Ioc;
using Bit.App.Abstractions;
using System;
using System.Reflection;
namespace Bit.App.Controls
{
@ -45,12 +47,35 @@ namespace Bit.App.Controls
{
if(!VerticalOptions.Expands && Device.RuntimePlatform != Device.UWP)
{
var reflectionService = Resolver.Resolve<IReflectionService>();
var baseBaseOnSizeRequest = reflectionService.GetVisualElementOnSizeRequest(this);
return baseBaseOnSizeRequest(widthConstraint, heightConstraint);
var baseOnSizeRequest = GetVisualElementOnSizeRequest();
return baseOnSizeRequest(widthConstraint, heightConstraint);
}
return base.OnSizeRequest(widthConstraint, heightConstraint);
}
private Func<double, double, SizeRequest> GetVisualElementOnSizeRequest()
{
var handle = typeof(VisualElement).GetMethod(
"OnSizeRequest",
BindingFlags.Instance | BindingFlags.NonPublic,
null,
new Type[] { typeof(double), typeof(double) },
null)?.MethodHandle;
if(!handle.HasValue)
{
throw new ArgumentNullException("handle could not be found.");
}
var pointer = handle.Value.GetFunctionPointer();
if(pointer == null)
{
throw new ArgumentNullException("pointer could not be found.");
}
return (Func<double, double, SizeRequest>)Activator.CreateInstance(
typeof(Func<double, double, SizeRequest>), this, pointer);
}
}
}

View file

@ -1,108 +0,0 @@
using Bit.App.Abstractions;
using System;
using Xamarin.Forms;
using XLabs.Ioc;
namespace Bit.App.Controls
{
public class MemoryContentView : ContentView
{
private readonly IMemoryService _memoryService;
public MemoryContentView()
{
_memoryService = Resolver.Resolve<IMemoryService>();
var grid = new Grid
{
Padding = 5,
BackgroundColor = Color.White,
RowDefinitions = new RowDefinitionCollection
{
new RowDefinition { Height = GridLength.Auto },
new RowDefinition { Height = GridLength.Auto },
new RowDefinition { Height = GridLength.Auto },
new RowDefinition { Height = GridLength.Auto },
new RowDefinition { Height = GridLength.Auto },
new RowDefinition { Height = GridLength.Auto },
new RowDefinition { Height = GridLength.Auto }
},
ColumnDefinitions = new ColumnDefinitionCollection
{
new ColumnDefinition { Width = GridLength.Star },
new ColumnDefinition { Width = GridLength.Star }
}
};
grid.Children.Add(new Label { Text = "Used Memory:" }, 0, 0);
grid.Children.Add(new Label { Text = "Free Memory:" }, 0, 1);
grid.Children.Add(new Label { Text = "Heap Memory:" }, 0, 2);
grid.Children.Add(new Label { Text = "Max Memory:" }, 0, 3);
grid.Children.Add(new Label { Text = "% Used Heap:" }, 0, 4);
grid.Children.Add(new Label { Text = "% Used Max:" }, 0, 5);
UsedMemory = new Label { Text = "Used Memory:", HorizontalTextAlignment = TextAlignment.End };
FreeMemory = new Label { Text = "Free Memory:", HorizontalTextAlignment = TextAlignment.End };
HeapMemory = new Label { Text = "Heap Memory:", HorizontalTextAlignment = TextAlignment.End };
MaxMemory = new Label { Text = "Max Memory:", HorizontalTextAlignment = TextAlignment.End };
HeapUsage = new Label { Text = "% Used Heap:", HorizontalTextAlignment = TextAlignment.End };
TotalUsage = new Label { Text = "% Used Max:", HorizontalTextAlignment = TextAlignment.End };
grid.Children.Add(UsedMemory, 1, 0);
grid.Children.Add(FreeMemory, 1, 1);
grid.Children.Add(HeapMemory, 1, 2);
grid.Children.Add(MaxMemory, 1, 3);
grid.Children.Add(HeapUsage, 1, 4);
grid.Children.Add(TotalUsage, 1, 5);
var button = new ExtendedButton { Text = "Refresh", BackgroundColor = Color.Transparent };
button.Clicked += Button_Clicked;
grid.Children.Add(button, 0, 6);
Grid.SetColumnSpan(button, 2);
Content = grid;
RefreshScreen();
}
private void Button_Clicked(object sender, EventArgs e)
{
RefreshScreen();
}
public Label UsedMemory { get; set; }
public Label FreeMemory { get; set; }
public Label HeapMemory { get; set; }
public Label MaxMemory { get; set; }
public Label HeapUsage { get; set; }
public Label TotalUsage { get; set; }
void RefreshScreen()
{
UsedMemory.Text = FreeMemory.Text = HeapMemory.Text = MaxMemory.Text =
HeapUsage.Text = TotalUsage.Text = string.Empty;
UsedMemory.TextColor = FreeMemory.TextColor = HeapMemory.TextColor =
MaxMemory.TextColor = HeapUsage.TextColor = TotalUsage.TextColor = Color.Black;
if(_memoryService != null)
{
var info = _memoryService.GetInfo();
if(info != null)
{
UsedMemory.Text = string.Format("{0:N} mb", Math.Round(info.UsedMemory / 1024 / 1024D, 2));
FreeMemory.Text = string.Format("{0:N} mb", Math.Round(info.FreeMemory / 1024 / 1024D, 2));
HeapMemory.Text = string.Format("{0:N} mb", Math.Round(info.TotalMemory / 1024 / 1024D, 2));
MaxMemory.Text = string.Format("{0:N} mb", Math.Round(info.MaxMemory / 1024 / 1024D, 2));
HeapUsage.Text = string.Format("{0:P}", info.HeapUsage());
TotalUsage.Text = string.Format("{0:P}", info.Usage());
if(info.Usage() > 0.8)
{
FreeMemory.TextColor = UsedMemory.TextColor = TotalUsage.TextColor = Color.Red;
}
}
}
}
}
}

View file

@ -114,7 +114,6 @@ namespace Bit.UWP
container.RegisterSingleton<ITokenService, TokenService>();
container.RegisterSingleton<ISettingsService, SettingsService>();
container.RegisterSingleton<IAppSettingsService, AppSettingsService>();
container.RegisterSingleton<IReflectionService, ReflectionService>();
// Repositories
container.RegisterSingleton<IFolderRepository, FolderRepository>();

View file

@ -1,17 +0,0 @@
using System;
using System.Reflection;
using Bit.App.Abstractions;
using Bit.App.Controls;
using Xamarin.Forms;
namespace Bit.UWP.Services
{
public class ReflectionService : IReflectionService
{
public Func<double, double, SizeRequest> GetVisualElementOnSizeRequest(ExtendedTableView tableView)
{
// TODO
throw new NotImplementedException();
}
}
}

View file

@ -106,7 +106,6 @@
<Compile Include="Services\GoogleAnalyticsService.cs" />
<Compile Include="Services\HttpService.cs" />
<Compile Include="Services\UwpPushNotificationService.cs" />
<Compile Include="Services\ReflectionService.cs" />
<Compile Include="Services\SecureStorageService.cs" />
<Compile Include="Services\KeyDerivationService.cs" />
<Compile Include="Services\LocalizeService.cs" />

View file

@ -249,7 +249,6 @@ namespace Bit.iOS
container.RegisterSingleton<IDeviceActionService, DeviceActionService>();
container.RegisterSingleton<IAppIdService, AppIdService>();
container.RegisterSingleton<IPasswordGenerationService, PasswordGenerationService>();
container.RegisterSingleton<IReflectionService, ReflectionService>();
container.RegisterSingleton<ILockService, LockService>();
container.RegisterSingleton<IAppInfoService, AppInfoService>();
container.RegisterSingleton<IGoogleAnalyticsService, GoogleAnalyticsService>();

View file

@ -1,34 +0,0 @@
using System;
using System.Reflection;
using Bit.App.Abstractions;
using Bit.App.Controls;
using Xamarin.Forms;
namespace Bit.iOS.Services
{
public class ReflectionService : IReflectionService
{
public Func<double, double, SizeRequest> GetVisualElementOnSizeRequest(ExtendedTableView tableView)
{
var handle = typeof(VisualElement).GetMethod(
"OnSizeRequest",
BindingFlags.Instance | BindingFlags.NonPublic,
null,
new Type[] { typeof(double), typeof(double) },
null)?.MethodHandle;
if(!handle.HasValue)
{
throw new ArgumentNullException("handle could not be found.");
}
var pointer = handle.Value.GetFunctionPointer();
if(pointer == null)
{
throw new ArgumentNullException("pointer could not be found.");
}
return (Func<double, double, SizeRequest>)Activator.CreateInstance(typeof(Func<double, double, SizeRequest>), tableView, pointer);
}
}
}

View file

@ -238,7 +238,6 @@
<Compile Include="AppDelegate.cs" />
<Compile Include="Services\iOSPushNotificationHandler.cs" />
<Compile Include="Services\iOSPushNotificationService.cs" />
<Compile Include="Services\ReflectionService.cs" />
<None Include="Entitlements.plist" />
<None Include="Info.plist" />
<Compile Include="Properties\AssemblyInfo.cs" />