Skip to content

Commit

Permalink
Fix MediaElement Layout for Android (#1873)
Browse files Browse the repository at this point in the history
* The primary changes to the `MauiMediaElement` class involve a significant refactoring to enhance layout management and simplify the handling of fullscreen mode for the media player. The key modifications include transitioning to a `RelativeLayout` for better flexibility in positioning the `StyledPlayerView`, removing explicit player size management in favor of relying on layout parameters, and streamlining the process for entering and exiting fullscreen mode. These adjustments lead to a more readable and maintainable codebase, with improved centering and sizing of the player view.

### Summary of Changes:

1. **Refactoring to Use RelativeLayout**: Transitioned the layout management of the `StyledPlayerView` from a `CoordinatorLayout` to a `RelativeLayout` for improved flexibility in positioning and sizing. This change allows for easier adjustments and better control over the player view's layout within the media element.
   - *Code Change*: Refactored `MauiMediaElement` to incorporate `RelativeLayout`.

2. **Removal of Player Size Management**: Eliminated the explicit management of the player's height and width variables, relying instead on the layout parameters of the `RelativeLayout` and `StyledPlayerView` for size adjustments. This simplification removes unnecessary code and leverages the layout system for resizing.
   - *Code Change*: Removed `playerHeight` and `playerWidth` variables and related size management code.

3. **Adjustments for Fullscreen Mode**: Simplified the handling of fullscreen mode by adding the player view directly to the window's decor view in fullscreen, and leveraging the `RelativeLayout` for re-adding the player view when exiting fullscreen. This approach minimizes the need for manual layout parameter adjustments.
   - *Code Change*: Updated fullscreen mode handling to use window's decor view and `RelativeLayout`.

4. **Simplification of Layout Management**: By using a `RelativeLayout`, the process of adding and removing the player view during fullscreen transitions has been streamlined, making the code more readable and the transitions smoother.
   - *Code Change*: Simplified layout management code for fullscreen transitions.

5. **Enhanced Centering of Player View**: Configured the `RelativeLayout` with layout parameters that ensure the `StyledPlayerView` is centered within the `MauiMediaElement`, particularly in fullscreen mode, by setting the gravity to `GravityFlags.Center`.
   - *Code Change*: Set `RelativeLayout` gravity to center the `StyledPlayerView`.

These changes collectively enhance the layout management of the media player, making the codebase more maintainable and the player's behavior more predictable and consistent, especially in terms of handling fullscreen mode and player view positioning.

* The most significant changes made to the code involve refactoring the initialization and layout parameter setting of `RelativeLayout`, adjusting the handling of fullscreen mode transitions, optimizing system bars visibility management, and general code cleanup for simplification and efficiency. These changes aim to enhance the clarity, maintainability, and functionality of the media player component, particularly in its layout setup and fullscreen behavior.

### Summary of Changes:

1. **Refactoring RelativeLayout Initialization and Layout Parameters:**
   - Simplified the process of setting up a `RelativeLayout` by directly assigning `RelativeLayout.LayoutParams` with predefined rules (`CenterInParent`, `CenterVertical`, and `CenterHorizontal`) during its instantiation, making the layout intentions clearer and the setup more efficient.

2. **Adjustment in Fullscreen Mode Handling:**
   - Standardized the approach to transitioning into and out of fullscreen mode by consistently adding or removing the `relativeLayout` (containing the `playerView`) to the `layout`, improving the maintainability and consistency of fullscreen functionality.

3. **System Bars Visibility Management:**
   - Optimized the management of system bars' visibility by moving the call to `SetSystemBarsVisibility()` outside of conditional blocks, ensuring consistent updates in both entering and exiting fullscreen scenarios, and reducing redundant code.

4. **Code Cleanup and Simplification:**
   - Removed unnecessary lines and steps, such as duplicated variable declarations and redundant view manipulations, contributing to a cleaner and more understandable codebase.

These changes collectively contribute to a more streamlined, efficient, and maintainable codebase, particularly enhancing the handling of layout parameters and fullscreen mode transitions for the media player component.

* The primary change involves the removal of a redundant line of code in the `MauiMediaElement` constructor within the `MauiMediaElement.android.cs` file. This modification aims to streamline the code by eliminating unnecessary repetition, specifically the assignment of `LayoutParameters` to `relativeLayout`, which is already addressed during its initialization.

### Summary of Change:
- **Redundancy Elimination in `MauiMediaElement`**: The specific line `relativeLayout.LayoutParameters = layout;` was removed from the constructor in the `MauiMediaElement.android.cs` file. This adjustment was made to remove redundant code, enhancing the clarity and efficiency of the codebase by avoiding the duplicate setting of `LayoutParameters` for `relativeLayout`.

### Detailed Change:
- **File**: `MauiMediaElement.android.cs`
  - **Change**: Removed the line `relativeLayout.LayoutParameters = layout;` from the constructor of `MauiMediaElement`.
  - **Reason**: This line was deemed redundant as the `LayoutParameters` were already specified during the initialization of `relativeLayout`, making this explicit assignment unnecessary.

---------

Co-authored-by: Brandon Minnick <13558917+brminnick@users.noreply.github.com>
  • Loading branch information
ne0rrmatrix and brminnick authored Jun 5, 2024
1 parent 4347567 commit 0bcb071
Showing 1 changed file with 20 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ public class MauiMediaElement : CoordinatorLayout
int defaultSystemUiVisibility;
bool isSystemBarVisible;
bool isFullScreen;
int playerHeight;
int playerWidth;
readonly RelativeLayout relativeLayout;

#pragma warning disable CS8618 // Non-nullable field is uninitialized. Consider declaring as nullable.
#pragma warning disable IDE0060 // Remove unused parameter
Expand All @@ -45,9 +44,18 @@ public MauiMediaElement(Context context, StyledPlayerView playerView) : base(con
playerView.FullscreenButtonClick += OnFullscreenButtonClick;
this.playerView.SetBackgroundColor(Android.Graphics.Color.Black);

AddView(playerView);
}
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);
}
public override void OnDetachedFromWindow()
{
if (isFullScreen)
Expand Down Expand Up @@ -129,36 +137,22 @@ void OnFullscreenButtonClick(object? sender, StyledPlayerView.FullscreenButtonCl
}

var (_, currentWindow, _, _) = VerifyAndRetrieveCurrentWindowResources();

// Hide the SystemBars and Status bar
var layout = currentWindow?.DecorView as ViewGroup;

if (e.IsFullScreen)
{
isFullScreen = true;
playerHeight = playerView.Height;
playerWidth = playerView.Width;
DisplayMetrics displayMetrics = new DisplayMetrics();
currentWindow?.WindowManager?.DefaultDisplay?.GetMetrics(displayMetrics);
var layout = currentWindow?.DecorView as ViewGroup;

RemoveView(playerView);
RelativeLayout.LayoutParams item = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.MatchParent);
item.Width = displayMetrics.WidthPixels;
item.Height = displayMetrics.HeightPixels;
layout?.AddView(playerView, item);
SetSystemBarsVisibility();
RemoveView(relativeLayout);
layout?.AddView(relativeLayout);
}
else
{
isFullScreen = false;
var layout = currentWindow?.DecorView as ViewGroup;
RelativeLayout.LayoutParams item = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.MatchParent);
item.Width = playerWidth;
item.Height = playerHeight;

layout?.RemoveView(playerView);
AddView(playerView, item);
SetSystemBarsVisibility();
layout?.RemoveView(relativeLayout);
AddView(relativeLayout);
}
// Hide/Show the SystemBars and Status bar
SetSystemBarsVisibility();
}

void SetSystemBarsVisibility()
Expand Down

0 comments on commit 0bcb071

Please sign in to comment.