• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.Parcel;
19 import android.os.RemoteException;
20 import com.google.android.enterprise.connectedapps.internal.ByteUtilities;
21 import com.google.auto.value.AutoValue;
22 import org.checkerframework.checker.nullness.qual.Nullable;
23 
24 public class TestService extends ICrossProfileService.Stub {
25 
26   @AutoValue
27   abstract static class LoggedCrossProfileMethodCall {
getCrossProfileTypeIdentifier()28     abstract long getCrossProfileTypeIdentifier();
29 
getMethodIdentifier()30     abstract long getMethodIdentifier();
31 
getParams()32     abstract Parcel getParams();
33 
34     @Nullable
callback()35     abstract ICrossProfileCallback callback();
36 
create( long crossProfileTypeIdentifier, long methodIdentifier, Parcel params, ICrossProfileCallback callback)37     static LoggedCrossProfileMethodCall create(
38         long crossProfileTypeIdentifier,
39         long methodIdentifier,
40         Parcel params,
41         ICrossProfileCallback callback) {
42       return new AutoValue_TestService_LoggedCrossProfileMethodCall(
43           crossProfileTypeIdentifier, methodIdentifier, params, callback);
44     }
45   }
46 
47   private LoggedCrossProfileMethodCall lastCall;
48   private Parcel responseParcel = Parcel.obtain(); // Recycled in #setResponseParcel
49 
lastCall()50   LoggedCrossProfileMethodCall lastCall() {
51     return lastCall;
52   }
53 
54   /**
55    * Set the parcel to be returned from a call to this service.
56    *
57    * <p>The previously set parcel will be recycled.
58    */
setResponseParcel(Parcel responseParcel)59   void setResponseParcel(Parcel responseParcel) {
60     this.responseParcel.recycle();
61     this.responseParcel = responseParcel;
62   }
63 
64   @Override
prepareCall(long callId, int blockId, int numBytes, byte[] paramsBytes)65   public void prepareCall(long callId, int blockId, int numBytes, byte[] paramsBytes) {}
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 
81     if (lastCall != null) {
82       lastCall.getParams().recycle();
83     }
84 
85     lastCall =
86         LoggedCrossProfileMethodCall.create(
87             crossProfileTypeIdentifier, methodIdentifier, parcel, callback);
88 
89     byte[] parcelBytes = responseParcel.marshall();
90     return prepareResponse(parcelBytes);
91   }
92 
prepareResponse(byte[] parcelBytes)93   private static byte[] prepareResponse(byte[] parcelBytes) {
94     // This doesn't deal with large responses.
95     return ByteUtilities.joinByteArrays(new byte[] {0}, parcelBytes);
96   }
97 
98   @Override
fetchResponse(long callId, int blockId)99   public byte[] fetchResponse(long callId, int blockId) {
100     return null;
101   }
102 }
103