1 /* 2 * Copyright 2021 Google LLC 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 * https://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.google.android.enterprise.connectedapps; 17 18 import android.os.Bundle; 19 import android.os.Parcel; 20 import android.os.RemoteException; 21 import com.google.android.enterprise.connectedapps.internal.Bundler; 22 import com.google.android.enterprise.connectedapps.internal.ByteUtilities; 23 import com.google.auto.value.AutoValue; 24 import org.checkerframework.checker.nullness.qual.Nullable; 25 26 public class TestService extends ICrossProfileService.Stub { 27 28 @AutoValue 29 abstract static class LoggedCrossProfileMethodCall { getCrossProfileTypeIdentifier()30 abstract long getCrossProfileTypeIdentifier(); 31 getMethodIdentifier()32 abstract long getMethodIdentifier(); 33 getParams()34 abstract Bundle getParams(); 35 36 @Nullable callback()37 abstract ICrossProfileCallback callback(); 38 create( long crossProfileTypeIdentifier, long methodIdentifier, Bundle params, ICrossProfileCallback callback)39 static LoggedCrossProfileMethodCall create( 40 long crossProfileTypeIdentifier, 41 long methodIdentifier, 42 Bundle params, 43 ICrossProfileCallback callback) { 44 return new AutoValue_TestService_LoggedCrossProfileMethodCall( 45 crossProfileTypeIdentifier, methodIdentifier, params, callback); 46 } 47 } 48 49 private LoggedCrossProfileMethodCall lastCall; 50 private Bundle responseBundle = new Bundle(Bundler.class.getClassLoader()); 51 lastCall()52 LoggedCrossProfileMethodCall lastCall() { 53 return lastCall; 54 } 55 56 /** Set the bundle to be returned from a call to this service. */ setResponseBundle(Bundle responseBundle)57 void setResponseBundle(Bundle responseBundle) { 58 this.responseBundle = responseBundle; 59 } 60 61 @Override prepareCall(long callId, int blockId, int numBytes, byte[] paramsBytes)62 public void prepareCall(long callId, int blockId, int numBytes, byte[] paramsBytes) {} 63 64 @Override prepareBundle(long callId, int blockId, Bundle bundle)65 public void prepareBundle(long callId, int blockId, Bundle bundle) {} 66 67 @Override call( long callId, int blockId, long crossProfileTypeIdentifier, int methodIdentifier, byte[] paramsBytes, ICrossProfileCallback callback)68 public byte[] call( 69 long callId, 70 int blockId, 71 long crossProfileTypeIdentifier, 72 int methodIdentifier, 73 byte[] paramsBytes, 74 ICrossProfileCallback callback) 75 throws RemoteException { 76 77 Parcel parcel = Parcel.obtain(); // Recycled by this method on next call 78 parcel.unmarshall(paramsBytes, 0, paramsBytes.length); 79 parcel.setDataPosition(0); 80 Bundle bundle = new Bundle(Bundler.class.getClassLoader()); 81 bundle.readFromParcel(parcel); 82 83 lastCall = 84 LoggedCrossProfileMethodCall.create( 85 crossProfileTypeIdentifier, methodIdentifier, bundle, callback); 86 87 Parcel responseParcel = Parcel.obtain(); 88 responseBundle.writeToParcel(responseParcel, /* flags= */ 0); 89 byte[] parcelBytes = responseParcel.marshall(); 90 responseParcel.recycle(); 91 92 return prepareResponse(parcelBytes); 93 } 94 prepareResponse(byte[] parcelBytes)95 private static byte[] prepareResponse(byte[] parcelBytes) { 96 // This doesn't deal with large responses. 97 return ByteUtilities.joinByteArrays(new byte[] {0}, parcelBytes); 98 } 99 100 @Override fetchResponse(long callId, int blockId)101 public byte[] fetchResponse(long callId, int blockId) { 102 return null; 103 } 104 105 @Override fetchResponseBundle(long callId, int bundleId)106 public Bundle fetchResponseBundle(long callId, int bundleId) { 107 return null; 108 } 109 } 110