No hardcoded display dimensions in the VDM Client

Fix: 314729898
Test: manual

Change-Id: I77618a88630d5fe38f90a05f9184fbb5051b2c42
diff --git a/samples/VirtualDeviceManager/client/res/layout/display_fragment.xml b/samples/VirtualDeviceManager/client/res/layout/display_fragment.xml
index 29a4e25..1907398 100644
--- a/samples/VirtualDeviceManager/client/res/layout/display_fragment.xml
+++ b/samples/VirtualDeviceManager/client/res/layout/display_fragment.xml
@@ -1,16 +1,21 @@
-<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
-    android:id="@+id/display_focus"
+<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    xmlns:app="http://schemas.android.com/apk/res-auto"
     android:layout_width="wrap_content"
     android:layout_height="match_parent"
-    android:gravity="center"
+    android:gravity="center_horizontal"
     android:orientation="vertical"
     android:padding="5dp">
 
     <LinearLayout
+        android:id="@+id/display_header"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
-        android:gravity="center"
-        android:orientation="vertical">
+        android:orientation="vertical"
+        app:layout_constraintBottom_toTopOf="@id/remote_display_view"
+        app:layout_constraintEnd_toEndOf="parent"
+        app:layout_constraintStart_toStartOf="parent"
+        app:layout_constraintTop_toTopOf="parent"
+        app:layout_constraintVertical_chainStyle="packed">
 
         <TextView
             android:id="@+id/display_title"
@@ -64,17 +69,23 @@
         </LinearLayout>
     </LinearLayout>
 
-    <FrameLayout
-        android:id="@+id/frame"
-        android:layout_width="250dp"
-        android:layout_height="440dp"
-        android:layout_gravity="center">
+    <View
+        android:id="@+id/strut"
+        android:layout_width="0dp"
+        android:layout_height="0dp"
+        app:layout_constraintBottom_toTopOf="@id/remote_display_view"
+        app:layout_constraintEnd_toEndOf="@id/display_header"
+        app:layout_constraintStart_toStartOf="@id/display_header"
+        app:layout_constraintTop_toBottomOf="@id/display_header" />
 
-        <TextureView
-            android:id="@+id/remote_display_view"
-            android:layout_width="250dp"
-            android:layout_height="440dp"
-            android:layout_gravity="center" />
-    </FrameLayout>
+    <TextureView
+        android:id="@+id/remote_display_view"
+        android:layout_width="0dp"
+        android:layout_height="0dp"
+        app:layout_constraintBottom_toBottomOf="parent"
+        app:layout_constraintDimensionRatio="9:16"
+        app:layout_constraintEnd_toEndOf="@id/display_header"
+        app:layout_constraintStart_toStartOf="@id/display_header"
+        app:layout_constraintTop_toBottomOf="@id/display_header" />
 
-</LinearLayout>
\ No newline at end of file
+</androidx.constraintlayout.widget.ConstraintLayout>
diff --git a/samples/VirtualDeviceManager/client/src/com/example/android/vdmdemo/client/DisplayAdapter.java b/samples/VirtualDeviceManager/client/src/com/example/android/vdmdemo/client/DisplayAdapter.java
index 3a0498f..00fdac3 100644
--- a/samples/VirtualDeviceManager/client/src/com/example/android/vdmdemo/client/DisplayAdapter.java
+++ b/samples/VirtualDeviceManager/client/src/com/example/android/vdmdemo/client/DisplayAdapter.java
@@ -27,7 +27,6 @@
 import android.view.TextureView;
 import android.view.View;
 import android.view.ViewGroup;
-import android.widget.FrameLayout;
 import android.widget.TextView;
 
 import androidx.annotation.NonNull;
@@ -149,7 +148,6 @@
         private InputManager.FocusListener mFocusListener = null;
         private Surface mSurface = null;
         private TextureView mTextureView = null;
-        private FrameLayout mTextureFrame = null;
         private TextView mDisplayTitle = null;
         private View mRotateButton = null;
         private int mDisplayId = 0;
@@ -163,10 +161,12 @@
             mRotateButton.setEnabled(rotationDegrees == 0 || resize);
 
             // Make sure the rotation is visible.
-            ViewGroup.LayoutParams frameLayoutParams = mTextureFrame.getLayoutParams();
-            frameLayoutParams.width = Math.max(mTextureView.getWidth(), mTextureView.getHeight());
-            frameLayoutParams.height = frameLayoutParams.width;
-            mTextureFrame.setLayoutParams(frameLayoutParams);
+            View strut = itemView.findViewById(R.id.strut);
+            ViewGroup.LayoutParams layoutParams = strut.getLayoutParams();
+            layoutParams.width = Math.max(mTextureView.getWidth(), mTextureView.getHeight());
+            strut.setLayoutParams(layoutParams);
+            final int postRotationWidth = (resize || rotationDegrees % 180 != 0)
+                    ? mTextureView.getHeight() : mTextureView.getWidth();
 
             mTextureView
                     .animate()
@@ -181,17 +181,9 @@
                                                     0,
                                                     mTextureView.getHeight(),
                                                     mTextureView.getWidth()));
-                                } else {
-                                    frameLayoutParams.width =
-                                            (rotationDegrees % 180 == 0)
-                                                    ? mTextureView.getWidth()
-                                                    : mTextureView.getHeight();
-                                    frameLayoutParams.height =
-                                            (rotationDegrees % 180 == 0)
-                                                    ? mTextureView.getHeight()
-                                                    : mTextureView.getWidth();
-                                    mTextureFrame.setLayoutParams(frameLayoutParams);
                                 }
+                                layoutParams.width = postRotationWidth;
+                                strut.setLayoutParams(layoutParams);
                             })
                     .start();
         }
@@ -204,11 +196,6 @@
             layoutParams.width = newBounds.width();
             layoutParams.height = newBounds.height();
             mTextureView.setLayoutParams(layoutParams);
-
-            ViewGroup.LayoutParams frameLayoutParams = mTextureFrame.getLayoutParams();
-            frameLayoutParams.width = newBounds.width();
-            frameLayoutParams.height = newBounds.height();
-            mTextureFrame.setLayoutParams(frameLayoutParams);
         }
 
         private void setDisplayTitle(String title) {
@@ -235,11 +222,10 @@
 
             mDisplayTitle = itemView.findViewById(R.id.display_title);
             mTextureView = itemView.findViewById(R.id.remote_display_view);
-            mTextureFrame = itemView.findViewById(R.id.frame);
 
             mFocusListener =
                     focusedDisplayId -> {
-                        View displayFocusIndicator = itemView.findViewById(R.id.display_focus);
+                        View displayFocusIndicator = itemView.findViewById(R.id.display_header);
                         if (focusedDisplayId == mDisplayId) {
                             displayFocusIndicator.setBackgroundResource(R.drawable.focus_frame);
                         } else {
diff --git a/samples/VirtualDeviceManager/common/src/com/example/android/vdmdemo/common/ConnectivityFragment.java b/samples/VirtualDeviceManager/common/src/com/example/android/vdmdemo/common/ConnectivityFragment.java
index cdbea98..a28abfb 100644
--- a/samples/VirtualDeviceManager/common/src/com/example/android/vdmdemo/common/ConnectivityFragment.java
+++ b/samples/VirtualDeviceManager/common/src/com/example/android/vdmdemo/common/ConnectivityFragment.java
@@ -41,26 +41,27 @@
             new ConnectionManager.ConnectionCallback() {
                 @Override
                 public void onConnecting(String remoteDeviceName) {
-                    mStatus.setText(getContext().getString(R.string.connecting, remoteDeviceName));
-                    mStatus.setBackgroundColor(mDefaultBackgroundColor);
+                    updateStatus(
+                            getContext().getString(R.string.connecting, remoteDeviceName),
+                            mDefaultBackgroundColor);
                 }
 
                 @Override
                 public void onConnected(String remoteDeviceName) {
-                    mStatus.setText(getContext().getString(R.string.connected, remoteDeviceName));
-                    mStatus.setBackgroundColor(Color.GREEN);
+                    updateStatus(
+                            getContext().getString(R.string.connected, remoteDeviceName),
+                            Color.GREEN);
                 }
 
                 @Override
                 public void onDisconnected() {
-                    mStatus.setText(getContext().getString(R.string.disconnected));
-                    mStatus.setBackgroundColor(mDefaultBackgroundColor);
+                    updateStatus(
+                            getContext().getString(R.string.disconnected), mDefaultBackgroundColor);
                 }
 
                 @Override
                 public void onError(String message) {
-                    mStatus.setText(message);
-                    mStatus.setBackgroundColor(Color.RED);
+                    updateStatus(message, Color.RED);
                 }
             };
 
@@ -103,4 +104,11 @@
         super.onDestroyView();
         mConnectionManager.removeConnectionCallback(mConnectionCallback);
     }
+
+    private void updateStatus(String message, int backgroundColor) {
+        getActivity().runOnUiThread(() -> {
+            mStatus.setText(message);
+            mStatus.setBackgroundColor(backgroundColor);
+        });
+    }
 }