Skip to content

Commit

Permalink
feat(android): adding androidback property to showCamera (#11270)
Browse files Browse the repository at this point in the history
Fixes TIMOB-25633
  • Loading branch information
m1ga committed Apr 29, 2020
1 parent 302d9de commit b890f7c
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileDescriptor;
import java.io.InputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;

import org.appcelerator.kroll.KrollDict;
import org.appcelerator.kroll.KrollFunction;
Expand All @@ -29,8 +29,8 @@
import org.appcelerator.titanium.TiBlob;
import org.appcelerator.titanium.TiC;
import org.appcelerator.titanium.TiFileProxy;
import org.appcelerator.titanium.io.TiFileProvider;
import org.appcelerator.titanium.io.TiFileFactory;
import org.appcelerator.titanium.io.TiFileProvider;
import org.appcelerator.titanium.io.TitaniumBlob;
import org.appcelerator.titanium.proxy.TiViewProxy;
import org.appcelerator.titanium.util.TiActivityResultHandler;
Expand All @@ -43,11 +43,11 @@

import android.Manifest;
import android.app.Activity;
import android.content.ClipData;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.ClipData;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
Expand Down Expand Up @@ -268,7 +268,7 @@ static public Uri getMediaUriFrom(File file)
// Attempt to find existing media.
final String[] projection =
new String[] { MediaStore.MediaColumns._ID, MediaStore.MediaColumns.DISPLAY_NAME,
MediaStore.MediaColumns.RELATIVE_PATH };
MediaStore.MediaColumns.RELATIVE_PATH };
final Uri contentUri =
isMovie ? MediaStore.Video.Media.EXTERNAL_CONTENT_URI : MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
try (Cursor cursor = contentResolver.query(contentUri, projection, null, null, null)) {
Expand Down Expand Up @@ -407,6 +407,7 @@ private void launchCameraActivity(KrollDict cameraOptions, TiViewProxy overLayPr
KrollFunction successCallback = null;
KrollFunction cancelCallback = null;
KrollFunction errorCallback = null;
KrollFunction androidbackCallback = null;
boolean saveToPhotoGallery = false;
boolean autohide = true;
int videoMaximumDuration = 0;
Expand All @@ -425,6 +426,9 @@ private void launchCameraActivity(KrollDict cameraOptions, TiViewProxy overLayPr
if (cameraOptions.containsKeyAndNotNull(TiC.EVENT_ERROR)) {
errorCallback = (KrollFunction) cameraOptions.get(TiC.EVENT_ERROR);
}
if (cameraOptions.containsKeyAndNotNull(TiC.EVENT_ANDROID_BACK)) {
androidbackCallback = (KrollFunction) cameraOptions.get(TiC.EVENT_ANDROID_BACK);
}
if (cameraOptions.containsKeyAndNotNull(PROP_AUTOSAVE)) {
saveToPhotoGallery = cameraOptions.getBoolean(PROP_AUTOSAVE);
}
Expand Down Expand Up @@ -464,6 +468,7 @@ private void launchCameraActivity(KrollDict cameraOptions, TiViewProxy overLayPr
TiCameraActivity.successCallback = successCallback;
TiCameraActivity.cancelCallback = cancelCallback;
TiCameraActivity.errorCallback = errorCallback;
TiCameraActivity.androidbackCallback = androidbackCallback;
TiCameraActivity.saveToPhotoGallery = saveToPhotoGallery;
TiCameraActivity.autohide = autohide;
TiCameraActivity.overlayProxy = overLayProxy;
Expand Down Expand Up @@ -572,18 +577,13 @@ public void requestCameraPermissions(@Kroll.argument(optional = true) KrollFunct

String[] permissions = null;
if (!hasCameraPermission() && !hasStoragePermission()) {
permissions = new String[] {
Manifest.permission.CAMERA,
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE
};
permissions = new String[] { Manifest.permission.CAMERA, Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE };
} else if (!hasCameraPermission()) {
permissions = new String[] { Manifest.permission.CAMERA };
} else {
permissions = new String[] {
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE
};
permissions =
new String[] { Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE };
}

TiBaseActivity.registerPermissionRequestCallback(TiC.PERMISSION_CODE_CAMERA, permissionCallback,
Expand Down Expand Up @@ -863,16 +863,18 @@ public void onError(Activity activity, int requestCode, Exception e)
}
}

@Kroll.method
@Kroll.setProperty
public void setCameraFlashMode(int flashMode)
@Kroll
.method
@Kroll.setProperty
public void setCameraFlashMode(int flashMode)
{
TiCameraActivity.setFlashMode(flashMode);
}

@Kroll.method
@Kroll.getProperty
public int getCameraFlashMode()
@Kroll
.method
@Kroll.getProperty
public int getCameraFlashMode()
{
return TiCameraActivity.cameraFlashMode;
}
Expand Down Expand Up @@ -1443,16 +1445,18 @@ public void switchCamera(int whichCamera)
activity.switchCamera(whichCamera);
}

@Kroll.method
@Kroll.getProperty
public boolean getIsCameraSupported()
@Kroll
.method
@Kroll.getProperty
public boolean getIsCameraSupported()
{
return Camera.getNumberOfCameras() > 0;
}

@Kroll.method
@Kroll.getProperty
public int[] getAvailableCameras()
@Kroll
.method
@Kroll.getProperty
public int[] getAvailableCameras()
{
int cameraCount = Camera.getNumberOfCameras();
if (cameraCount == 0) {
Expand Down Expand Up @@ -1482,9 +1486,10 @@ public int[] getAvailableCameras()
return result;
}

@Kroll.method
@Kroll.getProperty
public boolean getCanRecord()
@Kroll
.method
@Kroll.getProperty
public boolean getCanRecord()
{
return TiApplication.getInstance().getPackageManager().hasSystemFeature("android.hardware.microphone");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,19 @@
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.hardware.Camera;
import android.hardware.Camera.AutoFocusCallback;
import android.hardware.Camera.CameraInfo;
import android.hardware.Camera.Parameters;
import android.hardware.Camera.PictureCallback;
import android.hardware.Camera.ShutterCallback;
import android.hardware.Camera.Size;
import android.media.CamcorderProfile;
import android.media.MediaRecorder;
import android.hardware.Camera.Size;
import android.net.Uri;
import android.os.Bundle;
import androidx.appcompat.app.ActionBar;

import android.os.Environment;
import android.view.Gravity;
import android.view.KeyEvent;
Expand All @@ -50,7 +49,7 @@
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.content.pm.PackageManager;
import androidx.appcompat.app.ActionBar;

@SuppressWarnings("deprecation")
public class TiCameraActivity extends TiBaseActivity implements SurfaceHolder.Callback, MediaRecorder.OnInfoListener
Expand Down Expand Up @@ -82,7 +81,7 @@ public class TiCameraActivity extends TiBaseActivity implements SurfaceHolder.Ca

public static MediaModule mediaContext;
public static KrollObject callbackContext;
public static KrollFunction successCallback, errorCallback, cancelCallback;
public static KrollFunction successCallback, errorCallback, cancelCallback, androidbackCallback;
public static boolean saveToPhotoGallery = false;
public static int whichCamera = MediaModule.CAMERA_REAR;
public static int cameraFlashMode = MediaModule.CAMERA_FLASH_OFF;
Expand Down Expand Up @@ -872,8 +871,8 @@ private void openCamera(int cameraId)
camera = Camera.open(cameraId);
}
} catch (Exception e) {
String errorMessage
= "Could not open camera. "
String errorMessage =
"Could not open camera. "
+ "Camera may be in use by another process or device policy manager has disabled the camera.";
Log.e(TAG, errorMessage, e);
}
Expand Down Expand Up @@ -936,12 +935,18 @@ public void run()
@Override
public void onBackPressed()
{
if (cancelCallback != null) {
if (androidbackCallback != null) {
KrollDict response = new KrollDict();
response.putCodeAndMessage(-1, "User cancelled the request");
cancelCallback.callAsync(callbackContext, response);
response.putCodeAndMessage(-1, "User pressed androidback");
androidbackCallback.callAsync(callbackContext, response);
} else {
if (cancelCallback != null) {
KrollDict response = new KrollDict();
response.putCodeAndMessage(-1, "User cancelled the request");
cancelCallback.callAsync(callbackContext, response);
}
super.onBackPressed();
}
super.onBackPressed();
}

@Override
Expand Down
19 changes: 14 additions & 5 deletions apidoc/Titanium/Media/Media.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ description: |
For examples of using the Media APIs, refer to the
[Working with Media APIs guide](https://docs.appcelerator.com/platform/latest/#!/guide/Working_with_Media_APIs)
in addition to the other media submodule API documentation.
**Note**: Some third party Android camera apps may choose to ignore video recording quality
settings. If you wish to specifically set the video quality, don't assume
EXTRA_VIDEO_QUALITY intent will be respected by the camera app and use Titanium's built-in
camera window which can be used to assign the "overlay" property when calling the
**Note**: Some third party Android camera apps may choose to ignore video recording quality
settings. If you wish to specifically set the video quality, don't assume
EXTRA_VIDEO_QUALITY intent will be respected by the camera app and use Titanium's built-in
camera window which can be used to assign the "overlay" property when calling the
showCamera() method.
extends: Titanium.Module
since: "0.1"
Expand Down Expand Up @@ -102,7 +102,7 @@ methods:
files may be imported by saving them with the `.mov` extension.
On Android this method *only supports saving images* to the device gallery prior to Titanium SDK 6.1.0.
On iOS the `NSPhotoLibraryAddUsageDescription` plist key is required in the tiapp.xml in order
to use this API. Also make sure to check for the gallery permissions with <Titanium.Media.hasPhotoGalleryPermissions>
and request the permissions with <Titanium.Media.requestPhotoGalleryPermissions> if not granted so far.
Expand Down Expand Up @@ -1918,6 +1918,15 @@ properties:
- name: cancel
summary: Function to call if the user presses the cancel button.
type: Callback<FailureResponse>
- name: androidback
summary: Function to call if the user presses the back button.
description: |
On Android, this property is considered only if `overlay` is also set. If the user presses
the back button on the phone this callback function will be called. The camera view stays
open and you will need to call `Ti.Media.hideCamera();` if you want to close it.
type: Callback<FailureResponse>
platforms: [android]
since: {android: "9.0.0"}
- name: autohide
summary: Specifies if the camera should be hidden automatically after the media capture is completed.
type: Boolean
Expand Down

0 comments on commit b890f7c

Please sign in to comment.