Skip to content

Commit

Permalink
[Android] MediaPicker: Use dark navigation bar when previewing videos.
Browse files Browse the repository at this point in the history
This CL sets the navigation bar (and divider) color to black while playing videos.

(cherry picked from commit c142c80)

Bug: 1197165
Change-Id: I81a62a8eadea0dd636d174891ff0d9fad92eb95a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2815061
Commit-Queue: Finnur Thorarinsson <finnur@chromium.org>
Reviewed-by: Ted Choc <tedchoc@chromium.org>
Cr-Original-Commit-Position: refs/heads/master@{#870929}
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2821883
Reviewed-by: Finnur Thorarinsson <finnur@chromium.org>
Reviewed-by: Rayan Kanso <rayankans@chromium.org>
Cr-Commit-Position: refs/branch-heads/4472@{#32}
Cr-Branched-From: 3d60439-refs/heads/master@{#870763}
  • Loading branch information
Finnur Thorarinsson authored and Chromium LUCI CQ committed Apr 13, 2021
1 parent 0eb4aaa commit 3d9a179
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ public void onDialogDismissed() {
*/
public void startVideoPlaybackAsync(Uri uri) {
if (mDialog == null) return;
mVideoPlayer.startVideoPlaybackAsync(uri, mDialog.getWindow().getDecorView());
mVideoPlayer.startVideoPlaybackAsync(uri, mDialog.getWindow());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import android.animation.Animator;
import android.content.Context;
import android.content.res.Configuration;
import android.graphics.Color;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Build;
Expand All @@ -19,6 +20,7 @@
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
Expand All @@ -33,6 +35,7 @@

import org.chromium.base.task.PostTask;
import org.chromium.content_public.browser.UiThreadTaskTraits;
import org.chromium.ui.UiUtils;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
Expand Down Expand Up @@ -110,8 +113,8 @@ public interface VideoPlaybackStatusCallback {
// durations are 1/10th of normal length.
private static boolean sShortAnimationTimesForTesting;

// The DecorView for the dialog the player is shown in.
private View mDecorView;
// The Window for the dialog the player is shown in.
private Window mWindow;

// The Context to use.
private Context mContext;
Expand Down Expand Up @@ -178,6 +181,15 @@ public interface VideoPlaybackStatusCallback {
// The previous options for the System UI visibility.
private int mPreviousSystemUiVisibilityOptions;

// Keeps track of the previous navigation bar color when colors switch due to playback.
private int mPreviousNavBarColor;

// Keeps track of the previous navigation bar divider color when colors switch due to playback.
private int mPreviousNavBarDividerColor;

// Keeps track of whether navigation colors have been saved previously.
private boolean mPreviousNavBarColorsSaved;

// The object to convert touch events into gestures.
private GestureDetectorCompat mGestureDetector;

Expand Down Expand Up @@ -247,10 +259,11 @@ public void onConfigurationChanged(Configuration newConfig) {
/**
* Start playback of a video in an overlay above the photo picker.
* @param uri The uri of the video to start playing.
* @param decorView The decorView for the dialog.
* @param window The window for the dialog.
*/
public void startVideoPlaybackAsync(Uri uri, View decorView) {
mDecorView = decorView;
public void startVideoPlaybackAsync(Uri uri, Window window) {
mWindow = window;
syncNavBarColorToPlaybackStatus(/* playerOpening= */ true);

// Make the filename (uri) of the video visible at the top and de-emphasize the scheme part.
SpannableString fileName = new SpannableString(uri.toString());
Expand Down Expand Up @@ -316,9 +329,33 @@ public boolean closeVideoPlayer() {
stopVideoPlayback();
mVideoView.setMediaController(null);
mMuteButton.setImageResource(R.drawable.ic_volume_on_white_24dp);
syncNavBarColorToPlaybackStatus(/* playerOpening= */ false);
return true;
}

/**
* Updates the color of the navigation bar, divider and icons to reflect whether in playback
* mode or not. This function does nothing on Android O and older.
* @param playerOpening True when the video player is opening (false when closing).
*/
private void syncNavBarColorToPlaybackStatus(boolean playerOpening) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
if (playerOpening) {
if (mPreviousNavBarColorsSaved) {
return; // Don't overwrite previously saved colors.
}
mPreviousNavBarColor = mWindow.getNavigationBarColor();
mPreviousNavBarDividerColor = mWindow.getNavigationBarDividerColor();
}
mWindow.setNavigationBarColor(playerOpening ? Color.BLACK : mPreviousNavBarColor);
mWindow.setNavigationBarDividerColor(
playerOpening ? Color.BLACK : mPreviousNavBarDividerColor);
UiUtils.setNavigationBarIconColor(mWindow.getDecorView().getRootView(), !playerOpening);

mPreviousNavBarColorsSaved = playerOpening;
}
}

private void adjustVideoLayoutParamsToOrientation() {
if (mMediaPlayer == null || mMediaPlayer.getVideoWidth() == 0
|| mMediaPlayer.getVideoHeight() == 0) {
Expand Down Expand Up @@ -422,7 +459,7 @@ public void onClick(View view) {
@Override
public void onSystemUiVisibilityChange(int visibility) {
if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {
mDecorView.setOnSystemUiVisibilityChangeListener(null);
mWindow.getDecorView().setOnSystemUiVisibilityChangeListener(null);
onExitFullScreenMode();

if (!mFullScreenToggledInApp) {
Expand Down Expand Up @@ -755,15 +792,16 @@ private void onExitFullScreenMode() {

private void toggleAndroidSystemUiForFullscreen() {
mFullScreenToggledInApp = true;
View decorView = mWindow.getDecorView();
if (!mFullScreenEnabled) {
mDecorView.setOnSystemUiVisibilityChangeListener(this);
mPreviousSystemUiVisibilityOptions = mDecorView.getSystemUiVisibility();
mDecorView.setSystemUiVisibility(mPreviousSystemUiVisibilityOptions
decorView.setOnSystemUiVisibilityChangeListener(this);
mPreviousSystemUiVisibilityOptions = decorView.getSystemUiVisibility();
decorView.setSystemUiVisibility(mPreviousSystemUiVisibilityOptions
| View.SYSTEM_UI_FLAG_IMMERSIVE | View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_LOW_PROFILE);
} else {
mDecorView.setSystemUiVisibility(mPreviousSystemUiVisibilityOptions);
decorView.setSystemUiVisibility(mPreviousSystemUiVisibilityOptions);
}

// Calling setSystemUiVisibility will result in Android showing/hiding its system UI to go
Expand Down

0 comments on commit 3d9a179

Please sign in to comment.