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 com.android.ondevicepersonalization.services.federatedcompute; 18 19 import android.annotation.NonNull; 20 21 import java.io.ByteArrayInputStream; 22 import java.io.ByteArrayOutputStream; 23 import java.io.IOException; 24 import java.io.ObjectInputStream; 25 import java.io.ObjectOutputStream; 26 import java.io.Serializable; 27 28 /** 29 * ContextData object to pass to federatedcompute 30 * TODO(278106108): Move this class depending on scheduling impl. 31 */ 32 public class ContextData implements Serializable { 33 @NonNull 34 String mPackageName; 35 36 @NonNull 37 String mClassName; 38 ContextData(@onNull String packageName, @NonNull String className)39 public ContextData(@NonNull String packageName, @NonNull String className) { 40 this.mPackageName = packageName; 41 this.mClassName = className; 42 } 43 44 /** 45 * Converts the given ContextData into a serialized byte[] 46 */ toByteArray(ContextData contextData)47 public static byte[] toByteArray(ContextData contextData) throws IOException { 48 try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); 49 ObjectOutputStream objectOutputStream = new ObjectOutputStream( 50 byteArrayOutputStream)) { 51 objectOutputStream.writeObject(contextData); 52 return byteArrayOutputStream.toByteArray(); 53 } 54 } 55 56 /** 57 * Converts the given serialized byte[] into a ContextData object 58 */ fromByteArray(byte[] arr)59 public static ContextData fromByteArray(byte[] arr) throws IOException, ClassNotFoundException { 60 try (ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(arr); 61 ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream)) { 62 return (ContextData) objectInputStream.readObject(); 63 } 64 } 65 66 @NonNull getPackageName()67 public String getPackageName() { 68 return mPackageName; 69 } 70 71 @NonNull getClassName()72 public String getClassName() { 73 return mClassName; 74 } 75 } 76