1/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.test.hwui;
18
19import android.app.Activity;
20import android.graphics.Bitmap;
21import android.net.Uri;
22import android.os.Bundle;
23import android.os.Environment;
24import android.view.PixelCopy;
25import android.view.View;
26import android.widget.Button;
27import android.widget.FrameLayout;
28import android.widget.LinearLayout;
29import android.widget.Toast;
30import android.widget.VideoView;
31
32import java.io.FileOutputStream;
33
34public class VideoViewCaptureActivity extends Activity {
35    private VideoView mVideoView;
36    private int mVideoWidth, mVideoHeight;
37
38    @Override
39    protected void onCreate(Bundle savedInstanceState) {
40        super.onCreate(savedInstanceState);
41        mVideoView = new VideoView(this);
42        mVideoView.setOnPreparedListener(mp -> {
43            mp.setLooping(true);
44            mVideoWidth = mp.getVideoWidth();
45            mVideoHeight = mp.getVideoHeight();
46            mVideoView.start();
47        });
48
49        Uri uri = Uri.parse("android.resource://com.android.test.hwui/" + R.raw.colorgrid_video);
50        mVideoView.setVideoURI(uri);
51
52        Button button = new Button(this);
53        button.setText("Copy bitmap to /sdcard/surfaceview.png");
54        button.setOnClickListener((View v) -> {
55            final Bitmap b = Bitmap.createBitmap(
56                    mVideoWidth, mVideoHeight,
57                    Bitmap.Config.ARGB_8888);
58            PixelCopy.request(mVideoView, b,
59                    (int result) -> {
60                        if (result != PixelCopy.SUCCESS) {
61                            Toast.makeText(VideoViewCaptureActivity.this,
62                                    "Failed to copy", Toast.LENGTH_SHORT).show();
63                            return;
64                        }
65                        try {
66                            try (FileOutputStream out = new FileOutputStream(
67                                    Environment.getExternalStorageDirectory() + "/surfaceview.png");) {
68                                b.compress(Bitmap.CompressFormat.PNG, 100, out);
69                            }
70                        } catch (Exception e) {
71                            // Ignore
72                        }
73                    }, mVideoView.getHandler());
74        });
75
76        FrameLayout content = new FrameLayout(this);
77        LinearLayout layout = new LinearLayout(this);
78        layout.setOrientation(LinearLayout.VERTICAL);
79        layout.addView(button, LinearLayout.LayoutParams.MATCH_PARENT,
80                LinearLayout.LayoutParams.WRAP_CONTENT);
81        layout.addView(mVideoView, LinearLayout.LayoutParams.MATCH_PARENT,
82                LinearLayout.LayoutParams.MATCH_PARENT);
83
84        content.addView(layout, new FrameLayout.LayoutParams(
85                FrameLayout.LayoutParams.MATCH_PARENT,
86                FrameLayout.LayoutParams.MATCH_PARENT));
87        setContentView(content);
88    }
89}
90