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.cluster.demorenderer;
17 
18 import static android.car.navigation.CarNavigationManager.TURN_SIDE_LEFT;
19 import static android.car.navigation.CarNavigationManager.TURN_SIDE_RIGHT;
20 import static android.car.navigation.CarNavigationManager.TURN_TURN;
21 
22 import android.car.cluster.renderer.NavigationRenderer;
23 import android.content.Context;
24 import android.graphics.Bitmap;
25 import android.util.Log;
26 import android.util.Pair;
27 
28 import java.util.HashMap;
29 import java.util.Map;
30 
31 /**
32  * Demo implementation of {@link NavigationRenderer}.
33  */
34 public class DemoNavigationRenderer extends NavigationRenderer {
35 
36     private static final String TAG = DemoNavigationRenderer.class.getSimpleName();
37 
38     private final DemoInstrumentClusterView mView;
39     private final Context mContext;
40 
41     private final static Map<Pair<Integer, Integer>, Integer> sTurns;
42 
43     static {
44         sTurns = new HashMap<>();
sTurns.put(new Pair<>(TURN_TURN, TURN_SIDE_LEFT), R.string.turn_left)45         sTurns.put(new Pair<>(TURN_TURN, TURN_SIDE_LEFT), R.string.turn_left);
sTurns.put(new Pair<>(TURN_TURN, TURN_SIDE_RIGHT), R.string.turn_right)46         sTurns.put(new Pair<>(TURN_TURN, TURN_SIDE_RIGHT), R.string.turn_right);
47         // TODO: add more localized strings here.
48     }
49 
DemoNavigationRenderer(DemoInstrumentClusterView view)50     DemoNavigationRenderer(DemoInstrumentClusterView view) {
51         mView = view;
52         mContext = view.getContext();
53     }
54 
55     @Override
onStartNavigation()56     public void onStartNavigation() {
57         mView.showNavigation();
58     }
59 
60     @Override
onStopNavigation()61     public void onStopNavigation() {
62         mView.hideNavigation();
63     }
64 
65     @Override
onNextTurnChanged(int event, String road, int turnAngle, int turnNumber, final Bitmap image, int turnSide)66     public void onNextTurnChanged(int event, String road, int turnAngle, int turnNumber,
67             final Bitmap image, int turnSide) {
68         String localizedAction = getLocalizedNavigationAction(event, turnSide);
69         final String localizedTitle = String.format(
70                 mContext.getString(R.string.nav_event_title_format), localizedAction, road);
71 
72         mView.setNextTurn(image, localizedTitle);
73     }
74 
75     @Override
onNextTurnDistanceChanged(final int distanceMeters, int timeSeconds)76     public void onNextTurnDistanceChanged(final int distanceMeters, int timeSeconds) {
77         mView.setNextTurnDistance(toHumanReadableDistance(distanceMeters));
78     }
79 
getLocalizedNavigationAction(int event, int turnSide)80     private String getLocalizedNavigationAction(int event, int turnSide) {
81         Pair<Integer, Integer> key = new Pair<>(event, turnSide);
82         if (sTurns.containsKey(key)) {
83             Integer resourceId = sTurns.get(key);
84             return mContext.getResources().getString(resourceId);
85         } else {
86             Log.w(TAG, "Navigation event / turn not localized: " + event + ", " + turnSide);
87             return String.format("Event: %d, Side: %d", event, turnSide);
88         }
89     }
90 
toHumanReadableDistance(int meters)91     private String toHumanReadableDistance(int meters) {
92         // TODO: implement.
93         return "in " + String.valueOf(meters) + " " + mContext.getString(R.string.meters);
94     }
95 }
96