Use DI to implement battery detection, add BatteryCutoff property

- Removed the Xamarin.Essentials package from osu.Game and added it to osu.iOS and osu.Android only.
- iOS and Android implementations use Xamarin.Essentials.Battery, while the Desktop implementation
only returns 100% battery for now.
- Added a BatteryCutoff property to PowerStatus so it can be different for each platform (default 20%, 25% on iOS)
This commit is contained in:
Christine Chen
2021-04-08 19:34:35 -04:00
parent 0a6baf670e
commit 6bccb3aab6
10 changed files with 103 additions and 79 deletions

View File

@ -40,6 +40,7 @@ using osu.Game.Rulesets;
using osu.Game.Rulesets.Mods;
using osu.Game.Scoring;
using osu.Game.Skinning;
using osu.Game.Utils;
using osuTK.Input;
using RuntimeInfo = osu.Framework.RuntimeInfo;
@ -95,6 +96,8 @@ namespace osu.Game
protected Storage Storage { get; set; }
protected PowerStatus powerStatus;
[Cached]
[Cached(typeof(IBindable<RulesetInfo>))]
protected readonly Bindable<RulesetInfo> Ruleset = new Bindable<RulesetInfo>();
@ -329,6 +332,8 @@ namespace osu.Game
dependencies.CacheAs(MusicController);
Ruleset.BindValueChanged(onRulesetChanged);
dependencies.CacheAs(powerStatus);
}
private void onRulesetChanged(ValueChangedEvent<RulesetInfo> r)

View File

@ -24,9 +24,9 @@ using osu.Game.Overlays.Notifications;
using osu.Game.Screens.Menu;
using osu.Game.Screens.Play.PlayerSettings;
using osu.Game.Users;
using osu.Game.Utils;
using osuTK;
using osuTK.Graphics;
using Xamarin.Essentials;
namespace osu.Game.Screens.Play
{
@ -113,6 +113,9 @@ namespace osu.Game.Screens.Play
[Resolved]
private AudioManager audioManager { get; set; }
[Resolved]
private PowerStatus powerStatus { get; set; }
public PlayerLoader(Func<Player> createPlayer)
{
this.createPlayer = createPlayer;
@ -265,7 +268,6 @@ namespace osu.Game.Screens.Play
}
#endregion
protected override void Update()
{
base.Update();
@ -477,45 +479,18 @@ namespace osu.Game.Screens.Play
#region Low battery warning
private Bindable<bool> batteryWarningShownOnce;
// Send a warning if battery is less than 20%
public const double battery_tolerance = 0.2;
private void showBatteryWarningIfNeeded()
{
if (!batteryWarningShownOnce.Value)
{
// Checks if the notification has not been shown yet, device is unplugged and if device battery is low.
if (!batteryManager.PluggedIn && batteryManager.ChargeLevel < battery_tolerance)
// Checks if the notification has not been shown yet, device is unplugged and if device battery is at or below the cutoff
if (!powerStatus.IsCharging && powerStatus.ChargeLevel <= powerStatus.BatteryCutoff)
{
Console.WriteLine("Battery level: {0}", batteryManager.ChargeLevel);
notificationOverlay?.Post(new BatteryWarningNotification());
batteryWarningShownOnce.Value = true;
}
}
}
public BatteryManager batteryManager = new BatteryManager();
public class BatteryManager
{
public double ChargeLevel;
public bool PluggedIn;
public BatteryManager()
{
// Attempt to get battery information using Xamarin.Essentials
// Xamarin.Essentials throws a NotSupportedOrImplementedException on .NET standard so this only works on iOS/Android
// https://github.com/xamarin/Essentials/blob/7218ab88f7fbe00ec3379bd54e6c0ce35ffc0c22/Xamarin.Essentials/Battery/Battery.netstandard.tvos.cs
try
{
ChargeLevel = Battery.ChargeLevel;
PluggedIn = Battery.PowerSource == BatteryPowerSource.Battery;
}
catch (NotImplementedException e)
{
Console.WriteLine("Could not access battery info: {0}", e);
ChargeLevel = 1.0;
PluggedIn = false;
}
}
}
private class BatteryWarningNotification : SimpleNotification
{

View File

@ -7,11 +7,16 @@ using osu.Framework.Screens;
using osu.Framework.Testing;
using osu.Game.Graphics;
using osu.Game.Screens.Backgrounds;
using osu.Game.Utils;
namespace osu.Game.Tests
{
public class OsuTestBrowser : OsuGameBase
{
public OsuTestBrowser()
{
powerStatus = new DefaultPowerStatus();
}
protected override void LoadComplete()
{
base.LoadComplete();

View File

@ -0,0 +1,22 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
namespace osu.Game.Utils
{
public abstract class PowerStatus
{
/// <summary>
/// The maximum battery level before a warning notification
/// is sent.
/// </summary>
public virtual double BatteryCutoff { get; } = 0.2;
public virtual double ChargeLevel { get; set; }
public virtual bool IsCharging { get; set; }
}
public class DefaultPowerStatus : PowerStatus
{
public override double ChargeLevel { get; set; } = 1;
public override bool IsCharging { get; set; } = true;
}
}