aboutsummaryrefslogtreecommitdiff
path: root/shadows/framework/src/main/java/org/robolectric/shadows/ShadowCamera.java
blob: 0c2c3a8956681170d241e52462b37c6865344dfe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
package org.robolectric.shadows;

import static android.os.Build.VERSION_CODES.JELLY_BEAN_MR1;
import static org.robolectric.shadow.api.Shadow.newInstanceOf;

import android.hardware.Camera;
import android.os.Build;
import android.view.SurfaceHolder;
import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;
import org.robolectric.annotation.RealObject;
import org.robolectric.annotation.Resetter;
import org.robolectric.shadow.api.Shadow;
import org.robolectric.util.ReflectionHelpers;
import org.robolectric.util.ReflectionHelpers.ClassParameter;

@Implements(Camera.class)
public class ShadowCamera {
  // These are completely arbitrary and likely outdated default parameters that have been added long
  // ago.
  private static final ImmutableMap<String, String> DEFAULT_PARAMS =
      ImmutableMap.<String, String>builder()
          .put("picture-size", "1280x960")
          .put("preview-size", "640x480")
          .put("preview-fps-range", "10,30")
          .put("preview-frame-rate", "30")
          .put("preview-format", "yuv420sp")
          .put("picture-format-values", "yuv420sp,jpeg")
          .put("preview-format-values", "yuv420sp,jpeg")
          .put("picture-size-values", "320x240,640x480,800x600")
          .put("preview-size-values", "320x240,640x480")
          .put("preview-fps-range-values", "(15000,15000),(10000,30000)")
          .put("preview-frame-rate-values", "10,15,30")
          .put("exposure-compensation", "0")
          .put("exposure-compensation-step", "0.5")
          .put("min-exposure-compensation", "-6")
          .put("max-exposure-compensation", "6")
          .put("focus-mode-values", Camera.Parameters.FOCUS_MODE_AUTO)
          .put("focus-mode", Camera.Parameters.FOCUS_MODE_AUTO)
          .put(
              "flash-mode-values",
              Camera.Parameters.FLASH_MODE_AUTO
                  + ","
                  + Camera.Parameters.FLASH_MODE_ON
                  + ","
                  + Camera.Parameters.FLASH_MODE_OFF)
          .put("flash-mode", Camera.Parameters.FLASH_MODE_AUTO)
          .put("max-num-focus-areas", "1")
          .put("max-num-metering-areas", "1")
          .build();

  private static int lastOpenedCameraId;

  private int id;
  private boolean locked = true;
  private boolean previewing;
  private boolean released;
  private Camera.Parameters parameters;
  private Camera.PreviewCallback previewCallback;
  private List<byte[]> callbackBuffers = new ArrayList<>();
  private SurfaceHolder surfaceHolder;
  private int displayOrientation;
  private Camera.AutoFocusCallback autoFocusCallback;
  private boolean autoFocusing;
  private boolean shutterSoundEnabled = true;

  private static final Map<Integer, Camera.CameraInfo> cameras = new HashMap<>();
  private static final Map<Integer, Camera.Parameters> cameraParameters = new HashMap<>();

  @RealObject private Camera realCamera;

  @Implementation
  protected static Camera open() {
    return open(0);
  }

  @Implementation
  protected static Camera open(int cameraId) {
    lastOpenedCameraId = cameraId;
    Camera camera = newInstanceOf(Camera.class);
    ShadowCamera shadowCamera = Shadow.extract(camera);
    shadowCamera.id = cameraId;
    if (cameraParameters.containsKey(cameraId)) {
      shadowCamera.parameters = cameraParameters.get(cameraId);
    } else {
      cameraParameters.put(cameraId, camera.getParameters());
    }
    return camera;
  }

  public static int getLastOpenedCameraId() {
    return lastOpenedCameraId;
  }

  @Implementation
  protected void unlock() {
    locked = false;
  }

  @Implementation
  protected void reconnect() {
    locked = true;
  }

  @Implementation
  protected Camera.Parameters getParameters() {
    if (parameters == null) {
      parameters =
          ReflectionHelpers.callConstructor(
              Camera.Parameters.class, ClassParameter.from(Camera.class, realCamera));
      Joiner.MapJoiner mapJoiner = Joiner.on(";").withKeyValueSeparator("=");
      parameters.unflatten(mapJoiner.join(DEFAULT_PARAMS));
    }
    return parameters;
  }

  @Implementation
  protected void setParameters(Camera.Parameters params) {
    parameters = params;
  }

  @Implementation
  protected void setPreviewDisplay(SurfaceHolder holder) {
    surfaceHolder = holder;
  }

  @Implementation
  protected void startPreview() {
    previewing = true;
  }

  @Implementation
  protected void stopPreview() {
    previewing = false;
  }

  @Implementation
  protected void release() {
    released = true;
  }

  @Implementation
  protected void setPreviewCallback(Camera.PreviewCallback cb) {
    previewCallback = cb;
  }

  @Implementation
  protected void setOneShotPreviewCallback(Camera.PreviewCallback cb) {
    previewCallback = cb;
  }

  @Implementation
  protected void setPreviewCallbackWithBuffer(Camera.PreviewCallback cb) {
    previewCallback = cb;
  }

  /**
   * Allows test cases to invoke the preview callback, to simulate a frame of camera data.
   *
   * @param data byte buffer of simulated camera data
   */
  public void invokePreviewCallback(byte[] data) {
    if (previewCallback != null) {
      previewCallback.onPreviewFrame(data, realCamera);
    }
  }

  @Implementation
  protected void addCallbackBuffer(byte[] callbackBuffer) {
    callbackBuffers.add(callbackBuffer);
  }

  public List<byte[]> getAddedCallbackBuffers() {
    return Collections.unmodifiableList(callbackBuffers);
  }

  @Implementation
  protected void setDisplayOrientation(int degrees) {
    displayOrientation = degrees;
    if (cameras.containsKey(id)) {
      cameras.get(id).orientation = degrees;
    }
  }

  public int getDisplayOrientation() {
    return displayOrientation;
  }

  @Implementation
  protected void autoFocus(Camera.AutoFocusCallback callback) {
    autoFocusCallback = callback;
    autoFocusing = true;
  }

  @Implementation
  protected void cancelAutoFocus() {
    autoFocusCallback = null;
    autoFocusing = false;
  }

  public boolean hasRequestedAutoFocus() {
    return autoFocusing;
  }

  public void invokeAutoFocusCallback(boolean success, Camera camera) {
    if (autoFocusCallback == null) {
      throw new IllegalStateException(
          "cannot invoke AutoFocusCallback before autoFocus() has been called "
              + "or after cancelAutoFocus() has been called "
              + "or after the callback has been invoked.");
    }
    autoFocusCallback.onAutoFocus(success, camera);
    autoFocusCallback = null;
    autoFocusing = false;
  }

  @Implementation
  protected static void getCameraInfo(int cameraId, Camera.CameraInfo cameraInfo) {
    Camera.CameraInfo foundCam = cameras.get(cameraId);
    cameraInfo.facing = foundCam.facing;
    cameraInfo.orientation = foundCam.orientation;
    // canDisableShutterSound was added in API 17.
    if (Build.VERSION.SDK_INT >= JELLY_BEAN_MR1) {
      cameraInfo.canDisableShutterSound = foundCam.canDisableShutterSound;
    }
  }

  @Implementation
  protected static int getNumberOfCameras() {
    return cameras.size();
  }

  @Implementation
  protected void takePicture(
      Camera.ShutterCallback shutter, Camera.PictureCallback raw, Camera.PictureCallback jpeg) {
    if (shutter != null) {
      shutter.onShutter();
    }

    if (raw != null) {
      raw.onPictureTaken(new byte[0], realCamera);
    }

    if (jpeg != null) {
      jpeg.onPictureTaken(new byte[0], realCamera);
    }
  }

  @Implementation(minSdk = JELLY_BEAN_MR1)
  protected boolean enableShutterSound(boolean enabled) {
    if (!enabled && cameras.containsKey(id) && !cameras.get(id).canDisableShutterSound) {
      return false;
    }
    shutterSoundEnabled = enabled;
    return true;
  }

  /** Returns {@code true} if the default shutter sound is played when taking a picture. */
  public boolean isShutterSoundEnabled() {
    return shutterSoundEnabled;
  }

  public boolean isLocked() {
    return locked;
  }

  public boolean isPreviewing() {
    return previewing;
  }

  public boolean isReleased() {
    return released;
  }

  public SurfaceHolder getPreviewDisplay() {
    return surfaceHolder;
  }

  /**
   * Add a mock {@code Camera.CameraInfo} object to simulate the existence of one or more cameras.
   * By default, no cameras are defined.
   *
   * @param id The camera id
   * @param camInfo The CameraInfo
   */
  public static void addCameraInfo(int id, Camera.CameraInfo camInfo) {
    cameras.put(id, camInfo);
  }

  @Resetter
  public static void clearCameraInfo() {
    cameras.clear();
    cameraParameters.clear();
  }

  /** Shadows the Android {@code Camera.Parameters} class. */
  @Implements(Camera.Parameters.class)
  public static class ShadowParameters {

    @SuppressWarnings("nullness:initialization.field.uninitialized") // Managed by Robolectric
    @RealObject
    private Camera.Parameters realParameters;

    public void initSupportedPreviewSizes() {
      realParameters.remove("preview-size-values");
    }

    public void setSupportedFocusModes(String... focusModes) {
      realParameters.set("focus-mode-values", Joiner.on(",").join(focusModes));
      if (focusModes.length == 0) {
        realParameters.remove("focus-mode");
      }
    }

    public void setSupportedFlashModes(String... flashModes) {
      realParameters.set("flash-mode-values", Joiner.on(",").join(flashModes));
      if (flashModes.length == 0) {
        realParameters.remove("flash-mode");
      }
    }

    /**
     * Allows test cases to set the maximum number of focus areas. See {@link
     * Camera.Parameters#getMaxNumFocusAreas}.
     */
    public void setMaxNumFocusAreas(int maxNumFocusAreas) {
      realParameters.set("max-num-focus-areas", maxNumFocusAreas);
    }

    public void addSupportedPreviewSize(int width, int height) {
      List<String> sizesStrings = new ArrayList<>();
      List<Camera.Size> sizes = realParameters.getSupportedPreviewSizes();
      if (sizes == null) {
        sizes = ImmutableList.of();
      }
      for (Camera.Size size : sizes) {
        sizesStrings.add(size.width + "x" + size.height);
      }
      sizesStrings.add(width + "x" + height);
      realParameters.set("preview-size-values", Joiner.on(",").join(sizesStrings));
    }

    /**
     * Allows test cases to set the maximum number of metering areas. See {@link
     * Camera.Parameters#getMaxNumMeteringAreas}.
     */
    public void setMaxNumMeteringAreas(int maxNumMeteringAreas) {
      realParameters.set("max-num-metering-areas", maxNumMeteringAreas);
    }

    public int getPreviewWidth() {
      return realParameters.getPreviewSize().width;
    }

    public int getPreviewHeight() {
      return realParameters.getPreviewSize().height;
    }

    public int getPictureWidth() {
      return realParameters.getPictureSize().width;
    }

    public int getPictureHeight() {
      return realParameters.getPictureSize().height;
    }

    public int getRotation() {
      return realParameters.getInt("rotation");
    }
  }
}