Home | History | Annotate | Download | only in navigation
      1 /*
      2  * Copyright (C) 2016 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.car.navigation;
     17 
     18 import android.car.CarApiUtil;
     19 import android.car.CarLibLog;
     20 import android.car.CarManagerBase;
     21 import android.car.CarNotConnectedException;
     22 import android.car.cluster.renderer.IInstrumentClusterNavigation;
     23 import android.os.Bundle;
     24 import android.os.IBinder;
     25 import android.os.RemoteException;
     26 import android.util.Log;
     27 
     28 /**
     29  * API for providing navigation status for instrument cluster.
     30  */
     31 public final class CarNavigationStatusManager implements CarManagerBase {
     32     private static final String TAG = CarLibLog.TAG_NAV;
     33 
     34     private final IInstrumentClusterNavigation mService;
     35 
     36     /**
     37      * Only for CarServiceLoader
     38      * @hide
     39      */
     40     public CarNavigationStatusManager(IBinder service) {
     41         mService = IInstrumentClusterNavigation.Stub.asInterface(service);
     42     }
     43 
     44     /**
     45      * Sends events from navigation app to instrument cluster.
     46      *
     47      * <p>The event type and bundle can be populated by
     48      * {@link android.support.car.navigation.CarNavigationStatusEvent}.
     49      *
     50      * @param eventType event type
     51      * @param bundle object that holds data about the event
     52      * @throws CarNotConnectedException if the connection to the car service has been lost.
     53      */
     54     public void sendEvent(int eventType, Bundle bundle) throws CarNotConnectedException {
     55         try {
     56             mService.onEvent(eventType, bundle);
     57         } catch (IllegalStateException e) {
     58             CarApiUtil.checkCarNotConnectedExceptionFromCarService(e);
     59         } catch (RemoteException e) {
     60             handleCarServiceRemoteExceptionAndThrow(e);
     61         }
     62     }
     63 
     64     /** @hide */
     65     @Override
     66     public void onCarDisconnected() {}
     67 
     68     /** Returns navigation features of instrument cluster */
     69     public CarNavigationInstrumentCluster getInstrumentClusterInfo()
     70             throws CarNotConnectedException {
     71         try {
     72             return mService.getInstrumentClusterInfo();
     73         } catch (RemoteException e) {
     74             handleCarServiceRemoteExceptionAndThrow(e);
     75         }
     76         return null;
     77     }
     78 
     79     private void handleCarServiceRemoteExceptionAndThrow(RemoteException e)
     80             throws CarNotConnectedException {
     81         handleCarServiceRemoteException(e);
     82         throw new CarNotConnectedException();
     83     }
     84 
     85     private void handleCarServiceRemoteException(RemoteException e) {
     86         Log.w(TAG, "RemoteException from car service:" + e.getMessage());
     87         // nothing to do for now
     88     }
     89 }
     90