1 /* 2 * Copyright (C) 2021 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.car.internal.test; 18 19 import android.app.Service; 20 import android.car.apitest.IStableAIDLTestBinder; 21 import android.car.apitest.IStableAIDLTestCallback; 22 import android.car.apitest.StableAIDLTestLargeParcelable; 23 import android.content.Intent; 24 import android.os.IBinder; 25 import android.os.RemoteException; 26 import android.os.ServiceSpecificException; 27 28 import com.android.car.internal.LargeParcelable; 29 30 public final class IStableAIDLBinderTestService extends Service { 31 private static final String TAG = IStableAIDLBinderTestService.class.getSimpleName(); 32 33 private final IStableAIDLBinderTestImpl mBinder = new IStableAIDLBinderTestImpl(); 34 35 @Override onBind(Intent intent)36 public IBinder onBind(Intent intent) { 37 return mBinder; 38 } 39 40 // This class shows how binder call is wrapped to make it more efficient with shared memory. 41 // Most code is copied from auto-generated code with only small changes. 42 private static final class IStableAIDLBinderTestImpl extends IStableAIDLTestBinder.Stub { 43 // copied due to package scope. 44 static final int TRANSACTION_echo = (android.os.IBinder.FIRST_CALL_TRANSACTION + 0); 45 46 @Override echo(StableAIDLTestLargeParcelable p)47 public StableAIDLTestLargeParcelable echo(StableAIDLTestLargeParcelable p) { 48 return p; 49 } 50 51 @Override echoWithLong(StableAIDLTestLargeParcelable p, long v)52 public long echoWithLong(StableAIDLTestLargeParcelable p, long v) { 53 StableAIDLTestLargeParcelable r = 54 (StableAIDLTestLargeParcelable) LargeParcelable.reconstructStableAIDLParcelable( 55 p, false); 56 return calcByteSum(r) + v; 57 } 58 59 @Override echoWithCallback(IStableAIDLTestCallback callback, StableAIDLTestLargeParcelable p)60 public void echoWithCallback(IStableAIDLTestCallback callback, 61 StableAIDLTestLargeParcelable p) { 62 try { 63 callback.reply(p); 64 } catch (RemoteException e) { 65 throw new ServiceSpecificException(-1, "failed to send reply: " + e.toString()); 66 } 67 } 68 } 69 calcByteSum(StableAIDLTestLargeParcelable p)70 public static long calcByteSum(StableAIDLTestLargeParcelable p) { 71 long ret = 0; 72 if (p != null && p.payload != null) { 73 for (int i = 0; i < p.payload.length; i++) { 74 ret = ret + p.payload[i]; 75 } 76 } 77 return ret; 78 } 79 } 80