Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AAR-232: (android) Camera plugin code cleanup - Part #2 #425

Merged
merged 1 commit into from Mar 1, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
104 changes: 49 additions & 55 deletions src/android/CameraLauncher.java
Expand Up @@ -18,34 +18,13 @@ Licensed to the Apache Software Foundation (ASF) under one
*/
package org.apache.cordova.camera;

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Organizing imports

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;
Expand All @@ -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,
Expand All @@ -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
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Organizing hard coded literal strings.

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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -358,7 +357,6 @@ private File createCaptureFile(int encodingType, String fileName) {
}



/**
* Get image from photo library.
*
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixing mis-formatted comment.

/*
* 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;

}


}