Home | History | Annotate | Download | only in common
      1 /*
      2  * Copyright (C) 2013 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 com.android.accessorydisplay.common;
     18 
     19 import com.android.accessorydisplay.common.Transport;
     20 
     21 import android.content.Context;
     22 import android.os.Looper;
     23 
     24 import java.nio.ByteBuffer;
     25 
     26 /**
     27  * Base implementation of a service that communicates over a transport.
     28  * <p>
     29  * This object's interface is single-threaded.  It is only intended to be
     30  * accessed from the {@link Looper} thread on which the transport was created.
     31  * </p>
     32  */
     33 public abstract class Service implements Transport.Callback {
     34     private final Context mContext;
     35     private final Transport mTransport;
     36     private final int mServiceId;
     37 
     38     public Service(Context context, Transport transport, int serviceId) {
     39         mContext = context;
     40         mTransport = transport;
     41         mServiceId = serviceId;
     42     }
     43 
     44     public Context getContext() {
     45         return mContext;
     46     }
     47 
     48     public int getServiceId() {
     49         return mServiceId;
     50     }
     51 
     52     public Transport getTransport() {
     53         return mTransport;
     54     }
     55 
     56     public Logger getLogger() {
     57         return mTransport.getLogger();
     58     }
     59 
     60     public void start() {
     61         mTransport.registerService(mServiceId, this);
     62     }
     63 
     64     public void stop() {
     65         mTransport.unregisterService(mServiceId);
     66     }
     67 
     68     @Override
     69     public void onMessageReceived(int service, int what, ByteBuffer content) {
     70     }
     71 }
     72