Merge "Remove TODOs" into androidx-master-dev
diff --git a/camera/camera-core/src/main/java/androidx/camera/core/CameraControl.java b/camera/camera-core/src/main/java/androidx/camera/core/CameraControl.java
index 85e4780..43b930a 100644
--- a/camera/camera-core/src/main/java/androidx/camera/core/CameraControl.java
+++ b/camera/camera-core/src/main/java/androidx/camera/core/CameraControl.java
@@ -30,22 +30,33 @@
  */
 public interface CameraControl {
     /**
-     * Starts a focus and metering action by the {@link FocusMeteringAction}.
+     * Starts a focus and metering action configured by the {@link FocusMeteringAction}.
      *
-     * <p>The {@link FocusMeteringAction} contains the configuration of multiple 3A
-     * {@link MeteringPoint}s, auto-cancel duration and{ @link OnAutoFocusListener} to receive the
-     * auto-focus result. Check {@link FocusMeteringAction} for more details.
+     * <p>It will trigger a auto focus action and enable AF/AE/AWB metering regions. The action
+     * is configured by a {@link FocusMeteringAction} which contains the configuration of
+     * multiple AF/AE/AWB {@link MeteringPoint}s, auto-cancel duration and
+     * {@link OnAutoFocusListener} to receive the auto focus result. Check
+     * {@link FocusMeteringAction}
+     * for more details.
+     *
+     * <p>Only one {@link FocusMeteringAction} is allowed to run at a time. If multiple
+     * {@link FocusMeteringAction} are executed in a row, only the latest one will work and
+     * other actions will be cancelled.
+     *
+     * <p>If the {@link FocusMeteringAction} specifies more AF/AE/AWB regions than what is
+     * supported on current device, only the first AF/AE/AWB regions which are supported on the
+     * device will be enabled.
      *
      * @param action the {@link FocusMeteringAction} to be executed.
      */
     void startFocusAndMetering(@NonNull FocusMeteringAction action);
 
     /**
-     * Cancels current {@link FocusMeteringAction}.
+     * Cancels current {@link FocusMeteringAction} and clears AF/AE/AWB regions.
      *
-     * <p>It clears the 3A regions and update current AF mode to CONTINOUS AF (if supported).
-     * If auto-focus does not complete, it will notify the {@link OnAutoFocusListener} with
-     * isFocusLocked set to false.
+     * <p>Clear the AF/AE/AWB regions and update current AF mode to CONTINUOUS AF (if
+     * supported). If auto-focus does not complete, it will notify the
+     * {@link OnAutoFocusListener} with isFocusLocked set to false.
      */
     void cancelFocusAndMetering();
 
@@ -59,10 +70,10 @@
      * applications' duty to clamp the ratio.
      *
      * @return a {@link ListenableFuture} which is finished when current repeating request
-     *     result contains the requested zoom ratio. It fails with
-     *     {@link OperationCanceledException} if there is newer value being set or camera is closed.
-     *     If ratio is out of range, it fails with
-     *     {@link CameraControl.ArgumentOutOfRangeException}.
+     * result contains the requested zoom ratio. It fails with
+     * {@link OperationCanceledException} if there is newer value being set or camera is closed.
+     * If ratio is out of range, it fails with
+     * {@link CameraControl.ArgumentOutOfRangeException}.
      */
     @NonNull
     ListenableFuture<Void> setZoomRatio(float ratio);
@@ -78,10 +89,10 @@
      * applications' duty to clamp the zoomPercentage within [0..1].
      *
      * @return a {@link ListenableFuture} which is finished when current repeating request
-     *     result contains the requested zoom percentage. It fails with
-     *     {@link OperationCanceledException} if there is newer value being set or camera is closed.
-     *     If percentage is out of range, it fails with
-     *     {@link CameraControl.ArgumentOutOfRangeException}.
+     * result contains the requested zoom percentage. It fails with
+     * {@link OperationCanceledException} if there is newer value being set or camera is closed.
+     * If percentage is out of range, it fails with
+     * {@link CameraControl.ArgumentOutOfRangeException}.
      */
     @NonNull
     ListenableFuture<Void> setZoomPercentage(@FloatRange(from = 0f, to = 1f) float percentage);
diff --git a/camera/camera-core/src/main/java/androidx/camera/core/DisplayOrientedMeteringPointFactory.java b/camera/camera-core/src/main/java/androidx/camera/core/DisplayOrientedMeteringPointFactory.java
index 3850350..61e747a 100644
--- a/camera/camera-core/src/main/java/androidx/camera/core/DisplayOrientedMeteringPointFactory.java
+++ b/camera/camera-core/src/main/java/androidx/camera/core/DisplayOrientedMeteringPointFactory.java
@@ -19,21 +19,31 @@
 import android.content.Context;
 import android.graphics.PointF;
 import android.view.Display;
+import android.view.View;
 import android.view.WindowManager;
 
 import androidx.annotation.NonNull;
 
 /**
- * A {@link MeteringPointFactory} that can create {@link MeteringPoint} by display oriented x, y.
+ * A {@link MeteringPointFactory} that can convert a {@link View} (x, y) into a
+ * {@link MeteringPoint} which can then be used to construct a {@link FocusMeteringAction} to
+ * start a focus and metering action.
  *
- * <p>This factory will consider the current display rotation and the lens facing to translate the
- * x/y correctly. Using this factory, apps do not need to handle the device rotation. They
- * can simply pass the x/y retrieved from their View. However if the camera preview is cropped,
- * scaled or rotated, it is apps' duty to transform the coordinates first.
+ * <p>For apps showing full camera preview in a View without any scaling, cropping or
+ * rotating applied, they can simply use view width and height to create the
+ * {@link DisplayOrientedMeteringPointFactory} and then pass {@link View} (x, y) to create a
+ * {@link MeteringPoint}. This factory will convert the (x, y) into the sensor (x, y) based on
+ * display rotation and {@link LensFacing}.
  *
- * <p> The width/height of this factory is the logical width/height of the preview FoV and X/Y
- * is the logical XY inside the FOV. User can set the width and height to 1.0 which will make the
- * XY the normalized coordinates [0..1].
+ * <p>If camera preview is scaled, cropped or rotated in the {@link View}, it is applications'
+ * duty to transform the coordinates properly so that the width and height of this
+ * factory represents the full Preview FOV and also the (x,y) passed to create
+ * {@link MeteringPoint} needs to be adjusted by apps to the  coordinates left-top (0,0) -
+ * right-bottom (width, height). For Example, if the preview is scaled to 2X from the center and
+ * is cropped in a {@link View}. Assuming that the dimension of View is (240, 320), then the
+ * width/height of this {@link DisplayOrientedMeteringPointFactory} should be (480, 640).  And
+ * the (x, y) from the {@link View} should be converted to (x + (480-240)/2, y + (640 - 320)/2)
+ * first.
  */
 public final class DisplayOrientedMeteringPointFactory extends MeteringPointFactory {
     /** The logical width of FoV in current display orientation */
@@ -49,17 +59,22 @@
     private final CameraInfo mCameraInfo;
 
     /**
-     * Creates the {@link MeteringPointFactory} with default display orientation.
+     * Creates a {@link DisplayOrientedMeteringPointFactory} for converting View (x, y) into a
+     * {@link MeteringPoint} based on default display's orientation and {@link LensFacing}.
      *
-     * <p>The width/height is the logical width/height of the preview FoV and X/Y is the logical
-     * XY inside the FOV. User can set the width and height to 1.0 which will make the XY the
-     * normalized coordinates [0..1]. Or user can set the width/height to the View width/height and
-     * then X/Y becomes the X/Y in the view.
+     * <p>The width/height of this factory forms a coordinate left-top (0, 0) - right-bottom
+     * (width, height) which represents the full camera preview FOV in default display's
+     * orientation. The (x, y) passed to {@link MeteringPointFactory#convertPoint(float, float)}
+     * should be adjusted to this coordinate first. For apps showing full camera preview in a
+     * {@link View}, it is as simple as passing View's width/height and passing View (x, y)
+     * directly to create a {@link MeteringPoint}.
      *
      * @param context    context to get the {@link WindowManager} for default display rotation.
      * @param lensFacing current lens facing.
-     * @param width      the logical width of FoV in current display orientation.
-     * @param height     the logical height of FoV in current display orientation.
+     * @param width      the width of the coordinate which are mapped to the full camera preview
+     *                   FOV in default display's orientation.
+     * @param height     the height of the coordinate which are mapped to the full camera preview
+     *                   FOVin default display's orientation.
      */
     public DisplayOrientedMeteringPointFactory(@NonNull Context context,
             @NonNull LensFacing lensFacing, float width, float height) {
@@ -68,18 +83,23 @@
     }
 
     /**
-     * Creates the  {@link MeteringPointFactory}  with custom display orientation. This is used
-     * in multi-display situation.
+     * Creates a {@link DisplayOrientedMeteringPointFactory} for converting View (x, y) into a
+     * {@link MeteringPoint} based on custom display's rotation and {@link LensFacing}. This is
+     * used in multi-display situation.
      *
-     * <p>The width/height is the logical width/height of the preview FoV and X/Y is the logical
-     * XY inside the FOV. User can set the width and height to 1.0 which will make the XY the
-     * normalized coordinates [0..1]. Or user can set the width/height to the View width/height and
-     * then X/Y becomes the X/Y in the view.
-     * {@link Display} is used to dete
+     * <p>The width/height of this factory forms a coordinate left-top (0, 0) - right-bottom
+     * (width, height) which represents the full camera preview FOV in given display's
+     * orientation. The (x, y) passed to {@link MeteringPointFactory#convertPoint(float, float)}
+     * should be adjusted to this coordinate first. For apps showing full camera preview in a
+     * {@link View}, it is as simple as passing View's width/height and passing View (x, y)
+     * directly to create a {@link MeteringPoint}.
+     *
      * @param display    {@link Display} to get the orientation from.
      * @param lensFacing current lens facing.
-     * @param width      the logical width of FoV in current display orientation.
-     * @param height     the logical height of FoV in current display orientation.
+     * @param width      the width of the coordinate which are mapped to the full camera preview
+     *                   FOV in given display's orientation.
+     * @param height     the height of the coordinate which are mapped to the full camera preview
+     *                   FOV in given display's orientation.
      */
     public DisplayOrientedMeteringPointFactory(@NonNull Display display,
             @NonNull LensFacing lensFacing, float width, float height) {
@@ -100,7 +120,7 @@
      */
     @NonNull
     @Override
-    protected PointF translatePoint(float x, float y) {
+    protected PointF convertPoint(float x, float y) {
         float width = mWidth;
         float height = mHeight;
 
diff --git a/camera/camera-core/src/main/java/androidx/camera/core/MeteringPointFactory.java b/camera/camera-core/src/main/java/androidx/camera/core/MeteringPointFactory.java
index 4423169..2f4ae8c 100644
--- a/camera/camera-core/src/main/java/androidx/camera/core/MeteringPointFactory.java
+++ b/camera/camera-core/src/main/java/androidx/camera/core/MeteringPointFactory.java
@@ -51,7 +51,7 @@
      * @return a {@link PointF} consisting of translated normalized crop region x/y,
      */
     @NonNull
-    protected abstract PointF translatePoint(float x, float y);
+    protected abstract PointF convertPoint(float x, float y);
 
     /**
      * Creates a {@link MeteringPoint} by x, y.
@@ -79,8 +79,8 @@
      */
     @NonNull
     public final MeteringPoint createPoint(float x, float y, float size, float weight) {
-        PointF translatedXY = translatePoint(x, y);
-        return new MeteringPoint(translatedXY.x, translatedXY.y, size, weight,
+        PointF convertedPoint = convertPoint(x, y);
+        return new MeteringPoint(convertedPoint.x, convertedPoint.y, size, weight,
                 mFovAspectRatio);
     }
 }
diff --git a/camera/camera-core/src/main/java/androidx/camera/core/SensorOrientedMeteringPointFactory.java b/camera/camera-core/src/main/java/androidx/camera/core/SensorOrientedMeteringPointFactory.java
index 9f9fcef..63688e0 100644
--- a/camera/camera-core/src/main/java/androidx/camera/core/SensorOrientedMeteringPointFactory.java
+++ b/camera/camera-core/src/main/java/androidx/camera/core/SensorOrientedMeteringPointFactory.java
@@ -108,7 +108,7 @@
      */
     @Override
     @NonNull
-    protected PointF translatePoint(float x, float y) {
+    protected PointF convertPoint(float x, float y) {
         PointF pt = new PointF(x / mWidth, y / mHeight);
         return pt;
     }
diff --git a/camera/camera-view/src/main/java/androidx/camera/view/TextureViewMeteringPointFactory.java b/camera/camera-view/src/main/java/androidx/camera/view/TextureViewMeteringPointFactory.java
index dab8a31..55149f4 100644
--- a/camera/camera-view/src/main/java/androidx/camera/view/TextureViewMeteringPointFactory.java
+++ b/camera/camera-view/src/main/java/androidx/camera/view/TextureViewMeteringPointFactory.java
@@ -48,7 +48,7 @@
      */
     @NonNull
     @Override
-    protected PointF translatePoint(float x, float y) {
+    protected PointF convertPoint(float x, float y) {
         Matrix transform = new Matrix();
         mTextureView.getTransform(transform);
 
diff --git a/media2/widget/src/androidTest/java/androidx/media2/widget/MediaControlViewTestActivity.java b/media2/widget/src/androidTest/java/androidx/media2/widget/MediaControlViewTestActivity.java
index 4535176..5dd2103 100644
--- a/media2/widget/src/androidTest/java/androidx/media2/widget/MediaControlViewTestActivity.java
+++ b/media2/widget/src/androidTest/java/androidx/media2/widget/MediaControlViewTestActivity.java
@@ -35,6 +35,7 @@
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
+        TestUtils.setKeepScreenOn(this);
         setContentView(R.layout.mediacontrolviewtest_layout);
         mSurfaceView = findViewById(R.id.surfaceview);
     }
diff --git a/media2/widget/src/androidTest/java/androidx/media2/widget/MediaControlView_WithPlayerTest.java b/media2/widget/src/androidTest/java/androidx/media2/widget/MediaControlView_WithPlayerTest.java
index ef6974c..e9a2912 100644
--- a/media2/widget/src/androidTest/java/androidx/media2/widget/MediaControlView_WithPlayerTest.java
+++ b/media2/widget/src/androidTest/java/androidx/media2/widget/MediaControlView_WithPlayerTest.java
@@ -95,8 +95,6 @@
         Uri fileSchemeUri = Uri.parse("android.resource://" + mContext.getPackageName() + "/"
                 + androidx.media2.widget.test.R.raw.test_file_scheme_video);
         mFileSchemeMediaItem = createTestMediaItem(fileSchemeUri);
-
-        setKeepScreenOn(mActivityRule);
         checkAttachedToWindow(mMediaControlView);
     }
 
diff --git a/media2/widget/src/androidTest/java/androidx/media2/widget/MediaControlView_WithoutPlayerTest.java b/media2/widget/src/androidTest/java/androidx/media2/widget/MediaControlView_WithoutPlayerTest.java
index 9f2642a..a4fa4dd 100644
--- a/media2/widget/src/androidTest/java/androidx/media2/widget/MediaControlView_WithoutPlayerTest.java
+++ b/media2/widget/src/androidTest/java/androidx/media2/widget/MediaControlView_WithoutPlayerTest.java
@@ -61,8 +61,6 @@
         mActivity = mActivityRule.getActivity();
         mMediaControlView = mActivity.findViewById(
                 androidx.media2.widget.test.R.id.mediacontrolview);
-
-        setKeepScreenOn(mActivityRule);
         checkAttachedToWindow(mMediaControlView);
     }
 
diff --git a/media2/widget/src/androidTest/java/androidx/media2/widget/MediaWidgetTestBase.java b/media2/widget/src/androidTest/java/androidx/media2/widget/MediaWidgetTestBase.java
index dac6636..c96ba43 100644
--- a/media2/widget/src/androidTest/java/androidx/media2/widget/MediaWidgetTestBase.java
+++ b/media2/widget/src/androidTest/java/androidx/media2/widget/MediaWidgetTestBase.java
@@ -16,19 +16,12 @@
 
 package androidx.media2.widget;
 
-import static android.content.Context.KEYGUARD_SERVICE;
-
 import static org.junit.Assert.assertTrue;
 
-import android.app.Activity;
-import android.app.Instrumentation;
-import android.app.KeyguardManager;
 import android.content.Context;
 import android.net.Uri;
-import android.os.Build;
 import android.text.TextUtils;
 import android.view.View;
-import android.view.WindowManager;
 
 import androidx.annotation.GuardedBy;
 import androidx.annotation.NonNull;
@@ -43,8 +36,6 @@
 import androidx.media2.session.MediaSession;
 import androidx.media2.widget.test.R;
 import androidx.test.core.app.ApplicationProvider;
-import androidx.test.platform.app.InstrumentationRegistry;
-import androidx.test.rule.ActivityTestRule;
 
 import org.junit.Before;
 
@@ -82,31 +73,6 @@
         mSessionCallbackExecutor = Executors.newFixedThreadPool(1);
     }
 
-    static <T extends Activity> void setKeepScreenOn(ActivityTestRule<T> activityRule)
-            throws Throwable {
-        final Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation();
-        final Activity activity = activityRule.getActivity();
-        activityRule.runOnUiThread(new Runnable() {
-            @Override
-            public void run() {
-                if (Build.VERSION.SDK_INT >= 27) {
-                    activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
-                    activity.setTurnScreenOn(true);
-                    activity.setShowWhenLocked(true);
-                    KeyguardManager keyguardManager = (KeyguardManager)
-                            instrumentation.getTargetContext().getSystemService(KEYGUARD_SERVICE);
-                    keyguardManager.requestDismissKeyguard(activity, null);
-                } else {
-                    activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
-                            | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
-                            | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
-                            | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
-                }
-            }
-        });
-        instrumentation.waitForIdleSync();
-    }
-
     static void checkAttachedToWindow(View view) throws Exception {
         if (!view.isAttachedToWindow()) {
             final CountDownLatch latch = new CountDownLatch(1);
diff --git a/media2/widget/src/androidTest/java/androidx/media2/widget/TestUtils.java b/media2/widget/src/androidTest/java/androidx/media2/widget/TestUtils.java
new file mode 100644
index 0000000..3d2ba52
--- /dev/null
+++ b/media2/widget/src/androidTest/java/androidx/media2/widget/TestUtils.java
@@ -0,0 +1,44 @@
+/*
+ * Copyright 2019 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 androidx.media2.widget;
+
+import static android.content.Context.KEYGUARD_SERVICE;
+
+import android.app.Activity;
+import android.app.KeyguardManager;
+import android.os.Build;
+import android.view.WindowManager;
+
+final class TestUtils {
+    static void setKeepScreenOn(Activity activity) {
+        if (Build.VERSION.SDK_INT >= 27) {
+            activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
+            activity.setTurnScreenOn(true);
+            activity.setShowWhenLocked(true);
+            KeyguardManager keyguardManager =
+                    (KeyguardManager) activity.getSystemService(KEYGUARD_SERVICE);
+            keyguardManager.requestDismissKeyguard(activity, null);
+        } else {
+            activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
+                    | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
+                    | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
+                    | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
+        }
+    }
+
+    private TestUtils() {}
+}
diff --git a/media2/widget/src/androidTest/java/androidx/media2/widget/VideoViewTestActivity.java b/media2/widget/src/androidTest/java/androidx/media2/widget/VideoViewTestActivity.java
index a7be923..cf5955a 100644
--- a/media2/widget/src/androidTest/java/androidx/media2/widget/VideoViewTestActivity.java
+++ b/media2/widget/src/androidTest/java/androidx/media2/widget/VideoViewTestActivity.java
@@ -31,6 +31,7 @@
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
+        TestUtils.setKeepScreenOn(this);
         setContentView(R.layout.videoview_layout);
     }
 }
diff --git a/media2/widget/src/androidTest/java/androidx/media2/widget/VideoView_WithPlayerTest.java b/media2/widget/src/androidTest/java/androidx/media2/widget/VideoView_WithPlayerTest.java
index dee8015..fe1bb1b 100644
--- a/media2/widget/src/androidTest/java/androidx/media2/widget/VideoView_WithPlayerTest.java
+++ b/media2/widget/src/androidTest/java/androidx/media2/widget/VideoView_WithPlayerTest.java
@@ -92,8 +92,6 @@
         mVideoView = mActivity.findViewById(R.id.videoview);
         mMediaItem = createTestMediaItem();
         mPixelCopyHelper = new SynchronousPixelCopy();
-
-        setKeepScreenOn(mActivityRule);
         checkAttachedToWindow(mVideoView);
     }
 
diff --git a/media2/widget/src/androidTest/java/androidx/media2/widget/VideoView_WithoutPlayerTest.java b/media2/widget/src/androidTest/java/androidx/media2/widget/VideoView_WithoutPlayerTest.java
index 235a40e..26e3c26 100644
--- a/media2/widget/src/androidTest/java/androidx/media2/widget/VideoView_WithoutPlayerTest.java
+++ b/media2/widget/src/androidTest/java/androidx/media2/widget/VideoView_WithoutPlayerTest.java
@@ -47,8 +47,6 @@
     public void setup() throws Throwable {
         mActivity = mActivityRule.getActivity();
         mVideoView = mActivity.findViewById(R.id.videoview);
-
-        setKeepScreenOn(mActivityRule);
         checkAttachedToWindow(mVideoView);
     }