1 /* 2 * Copyright (C) 2014 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License 15 */ 16 17 package com.android.incallui; 18 19 import android.content.Context; 20 import android.graphics.SurfaceTexture; 21 import android.hardware.camera2.CameraAccessException; 22 import android.hardware.camera2.CameraCharacteristics; 23 import android.hardware.camera2.CameraManager; 24 import android.hardware.camera2.params.StreamConfigurationMap; 25 import android.util.Size; 26 27 import java.lang.String; 28 29 /** 30 * Used to track which camera is used for outgoing video. 31 */ 32 public class InCallCameraManager { 33 34 /** 35 * The camera ID for the front facing camera. 36 */ 37 private String mFrontFacingCameraId; 38 39 /** 40 * The camera ID for the rear facing camera. 41 */ 42 private String mRearFacingCameraId; 43 44 /** 45 * The currently active camera. 46 */ 47 private boolean mUseFrontFacingCamera; 48 49 /** 50 * Aspect ratio of the front facing camera. 51 */ 52 private float mFrontFacingCameraAspectRatio; 53 54 /** 55 * Aspect ratio of the rear facing camera. 56 */ 57 private float mRearFacingCameraAspectRatio; 58 59 /** 60 * Initializes the InCall CameraManager. 61 * 62 * @param context The current context. 63 */ 64 public InCallCameraManager(Context context) { 65 mUseFrontFacingCamera = true; 66 initializeCameraList(context); 67 } 68 69 /** 70 * Sets whether the front facing camera should be used or not. 71 * 72 * @param useFrontFacingCamera {@code True} if the front facing camera is to be used. 73 */ 74 public void setUseFrontFacingCamera(boolean useFrontFacingCamera) { 75 mUseFrontFacingCamera = useFrontFacingCamera; 76 } 77 78 /** 79 * Determines whether the front facing camera is currently in use. 80 * 81 * @return {@code True} if the front facing camera is in use. 82 */ 83 public boolean isUsingFrontFacingCamera() { 84 return mUseFrontFacingCamera; 85 } 86 87 /** 88 * Determines the active camera ID. 89 * 90 * @return The active camera ID. 91 */ 92 public String getActiveCameraId() { 93 if (mUseFrontFacingCamera) { 94 return mFrontFacingCameraId; 95 } else { 96 return mRearFacingCameraId; 97 } 98 } 99 100 /** 101 * Get the camera ID and aspect ratio for the front and rear cameras. 102 * 103 * @param context The context. 104 */ 105 private void initializeCameraList(Context context) { 106 if (context == null) { 107 return; 108 } 109 110 CameraManager cameraManager = null; 111 try { 112 cameraManager = (CameraManager) context.getSystemService( 113 Context.CAMERA_SERVICE); 114 } catch (Exception e) { 115 Log.e(this, "Could not get camera service."); 116 return; 117 } 118 119 if (cameraManager == null) { 120 return; 121 } 122 123 String[] cameraIds = {}; 124 try { 125 cameraIds = cameraManager.getCameraIdList(); 126 } catch (CameraAccessException e) { 127 Log.d(this, "Could not access camera: "+e); 128 // Camera disabled by device policy. 129 return; 130 } 131 132 for (int i = 0; i < cameraIds.length; i++) { 133 CameraCharacteristics c = null; 134 try { 135 c = cameraManager.getCameraCharacteristics(cameraIds[i]); 136 } catch (IllegalArgumentException e) { 137 // Device Id is unknown. 138 } catch (CameraAccessException e) { 139 // Camera disabled by device policy. 140 } 141 if (c != null) { 142 int facingCharacteristic = c.get(CameraCharacteristics.LENS_FACING); 143 if (facingCharacteristic == CameraCharacteristics.LENS_FACING_FRONT) { 144 mFrontFacingCameraId = cameraIds[i]; 145 } else if (facingCharacteristic == CameraCharacteristics.LENS_FACING_BACK) { 146 mRearFacingCameraId = cameraIds[i]; 147 } 148 } 149 } 150 } 151 } 152