-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
|
@@ -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; | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
|
||
} | ||
|
||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Organizing imports