AI2 Component  (Version nb184)
Camcorder.java
Go to the documentation of this file.
1 // -*- mode: java; c-basic-offset: 2; -*-
2 // Copyright 2009-2011 Google, All Rights reserved
3 // Copyright 2011-2018 MIT, All rights reserved
4 // Released under the Apache License, Version 2.0
5 // http://www.apache.org/licenses/LICENSE-2.0
6 
7 package com.google.appinventor.components.runtime;
8 
17 
18 import android.app.Activity;
19 import android.content.ContentValues;
20 import android.content.Intent;
21 import android.net.Uri;
22 import android.os.Environment;
23 import android.provider.MediaStore;
24 import android.util.Log;
25 import android.Manifest;
26 
27 import java.io.File;
28 import java.util.Date;
29 
39 @DesignerComponent(version = YaVersion.CAMCORDER_COMPONENT_VERSION,
40  description = "A component to record a video using the device's camcorder." +
41  "After the video is recorded, the name of the file on the phone " +
42  "containing the clip is available as an argument to the " +
43  "AfterRecording event. The file name can be used, for example, to set " +
44  "the source property of a VideoPlayer component.",
45  category = ComponentCategory.MEDIA,
46  nonVisible = true,
47  iconName = "images/camcorder.png")
48 @SimpleObject
49 @UsesPermissions(permissionNames = "android.permission.WRITE_EXTERNAL_STORAGE, android.permission.READ_EXTERNAL_STORAGE," +
50  "android.permission.CAMERA")
52  implements ActivityResultListener, Component {
53 
54  private static final String CAMCORDER_INTENT = "android.media.action.VIDEO_CAPTURE";
55  private final ComponentContainer container;
56 
57  /* Used to identify the call to startActivityForResult. Will be passed back
58  into the resultReturned() callback method. */
59  private int requestCode;
60 
61  // Have camera permission
62  private boolean havePermission = false;
63 
69  public Camcorder(ComponentContainer container) {
70  super(container.$form());
71  this.container = container;
72  }
73 
78  public void RecordVideo() {
79  String state = Environment.getExternalStorageState();
80  if (!havePermission) {
81  final Camcorder me = this;
82  form.runOnUiThread(new Runnable() {
83  @Override
84  public void run() {
85  form.askPermission(Manifest.permission.CAMERA,
87  @Override
88  public void HandlePermissionResponse(String permission, boolean granted) {
89  if (granted) {
90  me.havePermission = true;
91  me.RecordVideo();
92  } else {
93  form.dispatchPermissionDeniedEvent(me, "RecordVideo",
94  Manifest.permission.CAMERA);
95  }
96  }
97  });
98  }
99  });
100  return;
101  }
102 
103  if (Environment.MEDIA_MOUNTED.equals(state)) {
104  Log.i("CamcorderComponent", "External storage is available and writable");
105 
106  if (requestCode == 0) {
107  requestCode = form.registerForActivityResult(this);
108  }
109 
110  Intent intent = new Intent(CAMCORDER_INTENT);
111  container.$context().startActivityForResult(intent, requestCode);
112  } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
113  form.dispatchErrorOccurredEvent(this, "RecordVideo",
115  } else {
116  form.dispatchErrorOccurredEvent(this, "RecordVideo",
118  }
119  }
120 
121  @Override
122  public void resultReturned(int requestCode, int resultCode, Intent data) {
123  Log.i("CamcorderComponent",
124  "Returning result. Request code = " + requestCode + ", result code = " + resultCode);
125  if (requestCode == this.requestCode && resultCode == Activity.RESULT_OK) {
126  if (data != null && data.getData() != null) {
127  Uri tryClipUri = data.getData();
128  Log.i("CamcorderComponent", "Calling Camcorder.AfterPicture with clip path "
129  + tryClipUri.toString());
130  AfterRecording(tryClipUri.toString());
131  } else {
132  Log.i("CamcorderComponent", "Couldn't find a clip file from the Camcorder result");
133  form.dispatchErrorOccurredEvent(this, "TakeVideo",
135  }
136  } else {
137  Log.i("CamcorderComponent", "No clip filed rerturn; request failed");
138  form.dispatchErrorOccurredEvent(this, "TakeVideo",
140  }
141  }
142 
143  private void deleteFile(Uri fileUri) {
144  File fileToDelete = new File(fileUri.getPath());
145  try {
146  if (fileToDelete.delete()) {
147  Log.i("CamcorderComponent", "Deleted file " + fileUri.toString());
148  } else {
149  Log.i("CamcorderComponent", "Could not delete file " + fileUri.toString());
150  }
151  } catch (SecurityException e) {
152  Log.i("CamcorderComponent", "Got security exception trying to delete file "
153  + fileUri.toString());
154  }
155  }
156 
161  @SimpleEvent
162  public void AfterRecording(String clip) {
163  EventDispatcher.dispatchEvent(this, "AfterRecording", clip);
164  }
165 }
com.google.appinventor.components.runtime.EventDispatcher
Definition: EventDispatcher.java:22
com.google.appinventor.components.annotations.SimpleFunction
Definition: SimpleFunction.java:23
com.google.appinventor.components.runtime.util.ErrorMessages
Definition: ErrorMessages.java:17
com.google.appinventor.components.runtime.util.ErrorMessages.ERROR_MEDIA_EXTERNAL_STORAGE_NOT_AVAILABLE
static final int ERROR_MEDIA_EXTERNAL_STORAGE_NOT_AVAILABLE
Definition: ErrorMessages.java:97
com.google.appinventor.components.runtime.util
-*- mode: java; c-basic-offset: 2; -*-
Definition: AccountChooser.java:7
com.google.appinventor.components.common.YaVersion
Definition: YaVersion.java:14
com.google.appinventor.components
com.google.appinventor.components.annotations.DesignerComponent
Definition: DesignerComponent.java:22
com.google.appinventor.components.annotations.SimpleEvent
Definition: SimpleEvent.java:20
com.google.appinventor.components.runtime.File
Definition: File.java:53
com.google.appinventor.components.runtime.Camcorder.resultReturned
void resultReturned(int requestCode, int resultCode, Intent data)
Definition: Camcorder.java:122
com.google.appinventor.components.runtime.Camcorder.AfterRecording
void AfterRecording(String clip)
Definition: Camcorder.java:162
com.google.appinventor.components.annotations.UsesPermissions
Definition: UsesPermissions.java:21
com.google.appinventor.components.runtime.Camcorder
Definition: Camcorder.java:51
com.google.appinventor.components.runtime.PermissionResultHandler
Definition: PermissionResultHandler.java:15
com.google.appinventor.components.runtime.EventDispatcher.dispatchEvent
static boolean dispatchEvent(Component component, String eventName, Object...args)
Definition: EventDispatcher.java:188
com.google.appinventor.components.runtime.AndroidNonvisibleComponent
Definition: AndroidNonvisibleComponent.java:17
com.google.appinventor.components.runtime.ComponentContainer
Definition: ComponentContainer.java:16
com.google.appinventor.components.runtime
Copyright 2009-2011 Google, All Rights reserved.
Definition: AccelerometerSensor.java:8
com.google.appinventor.components.runtime.Component
Definition: Component.java:17
com.google.appinventor.components.common
Definition: ComponentCategory.java:7
com.google.appinventor.components.common.ComponentCategory
Definition: ComponentCategory.java:48
com.google.appinventor.components.runtime.ActivityResultListener
Definition: ActivityResultListener.java:16
com.google.appinventor.components.annotations.SimpleObject
Definition: SimpleObject.java:23
com.google
com
com.google.appinventor.components.runtime.util.ErrorMessages.ERROR_CAMCORDER_NO_CLIP_RETURNED
static final int ERROR_CAMCORDER_NO_CLIP_RETURNED
Definition: ErrorMessages.java:148
com.google.appinventor.components.runtime.ComponentContainer.$form
Form $form()
com.google.appinventor.components.runtime.ComponentContainer.$context
Activity $context()
com.google.appinventor.components.runtime.util.ErrorMessages.ERROR_MEDIA_EXTERNAL_STORAGE_READONLY
static final int ERROR_MEDIA_EXTERNAL_STORAGE_READONLY
Definition: ErrorMessages.java:96
com.google.appinventor.components.runtime.Camcorder.RecordVideo
void RecordVideo()
Definition: Camcorder.java:78
com.google.appinventor.components.annotations
com.google.appinventor
com.google.appinventor.components.runtime.Camcorder.Camcorder
Camcorder(ComponentContainer container)
Definition: Camcorder.java:69