From 5b1e9bb48add0c31a740da01c3ac2e61c4cb6947 Mon Sep 17 00:00:00 2001
From: idzkowski-google <57770516+idzkowski-google@users.noreply.github.com>
Date: Mon, 24 Feb 2020 13:01:11 -0800
Subject: [PATCH] Change Camera2Sample behavior to react on MediaStore intents
for capturing images and recording videos (#5)
---
.../app/src/main/AndroidManifest.xml | 16 ++++++++++++++
.../camera2sample/CameraActionHandler.java | 22 +++++++++++++++++++
.../glass/camera2sample/CameraFragment.java | 1 +
3 files changed, 39 insertions(+)
diff --git a/Camera2Sample/app/src/main/AndroidManifest.xml b/Camera2Sample/app/src/main/AndroidManifest.xml
index 9521a30..b8bf692 100644
--- a/Camera2Sample/app/src/main/AndroidManifest.xml
+++ b/Camera2Sample/app/src/main/AndroidManifest.xml
@@ -55,6 +55,22 @@
-->
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/Camera2Sample/app/src/main/java/com/example/glass/camera2sample/CameraActionHandler.java b/Camera2Sample/app/src/main/java/com/example/glass/camera2sample/CameraActionHandler.java
index ddb237c..35dc084 100644
--- a/Camera2Sample/app/src/main/java/com/example/glass/camera2sample/CameraActionHandler.java
+++ b/Camera2Sample/app/src/main/java/com/example/glass/camera2sample/CameraActionHandler.java
@@ -18,6 +18,7 @@
import android.annotation.SuppressLint;
import android.content.Context;
+import android.content.Intent;
import android.hardware.camera2.CameraAccessException;
import android.hardware.camera2.CameraCaptureSession;
import android.hardware.camera2.CameraCaptureSession.CaptureCallback;
@@ -30,6 +31,7 @@
import android.hardware.camera2.params.StreamConfigurationMap;
import android.media.ImageReader;
import android.media.ImageReader.OnImageAvailableListener;
+import android.provider.MediaStore;
import android.support.annotation.NonNull;
import android.util.Log;
import android.view.Surface;
@@ -355,6 +357,26 @@ public void setPreviewSurface(Surface previewSurface) {
this.previewSurface = previewSurface;
}
+ /**
+ * Handles given {@link Intent} and sets appropriate {@link CameraMode} depends on the intent
+ * action.
+ */
+ public void handleIntent(Intent intent) {
+ final String intentAction = intent.getAction();
+ if (intentAction != null) {
+ switch (intentAction) {
+ case MediaStore.ACTION_IMAGE_CAPTURE:
+ case MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA:
+ switchCameraMode(CameraMode.PICTURE);
+ break;
+ case MediaStore.INTENT_ACTION_VIDEO_CAMERA:
+ case MediaStore.ACTION_VIDEO_CAPTURE:
+ switchCameraMode(CameraMode.VIDEO);
+ break;
+ }
+ }
+ }
+
/**
* Switches {@link CameraMode} states. Possible scenarios:
*
diff --git a/Camera2Sample/app/src/main/java/com/example/glass/camera2sample/CameraFragment.java b/Camera2Sample/app/src/main/java/com/example/glass/camera2sample/CameraFragment.java
index 349dd0b..1c58f0d 100644
--- a/Camera2Sample/app/src/main/java/com/example/glass/camera2sample/CameraFragment.java
+++ b/Camera2Sample/app/src/main/java/com/example/glass/camera2sample/CameraFragment.java
@@ -137,6 +137,7 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
cameraActionHandler = new CameraActionHandler(getContext(), this);
+ cameraActionHandler.handleIntent(Objects.requireNonNull(getActivity()).getIntent());
}
@Override