OnDevicePersonalizationManager

public class OnDevicePersonalizationManager
继承自 Object

java.lang.Object
   ↳ android.adservices.ondevicepersonalization.OnDevicePersonalizationManager


OnDevicePersonalizationManager 提供了 API,供应用在隔离的进程中加载 IsolatedService 并与其交互。应用可以请求 IsolatedService 在应用的视图层次结构中的 SurfaceView 内生成要显示的内容,还可以将持久化结果写入设备存储,这些结果可供 Federated Analytics 用于跨设备统计分析,或供 Federated Learning 用于模型训练。显示的内容和持久化输出均不能被调用应用直接访问。

摘要

公共方法

void execute(ComponentName handler, PersistableBundle params, Executor executor, OutcomeReceiver<List<SurfacePackageToken>, Exception> receiver)

在 OnDevicePersonalization 沙盒中执行 IsolatedService

void requestSurfacePackage(SurfacePackageToken surfacePackageToken, IBinder surfaceViewHostToken, int displayId, int width, int height, Executor executor, OutcomeReceiver<SurfaceControlViewHost.SurfacePackageException> receiver)

请求将 SurfaceControlViewHost.SurfacePackage 插入调用应用中的 SurfaceView 中。

继承的方法

公共方法

execute

public void execute (ComponentName handler, 
                PersistableBundle params, 
                Executor executor, 
                OutcomeReceiver<List<SurfacePackageToken>, Exception> receiver)

在 OnDevicePersonalization 沙盒中执行 IsolatedService。平台会在隔离进程中绑定到指定的 IsolatedService,并使用调用者提供的参数调用 IsolatedWorker#onExecute(ExecuteInput, java.util.function.Consumer)。当 IsolatedService 执行完毕后,平台会向调用者返回引用服务结果的令牌。这些令牌随后可用于在调用应用中的 SurfaceView 中显示结果。

参数
handler ComponentNameIsolatedServiceComponentName。此值不能为 null

params PersistableBundle:一个从调用应用传递给 IsolatedServicePersistableBundle。此参数的预期内容由 IsolatedService 定义。平台不解释此参数。此值不能为 null

executor Executor:用于调用回调的 Executor。此值不能为 null。回调和监听器事件通过此 Executor 分派,从而轻松控制使用哪个线程。要通过应用的主线程分派事件,您可以使用 Context.getMainExecutor()。否则,请提供一个可分派到适当线程的 Executor

receiver OutcomeReceiver:此参数返回一个 SurfacePackageToken 对象列表,其中每个对象都是 IsolatedService 返回的 RenderingConfig 的不透明引用;如果失败,则返回 Exception。返回的 SurfacePackageToken 对象可在后续的 requestSurfacePackage(android.adservices.ondevicepersonalization.SurfacePackageToken, android.os.IBinder, int, int, int, java.util.concurrent.Executor, android.os.OutcomeReceiver) 调用中使用,以便在视图中显示结果。调用应用和 IsolatedService 必须就此列表的预期大小达成一致。返回的 SurfacePackageToken 对象列表中的条目可能为 null,表示服务对特定 Surface 没有输出。如果发生错误,接收器会返回以下异常之一:如果处理程序软件包未安装或没有有效的 ODP 清单,则返回 PackageManager.NameNotFoundException。如果找不到处理程序类,则返回 ClassNotFoundException。如果处理程序执行失败,则返回 OnDevicePersonalizationException

requestSurfacePackage

public void requestSurfacePackage (SurfacePackageToken surfacePackageToken, 
                IBinder surfaceViewHostToken, 
                int displayId, 
                int width, 
                int height, 
                Executor executor, 
                OutcomeReceiver<SurfaceControlViewHost.SurfacePackageException> receiver)

请求将 SurfaceControlViewHost.SurfacePackage 插入调用应用中的 SurfaceView 中。SurfacePackage 将包含一个 View,其中包含先前在 OnDevicePersonalization 沙盒中运行的 #execute(ComponentName, PersistableBundle, Executor, OutcomeReceiver) 调用结果中的内容。

参数
surfacePackageToken SurfacePackageToken:对先前对 #execute(ComponentName, PersistableBundle, Executor, OutcomeReceiver) 的调用返回的 SurfacePackageToken 的引用。此值不能为 null

surfaceViewHostToken IBinderSurfaceView 的 hostToken,在 SurfaceView 被添加到视图层次结构后由 SurfaceView.getHostToken() 返回。此值不能为 null

displayId int:要显示 SurfaceControlViewHost.SurfacePackage 的逻辑显示器的整数 ID,由 Context.getDisplay().getDisplayId() 返回。

width intSurfaceControlViewHost.SurfacePackage 的宽度(以像素为单位)。

height intSurfaceControlViewHost.SurfacePackage 的高度(以像素为单位)。

executor Executor:用于调用回调的 Executor。此值不能为 null。回调和监听器事件通过此 Executor 分派,从而轻松控制使用哪个线程。要通过应用的主线程分派事件,您可以使用 Context.getMainExecutor()。否则,请提供一个可分派到适当线程的 Executor

receiver OutcomeReceiver:成功时返回 SurfaceControlViewHost.SurfacePackage,失败时返回 Exception。如果处理程序执行失败,异常类型为 OnDevicePersonalizationException。此值不能为 null