2016-03-22 23:02:25 +03:00
|
|
|
using System;
|
|
|
|
using Android.Animation;
|
2018-06-28 14:02:36 +03:00
|
|
|
using Android.Content;
|
|
|
|
using Android.OS;
|
2016-04-25 22:57:44 +03:00
|
|
|
using Xamarin.Forms.Internals;
|
2016-03-22 23:02:25 +03:00
|
|
|
|
|
|
|
namespace Xamarin.Forms.Platform.Android
|
|
|
|
{
|
|
|
|
internal class AndroidTicker : Ticker, IDisposable
|
|
|
|
{
|
|
|
|
ValueAnimator _val;
|
2018-06-28 14:02:36 +03:00
|
|
|
bool _systemEnabled;
|
2016-03-22 23:02:25 +03:00
|
|
|
|
|
|
|
public AndroidTicker()
|
|
|
|
{
|
|
|
|
_val = new ValueAnimator();
|
|
|
|
_val.SetIntValues(0, 100); // avoid crash
|
|
|
|
_val.RepeatCount = ValueAnimator.Infinite;
|
|
|
|
_val.Update += OnValOnUpdate;
|
2018-06-28 14:02:36 +03:00
|
|
|
CheckPowerSaveModeStatus();
|
|
|
|
}
|
|
|
|
|
|
|
|
public override bool SystemEnabled => _systemEnabled;
|
|
|
|
|
|
|
|
internal void CheckPowerSaveModeStatus()
|
|
|
|
{
|
|
|
|
// Android disables animations when it's in power save mode
|
|
|
|
// So we need to keep track of whether we're in that mode and handle animations accordingly
|
|
|
|
// We can't just check ValueAnimator.AreAnimationsEnabled() because there's no event for that, and it's
|
|
|
|
// only supported on API >= 26
|
|
|
|
|
|
|
|
if (!Forms.IsLollipopOrNewer)
|
|
|
|
{
|
|
|
|
_systemEnabled = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var powerManager = (PowerManager)Forms.ApplicationContext.GetSystemService(Context.PowerService);
|
|
|
|
|
|
|
|
var powerSaveOn = powerManager.IsPowerSaveMode;
|
|
|
|
|
|
|
|
// If power saver is active, then animations will not run
|
|
|
|
_systemEnabled = !powerSaveOn;
|
|
|
|
|
|
|
|
// Notify the ticker that this value has changed, so it can manage animations in progress
|
|
|
|
OnSystemEnabledChanged();
|
2016-03-22 23:02:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
{
|
|
|
|
if (_val != null)
|
|
|
|
{
|
|
|
|
_val.Update -= OnValOnUpdate;
|
|
|
|
_val.Dispose();
|
|
|
|
}
|
|
|
|
_val = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override void DisableTimer()
|
|
|
|
{
|
|
|
|
_val?.Cancel();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override void EnableTimer()
|
|
|
|
{
|
|
|
|
_val?.Start();
|
|
|
|
}
|
|
|
|
|
|
|
|
void OnValOnUpdate(object sender, ValueAnimator.AnimatorUpdateEventArgs e)
|
|
|
|
{
|
|
|
|
SendSignals();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|