1 /* 2 * Copyright (C) 2019 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 17 package com.android.test.viewembed; 18 19 import android.app.Activity; 20 import android.graphics.Canvas; 21 import android.graphics.Color; 22 import android.graphics.PixelFormat; 23 import android.os.Bundle; 24 import android.view.Gravity; 25 import android.view.SurfaceControlViewHost; 26 import android.view.SurfaceHolder; 27 import android.view.SurfaceView; 28 import android.view.View; 29 import android.view.WindowManager; 30 import android.widget.Button; 31 import android.widget.FrameLayout; 32 33 34 public class SurfaceControlViewHostTest extends Activity implements SurfaceHolder.Callback{ 35 SurfaceView mView; 36 SurfaceControlViewHost mVr; 37 onCreate(Bundle savedInstanceState)38 protected void onCreate(Bundle savedInstanceState) { 39 FrameLayout content = new FrameLayout(this); 40 super.onCreate(savedInstanceState); 41 mView = new SurfaceView(this); 42 content.addView(mView, new FrameLayout.LayoutParams( 43 500, 500, Gravity.CENTER_HORIZONTAL | Gravity.TOP)); 44 setContentView(content); 45 46 mView.setZOrderOnTop(true); 47 mView.getHolder().addCallback(this); 48 } 49 addEmbeddedView()50 void addEmbeddedView() { 51 mVr = new SurfaceControlViewHost(this, this.getDisplay(), 52 mView.getHostToken()); 53 54 mView.setChildSurfacePackage(mVr.getSurfacePackage()); 55 56 Button v = new Button(this); 57 v.setBackgroundColor(Color.BLUE); 58 v.setOnClickListener(new View.OnClickListener() { 59 public void onClick(View v) { 60 v.setBackgroundColor(Color.RED); 61 } 62 }); 63 v.getViewTreeObserver().addOnWindowFocusChangeListener(focused -> 64 v.setBackgroundColor(focused ? Color.MAGENTA : Color.DKGRAY)); 65 WindowManager.LayoutParams lp = 66 new WindowManager.LayoutParams(500, 500, WindowManager.LayoutParams.TYPE_APPLICATION, 67 0, PixelFormat.OPAQUE); 68 mVr.setView(v, lp); 69 } 70 71 @Override surfaceCreated(SurfaceHolder holder)72 public void surfaceCreated(SurfaceHolder holder) { 73 addEmbeddedView(); 74 } 75 76 @Override surfaceChanged(SurfaceHolder holder, int format, int width, int height)77 public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { 78 Canvas canvas = holder.lockCanvas(); 79 canvas.drawColor(Color.GREEN); 80 holder.unlockCanvasAndPost(canvas); 81 } 82 83 @Override surfaceDestroyed(SurfaceHolder holder)84 public void surfaceDestroyed(SurfaceHolder holder) { 85 } 86 } 87