1 /* 2 * Copyright (C) 2021 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 android.server.wm.other; 18 19 import static android.server.wm.app.Components.RenderService.BROADCAST_EMBED_CONTENT; 20 import static android.server.wm.app.Components.RenderService.EXTRAS_BUNDLE; 21 import static android.server.wm.app.Components.RenderService.EXTRAS_DISPLAY_ID; 22 import static android.server.wm.app.Components.RenderService.EXTRAS_HOST_TOKEN; 23 import static android.server.wm.app.Components.RenderService.EXTRAS_SURFACE_PACKAGE; 24 import static android.server.wm.app.Components.UnresponsiveActivity.EXTRA_ON_MOTIONEVENT_DELAY_MS; 25 26 import android.app.Activity; 27 import android.content.BroadcastReceiver; 28 import android.content.ComponentName; 29 import android.content.Context; 30 import android.content.Intent; 31 import android.content.IntentFilter; 32 import android.content.ServiceConnection; 33 import android.os.Bundle; 34 import android.os.IBinder; 35 import android.view.SurfaceControlViewHost; 36 import android.view.SurfaceHolder; 37 import android.view.SurfaceView; 38 import android.view.ViewGroup; 39 import android.widget.RelativeLayout; 40 41 import java.util.concurrent.CountDownLatch; 42 43 44 public class HostActivity extends Activity implements SurfaceHolder.Callback{ 45 private SurfaceView mSurfaceView; 46 public CountDownLatch mEmbeddedViewAttachedLatch = new CountDownLatch(1); 47 private ServiceConnection mConnection = new ServiceConnection() { 48 @Override 49 public void onServiceConnected(ComponentName name, IBinder service) {} 50 51 @Override 52 public void onServiceDisconnected(ComponentName className) {} 53 }; 54 55 private final BroadcastReceiver mReceiver = new BroadcastReceiver() { 56 @Override 57 public void onReceive(Context context, Intent intent) { 58 SurfaceControlViewHost.SurfacePackage surfacePackage = 59 intent.getParcelableExtra(EXTRAS_SURFACE_PACKAGE); 60 if (surfacePackage != null) { 61 mSurfaceView.setChildSurfacePackage(surfacePackage); 62 mEmbeddedViewAttachedLatch.countDown(); 63 } 64 } 65 }; 66 67 @Override onCreate(Bundle savedInstanceState)68 public void onCreate(Bundle savedInstanceState) { 69 super.onCreate(savedInstanceState); 70 71 IntentFilter filter = new IntentFilter(BROADCAST_EMBED_CONTENT); 72 registerReceiver(mReceiver, filter, Context.RECEIVER_EXPORTED); 73 74 final RelativeLayout content = new RelativeLayout(this); 75 mSurfaceView = new SurfaceView(this); 76 mSurfaceView.setZOrderOnTop(true); 77 content.addView(mSurfaceView, 78 new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, 79 RelativeLayout.LayoutParams.MATCH_PARENT)); 80 setContentView(content, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 81 ViewGroup.LayoutParams.MATCH_PARENT)); 82 mSurfaceView.getHolder().addCallback(this); 83 } 84 85 @Override onPause()86 protected void onPause() { 87 unregisterReceiver(mReceiver); 88 super.onPause(); 89 } 90 91 @Override surfaceCreated(SurfaceHolder holder)92 public void surfaceCreated(SurfaceHolder holder) { 93 Intent mIntent = new Intent(); 94 mIntent.setComponent(new ComponentName( 95 "android.server.wm.app", "android.server.wm.app.RenderService")); 96 Bundle bundle = new Bundle(); 97 bundle.putBinder(EXTRAS_HOST_TOKEN, mSurfaceView.getHostToken()); 98 bundle.putInt(EXTRAS_DISPLAY_ID, getDisplay().getDisplayId()); 99 bundle.putInt(EXTRA_ON_MOTIONEVENT_DELAY_MS, 60000); 100 mIntent.putExtra(EXTRAS_BUNDLE, bundle); 101 bindService(mIntent, mConnection, Context.BIND_AUTO_CREATE | Context.BIND_IMPORTANT); 102 } 103 104 @Override surfaceChanged(SurfaceHolder holder, int format, int width, int height)105 public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {} 106 107 @Override surfaceDestroyed(SurfaceHolder holder)108 public void surfaceDestroyed(SurfaceHolder holder) {} 109 } 110