Merge "Fix Settings crashed on tapping on notification animation video"
This commit is contained in:
committed by
Android (Google) Code Review
commit
0b0f25c1f5
@@ -33,6 +33,7 @@ import android.view.View;
|
|||||||
class MediaAnimationController implements VideoPreference.AnimationController {
|
class MediaAnimationController implements VideoPreference.AnimationController {
|
||||||
private MediaPlayer mMediaPlayer;
|
private MediaPlayer mMediaPlayer;
|
||||||
private boolean mVideoReady;
|
private boolean mVideoReady;
|
||||||
|
private Surface mSurface;
|
||||||
|
|
||||||
MediaAnimationController(Context context, int videoId) {
|
MediaAnimationController(Context context, int videoId) {
|
||||||
final Uri videoPath = new Uri.Builder().scheme(ContentResolver.SCHEME_ANDROID_RESOURCE)
|
final Uri videoPath = new Uri.Builder().scheme(ContentResolver.SCHEME_ANDROID_RESOURCE)
|
||||||
@@ -86,10 +87,7 @@ class MediaAnimationController implements VideoPreference.AnimationController {
|
|||||||
@Override
|
@Override
|
||||||
public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int width,
|
public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int width,
|
||||||
int height) {
|
int height) {
|
||||||
if (mMediaPlayer != null) {
|
setSurface(surfaceTexture);
|
||||||
final Surface surface = new Surface(surfaceTexture);
|
|
||||||
mMediaPlayer.setSurface(surface);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -105,6 +103,7 @@ class MediaAnimationController implements VideoPreference.AnimationController {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onSurfaceTextureUpdated(SurfaceTexture surfaceTexture) {
|
public void onSurfaceTextureUpdated(SurfaceTexture surfaceTexture) {
|
||||||
|
setSurface(surfaceTexture);
|
||||||
if (mVideoReady) {
|
if (mVideoReady) {
|
||||||
if (preview.getVisibility() == View.VISIBLE) {
|
if (preview.getVisibility() == View.VISIBLE) {
|
||||||
preview.setVisibility(View.GONE);
|
preview.setVisibility(View.GONE);
|
||||||
@@ -146,4 +145,11 @@ class MediaAnimationController implements VideoPreference.AnimationController {
|
|||||||
mMediaPlayer.start();
|
mMediaPlayer.start();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void setSurface(SurfaceTexture surfaceTexture) {
|
||||||
|
if (mMediaPlayer != null && mSurface == null) {
|
||||||
|
mSurface = new Surface(surfaceTexture);
|
||||||
|
mMediaPlayer.setSurface(mSurface);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -50,6 +50,9 @@ public class VideoPreference extends Preference {
|
|||||||
private int mAnimationId;
|
private int mAnimationId;
|
||||||
private int mVectorAnimationId;
|
private int mVectorAnimationId;
|
||||||
private int mHeight = LinearLayout.LayoutParams.MATCH_PARENT - 1; // video height in pixels
|
private int mHeight = LinearLayout.LayoutParams.MATCH_PARENT - 1; // video height in pixels
|
||||||
|
private TextureView mVideo;
|
||||||
|
private ImageView mPreviewImage;
|
||||||
|
private ImageView mPlayButton;
|
||||||
|
|
||||||
public VideoPreference(Context context) {
|
public VideoPreference(Context context) {
|
||||||
super(context);
|
super(context);
|
||||||
@@ -108,19 +111,19 @@ public class VideoPreference extends Preference {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
final TextureView video = (TextureView) holder.findViewById(R.id.video_texture_view);
|
mVideo = (TextureView) holder.findViewById(R.id.video_texture_view);
|
||||||
final ImageView previewImage = (ImageView) holder.findViewById(R.id.video_preview_image);
|
mPreviewImage = (ImageView) holder.findViewById(R.id.video_preview_image);
|
||||||
final ImageView playButton = (ImageView) holder.findViewById(R.id.video_play_button);
|
mPlayButton = (ImageView) holder.findViewById(R.id.video_play_button);
|
||||||
final AspectRatioFrameLayout layout = (AspectRatioFrameLayout) holder.findViewById(
|
final AspectRatioFrameLayout layout = (AspectRatioFrameLayout) holder.findViewById(
|
||||||
R.id.video_container);
|
R.id.video_container);
|
||||||
|
|
||||||
previewImage.setImageResource(mPreviewId);
|
mPreviewImage.setImageResource(mPreviewId);
|
||||||
layout.setAspectRatio(mAspectRatio);
|
layout.setAspectRatio(mAspectRatio);
|
||||||
if (mHeight >= LinearLayout.LayoutParams.MATCH_PARENT) {
|
if (mHeight >= LinearLayout.LayoutParams.MATCH_PARENT) {
|
||||||
layout.setLayoutParams(new LinearLayout.LayoutParams(
|
layout.setLayoutParams(new LinearLayout.LayoutParams(
|
||||||
LinearLayout.LayoutParams.MATCH_PARENT, mHeight));
|
LinearLayout.LayoutParams.MATCH_PARENT, mHeight));
|
||||||
}
|
}
|
||||||
mAnimationController.attachView(video, previewImage, playButton);
|
mAnimationController.attachView(mVideo, mPreviewImage, mPlayButton);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -164,6 +167,9 @@ public class VideoPreference extends Preference {
|
|||||||
}
|
}
|
||||||
if (mAnimationId != 0) {
|
if (mAnimationId != 0) {
|
||||||
mAnimationController = new MediaAnimationController(mContext, mAnimationId);
|
mAnimationController = new MediaAnimationController(mContext, mAnimationId);
|
||||||
|
if (mVideo != null) {
|
||||||
|
mAnimationController.attachView(mVideo, mPreviewImage, mPlayButton);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
32
tests/robotests/res/xml-mcc999/video_preference.xml
Normal file
32
tests/robotests/res/xml-mcc999/video_preference.xml
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<!--
|
||||||
|
Copyright (C) 2019 The Android Open Source Project
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
-->
|
||||||
|
|
||||||
|
<PreferenceScreen
|
||||||
|
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
xmlns:settings="http://schemas.android.com/apk/res-auto">
|
||||||
|
<com.android.settings.widget.VideoPreference
|
||||||
|
android:key="video1"
|
||||||
|
settings:animation="@raw/adaptive_sleep"
|
||||||
|
settings:preview="@drawable/adaptive_sleep"
|
||||||
|
android:title="seek_bar_title"/>
|
||||||
|
<com.android.settings.widget.VideoPreference
|
||||||
|
android:key="video2"
|
||||||
|
settings:animation="@raw/adaptive_sleep"
|
||||||
|
settings:preview="@drawable/adaptive_sleep"
|
||||||
|
settings:vectorAnimation="@drawable/adaptive_sleep"
|
||||||
|
android:title="seek_bar_title"/>
|
||||||
|
</PreferenceScreen >
|
@@ -27,11 +27,13 @@ import static org.mockito.Mockito.when;
|
|||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.graphics.SurfaceTexture;
|
import android.graphics.SurfaceTexture;
|
||||||
|
import android.os.Bundle;
|
||||||
import android.view.LayoutInflater;
|
import android.view.LayoutInflater;
|
||||||
import android.view.TextureView;
|
import android.view.TextureView;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.widget.ImageView;
|
import android.widget.ImageView;
|
||||||
|
|
||||||
|
import androidx.preference.PreferenceFragmentCompat;
|
||||||
import androidx.preference.PreferenceViewHolder;
|
import androidx.preference.PreferenceViewHolder;
|
||||||
|
|
||||||
import com.android.settings.R;
|
import com.android.settings.R;
|
||||||
@@ -45,6 +47,7 @@ import org.mockito.MockitoAnnotations;
|
|||||||
import org.robolectric.RobolectricTestRunner;
|
import org.robolectric.RobolectricTestRunner;
|
||||||
import org.robolectric.RuntimeEnvironment;
|
import org.robolectric.RuntimeEnvironment;
|
||||||
import org.robolectric.annotation.Config;
|
import org.robolectric.annotation.Config;
|
||||||
|
import org.robolectric.shadows.androidx.fragment.FragmentController;
|
||||||
|
|
||||||
@RunWith(RobolectricTestRunner.class)
|
@RunWith(RobolectricTestRunner.class)
|
||||||
@Config(shadows = ShadowSettingsMediaPlayer.class)
|
@Config(shadows = ShadowSettingsMediaPlayer.class)
|
||||||
@@ -135,4 +138,29 @@ public class VideoPreferenceTest {
|
|||||||
verify(fakePreview).setVisibility(eq(View.GONE));
|
verify(fakePreview).setVisibility(eq(View.GONE));
|
||||||
assertThat(mAnimationController.isPlaying()).isTrue();
|
assertThat(mAnimationController.isPlaying()).isTrue();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Config(qualifiers = "mcc999")
|
||||||
|
public void onViewVisible_createAnimationController() {
|
||||||
|
final PreferenceFragmentCompat fragment = FragmentController.of(
|
||||||
|
new VideoPreferenceTest.TestFragment(),
|
||||||
|
new Bundle())
|
||||||
|
.create()
|
||||||
|
.start()
|
||||||
|
.resume()
|
||||||
|
.get();
|
||||||
|
|
||||||
|
final VideoPreference vp1 = fragment.findPreference("video1");
|
||||||
|
final VideoPreference vp2 = fragment.findPreference("video2");
|
||||||
|
|
||||||
|
assertThat(vp1.mAnimationController instanceof MediaAnimationController).isTrue();
|
||||||
|
assertThat(vp2.mAnimationController instanceof VectorAnimationController).isTrue();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class TestFragment extends PreferenceFragmentCompat {
|
||||||
|
@Override
|
||||||
|
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
|
||||||
|
addPreferencesFromResource(R.xml.video_preference);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user