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.support.car.navigation;
     17 
     18 import android.graphics.Bitmap;
     19 import android.support.car.CarNotConnectedException;
     20 
     21 /**
     22  * @hide
     23  */
     24 public class CarNavigationManagerEmbedded extends CarNavigationManager {
     25 
     26     private final android.car.navigation.CarNavigationManager mManager;
     27     private CarNavigationListenerProxy mListener;
     28 
     29     public CarNavigationManagerEmbedded(Object manager) {
     30         mManager = (android.car.navigation.CarNavigationManager) manager;
     31     }
     32 
     33     /**
     34      * @param status new instrument cluster navigation status.
     35      * @return true if successful.
     36      * @throws CarNotConnectedException
     37      */
     38     @Override
     39     public boolean sendNavigationStatus(int status) throws CarNotConnectedException {
     40         try {
     41             return mManager.sendNavigationStatus(status);
     42         } catch (android.car.CarNotConnectedException e) {
     43            throw new CarNotConnectedException(e);
     44         }
     45     }
     46 
     47     @Override
     48     public boolean sendNavigationTurnEvent(int event, String road, int turnAngle, int turnNumber,
     49             Bitmap image, int turnSide) throws CarNotConnectedException {
     50         try {
     51             return mManager.sendNavigationTurnEvent(event, road, turnAngle, turnNumber, image,
     52                     turnSide);
     53         } catch (android.car.CarNotConnectedException e) {
     54             throw new CarNotConnectedException(e);
     55         }
     56     }
     57 
     58     @Override
     59     public boolean sendNavigationTurnDistanceEvent(int distanceMeters, int timeSeconds)
     60             throws CarNotConnectedException {
     61         try {
     62             return mManager.sendNavigationTurnDistanceEvent(distanceMeters, timeSeconds);
     63         } catch (android.car.CarNotConnectedException e) {
     64             throw new CarNotConnectedException(e);
     65         }
     66     }
     67 
     68     @Override
     69     public boolean isInstrumentClusterSupported() throws CarNotConnectedException {
     70         try {
     71             return mManager.isInstrumentClusterSupported();
     72         } catch (android.car.CarNotConnectedException e) {
     73             throw new CarNotConnectedException(e);
     74         }
     75     }
     76 
     77     @Override
     78     public void onCarDisconnected() {
     79         //nothing to do
     80     }
     81 
     82     @Override
     83     public void registerListener(CarNavigationListener listener)
     84             throws CarNotConnectedException {
     85         CarNavigationListenerProxy proxy = null;
     86         synchronized (this) {
     87             proxy = new CarNavigationListenerProxy(listener);
     88             mListener = proxy;
     89         }
     90         try {
     91             mManager.registerListener(proxy);
     92         } catch (android.car.CarNotConnectedException e) {
     93             throw new CarNotConnectedException(e);
     94         }
     95     }
     96 
     97     @Override
     98     public void unregisterListener() {
     99         synchronized (this) {
    100             mListener = null;
    101         }
    102         mManager.unregisterListener();
    103     }
    104 
    105     private static CarNavigationInstrumentCluster convert(
    106             android.car.navigation.CarNavigationInstrumentCluster ic) {
    107         if (ic == null) {
    108             return null;
    109         }
    110         return new CarNavigationInstrumentCluster(ic.getMinIntervalMs(), ic.getType(),
    111                 ic.getImageWidth(), ic.getImageHeight(), ic.getImageColorDepthBits());
    112     }
    113 
    114     private static class CarNavigationListenerProxy implements
    115             android.car.navigation.CarNavigationManager.CarNavigationListener {
    116 
    117         private final CarNavigationListener mListener;
    118 
    119         private CarNavigationListenerProxy(CarNavigationListener listener) {
    120             mListener = listener;
    121         }
    122 
    123         @Override
    124         public void onInstrumentClusterStart(
    125                 android.car.navigation.CarNavigationInstrumentCluster instrumentCluster) {
    126             mListener.onInstrumentClusterStart(convert(instrumentCluster));
    127         }
    128 
    129         @Override
    130         public void onInstrumentClusterStop() {
    131             mListener.onInstrumentClusterStop();
    132         }
    133     }
    134 }
    135