1 /* 2 * Copyright (C) 2015 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.camera.device; 18 19 import com.android.camera.debug.Log; 20 import com.android.camera.debug.Log.Tag; 21 import com.google.common.annotations.VisibleForTesting; 22 23 import javax.annotation.ParametersAreNonnullByDefault; 24 import javax.annotation.concurrent.GuardedBy; 25 26 /** 27 * Shared object for tracking the active camera device across multiple 28 * implementations. 29 */ 30 @ParametersAreNonnullByDefault 31 public class ActiveCameraDeviceTracker { 32 private static final Tag TAG = new Tag("ActvCamDevTrckr"); 33 34 /** 35 * Singleton instance. 36 */ 37 private static class Singleton { 38 public static final ActiveCameraDeviceTracker INSTANCE = new ActiveCameraDeviceTracker(); 39 } 40 instance()41 public static ActiveCameraDeviceTracker instance() { 42 return Singleton.INSTANCE; 43 } 44 45 private final Object mLock; 46 47 @GuardedBy("mLock") 48 private CameraId mActiveCamera; 49 50 @GuardedBy("mLock") 51 private CameraId mPreviousCamera; 52 53 @VisibleForTesting ActiveCameraDeviceTracker()54 ActiveCameraDeviceTracker() { 55 mLock = new Object(); 56 } 57 getActiveCamera()58 public CameraId getActiveCamera() { 59 synchronized (mLock) { 60 return mActiveCamera; 61 } 62 } 63 getActiveOrPreviousCamera()64 public CameraId getActiveOrPreviousCamera() { 65 synchronized (mLock) { 66 if (mActiveCamera != null) { 67 68 return mActiveCamera; 69 } 70 Log.v(TAG, "Returning previously active camera: " + mPreviousCamera); 71 return mPreviousCamera; 72 } 73 } 74 onCameraOpening(CameraId key)75 public void onCameraOpening(CameraId key) { 76 synchronized (mLock) { 77 if (mActiveCamera != null && !mActiveCamera.equals(key)) { 78 mPreviousCamera = mActiveCamera; 79 } 80 81 Log.v(TAG, "Tracking active camera: " + mActiveCamera); 82 mActiveCamera = key; 83 } 84 } 85 onCameraClosed(CameraId key)86 public void onCameraClosed(CameraId key) { 87 synchronized (mLock) { 88 if (mActiveCamera != null && mActiveCamera.equals(key)) { 89 mPreviousCamera = mActiveCamera; 90 mActiveCamera = null; 91 } 92 } 93 } 94 } 95