Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 0b0f25c1 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Fix Settings crashed on tapping on notification animation video"

parents 59b709bc 9547fa73
Loading
Loading
Loading
Loading
+10 −4
Original line number Diff line number Diff line
@@ -33,6 +33,7 @@ import android.view.View;
class MediaAnimationController implements VideoPreference.AnimationController {
    private MediaPlayer mMediaPlayer;
    private boolean mVideoReady;
    private Surface mSurface;

    MediaAnimationController(Context context, int videoId) {
        final Uri videoPath = new Uri.Builder().scheme(ContentResolver.SCHEME_ANDROID_RESOURCE)
@@ -86,10 +87,7 @@ class MediaAnimationController implements VideoPreference.AnimationController {
            @Override
            public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int width,
                    int height) {
                if (mMediaPlayer != null) {
                    final Surface surface = new Surface(surfaceTexture);
                    mMediaPlayer.setSurface(surface);
                }
                setSurface(surfaceTexture);
            }

            @Override
@@ -105,6 +103,7 @@ class MediaAnimationController implements VideoPreference.AnimationController {

            @Override
            public void onSurfaceTextureUpdated(SurfaceTexture surfaceTexture) {
                setSurface(surfaceTexture);
                if (mVideoReady) {
                    if (preview.getVisibility() == View.VISIBLE) {
                        preview.setVisibility(View.GONE);
@@ -146,4 +145,11 @@ class MediaAnimationController implements VideoPreference.AnimationController {
            mMediaPlayer.start();
        }
    }

    private void setSurface(SurfaceTexture surfaceTexture) {
        if (mMediaPlayer != null && mSurface == null) {
            mSurface = new Surface(surfaceTexture);
            mMediaPlayer.setSurface(mSurface);
        }
    }
}
+11 −5
Original line number Diff line number Diff line
@@ -50,6 +50,9 @@ public class VideoPreference extends Preference {
    private int mAnimationId;
    private int mVectorAnimationId;
    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) {
        super(context);
@@ -108,19 +111,19 @@ public class VideoPreference extends Preference {
            return;
        }

        final TextureView video = (TextureView) holder.findViewById(R.id.video_texture_view);
        final ImageView previewImage = (ImageView) holder.findViewById(R.id.video_preview_image);
        final ImageView playButton = (ImageView) holder.findViewById(R.id.video_play_button);
        mVideo = (TextureView) holder.findViewById(R.id.video_texture_view);
        mPreviewImage = (ImageView) holder.findViewById(R.id.video_preview_image);
        mPlayButton = (ImageView) holder.findViewById(R.id.video_play_button);
        final AspectRatioFrameLayout layout = (AspectRatioFrameLayout) holder.findViewById(
                R.id.video_container);

        previewImage.setImageResource(mPreviewId);
        mPreviewImage.setImageResource(mPreviewId);
        layout.setAspectRatio(mAspectRatio);
        if (mHeight >= LinearLayout.LayoutParams.MATCH_PARENT) {
            layout.setLayoutParams(new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.MATCH_PARENT, mHeight));
        }
        mAnimationController.attachView(video, previewImage, playButton);
        mAnimationController.attachView(mVideo, mPreviewImage, mPlayButton);
    }

    @Override
@@ -164,6 +167,9 @@ public class VideoPreference extends Preference {
        }
        if (mAnimationId != 0) {
            mAnimationController = new MediaAnimationController(mContext, mAnimationId);
            if (mVideo != null) {
                mAnimationController.attachView(mVideo, mPreviewImage, mPlayButton);
            }
        }
    }

+32 −0
Original line number Diff line number Diff line
<?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 >
+28 −0
Original line number Diff line number Diff line
@@ -27,11 +27,13 @@ import static org.mockito.Mockito.when;

import android.content.Context;
import android.graphics.SurfaceTexture;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.TextureView;
import android.view.View;
import android.widget.ImageView;

import androidx.preference.PreferenceFragmentCompat;
import androidx.preference.PreferenceViewHolder;

import com.android.settings.R;
@@ -45,6 +47,7 @@ import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
import org.robolectric.shadows.androidx.fragment.FragmentController;

@RunWith(RobolectricTestRunner.class)
@Config(shadows = ShadowSettingsMediaPlayer.class)
@@ -135,4 +138,29 @@ public class VideoPreferenceTest {
        verify(fakePreview).setVisibility(eq(View.GONE));
        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);
        }
    }
}