Home | History | Annotate | Download | only in app
      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.app;
     17 
     18 import android.content.Context;
     19 import android.content.syncmanager.cts.SyncManagerCtsProto.Payload;
     20 import android.content.syncmanager.cts.SyncManagerCtsProto.Payload.Request;
     21 import android.content.syncmanager.cts.SyncManagerCtsProto.Payload.Response.SyncInvocations;
     22 
     23 import com.android.compatibility.common.util.BroadcastRpcBase;
     24 
     25 import com.google.protobuf.InvalidProtocolBufferException;
     26 
     27 public class CommReceiver extends BroadcastRpcBase.ReceiverBase<Payload, Payload> {
     28     @Override
     29     protected Payload handleRequest(Context context, Payload request) throws Exception {
     30         ContextHolder.sContext = context;
     31 
     32         final Request req = request.getRequest();
     33         final Payload.Response.Builder res = Payload.Response.newBuilder();
     34 
     35         if (req.hasAddAccount()) {
     36             SyncManagerCtsAuthenticator.ensureTestAccount(req.getAddAccount().getName());
     37 
     38         } else if (req.hasRemoveAllAccounts()) {
     39             SyncManagerCtsAuthenticator.removeAllAccounts();
     40 
     41         } else if (req.hasClearSyncInvocations()) {
     42             SyncManagerCtsSyncAdapter.clearSyncInvocations();
     43 
     44         } else if (req.hasGetSyncInvocations()) {
     45             res.setSyncInvocations(SyncInvocations.newBuilder()
     46                     .addAllSyncInvocations(SyncManagerCtsSyncAdapter.getSyncInvocations()));
     47 
     48         } else if (req.hasSetResult()) {
     49             SyncManagerCtsSyncAdapter.setResult(req.getSetResult());
     50 
     51         }
     52 
     53         return Payload.newBuilder().setResponse(res).build();
     54     }
     55 
     56     @Override
     57     protected byte[] responseToBytes(Payload payload) {
     58         return payload.toByteArray();
     59     }
     60 
     61     @Override
     62     protected Payload bytesToRequest(byte[] bytes) {
     63         try {
     64             return Payload.parseFrom(bytes);
     65         } catch (InvalidProtocolBufferException e) {
     66             throw new RuntimeException("InvalidProtocolBufferException", e);
     67         }
     68     }
     69 }
     70