Add ViewCompat.getDisplay

Bug: 30776880
Change-Id: I8fa46a8f6b29ae9ae41f44e16cd32b039102860a
diff --git a/api/current.txt b/api/current.txt
index 1e5774a..d23b9e9 100644
--- a/api/current.txt
+++ b/api/current.txt
@@ -6315,6 +6315,7 @@
     method public static android.content.res.ColorStateList getBackgroundTintList(android.view.View);
     method public static android.graphics.PorterDuff.Mode getBackgroundTintMode(android.view.View);
     method public static android.graphics.Rect getClipBounds(android.view.View);
+    method public static android.view.Display getDisplay(android.view.View);
     method public static float getElevation(android.view.View);
     method public static boolean getFitsSystemWindows(android.view.View);
     method public static int getImportantForAccessibility(android.view.View);
diff --git a/compat/gingerbread/android/support/v4/view/ViewCompatBase.java b/compat/gingerbread/android/support/v4/view/ViewCompatBase.java
index f4e113e..e7882bd 100644
--- a/compat/gingerbread/android/support/v4/view/ViewCompatBase.java
+++ b/compat/gingerbread/android/support/v4/view/ViewCompatBase.java
@@ -16,10 +16,13 @@
 
 package android.support.v4.view;
 
+import android.content.Context;
 import android.content.res.ColorStateList;
 import android.graphics.PorterDuff;
+import android.view.Display;
 import android.view.View;
 import android.view.ViewParent;
+import android.view.WindowManager;
 
 import java.lang.reflect.Field;
 
@@ -149,4 +152,14 @@
             }
         }
     }
+
+    static Display getDisplay(View view) {
+        if (isAttachedToWindow(view)) {
+            final WindowManager wm = (WindowManager) view.getContext().getSystemService(
+                    Context.WINDOW_SERVICE);
+            return wm.getDefaultDisplay();
+        }
+        return null;
+    }
+
 }
diff --git a/compat/java/android/support/v4/view/ViewCompat.java b/compat/java/android/support/v4/view/ViewCompat.java
index 4e14ec0..7af945d 100644
--- a/compat/java/android/support/v4/view/ViewCompat.java
+++ b/compat/java/android/support/v4/view/ViewCompat.java
@@ -31,6 +31,7 @@
 import android.support.v4.view.accessibility.AccessibilityNodeInfoCompat;
 import android.support.v4.view.accessibility.AccessibilityNodeProviderCompat;
 import android.util.Log;
+import android.view.Display;
 import android.view.MotionEvent;
 import android.view.VelocityTracker;
 import android.view.View;
@@ -484,6 +485,7 @@
         void offsetTopAndBottom(View view, int offset);
         void offsetLeftAndRight(View view, int offset);
         void setPointerIcon(View view, PointerIconCompat pointerIcon);
+        Display getDisplay(View view);
     }
 
     static class BaseViewCompatImpl implements ViewCompatImpl {
@@ -1147,6 +1149,11 @@
         public void setPointerIcon(View view, PointerIconCompat pointerIcon) {
             // no-op
         }
+
+        @Override
+        public Display getDisplay(View view) {
+            return ViewCompatBase.getDisplay(view);
+        }
     }
 
     static class HCViewCompatImpl extends BaseViewCompatImpl {
@@ -1539,6 +1546,11 @@
         public boolean isPaddingRelative(View view) {
             return ViewCompatJellybeanMr1.isPaddingRelative(view);
         }
+
+        @Override
+        public Display getDisplay(View view) {
+            return ViewCompatJellybeanMr1.getDisplay(view);
+        }
     }
 
     static class JbMr2ViewCompatImpl extends JbMr1ViewCompatImpl {
@@ -3342,7 +3354,7 @@
      * <p>
      * Compatibility:
      * <ul>
-     *     <li>API < 18: Always returns {@code false}</li>
+     *     <li>API &lt; 18: Always returns {@code false}</li>
      * </ul>
      *
      * @return whether the view hierarchy is currently undergoing a layout pass
@@ -3364,7 +3376,7 @@
      * <p>
      * Compatibility:
      * <ul>
-     *     <li>API < 19: Always returns {@code false}</li>
+     *     <li>API &lt; 19: Always returns {@code false}</li>
      * </ul>
      *
      * @return true if layout direction has been resolved.
@@ -3391,7 +3403,7 @@
      * <p>
      * Compatibility:
      * <ul>
-     *     <li>API < 21: No-op
+     *     <li>API &lt; 21: No-op
      * </ul>
      *
      * @param z The visual z position of this view, in pixels.
@@ -3532,5 +3544,19 @@
         IMPL.setPointerIcon(view, pointerIcon);
     }
 
+    /**
+     * Gets the logical display to which the view's window has been attached.
+     * <p>
+     * Compatibility:
+     * <ul>
+     * <li>API &lt; 17: Returns the default display when the view is attached. Otherwise, null.
+     * </ul>
+     *
+     * @return The logical display, or null if the view is not currently attached to a window.
+     */
+    public static Display getDisplay(@NonNull View view) {
+        return IMPL.getDisplay(view);
+    }
+
     protected ViewCompat() {}
 }
diff --git a/compat/jellybean-mr1/android/support/v4/view/ViewCompatJellybeanMr1.java b/compat/jellybean-mr1/android/support/v4/view/ViewCompatJellybeanMr1.java
index 4865a8b..c39ef2a 100644
--- a/compat/jellybean-mr1/android/support/v4/view/ViewCompatJellybeanMr1.java
+++ b/compat/jellybean-mr1/android/support/v4/view/ViewCompatJellybeanMr1.java
@@ -17,6 +17,7 @@
 package android.support.v4.view;
 
 import android.graphics.Paint;
+import android.view.Display;
 import android.view.View;
 
 /**
@@ -63,4 +64,8 @@
     public static boolean isPaddingRelative(View view) {
         return view.isPaddingRelative();
     }
+
+    public static Display getDisplay(View view) {
+        return view.getDisplay();
+    }
 }
diff --git a/compat/tests/AndroidManifest.xml b/compat/tests/AndroidManifest.xml
index a46860a..8228476 100644
--- a/compat/tests/AndroidManifest.xml
+++ b/compat/tests/AndroidManifest.xml
@@ -37,6 +37,8 @@
         <activity
             android:name="android.support.v4.ThemedYellowActivity"
             android:theme="@style/YellowTheme" />
+
+        <activity android:name="android.support.v4.view.ViewCompatActivity"/>
     </application>
 
     <instrumentation android:name="android.test.InstrumentationTestRunner"
diff --git a/compat/tests/java/android/support/v4/view/ViewCompatActivity.java b/compat/tests/java/android/support/v4/view/ViewCompatActivity.java
new file mode 100644
index 0000000..28ff642
--- /dev/null
+++ b/compat/tests/java/android/support/v4/view/ViewCompatActivity.java
@@ -0,0 +1,29 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package android.support.v4.view;
+
+import android.support.compat.test.R;
+import android.support.v4.BaseTestActivity;
+
+public class ViewCompatActivity extends BaseTestActivity {
+
+    @Override
+    protected int getContentViewLayoutResId() {
+        return R.layout.view_compat_activity;
+    }
+
+}
diff --git a/compat/tests/java/android/support/v4/view/ViewCompatTest.java b/compat/tests/java/android/support/v4/view/ViewCompatTest.java
index c4217a5..0b102a6 100644
--- a/compat/tests/java/android/support/v4/view/ViewCompatTest.java
+++ b/compat/tests/java/android/support/v4/view/ViewCompatTest.java
@@ -15,18 +15,39 @@
  */
 package android.support.v4.view;
 
+import android.app.Activity;
 import android.support.test.runner.AndroidJUnit4;
-import android.test.AndroidTestCase;
-import android.test.suitebuilder.annotation.SmallTest;
+import android.test.suitebuilder.annotation.MediumTest;
+import android.view.Display;
 import android.view.View;
+import android.support.v4.BaseInstrumentationTestCase;
+import android.support.compat.test.R;
+
+import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+
 
 @RunWith(AndroidJUnit4.class)
-@SmallTest
-public class ViewCompatTest {
+@MediumTest
+public class ViewCompatTest extends BaseInstrumentationTestCase<ViewCompatActivity> {
+
+    private View mView;
+
+    public ViewCompatTest() {
+        super(ViewCompatActivity.class);
+    }
+
+    @Before
+    public void setUp() {
+        final Activity activity = mActivityTestRule.getActivity();
+        mView = activity.findViewById(R.id.view);
+    }
+
     @Test
     public void testConstants() {
         // Compat constants must match core constants since they can be used interchangeably
@@ -34,4 +55,18 @@
         assertEquals("LTR constants", View.LAYOUT_DIRECTION_LTR, ViewCompat.LAYOUT_DIRECTION_LTR);
         assertEquals("RTL constants", View.LAYOUT_DIRECTION_RTL, ViewCompat.LAYOUT_DIRECTION_RTL);
     }
+
+    @Test
+    public void testGetDisplay() {
+        final Display display = ViewCompat.getDisplay(mView);
+        assertNotNull(display);
+    }
+
+    @Test
+    public void testGetDisplay_returnsNullForUnattachedView() {
+        final View view = new View(mActivityTestRule.getActivity());
+        final Display display = ViewCompat.getDisplay(view);
+        assertNull(display);
+    }
+
 }
diff --git a/compat/tests/res/layout/view_compat_activity.xml b/compat/tests/res/layout/view_compat_activity.xml
new file mode 100644
index 0000000..2a8acac
--- /dev/null
+++ b/compat/tests/res/layout/view_compat_activity.xml
@@ -0,0 +1,29 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+     Copyright (C) 2016 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+
+         http://www.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+<FrameLayout
+    android:id="@+id/container"
+    xmlns:android="http://schemas.android.com/apk/res/android"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent">
+
+    <View
+        android:id="@+id/view"
+        android:layout_width="64dp"
+        android:layout_height="64dp"
+        android:background="#f00"/>
+
+</FrameLayout>