Fix continous playing of gesture animation video.
In the gesture settings, we listen to surface texture updates to determine when both the view and video is ready, and we auto play the animation video. However, sometimes, the video listener will receive the surface texture updates after the settings activity is being paused, in which case, the video will continue to play until the user navigate back to that gesture settings page. In onSurfaceTextureUpdated(), check for view visibility before we try to update anything to avoid unnecessary operation. Change-Id: I46474c9f461d5705f599deb8b2535d8505f2fe75 Bug: 110923173 Test: make RunSettingsRoboTests
This commit is contained in:
@@ -48,10 +48,12 @@ public class VideoPreference extends Preference {
|
|||||||
MediaPlayer mMediaPlayer;
|
MediaPlayer mMediaPlayer;
|
||||||
@VisibleForTesting
|
@VisibleForTesting
|
||||||
boolean mAnimationAvailable;
|
boolean mAnimationAvailable;
|
||||||
private boolean mVideoReady;
|
@VisibleForTesting
|
||||||
|
boolean mVideoReady;
|
||||||
private boolean mVideoPaused;
|
private boolean mVideoPaused;
|
||||||
private float mAspectRadio = 1.0f;
|
private float mAspectRadio = 1.0f;
|
||||||
private int mPreviewResource;
|
private int mPreviewResource;
|
||||||
|
private boolean mViewVisible;
|
||||||
|
|
||||||
public VideoPreference(Context context, AttributeSet attrs) {
|
public VideoPreference(Context context, AttributeSet attrs) {
|
||||||
super(context, attrs);
|
super(context, attrs);
|
||||||
@@ -144,6 +146,9 @@ public class VideoPreference extends Preference {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onSurfaceTextureUpdated(SurfaceTexture surfaceTexture) {
|
public void onSurfaceTextureUpdated(SurfaceTexture surfaceTexture) {
|
||||||
|
if (!mViewVisible) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
if (mVideoReady) {
|
if (mVideoReady) {
|
||||||
if (imageView.getVisibility() == View.VISIBLE) {
|
if (imageView.getVisibility() == View.VISIBLE) {
|
||||||
imageView.setVisibility(View.GONE);
|
imageView.setVisibility(View.GONE);
|
||||||
@@ -172,6 +177,7 @@ public class VideoPreference extends Preference {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void onViewVisible(boolean videoPaused) {
|
public void onViewVisible(boolean videoPaused) {
|
||||||
|
mViewVisible = true;
|
||||||
mVideoPaused = videoPaused;
|
mVideoPaused = videoPaused;
|
||||||
if (mVideoReady && mMediaPlayer != null && !mMediaPlayer.isPlaying()) {
|
if (mVideoReady && mMediaPlayer != null && !mMediaPlayer.isPlaying()) {
|
||||||
mMediaPlayer.seekTo(0);
|
mMediaPlayer.seekTo(0);
|
||||||
@@ -179,6 +185,7 @@ public class VideoPreference extends Preference {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void onViewInvisible() {
|
public void onViewInvisible() {
|
||||||
|
mViewVisible = false;
|
||||||
if (mMediaPlayer != null && mMediaPlayer.isPlaying()) {
|
if (mMediaPlayer != null && mMediaPlayer.isPlaying()) {
|
||||||
mMediaPlayer.pause();
|
mMediaPlayer.pause();
|
||||||
}
|
}
|
||||||
|
@@ -17,11 +17,17 @@
|
|||||||
package com.android.settings.widget;
|
package com.android.settings.widget;
|
||||||
|
|
||||||
import static com.google.common.truth.Truth.assertThat;
|
import static com.google.common.truth.Truth.assertThat;
|
||||||
|
|
||||||
|
import static org.mockito.Mockito.mock;
|
||||||
|
import static org.mockito.Mockito.never;
|
||||||
|
import static org.mockito.Mockito.verify;
|
||||||
import static org.mockito.Mockito.when;
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
import android.graphics.SurfaceTexture;
|
||||||
import android.media.MediaPlayer;
|
import android.media.MediaPlayer;
|
||||||
import android.view.LayoutInflater;
|
import android.view.LayoutInflater;
|
||||||
|
import android.view.TextureView;
|
||||||
|
|
||||||
import com.android.settings.R;
|
import com.android.settings.R;
|
||||||
import com.android.settings.testutils.SettingsRobolectricTestRunner;
|
import com.android.settings.testutils.SettingsRobolectricTestRunner;
|
||||||
@@ -70,4 +76,20 @@ public class VideoPreferenceTest {
|
|||||||
(AspectRatioFrameLayout) mPreferenceViewHolder.findViewById(R.id.video_container);
|
(AspectRatioFrameLayout) mPreferenceViewHolder.findViewById(R.id.video_container);
|
||||||
assertThat(layout.mAspectRatio).isWithin(0.01f).of(VIDEO_WIDTH / (float) VIDEO_HEIGHT);
|
assertThat(layout.mAspectRatio).isWithin(0.01f).of(VIDEO_WIDTH / (float) VIDEO_HEIGHT);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void onSurfaceTextureUpdated_viewInvisible_shouldNotStartPlayingVideo() {
|
||||||
|
final TextureView video =
|
||||||
|
(TextureView) mPreferenceViewHolder.findViewById(R.id.video_texture_view);
|
||||||
|
mVideoPreference.mAnimationAvailable = true;
|
||||||
|
mVideoPreference.mVideoReady = true;
|
||||||
|
mVideoPreference.onBindViewHolder(mPreferenceViewHolder);
|
||||||
|
mVideoPreference.onViewInvisible();
|
||||||
|
when(mMediaPlayer.isPlaying()).thenReturn(false);
|
||||||
|
final TextureView.SurfaceTextureListener listener = video.getSurfaceTextureListener();
|
||||||
|
|
||||||
|
listener.onSurfaceTextureUpdated(mock(SurfaceTexture.class));
|
||||||
|
|
||||||
|
verify(mMediaPlayer, never()).start();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user