Fixing image sizes inside step cues, and other small bugs

Bug: 123100190
Test: croot && make RunDirectRenderingClusterSampleTests -j96
Change-Id: Ia24a2648e6cf943cf1f20de0c8a86a50a87a305a
(cherry picked from commit f837a7a12359f7ed622b494241f1fa544e556bd2)
diff --git a/tests/DirectRenderingClusterSample/res/layout/include_navigation_state.xml b/tests/DirectRenderingClusterSample/res/layout/include_navigation_state.xml
index 0a17c82..7f58e24 100644
--- a/tests/DirectRenderingClusterSample/res/layout/include_navigation_state.xml
+++ b/tests/DirectRenderingClusterSample/res/layout/include_navigation_state.xml
@@ -20,16 +20,19 @@
             android:id="@+id/distance"
             android:layout_height="wrap_content"
             android:layout_width="wrap_content"
+            android:maxLines="1"
             android:textSize="@dimen/distance_text_size"/>
-        <TextView
-            android:id="@+id/segment"
-            android:layout_height="wrap_content"
+        <android.car.cluster.sample.CueView
+            android:id="@+id/cue"
             android:layout_width="wrap_content"
-            android:textSize="@dimen/segment_text_size"/>
+            android:layout_height="wrap_content"
+            android:maxLines="1"
+            android:textSize="@dimen/cue_text_size"/>
         <TextView
             android:id="@+id/eta"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
+            android:maxLines="1"
             android:textSize="@dimen/eta_text_size"/>
     </LinearLayout>
 
@@ -38,11 +41,6 @@
         android:layout_height="wrap_content"
         android:orientation="vertical">
 
-        <android.car.cluster.sample.CueView
-            android:id="@+id/cue"
-            android:layout_width="wrap_content"
-            android:layout_height="wrap_content"
-            android:lineHeight="@dimen/cue_line_height"/>
         <android.car.cluster.sample.LaneView
             android:id="@+id/lane"
             android:layout_width="wrap_content"
diff --git a/tests/DirectRenderingClusterSample/res/values/dimens.xml b/tests/DirectRenderingClusterSample/res/values/dimens.xml
index e2c71d8..2979b36 100644
--- a/tests/DirectRenderingClusterSample/res/values/dimens.xml
+++ b/tests/DirectRenderingClusterSample/res/values/dimens.xml
@@ -35,7 +35,7 @@
     <!-- ETA -->
     <dimen name="eta_text_size">15sp</dimen>
     <!-- Cue -->
-    <dimen name="cue_line_height">15sp</dimen>
+    <dimen name="cue_text_size">15sp</dimen>
     <!-- Lane -->
     <dimen name="laneview_height">25dp</dimen>
     <dimen name="lane_width">50dp</dimen>
diff --git a/tests/DirectRenderingClusterSample/src/android/car/cluster/sample/CueView.java b/tests/DirectRenderingClusterSample/src/android/car/cluster/sample/CueView.java
index 0bb6a4a..e0d0d12 100644
--- a/tests/DirectRenderingClusterSample/src/android/car/cluster/sample/CueView.java
+++ b/tests/DirectRenderingClusterSample/src/android/car/cluster/sample/CueView.java
@@ -17,6 +17,7 @@
 
 import android.content.Context;
 import android.graphics.Bitmap;
+import android.graphics.drawable.BitmapDrawable;
 import android.text.SpannableStringBuilder;
 import android.text.style.ImageSpan;
 import android.util.AttributeSet;
@@ -56,7 +57,7 @@
                 builder.append(" ");
             }
             if (element.getImage() != null) {
-                Bitmap bitmap = ImageResolver.getInstance().getBitmapConstrained(mContext,
+                Bitmap bitmap = ImageResolver.getInstance().getBitmapConstrained(getContext(),
                         element.getImage(), 0, getLineHeight());
                 if (bitmap != null) {
                     String imageText = element.getText().isEmpty() ? mImageSpanText :
@@ -64,7 +65,9 @@
                     int start = builder.length();
                     int end = start + imageText.length();
                     builder.append(imageText);
-                    builder.setSpan(new ImageSpan(mContext, bitmap), start, end, 0);
+                    BitmapDrawable drawable = new BitmapDrawable(getResources(), bitmap);
+                    drawable.setBounds(0, 0, bitmap.getWidth(), bitmap.getHeight());
+                    builder.setSpan(new ImageSpan(drawable), start, end, 0);
                 }
             } else if (!element.getText().isEmpty()) {
                 builder.append(element.getText());
diff --git a/tests/DirectRenderingClusterSample/src/android/car/cluster/sample/ImageResolver.java b/tests/DirectRenderingClusterSample/src/android/car/cluster/sample/ImageResolver.java
index 39a5f20..f306143 100644
--- a/tests/DirectRenderingClusterSample/src/android/car/cluster/sample/ImageResolver.java
+++ b/tests/DirectRenderingClusterSample/src/android/car/cluster/sample/ImageResolver.java
@@ -102,7 +102,8 @@
             Log.d(TAG, String.format("Returning image %s (width: %d, height: %d)",
                     img.getRawContentUri(), width, height));
         }
-        return bitmap != null ? Bitmap.createScaledBitmap(bitmap, width, height, true) : null;
+        return bitmap != null ? Bitmap.createScaledBitmap(bitmap, adjusted.x, adjusted.y, true)
+                : null;
     }
 
     /**
diff --git a/tests/DirectRenderingClusterSample/src/android/car/cluster/sample/MainClusterActivity.java b/tests/DirectRenderingClusterSample/src/android/car/cluster/sample/MainClusterActivity.java
index f8e8cbc..3181b1e 100644
--- a/tests/DirectRenderingClusterSample/src/android/car/cluster/sample/MainClusterActivity.java
+++ b/tests/DirectRenderingClusterSample/src/android/car/cluster/sample/MainClusterActivity.java
@@ -264,9 +264,9 @@
 
         mClusterViewModel = ViewModelProviders.of(this).get(ClusterViewModel.class);
         mClusterViewModel.getNavigationFocus().observe(this, focus -> {
-            mNavStateController.setActive(focus);
             // If focus is lost, we launch the default navigation activity again.
             if (!focus) {
+                mNavStateController.update(null);
                 tryLaunchNavigationActivity();
             }
         });
diff --git a/tests/DirectRenderingClusterSample/src/android/car/cluster/sample/NavStateController.java b/tests/DirectRenderingClusterSample/src/android/car/cluster/sample/NavStateController.java
index 2a6bdfa..0d07962 100644
--- a/tests/DirectRenderingClusterSample/src/android/car/cluster/sample/NavStateController.java
+++ b/tests/DirectRenderingClusterSample/src/android/car/cluster/sample/NavStateController.java
@@ -33,7 +33,6 @@
 
 import java.time.Duration;
 import java.time.ZonedDateTime;
-import java.util.List;
 
 /**
  * View controller for navigation state rendering.
@@ -44,11 +43,9 @@
     private ImageView mManeuver;
     private LaneView mLane;
     private TextView mDistance;
-    private TextView mSegment;
     private TextView mEta;
     private CueView mCue;
     private Context mContext;
-    private View mNavigationState;
 
     /**
      * Creates a controller to coordinate updates to the views displaying navigation state
@@ -57,11 +54,9 @@
      * @param container {@link View} containing the navigation state views
      */
     public NavStateController(View container) {
-        mNavigationState = container;
         mManeuver = container.findViewById(R.id.maneuver);
         mLane = container.findViewById(R.id.lane);
         mDistance = container.findViewById(R.id.distance);
-        mSegment = container.findViewById(R.id.segment);
         mEta = container.findViewById(R.id.eta);
         mCue = container.findViewById(R.id.cue);
 
@@ -75,21 +70,16 @@
         if (Log.isLoggable(TAG, Log.DEBUG)) {
             Log.d(TAG, "Updating nav state: " + state);
         }
-        Step step = getImmediateStep(state);
-
-        List<Destination> destinations = state.getDestinations();
-        ZonedDateTime eta = null;
-        Traffic traffic = null;
-        if (!destinations.isEmpty()) {
-            eta = state.getDestinations().get(0).getEta();
-            traffic = state.getDestinations().get(0).getTraffic();
-        }
+        Step step = state != null && state.getSteps().size() > 0 ? state.getSteps().get(0) : null;
+        Destination destination = state != null && !state.getDestinations().isEmpty()
+                ? state.getDestinations().get(0) : null;
+        ZonedDateTime eta = destination != null ? destination.getEta() : null;
+        Traffic traffic = destination != null ? destination.getTraffic() : null;
 
         mEta.setText(eta != null ? formatEta(eta) : null);
         mEta.setTextColor(getTrafficColor(traffic));
         mManeuver.setImageDrawable(getManeuverIcon(step != null ? step.getManeuver() : null));
         mDistance.setText(formatDistance(step != null ? step.getDistance() : null));
-        mSegment.setText(getSegmentString(state.getCurrentSegment()));
         mCue.setRichText(step != null ? step.getCue() : null);
 
         if (step != null && step.getLanes().size() > 0) {
@@ -132,23 +122,6 @@
         }
     }
 
-    /**
-     * Updates whether turn-by-turn display is active or not. Turn-by-turn would be active whenever
-     * a navigation application has focus.
-     */
-    public void setActive(boolean active) {
-        if (Log.isLoggable(TAG, Log.DEBUG)) {
-            Log.d(TAG, "Navigation status active: " + active);
-        }
-        if (!active) {
-            mManeuver.setImageDrawable(null);
-            mDistance.setText(null);
-            mLane.setVisibility(View.GONE);
-            mCue.setText(null);
-            mSegment.setText(null);
-        }
-    }
-
     private String getSegmentString(Segment segment) {
         if (segment != null) {
             return segment.getName();
@@ -274,10 +247,6 @@
         return null;
     }
 
-    private Step getImmediateStep(@Nullable NavigationState state) {
-        return state != null && state.getSteps().size() > 0 ? state.getSteps().get(0) : null;
-    }
-
     private String formatDistance(@Nullable Distance distance) {
         if (distance == null || distance.getDisplayUnit() == Distance.Unit.UNKNOWN) {
             return null;
diff --git a/tests/DirectRenderingClusterSample/tests/robotests/src/android/car/cluster/sample/ImageResolverTest.java b/tests/DirectRenderingClusterSample/tests/robotests/src/android/car/cluster/sample/ImageResolverTest.java
index 0dee29e..746c0df 100644
--- a/tests/DirectRenderingClusterSample/tests/robotests/src/android/car/cluster/sample/ImageResolverTest.java
+++ b/tests/DirectRenderingClusterSample/tests/robotests/src/android/car/cluster/sample/ImageResolverTest.java
@@ -89,4 +89,14 @@
     public void adjustedSize_exceptionIfRequestedWidthAndHeightNoProvided() {
         assertEquals(null, mImageResolver.getAdjustedSize(5, 10, 0, 0));
     }
+
+    @Test
+    public void adjustedSize_flexibleWidth() {
+        assertEquals(new Point(20, 30), mImageResolver.getAdjustedSize(40, 60, 0, 30));
+    }
+
+    @Test
+    public void adjustedSize_flexibleHeight() {
+        assertEquals(new Point(20, 20), mImageResolver.getAdjustedSize(40, 40, 20, 0));
+    }
 }