HidlTestJava.java revision 64af5e8e25039f5d29af1d38d5c4cfa0e1d93996
1/*
2 * Copyright (C) 2016 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
17package com.android.commands.hidl_test_java;
18
19import android.hardware.tests.baz.V1_0.IBase;
20import android.hardware.tests.baz.V1_0.IBaz;
21import android.hardware.tests.baz.V1_0.IBazCallback;
22import android.os.HwBinder;
23import android.os.RemoteException;
24import android.util.Log;
25
26import java.util.ArrayList;
27
28public final class HidlTestJava {
29    private static final String TAG = "HidlTestJava";
30
31    public static void main(String[] args) {
32        int exitCode = 1;
33        try {
34            exitCode = new HidlTestJava().run(args);
35        } catch (Exception e) {
36            Log.e(TAG, "Error ", e);
37        }
38        System.exit(exitCode);
39    }
40
41    public int run(String[] args) throws RemoteException {
42        if (args[0].equals("-c")) {
43            client();
44        } else if (args[0].equals("-s")) {
45            server();
46        } else {
47            Log.e(TAG, "Usage: HidlTestJava  -c(lient) | -s(erver)");
48            return 1;
49        }
50
51        return 0;
52    }
53
54    public static String toString(IBase.Foo.Bar bar) {
55        StringBuilder builder = new StringBuilder();
56        builder.append("Bar(");
57        builder.append("z = ");
58        builder.append(bar.z);
59        builder.append(", ");
60        builder.append("s = ");
61        builder.append(toString(bar.s));
62        builder.append(")");
63
64        return builder.toString();
65    }
66
67    public static String toString(IBase.Foo foo) {
68        StringBuilder builder = new StringBuilder();
69        builder.append("Foo(");
70        builder.append("x = ");
71        builder.append(foo.x);
72        builder.append(", ");
73        builder.append("y = ");
74        builder.append(toString(foo.y));
75        builder.append(", ");
76        builder.append("aaa = ");
77        builder.append(toString(foo.aaa));
78        builder.append(")");
79
80        return builder.toString();
81    }
82
83    public static String toString(ArrayList<IBase.Foo.Bar> vec) {
84        StringBuilder builder = new StringBuilder();
85        builder.append("[");
86        for (int i = 0; i < vec.size(); ++i) {
87            if (i > 0) {
88                builder.append(", ");
89            }
90            builder.append(toString(vec.get(i)));
91        }
92        builder.append("]");
93
94        return builder.toString();
95    }
96
97    public static String fooVecToString(ArrayList<IBase.Foo> vec) {
98        StringBuilder builder = new StringBuilder();
99        builder.append("[");
100        for (int i = 0; i < vec.size(); ++i) {
101            if (i > 0) {
102                builder.append(", ");
103            }
104            builder.append(toString(vec.get(i)));
105        }
106        builder.append("]");
107
108        return builder.toString();
109    }
110
111    public static String macAddressVecToString(ArrayList<byte[]> vec) {
112        StringBuilder builder = new StringBuilder();
113        builder.append("[");
114        for (int i = 0; i < vec.size(); ++i) {
115            if (i > 0) {
116                builder.append(", ");
117            }
118            builder.append(toString(vec.get(i)));
119        }
120        builder.append("]");
121
122        return builder.toString();
123    }
124
125    public static String booleanVecToString(ArrayList<Boolean> vec) {
126        StringBuilder builder = new StringBuilder();
127        builder.append("[");
128        for (int i = 0; i < vec.size(); ++i) {
129            if (i > 0) {
130                builder.append(", ");
131            }
132            builder.append(toString(vec.get(i)));
133        }
134        builder.append("]");
135
136        return builder.toString();
137    }
138
139    public static String integerVecToString(ArrayList<Integer> vec) {
140        StringBuilder builder = new StringBuilder();
141        builder.append("[");
142        for (int i = 0; i < vec.size(); ++i) {
143            if (i > 0) {
144                builder.append(", ");
145            }
146            builder.append(toString(vec.get(i)));
147        }
148        builder.append("]");
149
150        return builder.toString();
151    }
152
153    public static String stringVecToString(ArrayList<String> vec) {
154        StringBuilder builder = new StringBuilder();
155        builder.append("[");
156        for (int i = 0; i < vec.size(); ++i) {
157            if (i > 0) {
158                builder.append(", ");
159            }
160            builder.append(toString(vec.get(i)));
161        }
162        builder.append("]");
163
164        return builder.toString();
165    }
166
167    public static String toString(IBase.Foo[] array) {
168        StringBuilder builder = new StringBuilder();
169        builder.append("[");
170        for (int i = 0; i < array.length; ++i) {
171            if (i > 0) {
172                builder.append(", ");
173            }
174            builder.append(toString(array[i]));
175        }
176        builder.append("]");
177
178        return builder.toString();
179    }
180
181    public static String toString(IBase.Foo.Bar[] array) {
182        StringBuilder builder = new StringBuilder();
183        builder.append("[");
184        for (int i = 0; i < array.length; ++i) {
185            if (i > 0) {
186                builder.append(", ");
187            }
188            builder.append(toString(array[i]));
189        }
190        builder.append("]");
191
192        return builder.toString();
193    }
194
195    public static String toString(int[] val) {
196        StringBuilder builder = new StringBuilder();
197        builder.append("[");
198        for (int i = 0; i < val.length; ++i) {
199            if (i > 0) {
200                builder.append(", ");
201            }
202            builder.append(val[i]);
203        }
204        builder.append("]");
205
206        return builder.toString();
207    }
208
209    public static String toString(boolean[] val) {
210        StringBuilder builder = new StringBuilder();
211        builder.append("[");
212        for (int i = 0; i < val.length; ++i) {
213            if (i > 0) {
214                builder.append(", ");
215            }
216            builder.append(val[i] ? "true" : "false");
217        }
218        builder.append("]");
219
220        return builder.toString();
221    }
222
223    public static String toString(String[] val) {
224        StringBuilder builder = new StringBuilder();
225        builder.append("[");
226        for (int i = 0; i < val.length; ++i) {
227            if (i > 0) {
228                builder.append(", ");
229            }
230            builder.append(toString(val[i]));
231        }
232        builder.append("]");
233
234        return builder.toString();
235    }
236
237    public static String toString(byte[] val) {
238        StringBuilder builder = new StringBuilder();
239        for (int i = 0; i < val.length; ++i) {
240            if (i > 0) {
241                builder.append(":");
242            }
243
244            byte b = val[i];
245            if (b < 16) {
246                builder.append("0");
247            }
248            builder.append(Integer.toHexString(b));
249        }
250
251        return builder.toString();
252    }
253
254    public static String toString(boolean x) {
255        return x ? "true" : "false";
256    }
257
258    public static String toString(int x) {
259        return Integer.toString(x);
260    }
261
262    public static String toString(double x) {
263        return Double.toString(x);
264    }
265
266    public static String toString(String s) {
267        return "'" + s + "'";
268    }
269
270    public static String toString(String[][] M) {
271        StringBuilder builder = new StringBuilder();
272
273        builder.append("[");
274        for (int i = 0; i < M.length; ++i) {
275            if (i > 0) {
276                builder.append(", ");
277            }
278            builder.append(toString(M[i]));
279        }
280        builder.append("]");
281
282        return builder.toString();
283    }
284
285    public static String toString(IBase.StringMatrix5x3 M) {
286        return toString(M.s);
287    }
288
289    public static String toString(IBase.StringMatrix3x5 M) {
290        return toString(M.s);
291    }
292
293    public static String toString(IBase.VectorOfArray vec) {
294        StringBuilder out = new StringBuilder();
295
296        out.append("VectorOfArray(");
297        for (int i = 0; i < vec.addresses.size(); ++i) {
298            if (i > 0) {
299                out.append(", ");
300            }
301
302            byte[] address = vec.addresses.get(i);
303
304            for (int j = 0; j < 6; ++j) {
305                if (j > 0) {
306                    out.append(":");
307                }
308
309                byte b = address[j];
310                if (b < 16) {
311                    out.append("0");
312                }
313                out.append(Integer.toHexString(b));
314            }
315        }
316
317        out.append(")");
318        return out.toString();
319    }
320
321    final class HidlDeathRecipient implements HwBinder.DeathRecipient {
322        final Object mLock = new Object();
323        boolean mCalled = false;
324        long mCookie = 0;
325
326        @Override
327        public void serviceDied(long cookie) {
328            synchronized (mLock) {
329                mCalled = true;
330                mCookie = cookie;
331                mLock.notify();
332            }
333        }
334
335        public boolean cookieMatches(long cookie) {
336            synchronized (mLock) {
337                return mCookie == cookie;
338            }
339        }
340
341        public boolean waitUntilServiceDied(long timeoutMillis) {
342            synchronized(mLock) {
343                while (!mCalled) {
344                    try {
345                        mLock.wait(timeoutMillis);
346                    } catch (InterruptedException e) {
347                        continue; // Spin for another loop
348                    }
349                    break; // got notified or timeout hit
350                }
351                return mCalled;
352            }
353        }
354    };
355
356    private void ExpectTrue(boolean x) {
357        if (x) {
358            return;
359        }
360
361        throw new RuntimeException();
362    }
363
364    private void Expect(String result, String s) {
365        if (result.equals(s)) {
366            return;
367        }
368
369        Log.e(TAG, "Expected '" + s + "', got '" + result + "'");
370        throw new RuntimeException();
371    }
372
373    class BazCallback extends IBazCallback.Stub {
374        private boolean mCalled;
375
376        public BazCallback() {
377            mCalled = false;
378        }
379
380        boolean wasCalled() {
381            return mCalled;
382        }
383
384        public void heyItsMe(IBazCallback cb) throws RemoteException {
385            mCalled = true;
386
387            cb.heyItsMe(null);
388        }
389
390        public void hey() {
391            mCalled = true;
392        }
393    }
394
395    private String numberToEnglish(int x) {
396        final String[] kDigits = {
397            "zero",
398            "one",
399            "two",
400            "three",
401            "four",
402            "five",
403            "six",
404            "seven",
405            "eight",
406            "nine",
407        };
408
409        if (x < 0) {
410            return "negative " + numberToEnglish(-x);
411        }
412
413        if (x < 10) {
414            return kDigits[x];
415        }
416
417        if (x <= 15) {
418            final String[] kSpecialTens = {
419                "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen",
420            };
421
422            return kSpecialTens[x - 10];
423        }
424
425        if (x < 20) {
426            return kDigits[x % 10] + "teen";
427        }
428
429        if (x < 100) {
430            final String[] kDecades = {
431                "twenty", "thirty", "forty", "fifty", "sixty", "seventy",
432                "eighty", "ninety",
433            };
434
435            return kDecades[x / 10 - 2] + kDigits[x % 10];
436        }
437
438        return "positively huge!";
439    }
440
441    private void client() throws RemoteException {
442        {
443            // Test access through base interface binder.
444            IBase baseProxy = IBase.getService("baz");
445            baseProxy.someBaseMethod();
446        }
447
448        IBaz proxy = IBaz.getService("baz");
449        proxy.someBaseMethod();
450
451        {
452            IBase.Foo foo = new IBase.Foo();
453            foo.x = 1;
454
455            for (int i = 0; i < 5; ++i) {
456                IBase.Foo.Bar bar = new IBase.Foo.Bar();
457                bar.z = 1.0f + (float)i * 0.01f;
458                bar.s = "Hello, world " + i;
459                foo.aaa.add(bar);
460            }
461
462            foo.y.z = 3.14f;
463            foo.y.s = "Lorem ipsum...";
464
465            IBase.Foo result = proxy.someOtherBaseMethod(foo);
466
467            Expect(toString(result),
468                   "Foo(x = 1, " +
469                       "y = Bar(z = 3.14, s = 'Lorem ipsum...'), " +
470                       "aaa = [Bar(z = 1.0, s = 'Hello, world 0'), " +
471                              "Bar(z = 1.01, s = 'Hello, world 1'), " +
472                              "Bar(z = 1.02, s = 'Hello, world 2'), " +
473                              "Bar(z = 1.03, s = 'Hello, world 3'), " +
474                              "Bar(z = 1.04, s = 'Hello, world 4')])");
475        }
476
477        {
478            IBase.Foo[] inputArray = new IBase.Foo[2];
479
480            IBase.Foo foo = new IBase.Foo();
481            foo.x = 1;
482
483            for (int i = 0; i < 5; ++i) {
484                IBase.Foo.Bar bar = new IBase.Foo.Bar();
485                bar.z = 1.0f + (float)i * 0.01f;
486                bar.s = "Hello, world " + i;
487                foo.aaa.add(bar);
488            }
489
490            foo.y.z = 3.14f;
491            foo.y.s = "Lorem ipsum...";
492
493            inputArray[0] = foo;
494
495            foo = new IBase.Foo();
496            foo.x = 2;
497
498            for (int i = 0; i < 3; ++i) {
499                IBase.Foo.Bar bar = new IBase.Foo.Bar();
500                bar.z = 2.0f - (float)i * 0.01f;
501                bar.s = "Lorem ipsum " + i;
502                foo.aaa.add(bar);
503            }
504
505            foo.y.z = 1.1414f;
506            foo.y.s = "Et tu brute?";
507
508            inputArray[1] = foo;
509
510            IBase.Foo[] outputArray = proxy.someMethodWithFooArrays(inputArray);
511
512            Expect(toString(outputArray),
513                   "[Foo(x = 2, " +
514                        "y = Bar(z = 1.1414, s = 'Et tu brute?'), " +
515                        "aaa = [Bar(z = 2.0, s = 'Lorem ipsum 0'), " +
516                               "Bar(z = 1.99, s = 'Lorem ipsum 1'), " +
517                               "Bar(z = 1.98, s = 'Lorem ipsum 2')]), " +
518                     "Foo(x = 1, " +
519                         "y = Bar(z = 3.14, s = 'Lorem ipsum...'), " +
520                         "aaa = [Bar(z = 1.0, s = 'Hello, world 0'), " +
521                                "Bar(z = 1.01, s = 'Hello, world 1'), " +
522                                "Bar(z = 1.02, s = 'Hello, world 2'), " +
523                                "Bar(z = 1.03, s = 'Hello, world 3'), " +
524                                "Bar(z = 1.04, s = 'Hello, world 4')])]");
525        }
526
527        {
528            ArrayList<IBase.Foo> inputVec = new ArrayList<IBase.Foo>();
529
530            IBase.Foo foo = new IBase.Foo();
531            foo.x = 1;
532
533            for (int i = 0; i < 5; ++i) {
534                IBase.Foo.Bar bar = new IBase.Foo.Bar();
535                bar.z = 1.0f + (float)i * 0.01f;
536                bar.s = "Hello, world " + i;
537                foo.aaa.add(bar);
538            }
539
540            foo.y.z = 3.14f;
541            foo.y.s = "Lorem ipsum...";
542
543            inputVec.add(foo);
544
545            foo = new IBase.Foo();
546            foo.x = 2;
547
548            for (int i = 0; i < 3; ++i) {
549                IBase.Foo.Bar bar = new IBase.Foo.Bar();
550                bar.z = 2.0f - (float)i * 0.01f;
551                bar.s = "Lorem ipsum " + i;
552                foo.aaa.add(bar);
553            }
554
555            foo.y.z = 1.1414f;
556            foo.y.s = "Et tu brute?";
557
558            inputVec.add(foo);
559
560            ArrayList<IBase.Foo> outputVec =
561                proxy.someMethodWithFooVectors(inputVec);
562
563            Expect(fooVecToString(outputVec),
564                   "[Foo(x = 2, " +
565                        "y = Bar(z = 1.1414, s = 'Et tu brute?'), " +
566                        "aaa = [Bar(z = 2.0, s = 'Lorem ipsum 0'), " +
567                               "Bar(z = 1.99, s = 'Lorem ipsum 1'), " +
568                               "Bar(z = 1.98, s = 'Lorem ipsum 2')]), " +
569                     "Foo(x = 1, " +
570                         "y = Bar(z = 3.14, s = 'Lorem ipsum...'), " +
571                         "aaa = [Bar(z = 1.0, s = 'Hello, world 0'), " +
572                                "Bar(z = 1.01, s = 'Hello, world 1'), " +
573                                "Bar(z = 1.02, s = 'Hello, world 2'), " +
574                                "Bar(z = 1.03, s = 'Hello, world 3'), " +
575                                "Bar(z = 1.04, s = 'Hello, world 4')])]");
576        }
577
578        {
579            IBase.VectorOfArray in = new IBase.VectorOfArray();
580
581            int k = 0;
582            for (int i = 0; i < 3; ++i) {
583                byte[] mac = new byte[6];
584                for (int j = 0; j < 6; ++j, ++k) {
585                    mac[j] = (byte)k;
586                }
587
588                in.addresses.add(mac);
589            }
590
591            IBase.VectorOfArray out = proxy.someMethodWithVectorOfArray(in);
592
593            Expect(toString(out),
594                   "VectorOfArray("
595                     + "0c:0d:0e:0f:10:11, "
596                     + "06:07:08:09:0a:0b, "
597                     + "00:01:02:03:04:05)");
598        }
599
600        {
601            ArrayList<byte[]> in = new ArrayList<byte[]>();
602
603            int k = 0;
604            for (int i = 0; i < 3; ++i) {
605                byte[] mac = new byte[6];
606                for (int j = 0; j < 6; ++j, ++k) {
607                    mac[j] = (byte)k;
608                }
609
610                in.add(mac);
611            }
612
613            ArrayList<byte[]> out = proxy.someMethodTakingAVectorOfArray(in);
614
615            Expect(macAddressVecToString(out),
616                   "[0c:0d:0e:0f:10:11, 06:07:08:09:0a:0b, 00:01:02:03:04:05]");
617        }
618
619        {
620            IBase.StringMatrix5x3 in = new IBase.StringMatrix5x3();
621            for (int i = 0; i < 5; ++i) {
622                for (int j = 0; j < 3; ++j) {
623                    in.s[i][j] = numberToEnglish(3 * i + j + 1);
624                }
625            }
626
627            IBase.StringMatrix3x5 out = proxy.transpose(in);
628
629            // [[1 2 3] [4 5 6] [7 8 9] [10 11 12] [13 14 15]]^T
630            // = [[1 4 7 10 13] [2 5 8 11 14] [3 6 9 12 15]]
631            Expect(toString(out),
632                   "[['one', 'four', 'seven', 'ten', 'thirteen'], "
633                   +"['two', 'five', 'eight', 'eleven', 'fourteen'], "
634                   +"['three', 'six', 'nine', 'twelve', 'fifteen']]");
635        }
636
637        {
638            String[][] in = new String[5][3];
639            for (int i = 0; i < 5; ++i) {
640                for (int j = 0; j < 3; ++j) {
641                    in[i][j] = numberToEnglish(3 * i + j + 1);
642                }
643            }
644
645            String[][] out = proxy.transpose2(in);
646
647            // [[1 2 3] [4 5 6] [7 8 9] [10 11 12] [13 14 15]]^T
648            // = [[1 4 7 10 13] [2 5 8 11 14] [3 6 9 12 15]]
649            Expect(toString(out),
650                   "[['one', 'four', 'seven', 'ten', 'thirteen'], "
651                   +"['two', 'five', 'eight', 'eleven', 'fourteen'], "
652                   +"['three', 'six', 'nine', 'twelve', 'fifteen']]");
653        }
654
655        Expect(toString(proxy.someBoolMethod(true)), "false");
656
657        {
658            boolean[] someBoolArray = new boolean[3];
659            someBoolArray[0] = true;
660            someBoolArray[1] = false;
661            someBoolArray[2] = true;
662
663            Expect(toString(proxy.someBoolArrayMethod(someBoolArray)),
664                   "[false, true, false, true]");
665
666            ArrayList<Boolean> someBoolVec = new ArrayList<Boolean>();
667            someBoolVec.add(true);
668            someBoolVec.add(false);
669            someBoolVec.add(true);
670
671            Expect(booleanVecToString(proxy.someBoolVectorMethod(someBoolVec)),
672                   "[false, true, false]");
673        }
674
675        proxy.doThis(1.0f);
676
677        Expect(toString(proxy.doThatAndReturnSomething(1)), "666");
678        Expect(toString(proxy.doQuiteABit(1, 2L, 3.0f, 4.0)), "666.5");
679
680        {
681            int[] paramArray = new int[15];
682            ArrayList<Integer> paramVec = new ArrayList<Integer>();
683            for (int i = 0; i < paramArray.length; ++i) {
684                paramArray[i] = i;
685                paramVec.add(i);
686            }
687
688            Expect(toString(proxy.doSomethingElse(paramArray)),
689                   "[0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, " +
690                   "0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 1, 2]");
691
692            Expect(integerVecToString(proxy.mapThisVector(paramVec)),
693                   "[0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28]");
694        }
695
696        Expect(toString(proxy.doStuffAndReturnAString()), "'Hello, world!'");
697
698        BazCallback cb = new BazCallback();
699        ExpectTrue(!cb.wasCalled());
700        proxy.callMe(cb);
701        ExpectTrue(cb.wasCalled());
702
703        Expect(toString(proxy.useAnEnum(IBaz.SomeEnum.goober)), "-64");
704
705        {
706            String[] stringArray = new String[3];
707            stringArray[0] = "one";
708            stringArray[1] = "two";
709            stringArray[2] = "three";
710
711            Expect(toString(proxy.haveSomeStrings(stringArray)),
712                   "['Hello', 'World']");
713
714            ArrayList<String> stringVec = new ArrayList<String>();
715            stringVec.add("one");
716            stringVec.add("two");
717            stringVec.add("three");
718
719            Expect(stringVecToString(proxy.haveAStringVec(stringVec)),
720                   "['Hello', 'World']");
721        }
722
723        proxy.returnABunchOfStrings(
724                new IBaz.returnABunchOfStringsCallback() {
725                    @Override
726                    public void onValues(String a, String b, String c) {
727                        Expect(a, "Eins");
728                        Expect(b, "Zwei");
729                        Expect(c, "Drei");
730                    }
731                });
732
733        proxy.returnABunchOfStrings((a,b,c) -> Expect(a + b + c, "EinsZweiDrei"));
734
735        proxy.callMeLater(new BazCallback());
736        System.gc();
737        proxy.iAmFreeNow();
738
739        {
740            IBaz.T t1 = new IBaz.T();
741            IBaz.T t2 = new IBaz.T();
742            for (int i = 0; i < 5; i++) {
743                for (int j = 0; j < 3; j++) {
744                    t1.matrix5x3[i][j] = t2.matrix5x3[i][j] = (i + 1) * (j + 1);
745                }
746            }
747            ExpectTrue(t1.equals(t2));
748            ExpectTrue(t1.hashCode() == t2.hashCode());
749            t2.matrix5x3[4][2] = -60;
750            ExpectTrue(!t1.equals(t2));
751        }
752
753        // --- DEATH RECIPIENT TESTING ---
754        // This must always be done last, since it will kill the native server process
755        HidlDeathRecipient recipient1 = new HidlDeathRecipient();
756        HidlDeathRecipient recipient2 = new HidlDeathRecipient();
757
758        final int cookie1 = 0x1481;
759        final int cookie2 = 0x1482;
760        ExpectTrue(proxy.linkToDeath(recipient1, cookie1));
761        ExpectTrue(proxy.linkToDeath(recipient2, cookie2));
762        ExpectTrue(proxy.unlinkToDeath(recipient2));
763        try {
764            proxy.dieNow();
765        } catch (RemoteException e) {
766            // Expected
767        }
768        ExpectTrue(recipient1.waitUntilServiceDied(2000 /*timeoutMillis*/));
769        ExpectTrue(!recipient2.waitUntilServiceDied(2000 /*timeoutMillis*/));
770        ExpectTrue(recipient1.cookieMatches(cookie1));
771        Log.d(TAG, "OK, exiting");
772
773    }
774
775    class Baz extends IBaz.Stub {
776        // from IBase
777        public void someBaseMethod() {
778            Log.d(TAG, "Baz someBaseMethod");
779        }
780
781        public IBase.Foo someOtherBaseMethod(IBase.Foo foo) {
782            Log.d(TAG, "Baz someOtherBaseMethod " + HidlTestJava.toString(foo));
783            return foo;
784        }
785
786        public IBase.Foo[] someMethodWithFooArrays(IBase.Foo[] fooInput) {
787            Log.d(TAG, "Baz someMethodWithFooArrays " + HidlTestJava.toString(fooInput));
788
789            IBase.Foo[] fooOutput = new IBase.Foo[2];
790            fooOutput[0] = fooInput[1];
791            fooOutput[1] = fooInput[0];
792
793            return fooOutput;
794        }
795
796        public ArrayList<IBase.Foo> someMethodWithFooVectors(
797                ArrayList<IBase.Foo> fooInput) {
798            Log.d(TAG, "Baz someMethodWithFooVectors " + HidlTestJava.fooVecToString(fooInput));
799
800            ArrayList<IBase.Foo> fooOutput = new ArrayList<IBase.Foo>();
801            fooOutput.add(fooInput.get(1));
802            fooOutput.add(fooInput.get(0));
803
804            return fooOutput;
805        }
806
807        public IBase.VectorOfArray someMethodWithVectorOfArray(
808                IBase.VectorOfArray in) {
809            Log.d(TAG, "Baz someMethodWithVectorOfArray " + HidlTestJava.toString(in));
810
811            IBase.VectorOfArray out = new IBase.VectorOfArray();
812            int n = in.addresses.size();
813            for (int i = 0; i < n; ++i) {
814                out.addresses.add(in.addresses.get(n - i - 1));
815            }
816
817            return out;
818        }
819
820        public ArrayList<byte[/* 6 */]> someMethodTakingAVectorOfArray(
821                ArrayList<byte[/* 6 */]> in) {
822            Log.d(TAG, "Baz someMethodTakingAVectorOfArray");
823
824            int n = in.size();
825            ArrayList<byte[]> out = new ArrayList<byte[]>();
826            for (int i = 0; i < n; ++i) {
827                out.add(in.get(n - i - 1));
828            }
829
830            return out;
831        }
832
833        public IBase.StringMatrix3x5 transpose(IBase.StringMatrix5x3 in) {
834            Log.d(TAG, "Baz transpose " + HidlTestJava.toString(in));
835
836            IBase.StringMatrix3x5 out = new IBase.StringMatrix3x5();
837            for (int i = 0; i < 3; ++i) {
838                for (int j = 0; j < 5; ++j) {
839                    out.s[i][j] = in.s[j][i];
840                }
841            }
842
843            return out;
844        }
845
846        public String[][] transpose2(String[][] in) {
847            Log.d(TAG, "Baz transpose2 " + HidlTestJava.toString(in));
848
849            String[][] out = new String[3][5];
850            for (int i = 0; i < 3; ++i) {
851                for (int j = 0; j < 5; ++j) {
852                    out[i][j] = in[j][i];
853                }
854            }
855
856            return out;
857        }
858
859        public boolean someBoolMethod(boolean x) {
860            Log.d(TAG, "Baz someBoolMethod(" + x + ")");
861
862            return !x;
863        }
864
865        public boolean[] someBoolArrayMethod(boolean[] x) {
866            Log.d(TAG, "Baz someBoolArrayMethod("
867                    + HidlTestJava.toString(x) + ")");
868
869            boolean[] out = new boolean[4];
870            out[0] = !x[0];
871            out[1] = !x[1];
872            out[2] = !x[2];
873            out[3] = true;
874
875            return out;
876        }
877
878        public ArrayList<Boolean> someBoolVectorMethod(ArrayList<Boolean> x) {
879            Log.d(TAG, "Baz someBoolVectorMethod(" + HidlTestJava.booleanVecToString(x) + ")");
880
881            ArrayList<Boolean> out = new ArrayList<Boolean>();
882            for (int i = 0; i < x.size(); ++i) {
883                out.add(!x.get(i));
884            }
885
886            return out;
887        }
888
889        public void doThis(float param) {
890            Log.d(TAG, "Baz doThis " + param);
891        }
892
893        public int doThatAndReturnSomething(long param) {
894            Log.d(TAG, "Baz doThatAndReturnSomething " + param);
895            return 666;
896        }
897
898        public double doQuiteABit(int a, long b, float c, double d) {
899            Log.d(TAG, "Baz doQuiteABit " + a + ", " + b + ", " + c + ", " + d);
900            return 666.5;
901        }
902
903        public int[] doSomethingElse(int[] param) {
904            Log.d(TAG, "Baz doSomethingElse " + HidlTestJava.toString(param));
905
906            int[] something = new int[32];
907            for (int i = 0; i < 15; ++i) {
908                something[i] = 2 * param[i];
909                something[15 + i] = param[i];
910            }
911            something[30] = 1;
912            something[31] = 2;
913
914            return something;
915        }
916
917        public String doStuffAndReturnAString() {
918            Log.d(TAG, "doStuffAndReturnAString");
919            return "Hello, world!";
920        }
921
922        public ArrayList<Integer> mapThisVector(ArrayList<Integer> param) {
923            Log.d(TAG, "mapThisVector " + HidlTestJava.integerVecToString(param));
924
925            ArrayList<Integer> out = new ArrayList<Integer>();
926
927            for (int i = 0; i < param.size(); ++i) {
928                out.add(2 * param.get(i));
929            }
930
931            return out;
932        }
933
934        public void takeAMask(byte bf, byte first, IBase.MyMask second, byte third,
935                takeAMaskCallback cb) {
936            cb.onValues(bf, (byte)(bf | first),
937                    (byte)(second.value & bf), (byte)((bf | bf) & third));
938        }
939
940        public byte returnABitField() {
941            return 0;
942        }
943
944        public int size(int size) {
945            return size;
946        }
947
948        class BazCallback extends IBazCallback.Stub {
949            public void heyItsMe(IBazCallback cb) {
950                Log.d(TAG, "SERVER: heyItsMe");
951            }
952
953            public void hey() {
954                Log.d(TAG, "SERVER: hey");
955            }
956        }
957
958        public void callMe(IBazCallback cb) throws RemoteException {
959            Log.d(TAG, "callMe");
960            cb.heyItsMe(new BazCallback());
961        }
962
963        private IBazCallback mStoredCallback;
964        public void callMeLater(IBazCallback cb) {
965            mStoredCallback = cb;
966        }
967
968        public void iAmFreeNow() throws RemoteException {
969            if (mStoredCallback != null) {
970                mStoredCallback.hey();
971            }
972        }
973
974        public void dieNow() {
975            // Not tested in Java
976        }
977
978        public byte useAnEnum(byte zzz) {
979            Log.d(TAG, "useAnEnum " + zzz);
980            return SomeEnum.quux;
981        }
982
983        public String[] haveSomeStrings(String[] array) {
984            Log.d(TAG, "haveSomeStrings ["
985                        + "\"" + array[0] + "\", "
986                        + "\"" + array[1] + "\", "
987                        + "\"" + array[2] + "\"]");
988
989            String[] result = new String[2];
990            result[0] = "Hello";
991            result[1] = "World";
992
993            return result;
994        }
995
996        public ArrayList<String> haveAStringVec(ArrayList<String> vector) {
997            Log.d(TAG, "haveAStringVec ["
998                        + "\"" + vector.get(0) + "\", "
999                        + "\"" + vector.get(1) + "\", "
1000                        + "\"" + vector.get(2) + "\"]");
1001
1002            ArrayList<String> result = new ArrayList<String>();
1003            result.add("Hello");
1004            result.add("World");
1005
1006            return result;
1007        }
1008
1009        public void returnABunchOfStrings(returnABunchOfStringsCallback cb) {
1010            cb.onValues("Eins", "Zwei", "Drei");
1011        }
1012    }
1013
1014    private void server() throws RemoteException {
1015        Baz baz = new Baz();
1016        baz.registerAsService("baz");
1017
1018        try {
1019            Thread.sleep(20000);
1020        } catch (InterruptedException e) {
1021        }
1022    }
1023}
1024