Home | History | Annotate | Download | only in car
      1 /*
      2  * Copyright (C) 2015 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 android.support.car;
     18 
     19 import android.content.Context;
     20 import android.os.Handler;
     21 
     22 /**
     23  * CarServiceLoader is the abstraction for loading different types of car service.
     24  * @hide
     25  */
     26 public abstract class CarServiceLoader {
     27 
     28     private final Context mContext;
     29     private final CarConnectionCallbackProxy mCallback;
     30     private final Handler mEventHandler;
     31 
     32     public CarServiceLoader(Context context, CarConnectionCallbackProxy callback, Handler handler) {
     33         mContext = context;
     34         mCallback = callback;
     35         mEventHandler = handler;
     36     }
     37 
     38     public abstract void connect() throws IllegalStateException;
     39     public abstract void disconnect();
     40     public abstract boolean isConnected();
     41 
     42     @Car.ConnectionType
     43     public abstract int getCarConnectionType() throws CarNotConnectedException;
     44 
     45     /**
     46      * Retrieves a manager object for a specified Car*Manager.
     47      * @param serviceName One of the android.car.Car#*_SERVICE constants.
     48      * @return An instance of the request manager.  Null if the manager is not supported on the
     49      * current vehicle.
     50      * @throws CarNotConnectedException if the connection to the car service has been lost.
     51      */
     52     public abstract Object getCarManager(String serviceName) throws CarNotConnectedException;
     53 
     54     protected Context getContext() {
     55         return mContext;
     56     }
     57 
     58     protected CarConnectionCallbackProxy getConnectionCallback() {
     59         return mCallback;
     60     }
     61 
     62     protected Handler getEventHandler() {
     63         return mEventHandler;
     64     }
     65 
     66     /**
     67      * Wrapper for CarConnectionCallback which does not return a {@link android.support.car.Car}
     68      * object.
     69      */
     70     public abstract static class CarConnectionCallbackProxy {
     71 
     72         /**
     73          * Called when the Car has been connected. Does not guarantee the car is still connected
     74          * while this callback is running, so {@link CarNotConnectedException}s may still be
     75          * thrown from {@link Car} method calls.
     76          */
     77         public abstract void onConnected();
     78         /**
     79          * Called when the Car has been disconnected.
     80          */
     81         public abstract void onDisconnected();
     82     }
     83 }
     84