/
AndroidTicker.cs
120 lines (103 loc) · 2.78 KB
/
AndroidTicker.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
using System;
using Android.Animation;
using Android.Content;
using Android.OS;
using Xamarin.Forms.Internals;
using GlobalSettings = Android.Provider.Settings.Global;
using SystemSettings = Android.Provider.Settings.System;
namespace Xamarin.Forms.Platform.Android
{
internal class AndroidTicker : Ticker, IDisposable
{
ValueAnimator _val;
bool _animatorEnabled;
public AndroidTicker()
{
_val = new ValueAnimator();
_val.SetIntValues(0, 100); // avoid crash
_val.RepeatCount = ValueAnimator.Infinite;
_val.Update += OnValOnUpdate;
_animatorEnabled = IsAnimatorEnabled();
CheckAnimationEnabledStatus();
}
public override bool SystemEnabled => _animatorEnabled;
internal void CheckAnimationEnabledStatus()
{
var animatorEnabled = IsAnimatorEnabled();
if (animatorEnabled != _animatorEnabled)
{
_animatorEnabled = animatorEnabled;
// Notify the ticker that this value has changed, so it can manage animations in progress
OnSystemEnabledChanged();
}
}
static bool IsAnimatorEnabled()
{
if (Forms.IsOreoOrNewer)
{
// For more recent API levels, we can just check this method and be done with it
return ValueAnimator.AreAnimatorsEnabled();
}
if (Forms.IsLollipopOrNewer)
{
// For API levels which support power saving but not AreAnimatorsEnabled, we can check the
// power save mode; for these API levels, power saving == ON will mean that animations are disabled
var powerManager = (PowerManager)Forms.ApplicationContext.GetSystemService(Context.PowerService);
if (powerManager.IsPowerSaveMode)
{
return false;
}
}
// If we're not in power save mode (or don't support it), we still need to check the AnimatorDurationScale;
// animations might be disabled by developer mode
var resolver = global::Android.App.Application.Context?.ContentResolver;
if (resolver == null)
{
return false;
}
float animationScale;
if (Forms.IsJellyBeanMr1OrNewer)
{
animationScale = GlobalSettings.GetFloat(resolver, GlobalSettings.AnimatorDurationScale, 1);
}
else
{
#pragma warning disable 0618
animationScale = SystemSettings.GetFloat(resolver, SystemSettings.AnimatorDurationScale, 1);
#pragma warning restore 0618
}
return animationScale > 0;
}
public void Dispose()
{
if (_val != null)
{
_val.Update -= OnValOnUpdate;
_val.Dispose();
}
_val = null;
}
protected override void DisableTimer()
{
if (Device.IsInvokeRequired)
{
Device.BeginInvokeOnMainThread(new Action(() =>
{
_val?.Cancel();
}));
}
else
{
_val?.Cancel();
}
}
protected override void EnableTimer()
{
_val?.Start();
}
void OnValOnUpdate(object sender, ValueAnimator.AnimatorUpdateEventArgs e)
{
SendSignals();
}
}
}