• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package org.robolectric.shadows;
2 
3 import android.annotation.NonNull;
4 import android.hardware.camera2.CameraAccessException;
5 import android.hardware.camera2.CameraCharacteristics;
6 import android.hardware.camera2.CameraManager;
7 import android.os.Build.VERSION_CODES;
8 import com.google.common.base.Preconditions;
9 import java.util.LinkedHashMap;
10 import java.util.Map;
11 import java.util.Set;
12 import org.robolectric.annotation.Implementation;
13 import org.robolectric.annotation.Implements;
14 
15 @Implements(value = CameraManager.class, minSdk = VERSION_CODES.LOLLIPOP)
16 public class ShadowCameraManager {
17 
18   // LinkedHashMap used to ensure getCameraIdList returns ids in the order in which they were added
19   private final Map<String, CameraCharacteristics> cameraIdToCharacteristics =
20       new LinkedHashMap<>();
21 
22   @Implementation
23   @NonNull
getCameraIdList()24   protected String[] getCameraIdList() throws CameraAccessException {
25     Set<String> cameraIds = cameraIdToCharacteristics.keySet();
26     return cameraIds.toArray(new String[0]);
27   }
28 
29   @Implementation
30   @NonNull
getCameraCharacteristics(@onNull String cameraId)31   protected CameraCharacteristics getCameraCharacteristics(@NonNull String cameraId) {
32     Preconditions.checkNotNull(cameraId);
33     CameraCharacteristics characteristics = cameraIdToCharacteristics.get(cameraId);
34     Preconditions.checkArgument(characteristics != null);
35     return characteristics;
36   }
37 
38   /**
39    * Adds the given cameraId and characteristics to this shadow.
40    *
41    * <p>The result from {@link #getCameraIdList()} will be in the order in which cameras were added.
42    *
43    * @throws IllegalArgumentException if there's already an existing camera with the given id.
44    */
addCamera(@onNull String cameraId, @NonNull CameraCharacteristics characteristics)45   public void addCamera(@NonNull String cameraId, @NonNull CameraCharacteristics characteristics) {
46     Preconditions.checkNotNull(cameraId);
47     Preconditions.checkNotNull(characteristics);
48     Preconditions.checkArgument(!cameraIdToCharacteristics.containsKey(cameraId));
49 
50     cameraIdToCharacteristics.put(cameraId, characteristics);
51   }
52 }
53