1 /* 2 * Copyright (C) 2022 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 static android.view.Display.DEFAULT_DISPLAY; 20 import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION; 21 22 import android.annotation.Nullable; 23 import android.app.Service; 24 import android.content.Context; 25 import android.content.Intent; 26 import android.graphics.Color; 27 import android.graphics.PixelFormat; 28 import android.hardware.display.DisplayManager; 29 import android.os.Handler; 30 import android.os.IBinder; 31 import android.os.Looper; 32 import android.os.RemoteException; 33 import android.view.Display; 34 import android.view.Gravity; 35 import android.view.SurfaceControlViewHost; 36 import android.view.WindowManager; 37 import android.widget.FrameLayout; 38 import android.widget.TextView; 39 40 public class EmbeddedWindowService extends Service { 41 private static final String TAG = "EmbeddedWindowService"; 42 private SurfaceControlViewHost mVr; 43 44 private Handler mHandler; 45 46 @Override onCreate()47 public void onCreate() { 48 super.onCreate(); 49 mHandler = new Handler(Looper.getMainLooper()); 50 } 51 52 @Nullable 53 @Override onBind(Intent intent)54 public IBinder onBind(Intent intent) { 55 // Return the interface 56 return new AttachEmbeddedWindow(); 57 } 58 59 public static class SlowView extends TextView { 60 SlowView(Context context)61 public SlowView(Context context) { 62 super(context); 63 } 64 65 @Override onMeasure(int widthMeasureSpec, int heightMeasureSpec)66 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 67 super.onMeasure(widthMeasureSpec, heightMeasureSpec); 68 try { 69 Thread.sleep(250); 70 } catch (InterruptedException e) { 71 } 72 } 73 } 74 75 private class AttachEmbeddedWindow extends IAttachEmbeddedWindow.Stub { 76 @Override attachEmbedded(IBinder hostToken, int width, int height, IAttachEmbeddedWindowCallback callback)77 public void attachEmbedded(IBinder hostToken, int width, int height, 78 IAttachEmbeddedWindowCallback callback) { 79 mHandler.post(() -> { 80 Context context = EmbeddedWindowService.this; 81 Display display = getApplicationContext().getSystemService( 82 DisplayManager.class).getDisplay(DEFAULT_DISPLAY); 83 mVr = new SurfaceControlViewHost(context, display, hostToken); 84 FrameLayout content = new FrameLayout(context); 85 86 SlowView slowView = new SlowView(context); 87 slowView.setText("INSIDE TEXT"); 88 slowView.setGravity(Gravity.CENTER); 89 slowView.setTextColor(Color.BLACK); 90 slowView.setBackgroundColor(Color.CYAN); 91 content.addView(slowView); 92 WindowManager.LayoutParams lp = 93 new WindowManager.LayoutParams(width, height, TYPE_APPLICATION, 94 0, PixelFormat.OPAQUE); 95 lp.setTitle("EmbeddedWindow"); 96 97 mVr.setView(content, lp); 98 try { 99 callback.onEmbeddedWindowAttached(mVr.getSurfacePackage()); 100 } catch (RemoteException e) { 101 } 102 }); 103 } 104 @Override relayout(WindowManager.LayoutParams lp)105 public void relayout(WindowManager.LayoutParams lp) { 106 mHandler.post(() -> mVr.relayout(lp)); 107 } 108 } 109 } 110