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 android.aidl.service; 18 19 import android.aidl.tests.BackendType; 20 import android.aidl.tests.ByteEnum; 21 import android.aidl.tests.ConstantExpressionEnum; 22 import android.aidl.tests.GenericStructuredParcelable; 23 import android.aidl.tests.ICppJavaTests; 24 import android.aidl.tests.INamedCallback; 25 import android.aidl.tests.INewName; 26 import android.aidl.tests.IOldName; 27 import android.aidl.tests.ITestService; 28 import android.aidl.tests.IntEnum; 29 import android.aidl.tests.LongEnum; 30 import android.aidl.tests.SimpleParcelable; 31 import android.aidl.tests.StructuredParcelable; 32 import android.aidl.tests.Union; 33 import android.aidl.tests.extension.ExtendableParcelable; 34 import android.aidl.tests.extension.MyExt; 35 import android.aidl.versioned.tests.BazUnion; 36 import android.aidl.versioned.tests.Foo; 37 import android.aidl.versioned.tests.IFooInterface; 38 import android.os.Binder; 39 import android.os.IBinder; 40 import android.os.Parcel; 41 import android.os.ParcelFileDescriptor; 42 import android.os.PersistableBundle; 43 import android.os.RemoteException; 44 import android.os.ServiceManager; 45 import android.os.ServiceSpecificException; 46 import android.util.Log; 47 import java.io.FileDescriptor; 48 import java.io.IOException; 49 import java.util.ArrayList; 50 import java.util.HashMap; 51 import java.util.List; 52 53 public class TestServiceServer extends ITestService.Stub { main(String[] args)54 public static void main(String[] args) { 55 TestServiceServer myServer = new TestServiceServer(); 56 ServiceManager.addService(ITestService.class.getName(), myServer); 57 58 FooInterface foo = new FooInterface(); 59 ServiceManager.addService(IFooInterface.class.getName(), foo); 60 61 Binder.joinThreadPool(); 62 } 63 64 private static class FooInterface extends IFooInterface.Stub { 65 @Override originalApi()66 public void originalApi() {} 67 @Override acceptUnionAndReturnString(BazUnion b)68 public String acceptUnionAndReturnString(BazUnion b) { 69 if (b.getTag() == BazUnion.intNum) { 70 return "" + b.getIntNum(); 71 } 72 throw new IllegalArgumentException(); 73 } 74 @Override returnsLengthOfFooArray(Foo[] foos)75 public int returnsLengthOfFooArray(Foo[] foos) { 76 return foos.length; 77 } 78 @Override ignoreParcelablesAndRepeatInt(Foo inFoo, Foo inoutFoo, Foo outFoo, int value)79 public int ignoreParcelablesAndRepeatInt(Foo inFoo, Foo inoutFoo, Foo outFoo, int value) { 80 return value; 81 } 82 @Override getInterfaceVersion()83 public final int getInterfaceVersion() { 84 return IFooInterface.VERSION; 85 } 86 @Override getInterfaceHash()87 public final String getInterfaceHash() { 88 return IFooInterface.HASH; 89 } 90 } 91 92 @Override onTransact(int code, Parcel data, Parcel reply, int flags)93 public boolean onTransact(int code, Parcel data, Parcel reply, int flags) throws RemoteException { 94 // UnimplementedMethod 95 if (code == 1) 96 return false; 97 return super.onTransact(code, data, reply, flags); 98 } 99 100 @Override UnimplementedMethod(int arg)101 public int UnimplementedMethod(int arg) throws RemoteException { 102 throw new UnsupportedOperationException(); 103 } 104 @Override TestOneway()105 public void TestOneway() throws RemoteException { 106 throw new RemoteException(); 107 } 108 @Override Deprecated()109 public void Deprecated() throws RemoteException {} 110 @Override RepeatBoolean(boolean token)111 public boolean RepeatBoolean(boolean token) throws RemoteException { 112 return token; 113 } 114 @Override RepeatByte(byte token)115 public byte RepeatByte(byte token) throws RemoteException { 116 return token; 117 } 118 @Override RepeatChar(char token)119 public char RepeatChar(char token) throws RemoteException { 120 return token; 121 } 122 @Override RepeatInt(int token)123 public int RepeatInt(int token) throws RemoteException { 124 return token; 125 } 126 @Override RepeatLong(long token)127 public long RepeatLong(long token) throws RemoteException { 128 return token; 129 } 130 @Override RepeatFloat(float token)131 public float RepeatFloat(float token) throws RemoteException { 132 return token; 133 } 134 @Override RepeatDouble(double token)135 public double RepeatDouble(double token) throws RemoteException { 136 return token; 137 } 138 @Override RepeatString(String token)139 public String RepeatString(String token) throws RemoteException { 140 return token; 141 } 142 @Override RepeatByteEnum(byte token)143 public byte RepeatByteEnum(byte token) throws RemoteException { 144 return token; 145 } 146 @Override RepeatIntEnum(int token)147 public int RepeatIntEnum(int token) throws RemoteException { 148 return token; 149 } 150 @Override RepeatLongEnum(long token)151 public long RepeatLongEnum(long token) throws RemoteException { 152 return token; 153 } 154 @Override ReverseBoolean(boolean[] input, boolean[] repeated)155 public boolean[] ReverseBoolean(boolean[] input, boolean[] repeated) throws RemoteException { 156 boolean[] reversed = new boolean[input.length]; 157 for (int i = 0; i < input.length; i++) { 158 repeated[i] = input[i]; 159 reversed[i] = input[input.length - i - 1]; 160 } 161 return reversed; 162 } 163 @Override ReverseByte(byte[] input, byte[] repeated)164 public byte[] ReverseByte(byte[] input, byte[] repeated) throws RemoteException { 165 byte[] reversed = new byte[input.length]; 166 for (int i = 0; i < input.length; i++) { 167 repeated[i] = input[i]; 168 reversed[i] = input[input.length - i - 1]; 169 } 170 return reversed; 171 } 172 @Override ReverseChar(char[] input, char[] repeated)173 public char[] ReverseChar(char[] input, char[] repeated) throws RemoteException { 174 char[] reversed = new char[input.length]; 175 for (int i = 0; i < input.length; i++) { 176 repeated[i] = input[i]; 177 reversed[i] = input[input.length - i - 1]; 178 } 179 return reversed; 180 } 181 @Override ReverseInt(int[] input, int[] repeated)182 public int[] ReverseInt(int[] input, int[] repeated) throws RemoteException { 183 int[] reversed = new int[input.length]; 184 for (int i = 0; i < input.length; i++) { 185 repeated[i] = input[i]; 186 reversed[i] = input[input.length - i - 1]; 187 } 188 return reversed; 189 } 190 @Override ReverseLong(long[] input, long[] repeated)191 public long[] ReverseLong(long[] input, long[] repeated) throws RemoteException { 192 long[] reversed = new long[input.length]; 193 for (int i = 0; i < input.length; i++) { 194 repeated[i] = input[i]; 195 reversed[i] = input[input.length - i - 1]; 196 } 197 return reversed; 198 } 199 @Override ReverseFloat(float[] input, float[] repeated)200 public float[] ReverseFloat(float[] input, float[] repeated) throws RemoteException { 201 float[] reversed = new float[input.length]; 202 for (int i = 0; i < input.length; i++) { 203 repeated[i] = input[i]; 204 reversed[i] = input[input.length - i - 1]; 205 } 206 return reversed; 207 } 208 @Override ReverseDouble(double[] input, double[] repeated)209 public double[] ReverseDouble(double[] input, double[] repeated) throws RemoteException { 210 double[] reversed = new double[input.length]; 211 for (int i = 0; i < input.length; i++) { 212 repeated[i] = input[i]; 213 reversed[i] = input[input.length - i - 1]; 214 } 215 return reversed; 216 } 217 @Override ReverseString(String[] input, String[] repeated)218 public String[] ReverseString(String[] input, String[] repeated) throws RemoteException { 219 String[] reversed = new String[input.length]; 220 for (int i = 0; i < input.length; i++) { 221 repeated[i] = input[i]; 222 reversed[i] = input[input.length - i - 1]; 223 } 224 return reversed; 225 } 226 @Override ReverseByteEnum(byte[] input, byte[] repeated)227 public byte[] ReverseByteEnum(byte[] input, byte[] repeated) throws RemoteException { 228 byte[] reversed = new byte[input.length]; 229 for (int i = 0; i < input.length; i++) { 230 repeated[i] = input[i]; 231 reversed[i] = input[input.length - i - 1]; 232 } 233 return reversed; 234 } 235 @Override ReverseIntEnum(int[] input, int[] repeated)236 public int[] ReverseIntEnum(int[] input, int[] repeated) throws RemoteException { 237 int[] reversed = new int[input.length]; 238 for (int i = 0; i < input.length; i++) { 239 repeated[i] = input[i]; 240 reversed[i] = input[input.length - i - 1]; 241 } 242 return reversed; 243 } 244 @Override ReverseLongEnum(long[] input, long[] repeated)245 public long[] ReverseLongEnum(long[] input, long[] repeated) throws RemoteException { 246 long[] reversed = new long[input.length]; 247 for (int i = 0; i < input.length; i++) { 248 repeated[i] = input[i]; 249 reversed[i] = input[input.length - i - 1]; 250 } 251 return reversed; 252 } 253 254 private static class MyNamedCallback extends INamedCallback.Stub { MyNamedCallback(String name)255 public MyNamedCallback(String name) { mName = name; } 256 @Override GetName()257 public String GetName() { 258 return mName; 259 } 260 private String mName; 261 } 262 263 private HashMap<String, MyNamedCallback> mNamedCallbacks = new HashMap<>(); 264 265 @Override GetOtherTestService(String name)266 public INamedCallback GetOtherTestService(String name) throws RemoteException { 267 if (!mNamedCallbacks.containsKey(name)) { 268 mNamedCallbacks.put(name, new MyNamedCallback(name)); 269 } 270 return mNamedCallbacks.get(name); 271 } 272 @Override VerifyName(INamedCallback service, String name)273 public boolean VerifyName(INamedCallback service, String name) throws RemoteException { 274 return name.equals(service.GetName()); 275 } 276 @Override ReverseStringList(List<String> input, List<String> repeated)277 public List<String> ReverseStringList(List<String> input, List<String> repeated) 278 throws RemoteException { 279 ArrayList<String> reversed = new ArrayList<String>(); 280 for (String a : input) { 281 repeated.add(a); 282 reversed.add(0, a); 283 } 284 return reversed; 285 } 286 @Override RepeatParcelFileDescriptor(ParcelFileDescriptor read)287 public ParcelFileDescriptor RepeatParcelFileDescriptor(ParcelFileDescriptor read) 288 throws RemoteException { 289 return read; 290 } 291 @Override ReverseParcelFileDescriptorArray( ParcelFileDescriptor[] input, ParcelFileDescriptor[] repeated)292 public ParcelFileDescriptor[] ReverseParcelFileDescriptorArray( 293 ParcelFileDescriptor[] input, ParcelFileDescriptor[] repeated) throws RemoteException { 294 ParcelFileDescriptor[] reversed = new ParcelFileDescriptor[input.length]; 295 for (int i = 0; i < input.length; i++) { 296 repeated[i] = input[i]; 297 try { 298 // extra dup, because of PARCELABLE_WRITE_RETURN_VALUE 299 reversed[i] = input[input.length - i - 1].dup(); 300 } catch (IOException e) { 301 throw new RuntimeException(e); 302 } 303 } 304 return reversed; 305 } 306 @Override ThrowServiceException(int code)307 public void ThrowServiceException(int code) throws RemoteException { 308 Log.i("TestServiceServer", "Throwing service specific exception " + code); 309 throw new ServiceSpecificException(code); 310 } 311 @Override RepeatNullableIntArray(int[] input)312 public int[] RepeatNullableIntArray(int[] input) throws RemoteException { 313 return input; 314 } 315 @Override RepeatNullableByteEnumArray(byte[] input)316 public byte[] RepeatNullableByteEnumArray(byte[] input) throws RemoteException { 317 return input; 318 } 319 @Override RepeatNullableIntEnumArray(int[] input)320 public int[] RepeatNullableIntEnumArray(int[] input) throws RemoteException { 321 return input; 322 } 323 @Override RepeatNullableLongEnumArray(long[] input)324 public long[] RepeatNullableLongEnumArray(long[] input) throws RemoteException { 325 return input; 326 } 327 @Override RepeatNullableString(String input)328 public String RepeatNullableString(String input) throws RemoteException { 329 return input; 330 } 331 @Override RepeatNullableStringList(List<String> input)332 public List<String> RepeatNullableStringList(List<String> input) throws RemoteException { 333 return input; 334 } 335 @Override RepeatNullableParcelable(StructuredParcelable input)336 public StructuredParcelable RepeatNullableParcelable(StructuredParcelable input) 337 throws RemoteException { 338 return input; 339 } 340 @Override TakesAnIBinder(IBinder input)341 public void TakesAnIBinder(IBinder input) throws RemoteException { 342 // do nothing 343 } 344 @Override TakesANullableIBinder(IBinder input)345 public void TakesANullableIBinder(IBinder input) throws RemoteException { 346 // do nothing 347 } 348 @Override RepeatUtf8CppString(String token)349 public String RepeatUtf8CppString(String token) throws RemoteException { 350 return token; 351 } 352 @Override RepeatNullableUtf8CppString(String token)353 public String RepeatNullableUtf8CppString(String token) throws RemoteException { 354 return token; 355 } 356 @Override ReverseUtf8CppString(String[] input, String[] repeated)357 public String[] ReverseUtf8CppString(String[] input, String[] repeated) throws RemoteException { 358 String[] reversed = new String[input.length]; 359 for (int i = 0; i < input.length; i++) { 360 repeated[i] = input[i]; 361 reversed[i] = input[input.length - i - 1]; 362 } 363 return reversed; 364 } 365 @Override ReverseNullableUtf8CppString(String[] input, String[] repeated)366 public String[] ReverseNullableUtf8CppString(String[] input, String[] repeated) 367 throws RemoteException { 368 if (input == null) 369 return null; 370 371 String[] reversed = new String[input.length]; 372 for (int i = 0; i < input.length; i++) { 373 repeated[i] = input[i]; 374 reversed[i] = input[input.length - i - 1]; 375 } 376 return reversed; 377 } 378 @Override ReverseUtf8CppStringList(List<String> input, List<String> repeated)379 public List<String> ReverseUtf8CppStringList(List<String> input, List<String> repeated) 380 throws RemoteException { 381 // note - cannot clear 'repeated' here, we can only change its length 382 if (input == null) 383 return null; 384 385 ArrayList<String> reversed = new ArrayList<String>(); 386 for (String a : input) { 387 repeated.add(a); 388 reversed.add(0, a); 389 } 390 return reversed; 391 } 392 @Override GetCallback(boolean return_null)393 public INamedCallback GetCallback(boolean return_null) throws RemoteException { 394 if (return_null) 395 return null; 396 return new MyNamedCallback("a callback named GetCallback"); 397 } 398 @Override FillOutStructuredParcelable(StructuredParcelable parcelable)399 public void FillOutStructuredParcelable(StructuredParcelable parcelable) throws RemoteException { 400 parcelable.shouldBeJerry = "Jerry"; 401 parcelable.shouldContainThreeFs = new int[] {parcelable.f, parcelable.f, parcelable.f}; 402 parcelable.shouldBeByteBar = ByteEnum.BAR; 403 parcelable.shouldBeIntBar = IntEnum.BAR; 404 parcelable.shouldBeLongBar = LongEnum.BAR; 405 parcelable.shouldContainTwoByteFoos = new byte[] {ByteEnum.FOO, ByteEnum.FOO}; 406 parcelable.shouldContainTwoIntFoos = new int[] {IntEnum.FOO, IntEnum.FOO}; 407 parcelable.shouldContainTwoLongFoos = new long[] {LongEnum.FOO, LongEnum.FOO}; 408 409 parcelable.const_exprs_1 = ConstantExpressionEnum.decInt32_1; 410 parcelable.const_exprs_2 = ConstantExpressionEnum.decInt32_2; 411 parcelable.const_exprs_3 = ConstantExpressionEnum.decInt64_1; 412 parcelable.const_exprs_4 = ConstantExpressionEnum.decInt64_2; 413 parcelable.const_exprs_5 = ConstantExpressionEnum.decInt64_3; 414 parcelable.const_exprs_6 = ConstantExpressionEnum.decInt64_4; 415 parcelable.const_exprs_7 = ConstantExpressionEnum.hexInt32_1; 416 parcelable.const_exprs_8 = ConstantExpressionEnum.hexInt32_2; 417 parcelable.const_exprs_9 = ConstantExpressionEnum.hexInt32_3; 418 parcelable.const_exprs_10 = ConstantExpressionEnum.hexInt64_1; 419 420 parcelable.shouldSetBit0AndBit2 = StructuredParcelable.BIT0 | StructuredParcelable.BIT2; 421 422 parcelable.u = Union.ns(new int[] {1, 2, 3}); 423 parcelable.shouldBeConstS1 = Union.s(Union.S1); 424 } 425 426 private static class MyOldName extends IOldName.Stub { 427 @Override RealName()428 public String RealName() { 429 return "OldName"; 430 } 431 } 432 433 @Override GetOldNameInterface()434 public IOldName GetOldNameInterface() throws RemoteException { 435 return new MyOldName(); 436 } 437 438 private static class MyNewName extends INewName.Stub { 439 @Override RealName()440 public String RealName() { 441 return "NewName"; 442 } 443 } 444 445 @Override GetNewNameInterface()446 public INewName GetNewNameInterface() throws RemoteException { 447 return new MyNewName(); 448 } 449 450 class MyCppJavaTests extends ICppJavaTests.Stub { 451 @Override RepeatSimpleParcelable(SimpleParcelable input, SimpleParcelable repeat)452 public SimpleParcelable RepeatSimpleParcelable(SimpleParcelable input, SimpleParcelable repeat) 453 throws RemoteException { 454 repeat.set(input.getName(), input.getNumber()); 455 return input; 456 } 457 @Override 458 public GenericStructuredParcelable<Integer, StructuredParcelable, Integer> RepeatGenericParcelable( GenericStructuredParcelable<Integer, StructuredParcelable, Integer> input, GenericStructuredParcelable<Integer, StructuredParcelable, Integer> repeat)459 RepeatGenericParcelable( 460 GenericStructuredParcelable<Integer, StructuredParcelable, Integer> input, 461 GenericStructuredParcelable<Integer, StructuredParcelable, Integer> repeat) 462 throws RemoteException { 463 repeat.a = input.a; 464 repeat.b = input.b; 465 return input; 466 } 467 @Override RepeatPersistableBundle(PersistableBundle input)468 public PersistableBundle RepeatPersistableBundle(PersistableBundle input) 469 throws RemoteException { 470 return input; 471 } 472 @Override ReverseSimpleParcelables( SimpleParcelable[] input, SimpleParcelable[] repeated)473 public SimpleParcelable[] ReverseSimpleParcelables( 474 SimpleParcelable[] input, SimpleParcelable[] repeated) throws RemoteException { 475 SimpleParcelable[] reversed = new SimpleParcelable[input.length]; 476 for (int i = 0; i < input.length; i++) { 477 repeated[i] = input[i]; 478 reversed[i] = input[input.length - i - 1]; 479 } 480 return reversed; 481 } 482 @Override ReversePersistableBundles( PersistableBundle[] input, PersistableBundle[] repeated)483 public PersistableBundle[] ReversePersistableBundles( 484 PersistableBundle[] input, PersistableBundle[] repeated) throws RemoteException { 485 PersistableBundle[] reversed = new PersistableBundle[input.length]; 486 for (int i = 0; i < input.length; i++) { 487 repeated[i] = input[i]; 488 reversed[i] = input[input.length - i - 1]; 489 } 490 return reversed; 491 } 492 @Override ReverseUnion(Union input, Union repeated)493 public Union ReverseUnion(Union input, Union repeated) throws RemoteException { 494 int[] repeatedArray = new int[input.getNs().length]; 495 int[] reversedArray = ReverseInt(input.getNs(), repeatedArray); 496 repeated.setNs(repeatedArray); 497 return Union.ns(reversedArray); 498 } 499 @Override ReverseNamedCallbackList(List<IBinder> input, List<IBinder> repeated)500 public List<IBinder> ReverseNamedCallbackList(List<IBinder> input, List<IBinder> repeated) 501 throws RemoteException { 502 ArrayList<IBinder> reversed = new ArrayList<IBinder>(); 503 for (IBinder a : input) { 504 repeated.add(a); 505 reversed.add(0, a); 506 } 507 return reversed; 508 } 509 @Override RepeatFileDescriptor(FileDescriptor read)510 public FileDescriptor RepeatFileDescriptor(FileDescriptor read) throws RemoteException { 511 return read; 512 } 513 @Override ReverseFileDescriptorArray( FileDescriptor[] input, FileDescriptor[] repeated)514 public FileDescriptor[] ReverseFileDescriptorArray( 515 FileDescriptor[] input, FileDescriptor[] repeated) throws RemoteException { 516 FileDescriptor[] reversed = new FileDescriptor[input.length]; 517 for (int i = 0; i < input.length; i++) { 518 repeated[i] = input[i]; 519 reversed[i] = input[input.length - i - 1]; 520 } 521 return reversed; 522 } 523 @Override TakesAnIBinderList(List<IBinder> input)524 public void TakesAnIBinderList(List<IBinder> input) throws RemoteException {} 525 @Override TakesANullableIBinderList(List<IBinder> input)526 public void TakesANullableIBinderList(List<IBinder> input) throws RemoteException {} 527 @Override RepeatExtendableParcelable(ExtendableParcelable ep, ExtendableParcelable ep2)528 public void RepeatExtendableParcelable(ExtendableParcelable ep, ExtendableParcelable ep2) 529 throws RemoteException { 530 ep2.a = ep.a; 531 ep2.b = ep.b; 532 // no way to copy currently w/o unparceling 533 ep2.ext.setParcelable(ep.ext.getParcelable(MyExt.class)); 534 ep2.c = ep.c; 535 ep2.ext2.setParcelable(null); 536 } 537 } 538 539 @Override GetCppJavaTests()540 public IBinder GetCppJavaTests() throws RemoteException { 541 return new MyCppJavaTests(); 542 } 543 @Override getBackendType()544 public byte getBackendType() throws RemoteException { 545 return BackendType.JAVA; 546 } 547 } 548