1/*
2 * Copyright (C) 2013 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 */
16package com.android.transitiontests;
17
18import android.app.Activity;
19import android.content.Context;
20import android.graphics.Canvas;
21import android.graphics.Color;
22import android.graphics.SurfaceTexture;
23import android.os.Bundle;
24import android.util.AttributeSet;
25import android.view.SurfaceHolder;
26import android.view.SurfaceView;
27import android.view.TextureView;
28import android.view.View;
29import android.view.ViewGroup;
30import android.transition.Crossfade;
31import android.transition.ChangeBounds;
32import android.transition.Scene;
33import android.transition.TransitionSet;
34import android.transition.TransitionManager;
35import android.widget.Button;
36
37import static android.widget.LinearLayout.LayoutParams;
38
39public class SurfaceAndTextureViews extends Activity {
40
41    SimpleView mView;
42    SimpleSurfaceView mSurfaceView;
43    SimpleTextureView mTextureView;
44    private static final int SMALL_SIZE = 200;
45
46    @Override
47    public void onCreate(Bundle savedInstanceState) {
48        super.onCreate(savedInstanceState);
49        setContentView(R.layout.surface_texture_views);
50
51        final ViewGroup container = findViewById(R.id.container);
52        Button toggleButton = findViewById(R.id.toggleButton);
53
54        mView = new SimpleView(this);
55        mView.setId(0);
56        mView.setLayoutParams(new LayoutParams(SMALL_SIZE, SMALL_SIZE));
57        container.addView(mView);
58
59        mSurfaceView = new SimpleSurfaceView(this);
60        mSurfaceView.setId(1);
61        mSurfaceView.setLayoutParams(new LayoutParams(SMALL_SIZE, SMALL_SIZE));
62        container.addView(mSurfaceView);
63
64        mTextureView = new SimpleTextureView(this);
65        mTextureView.setId(2);
66        mTextureView.setLayoutParams(new LayoutParams(SMALL_SIZE, SMALL_SIZE));
67        container.addView(mTextureView);
68
69        final TransitionSet transition = new TransitionSet();
70        transition.addTransition(new ChangeBounds()).addTransition(new Crossfade().addTarget(0).
71                addTarget(1).addTarget(2));
72
73        toggleButton.setOnClickListener(new View.OnClickListener() {
74            @Override
75            public void onClick(View v) {
76                Scene newScene = new Scene(container);
77                newScene.setEnterAction(new Runnable() {
78                    @Override
79                    public void run() {
80                        if (mView.getWidth() <= SMALL_SIZE) {
81                            mView.setLayoutParams(new LayoutParams(SMALL_SIZE * 2, SMALL_SIZE));
82                            mSurfaceView.setLayoutParams(new LayoutParams(SMALL_SIZE * 2, SMALL_SIZE));
83                            mTextureView.setLayoutParams(new LayoutParams(SMALL_SIZE * 2, SMALL_SIZE));
84                            mView.mColor = SimpleView.LARGE_COLOR;
85                            mSurfaceView.mColor = SimpleSurfaceView.LARGE_COLOR;
86                            mTextureView.mColor = SimpleTextureView.LARGE_COLOR;
87                        } else {
88                            mView.setLayoutParams(new LayoutParams(SMALL_SIZE, SMALL_SIZE));
89                            mSurfaceView.setLayoutParams(new LayoutParams(SMALL_SIZE, SMALL_SIZE));
90                            mTextureView.setLayoutParams(new LayoutParams(SMALL_SIZE, SMALL_SIZE));
91                            mView.mColor = SimpleView.SMALL_COLOR;
92                            mSurfaceView.mColor = SimpleSurfaceView.SMALL_COLOR;
93                            mTextureView.mColor = SimpleTextureView.SMALL_COLOR;
94                        }
95                    }
96                });
97                TransitionManager.go(newScene, transition);
98            }
99        });
100
101    }
102
103    static private class SimpleView extends View {
104        static final int SMALL_COLOR = Color.BLUE;
105        static final int LARGE_COLOR = Color.YELLOW;
106        int mColor = SMALL_COLOR;
107
108        private SimpleView(Context context) {
109            super(context);
110        }
111
112        @Override
113        protected void onDraw(Canvas canvas) {
114            canvas.drawColor(mColor);
115        }
116    }
117
118    static private class SimpleSurfaceView extends SurfaceView implements SurfaceHolder.Callback {
119
120        static final int SMALL_COLOR = Color.GREEN;
121        static final int LARGE_COLOR = Color.GRAY;
122        int mColor = SMALL_COLOR;
123        SurfaceHolder mHolder = null;
124
125        private SimpleSurfaceView(Context context) {
126            super(context);
127            SurfaceHolder holder = getHolder();
128            holder.addCallback(this);
129        }
130
131
132        @Override
133        public void surfaceCreated(SurfaceHolder holder) {
134            System.out.println("surfaceCreated");
135        }
136
137        @Override
138        public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
139            System.out.println("surfaceChanged: w h = " + width + ", " + height);
140            Canvas canvas = holder.lockCanvas();
141            canvas.drawColor(mColor);
142            holder.unlockCanvasAndPost(canvas);
143        }
144
145        @Override
146        public void surfaceDestroyed(SurfaceHolder holder) {
147            System.out.println("surfaceDestroyed");
148        }
149    }
150
151    static private class SimpleTextureView extends TextureView implements TextureView.SurfaceTextureListener {
152
153        static final int SMALL_COLOR = Color.RED;
154        static final int LARGE_COLOR = Color.CYAN;
155        int mColor = SMALL_COLOR;
156
157        private SimpleTextureView(Context context) {
158            super(context);
159            setSurfaceTextureListener(this);
160        }
161
162        private SimpleTextureView(Context context, AttributeSet attrs) {
163            super(context, attrs);
164            setSurfaceTextureListener(this);
165        }
166
167        private SimpleTextureView(Context context, AttributeSet attrs, int defStyle) {
168            super(context, attrs, defStyle);
169            setSurfaceTextureListener(this);
170        }
171
172        @Override
173        public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
174            System.out.println("SurfaceTexture available");
175        }
176
177        @Override
178        public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
179            System.out.println("SurfaceTexture size changed to " + width + ", " + height);
180            Canvas canvas = lockCanvas();
181            canvas.drawColor(mColor);
182            unlockCanvasAndPost(canvas);
183        }
184
185        @Override
186        public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
187            return false;
188        }
189
190        @Override
191        public void onSurfaceTextureUpdated(SurfaceTexture surface) {
192            System.out.println("SurfaceTexture updated");
193        }
194    }
195}
196