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.SurfaceControl; 26 import android.view.SurfaceHolder; 27 import android.view.SurfaceView; 28 import android.view.View; 29 import android.view.WindowManager; 30 import android.view.SurfaceControlViewHost; 31 import android.widget.Button; 32 import android.widget.FrameLayout; 33 34 35 public class SurfaceControlViewHostTest extends Activity implements SurfaceHolder.Callback{ 36 SurfaceView mView; 37 SurfaceControlViewHost mVr; 38 onCreate(Bundle savedInstanceState)39 protected void onCreate(Bundle savedInstanceState) { 40 FrameLayout content = new FrameLayout(this); 41 super.onCreate(savedInstanceState); 42 mView = new SurfaceView(this); 43 content.addView(mView, new FrameLayout.LayoutParams( 44 500, 500, Gravity.CENTER_HORIZONTAL | Gravity.TOP)); 45 setContentView(content); 46 47 mView.setZOrderOnTop(true); 48 mView.getHolder().addCallback(this); 49 50 addEmbeddedView(); 51 } 52 addEmbeddedView()53 void addEmbeddedView() { 54 mVr = new SurfaceControlViewHost(this, this.getDisplay(), 55 mView.getHostToken()); 56 57 mView.setChildSurfacePackage(mVr.getSurfacePackage()); 58 59 Button v = new Button(this); 60 v.setBackgroundColor(Color.BLUE); 61 v.setOnClickListener(new View.OnClickListener() { 62 public void onClick(View v) { 63 v.setBackgroundColor(Color.RED); 64 } 65 }); 66 WindowManager.LayoutParams lp = 67 new WindowManager.LayoutParams(500, 500, WindowManager.LayoutParams.TYPE_APPLICATION, 68 0, PixelFormat.OPAQUE); 69 mVr.setView(v, lp); 70 } 71 72 @Override surfaceCreated(SurfaceHolder holder)73 public void surfaceCreated(SurfaceHolder holder) { 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