1 /* 2 * Copyright (C) 2024 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 package com.android.wallpaper.util 17 18 import android.os.Bundle 19 import android.os.Message 20 import android.view.SurfaceControlViewHost 21 import android.view.SurfaceHolder 22 import android.view.SurfaceView 23 import android.view.View 24 import android.view.ViewGroup 25 26 /** Util class to generate surface view requests and parse responses */ 27 object SurfaceViewUtils { 28 private const val KEY_HOST_TOKEN = "host_token" 29 const val KEY_VIEW_WIDTH = "width" 30 const val KEY_VIEW_HEIGHT = "height" 31 private const val KEY_SURFACE_PACKAGE = "surface_package" 32 private const val KEY_CALLBACK = "callback" 33 const val KEY_WALLPAPER_COLORS = "wallpaper_colors" 34 const val KEY_DISPLAY_ID = "display_id" 35 36 /** Create a surface view request. */ createSurfaceViewRequestnull37 fun createSurfaceViewRequest(surfaceView: SurfaceView, extras: Bundle?) = 38 Bundle().apply { 39 putBinder(KEY_HOST_TOKEN, surfaceView.getHostToken()) 40 // TODO(b/305258307): Figure out why SurfaceView.getDisplay returns null in small 41 // preview 42 surfaceView.display?.let { putInt(KEY_DISPLAY_ID, it.displayId) } 43 putInt(KEY_VIEW_WIDTH, surfaceView.width) 44 putInt(KEY_VIEW_HEIGHT, surfaceView.height) 45 extras?.let { putAll(it) } 46 } 47 48 /** Return the surface package. */ getSurfacePackagenull49 fun getSurfacePackage(bundle: Bundle): SurfaceControlViewHost.SurfacePackage? { 50 return bundle.getParcelable(KEY_SURFACE_PACKAGE) 51 } 52 53 /** Return the message callback. */ getCallbacknull54 fun getCallback(bundle: Bundle): Message? { 55 return bundle.getParcelable(KEY_CALLBACK) 56 } 57 58 /** Removes the view from its parent and attaches to the surface control */ attachViewnull59 fun SurfaceView.attachView(view: View, newWidth: Int = width, newHeight: Int = height) { 60 // Detach view from its parent, if the view has one 61 (view.parent as ViewGroup?)?.removeView(view) 62 val host = SurfaceControlViewHost(context, display, hostToken) 63 host.setView(view, newWidth, newHeight) 64 setChildSurfacePackage(checkNotNull(host.surfacePackage)) 65 } 66 67 interface SurfaceCallback : SurfaceHolder.Callback { surfaceCreatednull68 override fun surfaceCreated(holder: SurfaceHolder) {} 69 surfaceChangednull70 override fun surfaceChanged(holder: SurfaceHolder, format: Int, width: Int, height: Int) {} 71 surfaceDestroyednull72 override fun surfaceDestroyed(holder: SurfaceHolder) {} 73 } 74 } 75