-
Notifications
You must be signed in to change notification settings - Fork 493
Expand file tree
/
Copy pathMauiMediaElement.android.cs
More file actions
264 lines (236 loc) · 8.55 KB
/
MauiMediaElement.android.cs
File metadata and controls
264 lines (236 loc) · 8.55 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
using System.Diagnostics.CodeAnalysis;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Util;
using Android.Views;
using Android.Widget;
using AndroidX.CoordinatorLayout.Widget;
using AndroidX.Core.View;
using AndroidX.Media3.UI;
using CommunityToolkit.Maui.Views;
namespace CommunityToolkit.Maui.Core.Views;
/// <summary>
/// The user-interface element that represents the <see cref="MediaElement"/> on Android.
/// </summary>
public class MauiMediaElement : CoordinatorLayout
{
readonly RelativeLayout relativeLayout;
readonly PlayerView playerView;
readonly WeakEventManager fullScreenEventManager = new();
int defaultSystemUiVisibility;
bool isSystemBarVisible;
bool isFullScreen;
internal event EventHandler<ScreenStateChangedEventArgs> FullScreenStateChanged
{
add => fullScreenEventManager.AddEventHandler(value);
remove => fullScreenEventManager.RemoveEventHandler(value);
}
#pragma warning disable CS8618 // Non-nullable field is uninitialized. Consider declaring as nullable.
#pragma warning disable IDE0060 // Remove unused parameter
[DynamicDependency(DynamicallyAccessedMemberTypes.PublicConstructors, typeof(MediaElement))]
public MauiMediaElement(nint javaReference, JniHandleOwnership transfer) : base(javaReference, transfer)
{
// Fixes no constructor found exception: https://github.com/CommunityToolkit/Maui/issues/3114
}
[DynamicDependency(DynamicallyAccessedMemberTypes.PublicConstructors, typeof(MediaElement))]
public MauiMediaElement(Context? context) : base(context)
{
// Fixes no constructor found exception: https://github.com/CommunityToolkit/Maui/issues/3114
}
[DynamicDependency(DynamicallyAccessedMemberTypes.PublicConstructors, typeof(MediaElement))]
public MauiMediaElement(Context? context, IAttributeSet? attrs) : base(context, attrs)
{
// Fixes no constructor found exception: https://github.com/CommunityToolkit/Maui/issues/3114
}
[DynamicDependency(DynamicallyAccessedMemberTypes.PublicConstructors, typeof(MediaElement))]
public MauiMediaElement(Context? context, IAttributeSet? attrs, int defStyleAttr) : base(context, attrs, defStyleAttr)
{
// Fixes no constructor found exception: https://github.com/CommunityToolkit/Maui/pull/1692#issuecomment-1955099758
}
#pragma warning restore CS8618 // Non-nullable field is uninitialized. Consider declaring as nullable.
#pragma warning restore IDE0060 // Remove unused parameter
/// <summary>
/// Initializes a new instance of the <see cref="MauiMediaElement"/> class.
/// </summary>
/// <param name="context">The application's <see cref="Context"/>.</param>
/// <param name="playerView">The <see cref="PlayerView"/> that acts as the platform media player.</param>
[DynamicDependency(DynamicallyAccessedMemberTypes.PublicConstructors, typeof(MediaElement))]
public MauiMediaElement(Context context, PlayerView playerView) : base(context)
{
this.playerView = playerView;
this.playerView.SetBackgroundColor(global::Android.Graphics.Color.Black);
playerView.FullscreenButtonClick += OnFullscreenButtonClick;
var layout = new RelativeLayout.LayoutParams(LayoutParams.WrapContent, LayoutParams.WrapContent);
layout.AddRule(LayoutRules.CenterInParent);
layout.AddRule(LayoutRules.CenterVertical);
layout.AddRule(LayoutRules.CenterHorizontal);
relativeLayout = new RelativeLayout(Platform.AppContext)
{
LayoutParameters = layout,
};
relativeLayout.AddView(playerView);
AddView(relativeLayout);
}
[DynamicDependency(DynamicallyAccessedMemberTypes.PublicConstructors, typeof(MediaElement))]
public override void OnDetachedFromWindow()
{
if (isFullScreen)
{
OnFullscreenButtonClick(this, new PlayerView.FullscreenButtonClickEventArgs(!isFullScreen));
}
base.OnDetachedFromWindow();
}
/// <summary>
/// Checks the visibility of the view
/// </summary>
/// <param name="changedView"></param>
/// <param name="visibility"></param>
protected override void OnVisibilityChanged(global::Android.Views.View changedView, [GeneratedEnum] ViewStates visibility)
{
base.OnVisibilityChanged(changedView, visibility);
if (isFullScreen && visibility is ViewStates.Visible)
{
SetSystemBarsVisibility();
}
}
/// <summary>
/// Releases the unmanaged resources used by the <see cref="MediaElement"/> and optionally releases the managed resources.
/// </summary>
/// <param name="disposing"><see langword="true"/> to release both managed and unmanaged resources; <see langword="false"/> to release only unmanaged resources.</param>
protected override void Dispose(bool disposing)
{
if (disposing)
{
try
{
if (playerView.Player is not null)
{
playerView.Player.PlayWhenReady = false;
}
// https://github.com/google/ExoPlayer/issues/1855#issuecomment-251041500
playerView.Player?.Release();
playerView.Player?.Dispose();
playerView.Dispose();
}
catch (ObjectDisposedException)
{
// playerView already disposed
}
}
base.Dispose(disposing);
}
void OnFullscreenButtonClick(object? sender, PlayerView.FullscreenButtonClickEventArgs e)
{
// Ensure there is a player view
if (playerView is null)
{
throw new InvalidOperationException("UpdatedPlayerView cannot be null when the FullScreen button is tapped");
}
var layout = CurrentPlatformContext.CurrentWindow.DecorView as ViewGroup;
// `p0` is the boolean value of isFullScreen being passed into the method.
// This is a binding issue that will not be fixed as it is now part of shipped API.
if (e.P0)
{
isFullScreen = true;
RemoveView(relativeLayout);
layout?.AddView(relativeLayout);
}
else
{
isFullScreen = false;
layout?.RemoveView(relativeLayout);
AddView(relativeLayout);
}
// Hide/Show the SystemBars and Status bar
SetSystemBarsVisibility();
var newState = e.P0 ? MediaElementScreenState.FullScreen : MediaElementScreenState.Default;
var oldState = e.P0 ? MediaElementScreenState.Default : MediaElementScreenState.FullScreen;
fullScreenEventManager.HandleEvent(this, new ScreenStateChangedEventArgs(oldState, newState), nameof(FullScreenStateChanged));
}
void SetSystemBarsVisibility()
{
var currentWindow = CurrentPlatformContext.CurrentWindow;
var windowInsetsControllerCompat = WindowCompat.GetInsetsController(currentWindow, currentWindow.DecorView);
var barTypes = WindowInsetsCompat.Type.StatusBars()
| WindowInsetsCompat.Type.SystemBars()
| WindowInsetsCompat.Type.NavigationBars();
if (isFullScreen)
{
WindowCompat.SetDecorFitsSystemWindows(currentWindow, false);
if (OperatingSystem.IsAndroidVersionAtLeast(30))
{
var windowInsets = currentWindow.DecorView.RootWindowInsets;
if (windowInsets is not null)
{
isSystemBarVisible = windowInsets.IsVisible(WindowInsetsCompat.Type.NavigationBars()) || windowInsets.IsVisible(WindowInsetsCompat.Type.StatusBars());
if (isSystemBarVisible)
{
currentWindow.InsetsController?.Hide(WindowInsets.Type.SystemBars());
}
}
}
else
{
defaultSystemUiVisibility = (int)currentWindow.DecorView.SystemUiFlags;
currentWindow.DecorView.SystemUiFlags = currentWindow.DecorView.SystemUiFlags
| SystemUiFlags.LayoutStable
| SystemUiFlags.LayoutHideNavigation
| SystemUiFlags.LayoutFullscreen
| SystemUiFlags.HideNavigation
| SystemUiFlags.Fullscreen
| SystemUiFlags.Immersive;
}
if (windowInsetsControllerCompat is not null)
{
windowInsetsControllerCompat.Hide(barTypes);
windowInsetsControllerCompat.SystemBarsBehavior = WindowInsetsControllerCompat.BehaviorShowTransientBarsBySwipe;
}
}
else
{
if (OperatingSystem.IsAndroidVersionAtLeast(30))
{
if (isSystemBarVisible)
{
currentWindow.InsetsController?.Show(WindowInsets.Type.SystemBars());
}
}
else
{
currentWindow.DecorView.SystemUiFlags = (SystemUiFlags)defaultSystemUiVisibility;
}
if (windowInsetsControllerCompat is not null)
{
windowInsetsControllerCompat.Show(barTypes);
windowInsetsControllerCompat.SystemBarsBehavior = WindowInsetsControllerCompat.BehaviorDefault;
}
WindowCompat.SetDecorFitsSystemWindows(currentWindow, true);
}
}
readonly record struct CurrentPlatformContext
{
public static Activity CurrentActivity
{
get
{
if (Platform.CurrentActivity is null)
{
throw new InvalidOperationException("CurrentActivity cannot be null");
}
return Platform.CurrentActivity;
}
}
public static global::Android.Views.Window CurrentWindow
{
get
{
if (CurrentActivity.Window is null)
{
throw new InvalidOperationException("Window cannot be null");
}
return CurrentActivity.Window;
}
}
}
}