From a13665d530f24a667a17d2a3bc61296fc316d3d2 Mon Sep 17 00:00:00 2001 From: Hazem Saleh Date: Fri, 1 Mar 2019 14:00:39 -0500 Subject: [PATCH] AAR-232: (android) Enhancement: Camera plugin code cleanup (#425) --- src/android/CameraLauncher.java | 122 +++++++++++++++----------------- 1 file changed, 58 insertions(+), 64 deletions(-) diff --git a/src/android/CameraLauncher.java b/src/android/CameraLauncher.java index 6cdf0c5..b133340 100644 --- a/src/android/CameraLauncher.java +++ b/src/android/CameraLauncher.java @@ -18,6 +18,37 @@ */ package org.apache.cordova.camera; +import android.Manifest; +import android.app.Activity; +import android.content.ActivityNotFoundException; +import android.content.ContentValues; +import android.content.Intent; +import android.content.pm.PackageManager; +import android.content.pm.PackageManager.NameNotFoundException; +import android.database.Cursor; +import android.graphics.Bitmap; +import android.graphics.Bitmap.CompressFormat; +import android.graphics.BitmapFactory; +import android.graphics.Matrix; +import android.media.ExifInterface; +import android.media.MediaScannerConnection; +import android.media.MediaScannerConnection.MediaScannerConnectionClient; +import android.net.Uri; +import android.os.Bundle; +import android.os.Environment; +import android.provider.MediaStore; +import android.support.v4.content.FileProvider; +import android.util.Base64; + +import org.apache.cordova.BuildHelper; +import org.apache.cordova.CallbackContext; +import org.apache.cordova.CordovaPlugin; +import org.apache.cordova.LOG; +import org.apache.cordova.PermissionHelper; +import org.apache.cordova.PluginResult; +import org.json.JSONArray; +import org.json.JSONException; + import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; @@ -29,43 +60,6 @@ import java.io.OutputStream; import java.text.SimpleDateFormat; import java.util.Date; -import org.apache.cordova.BuildHelper; -import org.apache.cordova.CallbackContext; -import org.apache.cordova.CordovaPlugin; -import org.apache.cordova.CordovaResourceApi; -import org.apache.cordova.LOG; -import org.apache.cordova.PermissionHelper; -import org.apache.cordova.PluginResult; -import org.json.JSONArray; -import org.json.JSONException; - -import android.Manifest; -import android.annotation.TargetApi; -import android.app.Activity; -import android.content.ActivityNotFoundException; -import android.content.ContentValues; -import android.content.Context; -import android.content.Intent; -import android.database.Cursor; -import android.graphics.Bitmap; -import android.graphics.Bitmap.CompressFormat; -import android.graphics.BitmapFactory; -import android.graphics.Matrix; -import android.media.ExifInterface; -import android.media.MediaScannerConnection; -import android.media.MediaScannerConnection.MediaScannerConnectionClient; -import android.net.Uri; -import android.os.Build; -import android.os.Bundle; -import android.os.Environment; -import android.provider.DocumentsContract; -import android.provider.MediaStore; -import android.provider.OpenableColumns; -import android.support.v4.content.FileProvider; -import android.util.Base64; -import android.content.pm.PackageManager; -import android.content.pm.PackageManager.NameNotFoundException; - /** * This class launches the camera view, allows the user to take a picture, closes the camera view, * and returns the captured image. When the camera view is closed, the screen displayed before @@ -87,13 +81,19 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect private static final int JPEG = 0; // Take a picture of type JPEG private static final int PNG = 1; // Take a picture of type PNG - private static final String JPEG_EXTENSION = ".jpg"; - private static final String PNG_EXTENSION = ".png"; + private static final String JPEG_TYPE = "jpg"; + private static final String PNG_TYPE = "png"; + private static final String JPEG_EXTENSION = "." + JPEG_TYPE; + private static final String PNG_EXTENSION = "." + PNG_TYPE; private static final String PNG_MIME_TYPE = "image/png"; private static final String JPEG_MIME_TYPE = "image/jpeg"; private static final String GET_PICTURE = "Get Picture"; private static final String GET_VIDEO = "Get Video"; private static final String GET_All = "Get All"; + private static final String CROPPED_URI_KEY = "croppedUri"; + private static final String IMAGE_URI_KEY = "imageUri"; + + private static final String TAKE_PICTURE_ACTION = "takePicture"; public static final int PERMISSION_DENIED_ERROR = 20; public static final int TAKE_PIC_SEC = 0; @@ -147,7 +147,7 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect this.applicationId = preferences.getString("applicationId", this.applicationId); - if (action.equals("takePicture")) { + if (action.equals(TAKE_PICTURE_ACTION)) { this.srcType = CAMERA; this.destType = FILE_URI; this.saveToPhotoAlbum = false; @@ -312,7 +312,6 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect PackageManager mPm = this.cordova.getActivity().getPackageManager(); if(intent.resolveActivity(mPm) != null) { - this.cordova.startActivityForResult((CordovaPlugin) this, intent, (CAMERA + 1) * 16 + returnType + 1); } else @@ -358,7 +357,6 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect } - /** * Get image from photo library. * @@ -640,7 +638,7 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect // Get filename from uri String fileName = realPath != null ? realPath.substring(realPath.lastIndexOf('/') + 1) : - "modified." + (this.encodingType == JPEG ? "jpg" : "png"); + "modified." + (this.encodingType == JPEG ? JPEG_TYPE : PNG_TYPE); String timeStamp = new SimpleDateFormat(TIME_FORMAT).format(new Date()); //String fileName = "IMG_" + timeStamp + (this.encodingType == JPEG ? ".jpg" : ".png"); @@ -1348,11 +1346,11 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect state.putBoolean("saveToPhotoAlbum", this.saveToPhotoAlbum); if (this.croppedUri != null) { - state.putString("croppedUri", this.croppedUri.toString()); + state.putString(CROPPED_URI_KEY, this.croppedUri.toString()); } if (this.imageUri != null) { - state.putString("imageUri", this.imageUri.getFileUri().toString()); + state.putString(IMAGE_URI_KEY, this.imageUri.getFileUri().toString()); } return state; @@ -1371,38 +1369,34 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect this.correctOrientation = state.getBoolean("correctOrientation"); this.saveToPhotoAlbum = state.getBoolean("saveToPhotoAlbum"); - if (state.containsKey("croppedUri")) { - this.croppedUri = Uri.parse(state.getString("croppedUri")); + if (state.containsKey(CROPPED_URI_KEY)) { + this.croppedUri = Uri.parse(state.getString(CROPPED_URI_KEY)); } - if (state.containsKey("imageUri")) { + if (state.containsKey(IMAGE_URI_KEY)) { //I have no idea what type of URI is being passed in - this.imageUri = new CordovaUri(Uri.parse(state.getString("imageUri"))); + this.imageUri = new CordovaUri(Uri.parse(state.getString(IMAGE_URI_KEY))); } this.callbackContext = callbackContext; } - /* - * This is dirty, but it does the job. - * - * Since the FilesProvider doesn't really provide you a way of getting a URL from the file, - * and since we actually need the Camera to create the file for us most of the time, we don't - * actually write the file, just generate the location based on a timestamp, we need to get it - * back from the Intent. - * - * However, the FilesProvider preserves the path, so we can at least write to it from here, since - * we own the context in this case. - */ - + /* + * This is dirty, but it does the job. + * + * Since the FilesProvider doesn't really provide you a way of getting a URL from the file, + * and since we actually need the Camera to create the file for us most of the time, we don't + * actually write the file, just generate the location based on a timestamp, we need to get it + * back from the Intent. + * + * However, the FilesProvider preserves the path, so we can at least write to it from here, since + * we own the context in this case. + */ private String getFileNameFromUri(Uri uri) { String fullUri = uri.toString(); String partial_path = fullUri.split("external_files")[1]; File external_storage = Environment.getExternalStorageDirectory(); String path = external_storage.getAbsolutePath() + partial_path; return path; - } - - }