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 17 package com.android.ondevicepersonalization.services.inference; 18 19 import static com.android.ondevicepersonalization.services.process.SharedIsolatedProcessRunner.TRUSTED_PARTNER_APPS_SIP; 20 21 import android.adservices.ondevicepersonalization.aidl.IIsolatedModelService; 22 import android.content.Context; 23 24 import com.android.federatedcompute.internal.util.AbstractServiceBinder; 25 import com.android.modules.utils.build.SdkLevel; 26 27 /** 28 * Provides {@link IsolatedModelService}. 29 */ 30 public class IsolatedModelServiceProvider { 31 public static final String ISOLATED_MODEL_SERVICE_NAME = 32 "com.android.ondevicepersonalization.services.inference.IsolatedModelService"; 33 private AbstractServiceBinder<IIsolatedModelService> mModelService; 34 35 36 /** 37 * Returns {@link IIsolatedModelService}. 38 */ getModelService(Context context)39 public IIsolatedModelService getModelService(Context context) { 40 // Inference service should always run in the trusted SIP. 41 String instanceName = SdkLevel.isAtLeastU() ? TRUSTED_PARTNER_APPS_SIP : null; 42 int bindFlag = SdkLevel.isAtLeastU() 43 ? Context.BIND_SHARED_ISOLATED_PROCESS 44 : Context.BIND_AUTO_CREATE; 45 mModelService = 46 AbstractServiceBinder.getIsolatedServiceBinderByServiceName( 47 context, 48 ISOLATED_MODEL_SERVICE_NAME, 49 context.getPackageName(), 50 instanceName, bindFlag, IIsolatedModelService.Stub::asInterface); 51 return mModelService.getService(Runnable::run); 52 } 53 54 /** 55 * Unbind {@link IsolatedModelService}. 56 */ unBindFromModelService()57 public void unBindFromModelService() { 58 mModelService.unbindFromService(); 59 } 60 } 61