From 5780bce6c420536041063fe3dad440740d3f9f32 Mon Sep 17 00:00:00 2001 From: Hazem Saleh Date: Sun, 24 Feb 2019 02:53:58 -0500 Subject: [PATCH] AAR-232: (android) Enhancement: Camera plugin code cleanup --- src/android/CameraLauncher.java | 104 +++++++++++++++----------------- 1 file changed, 49 insertions(+), 55 deletions(-) diff --git a/src/android/CameraLauncher.java b/src/android/CameraLauncher.java index 6cdf0c5f7..b1333400f 100644 --- a/src/android/CameraLauncher.java +++ b/src/android/CameraLauncher.java @@ -18,34 +18,13 @@ Licensed to the Apache Software Foundation (ASF) under one */ package org.apache.cordova.camera; -import java.io.ByteArrayOutputStream; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStream; -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.content.pm.PackageManager; +import android.content.pm.PackageManager.NameNotFoundException; import android.database.Cursor; import android.graphics.Bitmap; import android.graphics.Bitmap.CompressFormat; @@ -55,16 +34,31 @@ Licensed to the Apache Software Foundation (ASF) under one 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; + +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; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.text.SimpleDateFormat; +import java.util.Date; /** * This class launches the camera view, allows the user to take a picture, closes the camera view, @@ -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 boolean execute(String action, JSONArray args, CallbackContext callbackCo 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 void takePicture(int returnType, int encodingType) 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 @@ private File createCaptureFile(int encodingType, String fileName) { } - /** * Get image from photo library. * @@ -640,7 +638,7 @@ private String outputModifiedBitmap(Bitmap bitmap, Uri uri) throws IOException { // 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 Bundle onSaveInstanceState() { 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 void onRestoreStateForActivityResult(Bundle state, CallbackContext callba 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; - } - - }