1b6a4d96a310a1dee22ddea89b09130bcda206bb3Paul Rohde/*
2b6a4d96a310a1dee22ddea89b09130bcda206bb3Paul Rohde * Copyright (C) 2015 The Android Open Source Project
3b6a4d96a310a1dee22ddea89b09130bcda206bb3Paul Rohde *
4b6a4d96a310a1dee22ddea89b09130bcda206bb3Paul Rohde * Licensed under the Apache License, Version 2.0 (the "License");
5b6a4d96a310a1dee22ddea89b09130bcda206bb3Paul Rohde * you may not use this file except in compliance with the License.
6b6a4d96a310a1dee22ddea89b09130bcda206bb3Paul Rohde * You may obtain a copy of the License at
7b6a4d96a310a1dee22ddea89b09130bcda206bb3Paul Rohde *
8b6a4d96a310a1dee22ddea89b09130bcda206bb3Paul Rohde *      http://www.apache.org/licenses/LICENSE-2.0
9b6a4d96a310a1dee22ddea89b09130bcda206bb3Paul Rohde *
10b6a4d96a310a1dee22ddea89b09130bcda206bb3Paul Rohde * Unless required by applicable law or agreed to in writing, software
11b6a4d96a310a1dee22ddea89b09130bcda206bb3Paul Rohde * distributed under the License is distributed on an "AS IS" BASIS,
12b6a4d96a310a1dee22ddea89b09130bcda206bb3Paul Rohde * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13b6a4d96a310a1dee22ddea89b09130bcda206bb3Paul Rohde * See the License for the specific language governing permissions and
14b6a4d96a310a1dee22ddea89b09130bcda206bb3Paul Rohde * limitations under the License.
15b6a4d96a310a1dee22ddea89b09130bcda206bb3Paul Rohde */
16b6a4d96a310a1dee22ddea89b09130bcda206bb3Paul Rohde
17b6a4d96a310a1dee22ddea89b09130bcda206bb3Paul Rohdepackage com.android.camera.device;
18b6a4d96a310a1dee22ddea89b09130bcda206bb3Paul Rohde
19b6a4d96a310a1dee22ddea89b09130bcda206bb3Paul Rohdeimport java.util.Objects;
20b6a4d96a310a1dee22ddea89b09130bcda206bb3Paul Rohde
21b6a4d96a310a1dee22ddea89b09130bcda206bb3Paul Rohdeimport javax.annotation.concurrent.ThreadSafe;
22b6a4d96a310a1dee22ddea89b09130bcda206bb3Paul Rohde
23b6a4d96a310a1dee22ddea89b09130bcda206bb3Paul Rohde/**
24b6a4d96a310a1dee22ddea89b09130bcda206bb3Paul Rohde * Internal representation of a single camera device for a given API. Only one
25b6a4d96a310a1dee22ddea89b09130bcda206bb3Paul Rohde * device key may be active at any given time and other devices must be closed
26b6a4d96a310a1dee22ddea89b09130bcda206bb3Paul Rohde * before opening a new one.
27b6a4d96a310a1dee22ddea89b09130bcda206bb3Paul Rohde *
28b6a4d96a310a1dee22ddea89b09130bcda206bb3Paul Rohde * A single instance is considered equal if both API and provided cameraIds
29b6a4d96a310a1dee22ddea89b09130bcda206bb3Paul Rohde * match and the device key is suitable for use in hash maps. All values are
30b6a4d96a310a1dee22ddea89b09130bcda206bb3Paul Rohde * immutable.
31b6a4d96a310a1dee22ddea89b09130bcda206bb3Paul Rohde */
32b6a4d96a310a1dee22ddea89b09130bcda206bb3Paul Rohde@ThreadSafe
331d84d7107686aa428ee2eeb1a8caf0ea3e43b1dfPaul Rohdefinal class CameraDeviceKey {
34b6a4d96a310a1dee22ddea89b09130bcda206bb3Paul Rohde    /**
35b6a4d96a310a1dee22ddea89b09130bcda206bb3Paul Rohde     * Unified set of supported types.
36b6a4d96a310a1dee22ddea89b09130bcda206bb3Paul Rohde     */
37b6a4d96a310a1dee22ddea89b09130bcda206bb3Paul Rohde    public enum ApiType {
38b6a4d96a310a1dee22ddea89b09130bcda206bb3Paul Rohde        CAMERA_API1,
39b6a4d96a310a1dee22ddea89b09130bcda206bb3Paul Rohde        CAMERA_API2,
40b6a4d96a310a1dee22ddea89b09130bcda206bb3Paul Rohde        CAMERA_API_PORTABILITY_AUTO,
41b6a4d96a310a1dee22ddea89b09130bcda206bb3Paul Rohde        CAMERA_API_PORTABILITY_API1,
42b6a4d96a310a1dee22ddea89b09130bcda206bb3Paul Rohde        CAMERA_API_PORTABILITY_API2
43b6a4d96a310a1dee22ddea89b09130bcda206bb3Paul Rohde    }
44b6a4d96a310a1dee22ddea89b09130bcda206bb3Paul Rohde
45b6a4d96a310a1dee22ddea89b09130bcda206bb3Paul Rohde    private final ApiType mApiType;
461d84d7107686aa428ee2eeb1a8caf0ea3e43b1dfPaul Rohde    private final CameraId mCameraId;
47b6a4d96a310a1dee22ddea89b09130bcda206bb3Paul Rohde
48b6a4d96a310a1dee22ddea89b09130bcda206bb3Paul Rohde    /**
49b6a4d96a310a1dee22ddea89b09130bcda206bb3Paul Rohde     * @return the api type for this instances.
50b6a4d96a310a1dee22ddea89b09130bcda206bb3Paul Rohde     */
51b6a4d96a310a1dee22ddea89b09130bcda206bb3Paul Rohde    public ApiType getApiType() {
52b6a4d96a310a1dee22ddea89b09130bcda206bb3Paul Rohde        return mApiType;
53b6a4d96a310a1dee22ddea89b09130bcda206bb3Paul Rohde    }
54b6a4d96a310a1dee22ddea89b09130bcda206bb3Paul Rohde
55b6a4d96a310a1dee22ddea89b09130bcda206bb3Paul Rohde    /**
56b6a4d96a310a1dee22ddea89b09130bcda206bb3Paul Rohde     * @return the typed cameraId for this instances.
57b6a4d96a310a1dee22ddea89b09130bcda206bb3Paul Rohde     */
581d84d7107686aa428ee2eeb1a8caf0ea3e43b1dfPaul Rohde    public CameraId getCameraId() {
59b6a4d96a310a1dee22ddea89b09130bcda206bb3Paul Rohde        return mCameraId;
60b6a4d96a310a1dee22ddea89b09130bcda206bb3Paul Rohde    }
61b6a4d96a310a1dee22ddea89b09130bcda206bb3Paul Rohde
62d552f16540c59b28073e526e99fbcfe7dfa5ef09Paul Rohde    /**
631d84d7107686aa428ee2eeb1a8caf0ea3e43b1dfPaul Rohde     * Create a camera device key with an explicit API version.
64d552f16540c59b28073e526e99fbcfe7dfa5ef09Paul Rohde     */
651d84d7107686aa428ee2eeb1a8caf0ea3e43b1dfPaul Rohde    public CameraDeviceKey(ApiType apiType, CameraId cameraId) {
66b6a4d96a310a1dee22ddea89b09130bcda206bb3Paul Rohde        mApiType = apiType;
67b6a4d96a310a1dee22ddea89b09130bcda206bb3Paul Rohde        mCameraId = cameraId;
68b6a4d96a310a1dee22ddea89b09130bcda206bb3Paul Rohde    }
69b6a4d96a310a1dee22ddea89b09130bcda206bb3Paul Rohde
70b6a4d96a310a1dee22ddea89b09130bcda206bb3Paul Rohde    @Override
71b6a4d96a310a1dee22ddea89b09130bcda206bb3Paul Rohde    public String toString() {
72b6a4d96a310a1dee22ddea89b09130bcda206bb3Paul Rohde        return "CameraDeviceKey{" +
73b6a4d96a310a1dee22ddea89b09130bcda206bb3Paul Rohde              "mApiType: " + mApiType +
741d84d7107686aa428ee2eeb1a8caf0ea3e43b1dfPaul Rohde              ", mCameraId: " + mCameraId + "}";
75b6a4d96a310a1dee22ddea89b09130bcda206bb3Paul Rohde    }
76b6a4d96a310a1dee22ddea89b09130bcda206bb3Paul Rohde
77b6a4d96a310a1dee22ddea89b09130bcda206bb3Paul Rohde    @Override
78b6a4d96a310a1dee22ddea89b09130bcda206bb3Paul Rohde    public boolean equals(Object o) {
79b6a4d96a310a1dee22ddea89b09130bcda206bb3Paul Rohde        if (this == o) {
80b6a4d96a310a1dee22ddea89b09130bcda206bb3Paul Rohde            return true;
81b6a4d96a310a1dee22ddea89b09130bcda206bb3Paul Rohde        }
82b6a4d96a310a1dee22ddea89b09130bcda206bb3Paul Rohde        if (o == null || getClass() != o.getClass()) {
83b6a4d96a310a1dee22ddea89b09130bcda206bb3Paul Rohde            return false;
84b6a4d96a310a1dee22ddea89b09130bcda206bb3Paul Rohde        }
85b6a4d96a310a1dee22ddea89b09130bcda206bb3Paul Rohde
86b6a4d96a310a1dee22ddea89b09130bcda206bb3Paul Rohde        CameraDeviceKey other = (CameraDeviceKey) o;
87b6a4d96a310a1dee22ddea89b09130bcda206bb3Paul Rohde
88b6a4d96a310a1dee22ddea89b09130bcda206bb3Paul Rohde        if (mApiType != other.mApiType) {
89b6a4d96a310a1dee22ddea89b09130bcda206bb3Paul Rohde            return false;
90b6a4d96a310a1dee22ddea89b09130bcda206bb3Paul Rohde        }
91b6a4d96a310a1dee22ddea89b09130bcda206bb3Paul Rohde        if (!mCameraId.equals(other.mCameraId)) {
92b6a4d96a310a1dee22ddea89b09130bcda206bb3Paul Rohde            return false;
93b6a4d96a310a1dee22ddea89b09130bcda206bb3Paul Rohde        }
94b6a4d96a310a1dee22ddea89b09130bcda206bb3Paul Rohde
95b6a4d96a310a1dee22ddea89b09130bcda206bb3Paul Rohde        return true;
96b6a4d96a310a1dee22ddea89b09130bcda206bb3Paul Rohde    }
97b6a4d96a310a1dee22ddea89b09130bcda206bb3Paul Rohde
98b6a4d96a310a1dee22ddea89b09130bcda206bb3Paul Rohde    @Override
99b6a4d96a310a1dee22ddea89b09130bcda206bb3Paul Rohde    public int hashCode() {
100b6a4d96a310a1dee22ddea89b09130bcda206bb3Paul Rohde        return Objects.hash(mApiType, mCameraId);
101b6a4d96a310a1dee22ddea89b09130bcda206bb3Paul Rohde    }
102b6a4d96a310a1dee22ddea89b09130bcda206bb3Paul Rohde}
103