1 /* 2 * Copyright (C) 2023 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.DeviceAsWebcam; 18 19 import android.hardware.camera2.CameraManager; 20 21 import androidx.annotation.Nullable; 22 import androidx.core.util.Preconditions; 23 24 import java.util.regex.Pattern; 25 26 /** 27 * An identifier composed by a pair of camera device id and its underlying physical camera id. 28 */ 29 public class CameraId { 30 private static final String CAMERA_ID_SPLITTER = "-"; 31 public final String mainCameraId; 32 @Nullable 33 public final String physicalCameraId; 34 private final String identifier; 35 36 /** 37 * Constructor 38 * 39 * @param mainCameraId the main camera id retrieved via 40 * {@link CameraManager#getCameraIdList()}. 41 * @param physicalCameraId the physical camera id if the main camera is a logical camera. This 42 * can be {@code null}. 43 */ CameraId(String mainCameraId, @Nullable String physicalCameraId)44 public CameraId(String mainCameraId, @Nullable String physicalCameraId) { 45 Preconditions.checkNotNull(mainCameraId, "The specified id can't be null!"); 46 this.mainCameraId = mainCameraId; 47 this.physicalCameraId = physicalCameraId; 48 identifier = createIdentifier(mainCameraId, physicalCameraId); 49 } 50 51 @Override hashCode()52 public int hashCode() { 53 return identifier.hashCode(); 54 } 55 56 @Override equals(Object obj)57 public boolean equals(Object obj) { 58 if (this == obj) { 59 return true; 60 } 61 62 if (!(obj instanceof CameraId dest)) { 63 return false; 64 } 65 66 return identifier.equals(dest.identifier); 67 } 68 69 @Override toString()70 public String toString() { 71 return identifier; 72 } 73 74 /** 75 * Creates an identifier string to represent the camera. 76 */ createIdentifier(String cameraId, @Nullable String physicalCameraId)77 public static String createIdentifier(String cameraId, @Nullable String physicalCameraId) { 78 return cameraId + CAMERA_ID_SPLITTER + physicalCameraId; 79 } 80 81 /** 82 * Returns the CameraId restored from the specified identifier. 83 * 84 * <p>The identifier should be created by the {@link #createIdentifier(String, String)} 85 * function. If the identifier format is not matched, {@code null} will be returned by this 86 * function. 87 */ 88 @Nullable fromCameraIdString(@ullable String identifier)89 public static CameraId fromCameraIdString(@Nullable String identifier) { 90 if (identifier == null) { 91 return null; 92 } 93 94 String idPattern = "\\d+" + CAMERA_ID_SPLITTER + "(?:\\d+|null)"; 95 if (!Pattern.matches(idPattern, identifier)) { 96 return null; 97 } 98 99 int index = identifier.indexOf(CAMERA_ID_SPLITTER); 100 String mainCameraId = identifier.substring(0, index); 101 String physicalCameraId = identifier.substring(index + CAMERA_ID_SPLITTER.length()); 102 103 return new CameraId(mainCameraId, 104 physicalCameraId.equals("null") ? null : physicalCameraId); 105 } 106 } 107