Add resolutions for all cameras to the drop-down.

  Bug: 8766885

Change-Id: I485233876163913cb6f6b65046b502fa8145430e
diff --git a/apps/CtsVerifier/src/com/android/cts/verifier/camera/fov/CtsTestHelper.java b/apps/CtsVerifier/src/com/android/cts/verifier/camera/fov/CtsTestHelper.java
index c080729..bca217b 100644
--- a/apps/CtsVerifier/src/com/android/cts/verifier/camera/fov/CtsTestHelper.java
+++ b/apps/CtsVerifier/src/com/android/cts/verifier/camera/fov/CtsTestHelper.java
@@ -50,7 +50,7 @@
         String details = "PhotoSphere FOV test result:\n";
         for (int i = 0; i < resolutions.size(); i++) {
             SelectableResolution res = resolutions.get(i);
-            details += "Resolution:" + res.width + 'x' + res.height
+            details += "Camera:" + res.cameraId + ", Resolution:" + res.width + 'x' + res.height
                     + ", Measured FOV = " + res.measuredFOV + '\n';
 
         }
diff --git a/apps/CtsVerifier/src/com/android/cts/verifier/camera/fov/PhotoCaptureActivity.java b/apps/CtsVerifier/src/com/android/cts/verifier/camera/fov/PhotoCaptureActivity.java
index c5957c048..943ee3d 100644
--- a/apps/CtsVerifier/src/com/android/cts/verifier/camera/fov/PhotoCaptureActivity.java
+++ b/apps/CtsVerifier/src/com/android/cts/verifier/camera/fov/PhotoCaptureActivity.java
@@ -22,7 +22,6 @@
 import android.graphics.Color;
 import android.hardware.Camera;
 import android.hardware.Camera.PictureCallback;
-import android.hardware.Camera.Size;
 import android.os.Bundle;
 import android.os.PowerManager;
 import android.os.PowerManager.WakeLock;
@@ -52,7 +51,7 @@
  * An activity for showing the camera preview and taking a picture.
  */
 public class PhotoCaptureActivity extends Activity
-implements PictureCallback, SurfaceHolder.Callback {
+        implements PictureCallback, SurfaceHolder.Callback {
     private static final String TAG = PhotoCaptureActivity.class.getSimpleName();
     private static final int FOV_REQUEST_CODE = 1006;
     private static final String PICTURE_FILENAME = "photo.jpg";
@@ -65,6 +64,7 @@
     private ArrayAdapter<SelectableResolution> mAdapter;
 
     private Camera mCamera;
+    private Size mSurfaceSize;
     private boolean mCameraInitialized = false;
     private boolean mPreviewActive = false;
     private int mResolutionSpinnerIndex = -1;
@@ -119,6 +119,7 @@
                 if (mSupportedResolutions != null) {
                     SelectableResolution resolution = mSupportedResolutions.get(position);
 
+                    switchToCamera(resolution.cameraId);
                     Camera.Parameters params = mCamera.getParameters();
                     params.setPictureSize(resolution.width, resolution.height);
                     mCamera.setParameters(params);
@@ -131,54 +132,38 @@
         });
     }
 
-    /**
-     * Get the best supported focus mode.
-     *
-     * @param camera - Android camera object.
-     * @return the best supported focus mode.
-     */
-    protected String getFocusMode(Camera camera) {
-        List<String> modes = camera.getParameters().getSupportedFocusModes();
-        if (modes != null) {
-            if (modes.contains(Camera.Parameters.FOCUS_MODE_INFINITY)) {
-                Log.v(TAG, "Using Focus mode infinity");
-                return Camera.Parameters.FOCUS_MODE_INFINITY;
-            }
-            if (modes.contains(Camera.Parameters.FOCUS_MODE_FIXED)) {
-                Log.v(TAG, "Using Focus mode fixed");
-                return Camera.Parameters.FOCUS_MODE_FIXED;
-            }
-        }
-        Log.v(TAG, "Using Focus mode auto.");
-        return Camera.Parameters.FOCUS_MODE_AUTO;
-    }
-
     @Override
     protected void onResume() {
         super.onResume();
-        mCamera = Camera.open();
-
         // Keep the device from going to sleep.
         PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
         mWakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG);
         mWakeLock.acquire();
 
         if (mSupportedResolutions == null) {
-            // Get the supported picture sizes and fill the spinner.
-            List<Size> supportedSizes =
-                    mCamera.getParameters().getSupportedPictureSizes();
             mSupportedResolutions = new ArrayList<SelectableResolution>();
-            for (Size size : supportedSizes) {
-                mSupportedResolutions.add(
-                        new SelectableResolution(size.width, size.height));
+            int numCameras = Camera.getNumberOfCameras();
+            for (int cameraId = 0; cameraId < numCameras; ++cameraId) {
+                Camera camera = Camera.open(cameraId);
+
+                // Get the supported picture sizes and fill the spinner.
+                List<Camera.Size> supportedSizes =
+                        camera.getParameters().getSupportedPictureSizes();
+                for (Camera.Size size : supportedSizes) {
+                    mSupportedResolutions.add(
+                            new SelectableResolution(cameraId, size.width, size.height));
+                }
+                camera.release();
             }
         }
 
-        // find teh first untested one.
+        // Find the first untested entry.
         for (mResolutionSpinnerIndex = 0;
                 mResolutionSpinnerIndex < mSupportedResolutions.size();
                 mResolutionSpinnerIndex++) {
-            if (!mSupportedResolutions.get(mResolutionSpinnerIndex).tested) break;
+            if (!mSupportedResolutions.get(mResolutionSpinnerIndex).tested) {
+                break;
+            }
         }
 
         mAdapter = new ArrayAdapter<SelectableResolution>(
@@ -215,20 +200,23 @@
             fos.close();
             Log.d(TAG, "File saved to " + pictureFile.getAbsolutePath());
 
-            // Start activity which will use he taken picture to determine the FOV.
+            // Start activity which will use the taken picture to determine the
+            // FOV.
             startActivityForResult(new Intent(this, DetermineFovActivity.class),
                     FOV_REQUEST_CODE + mResolutionSpinnerIndex, null);
         } catch (IOException e) {
             Log.e(TAG, "Could not save picture file.", e);
             Toast.makeText(this, "Could not save picture file: " + e.getMessage(),
-                           Toast.LENGTH_LONG).show();
+                    Toast.LENGTH_LONG).show();
             return;
         }
     }
 
     @Override
     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
-        if (resultCode != RESULT_OK) return;
+        if (resultCode != RESULT_OK) {
+            return;
+        }
         int testIndex = requestCode - FOV_REQUEST_CODE;
         SelectableResolution res = mSupportedResolutions.get(testIndex);
         res.tested = true;
@@ -260,10 +248,10 @@
         }
         if (allPassed) {
             TestResult.setPassedResult(this, getClass().getName(),
-                                       CtsTestHelper.getTestDetails(mSupportedResolutions));
+                    CtsTestHelper.getTestDetails(mSupportedResolutions));
         } else {
             TestResult.setFailedResult(this, getClass().getName(),
-                                       CtsTestHelper.getTestDetails(mSupportedResolutions));
+                    CtsTestHelper.getTestDetails(mSupportedResolutions));
         }
         finish();
     }
@@ -271,32 +259,8 @@
     @Override
     public void surfaceChanged(
             SurfaceHolder holder, int format, int width, int height) {
-        if (mCamera == null || mSurfaceHolder.getSurface() == null) {
-            return;
-        }
-
-        try {
-            mCamera.setPreviewDisplay(mSurfaceHolder);
-        } catch (Throwable t) {
-            Log.e("TAG", "Could not set preview display", t);
-            Toast.makeText(this, t.getMessage(), Toast.LENGTH_LONG).show();
-            return;
-        }
-
-        // The picture size is taken and set from the spinner selection callback.
-        Camera.Parameters params = mCamera.getParameters();
-        params.setJpegThumbnailSize(0, 0);
-        params.setJpegQuality(100);
-        params.setFocusMode(getFocusMode(mCamera));
-        params.setZoom(0);
-
-        Camera.Size size = getBestPreviewSize(width, height, params);
-        if (size != null) {
-            params.setPreviewSize(size.width, size.height);
-            mCamera.setParameters(params);
-            mCameraInitialized = true;
-        }
-        startPreview();
+        mSurfaceSize = new Size(width, height);
+        initializeCamera();
     }
 
     @Override
@@ -309,6 +273,31 @@
         // Nothing to do.
     }
 
+    public void initializeCamera() {
+        if (mCamera == null || mSurfaceHolder.getSurface() == null) {
+            return;
+        }
+
+        try {
+            mCamera.setPreviewDisplay(mSurfaceHolder);
+        } catch (Throwable t) {
+            Log.e("TAG", "Could not set preview display", t);
+            Toast.makeText(this, t.getMessage(), Toast.LENGTH_LONG).show();
+            return;
+        }
+
+        Camera.Parameters params = setCameraParams(mCamera);
+
+        Camera.Size selectedPreviewSize =
+            getBestPreviewSize(mSurfaceSize.width, mSurfaceSize.height, params);
+        if (selectedPreviewSize != null) {
+            params.setPreviewSize(selectedPreviewSize.width, selectedPreviewSize.height);
+            mCamera.setParameters(params);
+            mCameraInitialized = true;
+        }
+        startPreview();
+    }
+
     private void startPreview() {
         if (mCameraInitialized && mCamera != null) {
             mCamera.startPreview();
@@ -316,6 +305,52 @@
         }
     }
 
+    private void switchToCamera(int cameraId) {
+        if (mCamera != null) {
+            mCamera.stopPreview();
+            mCamera.release();
+        }
+        mCamera = Camera.open(cameraId);
+        initializeCamera();
+    }
+
+    /**
+     * Get the best supported focus mode.
+     *
+     * @param camera - Android camera object.
+     * @return the best supported focus mode.
+     */
+    private static String getFocusMode(Camera camera) {
+        List<String> modes = camera.getParameters().getSupportedFocusModes();
+        if (modes != null) {
+            if (modes.contains(Camera.Parameters.FOCUS_MODE_INFINITY)) {
+                Log.v(TAG, "Using Focus mode infinity");
+                return Camera.Parameters.FOCUS_MODE_INFINITY;
+            }
+            if (modes.contains(Camera.Parameters.FOCUS_MODE_FIXED)) {
+                Log.v(TAG, "Using Focus mode fixed");
+                return Camera.Parameters.FOCUS_MODE_FIXED;
+            }
+        }
+        Log.v(TAG, "Using Focus mode auto.");
+        return Camera.Parameters.FOCUS_MODE_AUTO;
+    }
+
+    /**
+     * Set the common camera parameters on the given camera and returns the
+     * parameter object for further modification, if needed.
+     */
+    private static Camera.Parameters setCameraParams(Camera camera) {
+        // The picture size is taken and set from the spinner selection
+        // callback.
+        Camera.Parameters params = camera.getParameters();
+        params.setJpegThumbnailSize(0, 0);
+        params.setJpegQuality(100);
+        params.setFocusMode(getFocusMode(camera));
+        params.setZoom(0);
+        return params;
+    }
+
     private Camera.Size getBestPreviewSize(
             int width, int height, Camera.Parameters parameters) {
         Camera.Size result = null;
diff --git a/apps/CtsVerifier/src/com/android/cts/verifier/camera/fov/SelectableResolution.java b/apps/CtsVerifier/src/com/android/cts/verifier/camera/fov/SelectableResolution.java
index 5e87eee..9057dbf 100644
--- a/apps/CtsVerifier/src/com/android/cts/verifier/camera/fov/SelectableResolution.java
+++ b/apps/CtsVerifier/src/com/android/cts/verifier/camera/fov/SelectableResolution.java
@@ -20,21 +20,24 @@
  * A resolution to be used in the adapter that feeds the resolution spinner.
  */
 public class SelectableResolution {
+    public final int cameraId;
     public final int width;
     public final int height;
     public boolean passed;
     public boolean tested;
     public float measuredFOV;
 
-    public SelectableResolution(int width, int height) {
+    public SelectableResolution(int cameraId, int width, int height) {
+        this.cameraId = cameraId;
         this.width = width;
         this.height = height;
-        passed = false;
-        tested = false;
+        this.passed = false;
+        this.tested = false;
     }
 
     @Override
     public String toString() {
-        return width + " x " + height + " - " + (!tested ? "untested" : "done");
+        return "Cam " + cameraId + ": " + width + " x " + height + " - "
+                + (!tested ? "untested" : "done");
     }
 }
diff --git a/apps/CtsVerifier/src/com/android/cts/verifier/camera/fov/Size.java b/apps/CtsVerifier/src/com/android/cts/verifier/camera/fov/Size.java
new file mode 100644
index 0000000..709478b
--- /dev/null
+++ b/apps/CtsVerifier/src/com/android/cts/verifier/camera/fov/Size.java
@@ -0,0 +1,30 @@
+/*
+ * Copyright (C) 2013 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.android.cts.verifier.camera.fov;
+
+/**
+ * Simple immutable class for keeping a size.
+ */
+public class Size {
+    public final int width;
+    public final int height;
+
+    public Size(int width, int height) {
+        this.width = width;
+        this.height = height;
+    }
+}