Home | History | Annotate | Download | only in cts
      1 /*
      2  * Copyright (C) 2018 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 package android.content.syncmanager.cts;
     17 
     18 import static android.content.syncmanager.cts.common.Values.getCommReceiver;
     19 
     20 import android.content.syncmanager.cts.SyncManagerCtsProto.Payload;
     21 import android.content.syncmanager.cts.SyncManagerCtsProto.Payload.Request;
     22 import android.content.syncmanager.cts.SyncManagerCtsProto.Payload.Request.Builder;
     23 
     24 import com.android.compatibility.common.util.BroadcastRpcBase;
     25 
     26 import com.google.protobuf.InvalidProtocolBufferException;
     27 
     28 import java.util.function.Consumer;
     29 
     30 public class BroadcastRpc extends BroadcastRpcBase<Payload, Payload> {
     31     @Override
     32     protected byte[] requestToBytes(Payload testServiceRequest) {
     33         return testServiceRequest.toByteArray();
     34     }
     35 
     36     @Override
     37     protected Payload bytesToResponse(byte[] bytes) {
     38         try {
     39             return Payload.parseFrom(bytes);
     40         } catch (InvalidProtocolBufferException e) {
     41             throw new RuntimeException("InvalidProtocolBufferException", e);
     42         }
     43     }
     44 
     45     public Payload.Response invoke(String targetPackage, Consumer<Builder> c) throws Exception {
     46         final Builder rb =  Request.newBuilder();
     47         c.accept(rb);
     48         final Request req = rb.build();
     49         return super.invoke(getCommReceiver(targetPackage),
     50                 Payload.newBuilder().setRequest(req).build()).getResponse();
     51     }
     52 }
     53