1 /* 2 * Copyright (C) 2023 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.federatedcompute; 18 19 import android.annotation.NonNull; 20 import android.app.Service; 21 import android.content.Intent; 22 import android.federatedcompute.aidl.IExampleStoreCallback; 23 import android.federatedcompute.aidl.IExampleStoreService; 24 import android.os.Bundle; 25 import android.os.IBinder; 26 27 /** 28 * The abstract base class that client apps hosting their own Example Stores must implement. 29 * 30 * <p>The FederatedCompute will call into client apps' implementations to fetch data to use during 31 * the training of new models or get the aggregation analytic result. Apps must add a {@code 32 * <service>} entry to their manifest so that the FederatedCompute can bind to their implementation, 33 * like so: 34 * 35 * <pre>{@code 36 * <application> 37 * <service android:enabled="true" android:exported="true" android:name=".YourServiceClass"> 38 * <intent-filter> 39 * <action android:name="com.android.federatedcompute.EXAMPLE_STORE"/> 40 * <data android:scheme="app"/> 41 * </intent-filter> 42 * </service> 43 * </application> 44 * }</pre> 45 * 46 * @hide 47 */ 48 public abstract class ExampleStoreService extends Service { 49 private static final String TAG = "ExampleStoreService"; 50 private IBinder mIBinder; 51 52 @Override onCreate()53 public void onCreate() { 54 mIBinder = new ServiceBinder(); 55 } 56 57 @Override onBind(Intent intent)58 public IBinder onBind(Intent intent) { 59 return mIBinder; 60 } 61 62 class ServiceBinder extends IExampleStoreService.Stub { 63 @Override startQuery(Bundle params, IExampleStoreCallback callback)64 public void startQuery(Bundle params, IExampleStoreCallback callback) { 65 ExampleStoreService.this.startQuery( 66 params, new ExampleStoreQueryCallbackImpl(callback)); 67 } 68 } 69 /** 70 * The abstract method that client apps should implement to start a new example store query 71 * using the given selection criteria. 72 */ startQuery(@onNull Bundle params, @NonNull QueryCallback callback)73 public abstract void startQuery(@NonNull Bundle params, @NonNull QueryCallback callback); 74 /** 75 * The client apps use this callback to return their ExampleStoreIterator implementation to the 76 * federated training service. 77 */ 78 public interface QueryCallback { 79 /** Called when the iterator is ready for use. */ onStartQuerySuccess(@onNull ExampleStoreIterator iterator)80 void onStartQuerySuccess(@NonNull ExampleStoreIterator iterator); 81 /** Called when an error occurred and the iterator cannot not be created. */ onStartQueryFailure(int errorCode)82 void onStartQueryFailure(int errorCode); 83 } 84 } 85