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 javax.annotation.Nonnull;
20 import javax.annotation.Nullable;
21 
22 /**
23  * Identifier for Camera1 and Camera2 camera devices.
24  */
25 public final class CameraId {
26     private final Integer mLegacyCameraId;
27     private final String mCameraId;
28 
fromLegacyId(int camera1Id)29     public static CameraId fromLegacyId(int camera1Id) {
30         return new CameraId(computeCameraIdFromLegacyId(camera1Id), camera1Id);
31     }
32 
from(@onnull String camera2Id)33     public static CameraId from(@Nonnull String camera2Id) {
34         return new CameraId(camera2Id, computeLegacyIdFromCamera2Id(camera2Id));
35     }
36 
37     /**
38      * This should compute a Legacy Api1 camera Id for the given camera2 device.
39      * This class will return null if the camera2 identifier cannot be transformed
40      * into an api1 id.
41      */
computeLegacyIdFromCamera2Id(@onnull String camera2Id)42     private static Integer computeLegacyIdFromCamera2Id(@Nonnull String camera2Id) {
43         try {
44             return Integer.parseInt(camera2Id);
45         } catch (NumberFormatException ignored) {
46 
47         }
48 
49         return null;
50     }
51 
52     /**
53      * This should compute a Camera2 Id for the given legacy camera device.
54      * This should never return a null value.
55      */
computeCameraIdFromLegacyId(int camera1Id)56     private static String computeCameraIdFromLegacyId(int camera1Id) {
57         return String.valueOf(camera1Id);
58     }
59 
CameraId(@onnull String cameraId, @Nullable Integer legacyCameraId)60     private CameraId(@Nonnull String cameraId, @Nullable Integer legacyCameraId) {
61         mCameraId = cameraId;
62         mLegacyCameraId = legacyCameraId;
63     }
64 
65     /**
66      * Return the Camera Api2 String representation that this instance represents.
67      */
getValue()68     public String getValue() {
69         return mCameraId;
70     }
71 
72     /**
73      * Return the Legacy Api1 Camera index. It will throw an exception if the value
74      * does not exist, which should only happen if the device that is being opened
75      * is not supported on Api1.
76      */
getLegacyValue()77     public int getLegacyValue() throws UnsupportedOperationException {
78         if (mLegacyCameraId == null) {
79             throw new UnsupportedOperationException("Attempted to access a camera id that is not"
80                   + " supported on legacy camera API's: " + mCameraId);
81         }
82 
83         return mLegacyCameraId;
84     }
85 
86     /**
87      * Return true if this instance has a valid Legacy Api camera index.
88      */
hasLeagcyValue()89     public boolean hasLeagcyValue() {
90         return mLegacyCameraId != null;
91     }
92 
93     @Override
equals(Object other)94     public boolean equals(Object other) {
95         if (this == other) {
96             return true;
97         }
98         if (! (other instanceof CameraId)) {
99             return false;
100         }
101 
102         CameraId otherCameraId = (CameraId) other;
103 
104         // Note: mLegacyCameraId is omitted and only mCameraId is used as the
105         // canonical "equals" for these instances since a Camera2 id can be
106         // created from any Camera1 id.
107         return mCameraId.equals(otherCameraId.mCameraId);
108     }
109 
110     @Override
hashCode()111     public int hashCode() {
112         return mCameraId.hashCode();
113     }
114 
115     @Override
toString()116     public String toString() {
117         return "CameraId{" +
118               "Api2='" + mCameraId + "\',Api1:"+mLegacyCameraId+"}";
119     }
120 }
121