diff --git a/library/src/androidTest/java/com/google/android/cameraview/AspectRatioInstrumentationTest.java b/library/src/androidTest/java/com/google/android/cameraview/AspectRatioInstrumentationTest.java index 718f9b11..3fc87e74 100644 --- a/library/src/androidTest/java/com/google/android/cameraview/AspectRatioInstrumentationTest.java +++ b/library/src/androidTest/java/com/google/android/cameraview/AspectRatioInstrumentationTest.java @@ -20,6 +20,7 @@ import org.junit.runner.RunWith; import android.os.Bundle; +import android.os.Parcel; import android.support.test.rule.UiThreadTestRule; import android.support.test.runner.AndroidJUnit4; @@ -32,24 +33,22 @@ @RunWith(AndroidJUnit4.class) public class AspectRatioInstrumentationTest { - public UiThreadTestRule rule; - - public AspectRatioInstrumentationTest() { - rule = new UiThreadTestRule(); - } - @Test public void testParcel() { - final String key = "key"; - AspectRatio ratio = AspectRatio.of(4, 3); - Bundle b = new Bundle(); - b.putParcelable(key, ratio); - AspectRatio result = b.getParcelable(key); - assertNotNull(result); - assertThat(result.getX(), is(4)); - assertThat(result.getY(), is(3)); - // As the first instance is alive, the parceled result should still be the same instance - assertThat(result, is(sameInstance(ratio))); + final AspectRatio original = AspectRatio.of(4, 3); + final Parcel parcel = Parcel.obtain(); + try { + parcel.writeParcelable(original, 0); + parcel.setDataPosition(0); + final AspectRatio restored = parcel.readParcelable(getClass().getClassLoader()); + assertNotNull(restored); + assertThat(restored.getX(), is(4)); + assertThat(restored.getY(), is(3)); + // As the first instance is alive, the parceled result should still be the same instance + assertThat(restored, is(sameInstance(original))); + } finally { + parcel.recycle(); + } } } diff --git a/library/src/main/java/com/google/android/cameraview/CameraView.java b/library/src/main/java/com/google/android/cameraview/CameraView.java index 6cdebf29..3c8da246 100644 --- a/library/src/main/java/com/google/android/cameraview/CameraView.java +++ b/library/src/main/java/com/google/android/cameraview/CameraView.java @@ -129,7 +129,7 @@ public void onDisplayOrientationChanged(int displayOrientation) { @Override protected void onAttachedToWindow() { super.onAttachedToWindow(); - mDisplayOrientationDetector.enable(getDisplay()); + mDisplayOrientationDetector.enable(ViewCompat2.getDisplay(this)); } @Override diff --git a/library/src/main/java/com/google/android/cameraview/ViewCompat2.java b/library/src/main/java/com/google/android/cameraview/ViewCompat2.java new file mode 100644 index 00000000..4b2334c1 --- /dev/null +++ b/library/src/main/java/com/google/android/cameraview/ViewCompat2.java @@ -0,0 +1,50 @@ +/* + * 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 com.google.android.cameraview; + +import android.os.Build; +import android.support.annotation.NonNull; +import android.support.v4.hardware.display.DisplayManagerCompat; +import android.support.v4.view.ViewCompat; +import android.view.Display; +import android.view.View; + + +/** + * Some more addition to {@link android.support.v4.view.ViewCompat}. + */ +class ViewCompat2 { + + private ViewCompat2() { + } + + /** + * Gets the logical display to which the view's window has been attached. + * + * @param view The view. + * @return The logical display, or null if the view is not currently attached to a window. + */ + public static Display getDisplay(@NonNull View view) { + if (Build.VERSION.SDK_INT >= 17) { + return view.getDisplay(); + } + return ViewCompat.isAttachedToWindow(view) ? + DisplayManagerCompat.getInstance(view.getContext()) + .getDisplay(Display.DEFAULT_DISPLAY) : null; + } + +}