You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by fi...@apache.org on 2013/01/22 02:57:58 UTC
[43/52] [partial] support for 2.4.0rc1. "vendored" the platform libs
in. added Gord and Braden as contributors. removed dependency on unzip and
axed the old download-cordova code.
http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/d61deccd/lib/cordova-android/framework/src/org/apache/cordova/CameraLauncher.java
----------------------------------------------------------------------
diff --git a/lib/cordova-android/framework/src/org/apache/cordova/CameraLauncher.java b/lib/cordova-android/framework/src/org/apache/cordova/CameraLauncher.java
new file mode 100755
index 0000000..f9edd10
--- /dev/null
+++ b/lib/cordova-android/framework/src/org/apache/cordova/CameraLauncher.java
@@ -0,0 +1,786 @@
+/*
+ Licensed to the Apache Software Foundation (ASF) under one
+ or more contributor license agreements. See the NOTICE file
+ distributed with this work for additional information
+ regarding copyright ownership. The ASF licenses this file
+ to you under the Apache License, Version 2.0 (the
+ "License"); you may not use this file except in compliance
+ with the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing,
+ software distributed under the License is distributed on an
+ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ KIND, either express or implied. See the License for the
+ specific language governing permissions and limitations
+ under the License.
+*/
+package org.apache.cordova;
+
+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.OutputStream;
+
+import org.apache.commons.codec.binary.Base64;
+import org.apache.cordova.api.CallbackContext;
+import org.apache.cordova.api.CordovaPlugin;
+import org.apache.cordova.api.LOG;
+import org.apache.cordova.api.PluginResult;
+import org.json.JSONArray;
+import org.json.JSONException;
+
+import android.app.Activity;
+import android.content.ContentValues;
+import android.content.Intent;
+import android.database.Cursor;
+import android.graphics.Bitmap;
+import android.graphics.BitmapFactory;
+import android.graphics.Matrix;
+import android.graphics.Bitmap.CompressFormat;
+import android.media.MediaScannerConnection;
+import android.media.MediaScannerConnection.MediaScannerConnectionClient;
+import android.net.Uri;
+import android.os.Environment;
+import android.provider.MediaStore;
+import android.util.Log;
+
+/**
+ * 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
+ * the camera view was shown is redisplayed.
+ */
+public class CameraLauncher extends CordovaPlugin implements MediaScannerConnectionClient {
+
+ private static final int DATA_URL = 0; // Return base64 encoded string
+ private static final int FILE_URI = 1; // Return file uri (content://media/external/images/media/2 for Android)
+
+ private static final int PHOTOLIBRARY = 0; // Choose image from picture library (same as SAVEDPHOTOALBUM for Android)
+ private static final int CAMERA = 1; // Take picture from camera
+ private static final int SAVEDPHOTOALBUM = 2; // Choose image from picture library (same as PHOTOLIBRARY for Android)
+
+ private static final int PICTURE = 0; // allow selection of still pictures only. DEFAULT. Will return format specified via DestinationType
+ private static final int VIDEO = 1; // allow selection of video only, ONLY RETURNS URL
+ private static final int ALLMEDIA = 2; // allow selection from all media types
+
+ 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 GET_PICTURE = "Get Picture";
+ private static final String GET_VIDEO = "Get Video";
+ private static final String GET_All = "Get All";
+
+ private static final String LOG_TAG = "CameraLauncher";
+
+ private int mQuality; // Compression quality hint (0-100: 0=low quality & high compression, 100=compress of max quality)
+ private int targetWidth; // desired width of the image
+ private int targetHeight; // desired height of the image
+ private Uri imageUri; // Uri of captured image
+ private int encodingType; // Type of encoding to use
+ private int mediaType; // What type of media to retrieve
+ private boolean saveToPhotoAlbum; // Should the picture be saved to the device's photo album
+ private boolean correctOrientation; // Should the pictures orientation be corrected
+ //private boolean allowEdit; // Should we allow the user to crop the image. UNUSED.
+
+ public CallbackContext callbackContext;
+ private int numPics;
+
+ private MediaScannerConnection conn; // Used to update gallery app with newly-written files
+ private Uri scanMe; // Uri of image to be added to content store
+
+ //This should never be null!
+ //private CordovaInterface cordova;
+
+ /**
+ * Constructor.
+ */
+ public CameraLauncher() {
+ }
+
+// public void setContext(CordovaInterface mCtx) {
+// super.setContext(mCtx);
+// if (CordovaInterface.class.isInstance(mCtx))
+// cordova = (CordovaInterface) mCtx;
+// else
+// LOG.d(LOG_TAG, "ERROR: You must use the CordovaInterface for this to work correctly. Please implement it in your activity");
+// }
+
+ /**
+ * Executes the request and returns PluginResult.
+ *
+ * @param action The action to execute.
+ * @param args JSONArry of arguments for the plugin.
+ * @param callbackContext The callback id used when calling back into JavaScript.
+ * @return A PluginResult object with a status and message.
+ */
+ public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
+ this.callbackContext = callbackContext;
+
+ if (action.equals("takePicture")) {
+ int srcType = CAMERA;
+ int destType = FILE_URI;
+ this.saveToPhotoAlbum = false;
+ this.targetHeight = 0;
+ this.targetWidth = 0;
+ this.encodingType = JPEG;
+ this.mediaType = PICTURE;
+ this.mQuality = 80;
+
+ this.mQuality = args.getInt(0);
+ destType = args.getInt(1);
+ srcType = args.getInt(2);
+ this.targetWidth = args.getInt(3);
+ this.targetHeight = args.getInt(4);
+ this.encodingType = args.getInt(5);
+ this.mediaType = args.getInt(6);
+ //this.allowEdit = args.getBoolean(7); // This field is unused.
+ this.correctOrientation = args.getBoolean(8);
+ this.saveToPhotoAlbum = args.getBoolean(9);
+
+ // If the user specifies a 0 or smaller width/height
+ // make it -1 so later comparisons succeed
+ if (this.targetWidth < 1) {
+ this.targetWidth = -1;
+ }
+ if (this.targetHeight < 1) {
+ this.targetHeight = -1;
+ }
+
+ if (srcType == CAMERA) {
+ this.takePicture(destType, encodingType);
+ }
+ else if ((srcType == PHOTOLIBRARY) || (srcType == SAVEDPHOTOALBUM)) {
+ this.getImage(srcType, destType);
+ }
+ PluginResult r = new PluginResult(PluginResult.Status.NO_RESULT);
+ r.setKeepCallback(true);
+ callbackContext.sendPluginResult(r);
+ return true;
+ }
+ return false;
+ }
+
+ //--------------------------------------------------------------------------
+ // LOCAL METHODS
+ //--------------------------------------------------------------------------
+
+ /**
+ * Take a picture with the camera.
+ * When an image is captured or the camera view is cancelled, the result is returned
+ * in CordovaActivity.onActivityResult, which forwards the result to this.onActivityResult.
+ *
+ * The image can either be returned as a base64 string or a URI that points to the file.
+ * To display base64 string in an img tag, set the source to:
+ * img.src="data:image/jpeg;base64,"+result;
+ * or to display URI in an img tag
+ * img.src=result;
+ *
+ * @param quality Compression quality hint (0-100: 0=low quality & high compression, 100=compress of max quality)
+ * @param returnType Set the type of image to return.
+ */
+ public void takePicture(int returnType, int encodingType) {
+ // Save the number of images currently on disk for later
+ this.numPics = queryImgDB(whichContentStore()).getCount();
+
+ // Display camera
+ Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
+
+ // Specify file so that large image is captured and returned
+ File photo = createCaptureFile(encodingType);
+ intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
+ this.imageUri = Uri.fromFile(photo);
+
+ if (this.cordova != null) {
+ this.cordova.startActivityForResult((CordovaPlugin) this, intent, (CAMERA + 1) * 16 + returnType + 1);
+ }
+// else
+// LOG.d(LOG_TAG, "ERROR: You must use the CordovaInterface for this to work correctly. Please implement it in your activity");
+ }
+
+ /**
+ * Create a file in the applications temporary directory based upon the supplied encoding.
+ *
+ * @param encodingType of the image to be taken
+ * @return a File object pointing to the temporary picture
+ */
+ private File createCaptureFile(int encodingType) {
+ File photo = null;
+ if (encodingType == JPEG) {
+ photo = new File(DirectoryManager.getTempDirectoryPath(this.cordova.getActivity()), ".Pic.jpg");
+ } else if (encodingType == PNG) {
+ photo = new File(DirectoryManager.getTempDirectoryPath(this.cordova.getActivity()), ".Pic.png");
+ } else {
+ throw new IllegalArgumentException("Invalid Encoding Type: " + encodingType);
+ }
+ return photo;
+ }
+
+ /**
+ * Get image from photo library.
+ *
+ * @param quality Compression quality hint (0-100: 0=low quality & high compression, 100=compress of max quality)
+ * @param srcType The album to get image from.
+ * @param returnType Set the type of image to return.
+ */
+ // TODO: Images selected from SDCARD don't display correctly, but from CAMERA ALBUM do!
+ public void getImage(int srcType, int returnType) {
+ Intent intent = new Intent();
+ String title = GET_PICTURE;
+ if (this.mediaType == PICTURE) {
+ intent.setType("image/*");
+ }
+ else if (this.mediaType == VIDEO) {
+ intent.setType("video/*");
+ title = GET_VIDEO;
+ }
+ else if (this.mediaType == ALLMEDIA) {
+ // I wanted to make the type 'image/*, video/*' but this does not work on all versions
+ // of android so I had to go with the wildcard search.
+ intent.setType("*/*");
+ title = GET_All;
+ }
+
+ intent.setAction(Intent.ACTION_GET_CONTENT);
+ intent.addCategory(Intent.CATEGORY_OPENABLE);
+ if (this.cordova != null) {
+ this.cordova.startActivityForResult((CordovaPlugin) this, Intent.createChooser(intent,
+ new String(title)), (srcType + 1) * 16 + returnType + 1);
+ }
+ }
+
+ /**
+ * Called when the camera view exits.
+ *
+ * @param requestCode The request code originally supplied to startActivityForResult(),
+ * allowing you to identify who this result came from.
+ * @param resultCode The integer result code returned by the child activity through its setResult().
+ * @param intent An Intent, which can return result data to the caller (various data can be attached to Intent "extras").
+ */
+ public void onActivityResult(int requestCode, int resultCode, Intent intent) {
+
+ // Get src and dest types from request code
+ int srcType = (requestCode / 16) - 1;
+ int destType = (requestCode % 16) - 1;
+ int rotate = 0;
+
+ // If CAMERA
+ if (srcType == CAMERA) {
+ // If image available
+ if (resultCode == Activity.RESULT_OK) {
+ try {
+ // Create an ExifHelper to save the exif data that is lost during compression
+ ExifHelper exif = new ExifHelper();
+ try {
+ if (this.encodingType == JPEG) {
+ exif.createInFile(DirectoryManager.getTempDirectoryPath(this.cordova.getActivity()) + "/.Pic.jpg");
+ exif.readExifData();
+ rotate = exif.getOrientation();
+ }
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+
+ Bitmap bitmap = null;
+ Uri uri = null;
+
+ // If sending base64 image back
+ if (destType == DATA_URL) {
+ bitmap = getScaledBitmap(FileUtils.stripFileProtocol(imageUri.toString()));
+ if (bitmap == null) {
+ // Try to get the bitmap from intent.
+ bitmap = (Bitmap)intent.getExtras().get("data");
+ }
+
+ // Double-check the bitmap.
+ if (bitmap == null) {
+ Log.d(LOG_TAG, "I either have a null image path or bitmap");
+ this.failPicture("Unable to create bitmap!");
+ return;
+ }
+
+ if (rotate != 0 && this.correctOrientation) {
+ bitmap = getRotatedBitmap(rotate, bitmap, exif);
+ }
+
+ this.processPicture(bitmap);
+ checkForDuplicateImage(DATA_URL);
+ }
+
+ // If sending filename back
+ else if (destType == FILE_URI) {
+ if (!this.saveToPhotoAlbum) {
+ uri = Uri.fromFile(new File(DirectoryManager.getTempDirectoryPath(this.cordova.getActivity()), System.currentTimeMillis() + ".jpg"));
+ } else {
+ uri = getUriFromMediaStore();
+ }
+
+ if (uri == null) {
+ this.failPicture("Error capturing image - no media storage found.");
+ }
+
+ // If all this is true we shouldn't compress the image.
+ if (this.targetHeight == -1 && this.targetWidth == -1 && this.mQuality == 100 &&
+ !this.correctOrientation) {
+ writeUncompressedImage(uri);
+
+ this.callbackContext.success(uri.toString());
+ } else {
+ bitmap = getScaledBitmap(FileUtils.stripFileProtocol(imageUri.toString()));
+
+ if (rotate != 0 && this.correctOrientation) {
+ bitmap = getRotatedBitmap(rotate, bitmap, exif);
+ }
+
+ // Add compressed version of captured image to returned media store Uri
+ OutputStream os = this.cordova.getActivity().getContentResolver().openOutputStream(uri);
+ bitmap.compress(Bitmap.CompressFormat.JPEG, this.mQuality, os);
+ os.close();
+
+ // Restore exif data to file
+ if (this.encodingType == JPEG) {
+ String exifPath;
+ if (this.saveToPhotoAlbum) {
+ exifPath = FileUtils.getRealPathFromURI(uri, this.cordova);
+ } else {
+ exifPath = uri.getPath();
+ }
+ exif.createOutFile(exifPath);
+ exif.writeExifData();
+ }
+
+ }
+ // Send Uri back to JavaScript for viewing image
+ this.callbackContext.success(uri.toString());
+ }
+
+ this.cleanup(FILE_URI, this.imageUri, uri, bitmap);
+ bitmap = null;
+
+ } catch (IOException e) {
+ e.printStackTrace();
+ this.failPicture("Error capturing image.");
+ }
+ }
+
+ // If cancelled
+ else if (resultCode == Activity.RESULT_CANCELED) {
+ this.failPicture("Camera cancelled.");
+ }
+
+ // If something else
+ else {
+ this.failPicture("Did not complete!");
+ }
+ }
+
+ // If retrieving photo from library
+ else if ((srcType == PHOTOLIBRARY) || (srcType == SAVEDPHOTOALBUM)) {
+ if (resultCode == Activity.RESULT_OK) {
+ Uri uri = intent.getData();
+
+ // If you ask for video or all media type you will automatically get back a file URI
+ // and there will be no attempt to resize any returned data
+ if (this.mediaType != PICTURE) {
+ this.callbackContext.success(uri.toString());
+ }
+ else {
+ // This is a special case to just return the path as no scaling,
+ // rotating or compression needs to be done
+ if (this.targetHeight == -1 && this.targetWidth == -1 &&
+ this.mQuality == 100 && destType == FILE_URI && !this.correctOrientation) {
+ this.callbackContext.success(uri.toString());
+ } else {
+ // Get the path to the image. Makes loading so much easier.
+ String imagePath = FileUtils.getRealPathFromURI(uri, this.cordova);
+ String mimeType = FileUtils.getMimeType(imagePath);
+ // Log.d(LOG_TAG, "Real path = " + imagePath);
+ // Log.d(LOG_TAG, "mime type = " + mimeType);
+ // If we don't have a valid image so quit.
+ if (imagePath == null || mimeType == null ||
+ !(mimeType.equalsIgnoreCase("image/jpeg") || mimeType.equalsIgnoreCase("image/png"))) {
+ Log.d(LOG_TAG, "I either have a null image path or bitmap");
+ this.failPicture("Unable to retrieve path to picture!");
+ return;
+ }
+ Bitmap bitmap = getScaledBitmap(imagePath);
+ if (bitmap == null) {
+ Log.d(LOG_TAG, "I either have a null image path or bitmap");
+ this.failPicture("Unable to create bitmap!");
+ return;
+ }
+
+ if (this.correctOrientation) {
+ String[] cols = { MediaStore.Images.Media.ORIENTATION };
+ Cursor cursor = this.cordova.getActivity().getContentResolver().query(intent.getData(),
+ cols, null, null, null);
+ if (cursor != null) {
+ cursor.moveToPosition(0);
+ rotate = cursor.getInt(0);
+ cursor.close();
+ }
+ if (rotate != 0) {
+ Matrix matrix = new Matrix();
+ matrix.setRotate(rotate);
+ bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
+ }
+ }
+
+ // If sending base64 image back
+ if (destType == DATA_URL) {
+ this.processPicture(bitmap);
+ }
+
+ // If sending filename back
+ else if (destType == FILE_URI) {
+ // Do we need to scale the returned file
+ if (this.targetHeight > 0 && this.targetWidth > 0) {
+ try {
+ // Create an ExifHelper to save the exif data that is lost during compression
+ String resizePath = DirectoryManager.getTempDirectoryPath(this.cordova.getActivity()) + "/resize.jpg";
+ ExifHelper exif = new ExifHelper();
+ try {
+ if (this.encodingType == JPEG) {
+ exif.createInFile(resizePath);
+ exif.readExifData();
+ rotate = exif.getOrientation();
+ }
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+
+ OutputStream os = new FileOutputStream(resizePath);
+ bitmap.compress(Bitmap.CompressFormat.JPEG, this.mQuality, os);
+ os.close();
+
+ // Restore exif data to file
+ if (this.encodingType == JPEG) {
+ exif.createOutFile(FileUtils.getRealPathFromURI(uri, this.cordova));
+ exif.writeExifData();
+ }
+
+ // The resized image is cached by the app in order to get around this and not have to delete you
+ // application cache I'm adding the current system time to the end of the file url.
+ this.callbackContext.success("file://" + resizePath + "?" + System.currentTimeMillis());
+ } catch (Exception e) {
+ e.printStackTrace();
+ this.failPicture("Error retrieving image.");
+ }
+ }
+ else {
+ this.callbackContext.success(uri.toString());
+ }
+ }
+ if (bitmap != null) {
+ bitmap.recycle();
+ bitmap = null;
+ }
+ System.gc();
+ }
+ }
+ }
+ else if (resultCode == Activity.RESULT_CANCELED) {
+ this.failPicture("Selection cancelled.");
+ }
+ else {
+ this.failPicture("Selection did not complete!");
+ }
+ }
+ }
+
+ /**
+ * Figure out if the bitmap should be rotated. For instance if the picture was taken in
+ * portrait mode
+ *
+ * @param rotate
+ * @param bitmap
+ * @return rotated bitmap
+ */
+ private Bitmap getRotatedBitmap(int rotate, Bitmap bitmap, ExifHelper exif) {
+ Matrix matrix = new Matrix();
+ if (rotate == 180) {
+ matrix.setRotate(rotate);
+ } else {
+ matrix.setRotate(rotate, (float) bitmap.getWidth() / 2, (float) bitmap.getHeight() / 2);
+ }
+ bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
+ exif.resetOrientation();
+ return bitmap;
+ }
+
+ /**
+ * In the special case where the default width, height and quality are unchanged
+ * we just write the file out to disk saving the expensive Bitmap.compress function.
+ *
+ * @param uri
+ * @throws FileNotFoundException
+ * @throws IOException
+ */
+ private void writeUncompressedImage(Uri uri) throws FileNotFoundException,
+ IOException {
+ FileInputStream fis = new FileInputStream(FileUtils.stripFileProtocol(imageUri.toString()));
+ OutputStream os = this.cordova.getActivity().getContentResolver().openOutputStream(uri);
+ byte[] buffer = new byte[4096];
+ int len;
+ while ((len = fis.read(buffer)) != -1) {
+ os.write(buffer, 0, len);
+ }
+ os.flush();
+ os.close();
+ fis.close();
+ }
+
+ /**
+ * Create entry in media store for image
+ *
+ * @return uri
+ */
+ private Uri getUriFromMediaStore() {
+ ContentValues values = new ContentValues();
+ values.put(android.provider.MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
+ Uri uri;
+ try {
+ uri = this.cordova.getActivity().getContentResolver().insert(android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
+ } catch (UnsupportedOperationException e) {
+ LOG.d(LOG_TAG, "Can't write to external media storage.");
+ try {
+ uri = this.cordova.getActivity().getContentResolver().insert(android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI, values);
+ } catch (UnsupportedOperationException ex) {
+ LOG.d(LOG_TAG, "Can't write to internal media storage.");
+ return null;
+ }
+ }
+ return uri;
+ }
+
+ /**
+ * Return a scaled bitmap based on the target width and height
+ *
+ * @param imagePath
+ * @return
+ */
+ private Bitmap getScaledBitmap(String imagePath) {
+ // If no new width or height were specified return the original bitmap
+ if (this.targetWidth <= 0 && this.targetHeight <= 0) {
+ return BitmapFactory.decodeFile(imagePath);
+ }
+
+ // figure out the original width and height of the image
+ BitmapFactory.Options options = new BitmapFactory.Options();
+ options.inJustDecodeBounds = true;
+ BitmapFactory.decodeFile(imagePath, options);
+
+ // determine the correct aspect ratio
+ int[] widthHeight = calculateAspectRatio(options.outWidth, options.outHeight);
+
+ // Load in the smallest bitmap possible that is closest to the size we want
+ options.inJustDecodeBounds = false;
+ options.inSampleSize = calculateSampleSize(options.outWidth, options.outHeight, this.targetWidth, this.targetHeight);
+ Bitmap unscaledBitmap = BitmapFactory.decodeFile(imagePath, options);
+ if (unscaledBitmap == null) {
+ return null;
+ }
+
+ return Bitmap.createScaledBitmap(unscaledBitmap, widthHeight[0], widthHeight[1], true);
+ }
+
+ /**
+ * Maintain the aspect ratio so the resulting image does not look smooshed
+ *
+ * @param origWidth
+ * @param origHeight
+ * @return
+ */
+ public int[] calculateAspectRatio(int origWidth, int origHeight) {
+ int newWidth = this.targetWidth;
+ int newHeight = this.targetHeight;
+
+ // If no new width or height were specified return the original bitmap
+ if (newWidth <= 0 && newHeight <= 0) {
+ newWidth = origWidth;
+ newHeight = origHeight;
+ }
+ // Only the width was specified
+ else if (newWidth > 0 && newHeight <= 0) {
+ newHeight = (newWidth * origHeight) / origWidth;
+ }
+ // only the height was specified
+ else if (newWidth <= 0 && newHeight > 0) {
+ newWidth = (newHeight * origWidth) / origHeight;
+ }
+ // If the user specified both a positive width and height
+ // (potentially different aspect ratio) then the width or height is
+ // scaled so that the image fits while maintaining aspect ratio.
+ // Alternatively, the specified width and height could have been
+ // kept and Bitmap.SCALE_TO_FIT specified when scaling, but this
+ // would result in whitespace in the new image.
+ else {
+ double newRatio = newWidth / (double) newHeight;
+ double origRatio = origWidth / (double) origHeight;
+
+ if (origRatio > newRatio) {
+ newHeight = (newWidth * origHeight) / origWidth;
+ } else if (origRatio < newRatio) {
+ newWidth = (newHeight * origWidth) / origHeight;
+ }
+ }
+
+ int[] retval = new int[2];
+ retval[0] = newWidth;
+ retval[1] = newHeight;
+ return retval;
+ }
+
+ /**
+ * Figure out what ratio we can load our image into memory at while still being bigger than
+ * our desired width and height
+ *
+ * @param srcWidth
+ * @param srcHeight
+ * @param dstWidth
+ * @param dstHeight
+ * @return
+ */
+ public static int calculateSampleSize(int srcWidth, int srcHeight, int dstWidth, int dstHeight) {
+ final float srcAspect = (float)srcWidth / (float)srcHeight;
+ final float dstAspect = (float)dstWidth / (float)dstHeight;
+
+ if (srcAspect > dstAspect) {
+ return srcWidth / dstWidth;
+ } else {
+ return srcHeight / dstHeight;
+ }
+ }
+
+ /**
+ * Creates a cursor that can be used to determine how many images we have.
+ *
+ * @return a cursor
+ */
+ private Cursor queryImgDB(Uri contentStore) {
+ return this.cordova.getActivity().getContentResolver().query(
+ contentStore,
+ new String[] { MediaStore.Images.Media._ID },
+ null,
+ null,
+ null);
+ }
+
+ /**
+ * Cleans up after picture taking. Checking for duplicates and that kind of stuff.
+ * @param newImage
+ */
+ private void cleanup(int imageType, Uri oldImage, Uri newImage, Bitmap bitmap) {
+ if (bitmap != null) {
+ bitmap.recycle();
+ }
+
+ // Clean up initial camera-written image file.
+ (new File(FileUtils.stripFileProtocol(oldImage.toString()))).delete();
+
+ checkForDuplicateImage(imageType);
+ // Scan for the gallery to update pic refs in gallery
+ if (this.saveToPhotoAlbum && newImage != null) {
+ this.scanForGallery(newImage);
+ }
+
+ System.gc();
+ }
+
+ /**
+ * Used to find out if we are in a situation where the Camera Intent adds to images
+ * to the content store. If we are using a FILE_URI and the number of images in the DB
+ * increases by 2 we have a duplicate, when using a DATA_URL the number is 1.
+ *
+ * @param type FILE_URI or DATA_URL
+ */
+ private void checkForDuplicateImage(int type) {
+ int diff = 1;
+ Uri contentStore = whichContentStore();
+ Cursor cursor = queryImgDB(contentStore);
+ int currentNumOfImages = cursor.getCount();
+
+ if (type == FILE_URI && this.saveToPhotoAlbum) {
+ diff = 2;
+ }
+
+ // delete the duplicate file if the difference is 2 for file URI or 1 for Data URL
+ if ((currentNumOfImages - numPics) == diff) {
+ cursor.moveToLast();
+ int id = Integer.valueOf(cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media._ID)));
+ if (diff == 2) {
+ id--;
+ }
+ Uri uri = Uri.parse(contentStore + "/" + id);
+ this.cordova.getActivity().getContentResolver().delete(uri, null, null);
+ }
+ }
+
+ /**
+ * Determine if we are storing the images in internal or external storage
+ * @return Uri
+ */
+ private Uri whichContentStore() {
+ if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
+ return android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
+ } else {
+ return android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI;
+ }
+ }
+
+ /**
+ * Compress bitmap using jpeg, convert to Base64 encoded string, and return to JavaScript.
+ *
+ * @param bitmap
+ */
+ public void processPicture(Bitmap bitmap) {
+ ByteArrayOutputStream jpeg_data = new ByteArrayOutputStream();
+ try {
+ if (bitmap.compress(CompressFormat.JPEG, mQuality, jpeg_data)) {
+ byte[] code = jpeg_data.toByteArray();
+ byte[] output = Base64.encodeBase64(code);
+ String js_out = new String(output);
+ this.callbackContext.success(js_out);
+ js_out = null;
+ output = null;
+ code = null;
+ }
+ } catch (Exception e) {
+ this.failPicture("Error compressing image.");
+ }
+ jpeg_data = null;
+ }
+
+ /**
+ * Send error message to JavaScript.
+ *
+ * @param err
+ */
+ public void failPicture(String err) {
+ this.callbackContext.error(err);
+ }
+
+ private void scanForGallery(Uri newImage) {
+ this.scanMe = newImage;
+ if(this.conn != null) {
+ this.conn.disconnect();
+ }
+ this.conn = new MediaScannerConnection(this.cordova.getActivity().getApplicationContext(), this);
+ conn.connect();
+ }
+
+ public void onMediaScannerConnected() {
+ try{
+ this.conn.scanFile(this.scanMe.toString(), "image/*");
+ } catch (java.lang.IllegalStateException e){
+ LOG.e(LOG_TAG, "Can't scan file in MediaScanner after taking picture");
+ }
+
+ }
+
+ public void onScanCompleted(String path, Uri uri) {
+ this.conn.disconnect();
+ }
+}
http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/d61deccd/lib/cordova-android/framework/src/org/apache/cordova/Capture.java
----------------------------------------------------------------------
diff --git a/lib/cordova-android/framework/src/org/apache/cordova/Capture.java b/lib/cordova-android/framework/src/org/apache/cordova/Capture.java
new file mode 100644
index 0000000..3eecf37
--- /dev/null
+++ b/lib/cordova-android/framework/src/org/apache/cordova/Capture.java
@@ -0,0 +1,443 @@
+/*
+ Licensed to the Apache Software Foundation (ASF) under one
+ or more contributor license agreements. See the NOTICE file
+ distributed with this work for additional information
+ regarding copyright ownership. The ASF licenses this file
+ to you under the Apache License, Version 2.0 (the
+ "License"); you may not use this file except in compliance
+ with the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing,
+ software distributed under the License is distributed on an
+ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ KIND, either express or implied. See the License for the
+ specific language governing permissions and limitations
+ under the License.
+*/
+package org.apache.cordova;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+
+import org.apache.cordova.api.CallbackContext;
+import org.apache.cordova.api.CordovaPlugin;
+import org.apache.cordova.api.LOG;
+import org.apache.cordova.api.PluginResult;
+import org.json.JSONArray;
+import org.json.JSONException;
+import org.json.JSONObject;
+
+import android.app.Activity;
+import android.content.ContentValues;
+import android.content.Intent;
+import android.database.Cursor;
+import android.graphics.BitmapFactory;
+import android.media.MediaPlayer;
+import android.net.Uri;
+import android.os.Environment;
+import android.provider.MediaStore;
+import android.util.Log;
+
+public class Capture extends CordovaPlugin {
+
+ private static final String VIDEO_3GPP = "video/3gpp";
+ private static final String VIDEO_MP4 = "video/mp4";
+ private static final String AUDIO_3GPP = "audio/3gpp";
+ private static final String IMAGE_JPEG = "image/jpeg";
+
+ private static final int CAPTURE_AUDIO = 0; // Constant for capture audio
+ private static final int CAPTURE_IMAGE = 1; // Constant for capture image
+ private static final int CAPTURE_VIDEO = 2; // Constant for capture video
+ private static final String LOG_TAG = "Capture";
+
+ private static final int CAPTURE_INTERNAL_ERR = 0;
+// private static final int CAPTURE_APPLICATION_BUSY = 1;
+// private static final int CAPTURE_INVALID_ARGUMENT = 2;
+ private static final int CAPTURE_NO_MEDIA_FILES = 3;
+
+ private CallbackContext callbackContext; // The callback context from which we were invoked.
+ private long limit; // the number of pics/vids/clips to take
+ private double duration; // optional duration parameter for video recording
+ private JSONArray results; // The array of results to be returned to the user
+ private int numPics; // Number of pictures before capture activity
+
+ //private CordovaInterface cordova;
+
+// public void setContext(Context mCtx)
+// {
+// if (CordovaInterface.class.isInstance(mCtx))
+// cordova = (CordovaInterface) mCtx;
+// else
+// LOG.d(LOG_TAG, "ERROR: You must use the CordovaInterface for this to work correctly. Please implement it in your activity");
+// }
+
+ @Override
+ public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
+ this.callbackContext = callbackContext;
+ this.limit = 1;
+ this.duration = 0.0f;
+ this.results = new JSONArray();
+
+ JSONObject options = args.optJSONObject(0);
+ if (options != null) {
+ limit = options.optLong("limit", 1);
+ duration = options.optDouble("duration", 0.0f);
+ }
+
+ if (action.equals("getFormatData")) {
+ JSONObject obj = getFormatData(args.getString(0), args.getString(1));
+ callbackContext.success(obj);
+ return true;
+ }
+ else if (action.equals("captureAudio")) {
+ this.captureAudio();
+ }
+ else if (action.equals("captureImage")) {
+ this.captureImage();
+ }
+ else if (action.equals("captureVideo")) {
+ this.captureVideo(duration);
+ }
+ else {
+ return false;
+ }
+
+ return true;
+ }
+
+ /**
+ * Provides the media data file data depending on it's mime type
+ *
+ * @param filePath path to the file
+ * @param mimeType of the file
+ * @return a MediaFileData object
+ */
+ private JSONObject getFormatData(String filePath, String mimeType) throws JSONException {
+ JSONObject obj = new JSONObject();
+ // setup defaults
+ obj.put("height", 0);
+ obj.put("width", 0);
+ obj.put("bitrate", 0);
+ obj.put("duration", 0);
+ obj.put("codecs", "");
+
+ // If the mimeType isn't set the rest will fail
+ // so let's see if we can determine it.
+ if (mimeType == null || mimeType.equals("") || "null".equals(mimeType)) {
+ mimeType = FileUtils.getMimeType(filePath);
+ }
+ Log.d(LOG_TAG, "Mime type = " + mimeType);
+
+ if (mimeType.equals(IMAGE_JPEG) || filePath.endsWith(".jpg")) {
+ obj = getImageData(filePath, obj);
+ }
+ else if (mimeType.endsWith(AUDIO_3GPP)) {
+ obj = getAudioVideoData(filePath, obj, false);
+ }
+ else if (mimeType.equals(VIDEO_3GPP) || mimeType.equals(VIDEO_MP4)) {
+ obj = getAudioVideoData(filePath, obj, true);
+ }
+ return obj;
+ }
+
+ /**
+ * Get the Image specific attributes
+ *
+ * @param filePath path to the file
+ * @param obj represents the Media File Data
+ * @return a JSONObject that represents the Media File Data
+ * @throws JSONException
+ */
+ private JSONObject getImageData(String filePath, JSONObject obj) throws JSONException {
+ BitmapFactory.Options options = new BitmapFactory.Options();
+ options.inJustDecodeBounds = true;
+ BitmapFactory.decodeFile(FileUtils.stripFileProtocol(filePath), options);
+ obj.put("height", options.outHeight);
+ obj.put("width", options.outWidth);
+ return obj;
+ }
+
+ /**
+ * Get the Image specific attributes
+ *
+ * @param filePath path to the file
+ * @param obj represents the Media File Data
+ * @param video if true get video attributes as well
+ * @return a JSONObject that represents the Media File Data
+ * @throws JSONException
+ */
+ private JSONObject getAudioVideoData(String filePath, JSONObject obj, boolean video) throws JSONException {
+ MediaPlayer player = new MediaPlayer();
+ try {
+ player.setDataSource(filePath);
+ player.prepare();
+ obj.put("duration", player.getDuration() / 1000);
+ if (video) {
+ obj.put("height", player.getVideoHeight());
+ obj.put("width", player.getVideoWidth());
+ }
+ } catch (IOException e) {
+ Log.d(LOG_TAG, "Error: loading video file");
+ }
+ return obj;
+ }
+
+ /**
+ * Sets up an intent to capture audio. Result handled by onActivityResult()
+ */
+ private void captureAudio() {
+ Intent intent = new Intent(android.provider.MediaStore.Audio.Media.RECORD_SOUND_ACTION);
+
+ this.cordova.startActivityForResult((CordovaPlugin) this, intent, CAPTURE_AUDIO);
+ }
+
+ /**
+ * Sets up an intent to capture images. Result handled by onActivityResult()
+ */
+ private void captureImage() {
+ // Save the number of images currently on disk for later
+ this.numPics = queryImgDB(whichContentStore()).getCount();
+
+ Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
+
+ // Specify file so that large image is captured and returned
+ File photo = new File(DirectoryManager.getTempDirectoryPath(this.cordova.getActivity()), "Capture.jpg");
+ intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
+
+ this.cordova.startActivityForResult((CordovaPlugin) this, intent, CAPTURE_IMAGE);
+ }
+
+ /**
+ * Sets up an intent to capture video. Result handled by onActivityResult()
+ */
+ private void captureVideo(double duration) {
+ Intent intent = new Intent(android.provider.MediaStore.ACTION_VIDEO_CAPTURE);
+ // Introduced in API 8
+ //intent.putExtra(android.provider.MediaStore.EXTRA_DURATION_LIMIT, duration);
+
+ this.cordova.startActivityForResult((CordovaPlugin) this, intent, CAPTURE_VIDEO);
+ }
+
+ /**
+ * Called when the video view exits.
+ *
+ * @param requestCode The request code originally supplied to startActivityForResult(),
+ * allowing you to identify who this result came from.
+ * @param resultCode The integer result code returned by the child activity through its setResult().
+ * @param intent An Intent, which can return result data to the caller (various data can be attached to Intent "extras").
+ * @throws JSONException
+ */
+ public void onActivityResult(int requestCode, int resultCode, Intent intent) {
+
+ // Result received okay
+ if (resultCode == Activity.RESULT_OK) {
+ // An audio clip was requested
+ if (requestCode == CAPTURE_AUDIO) {
+ // Get the uri of the audio clip
+ Uri data = intent.getData();
+ // create a file object from the uri
+ results.put(createMediaFile(data));
+
+ if (results.length() >= limit) {
+ // Send Uri back to JavaScript for listening to audio
+ this.callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, results));
+ } else {
+ // still need to capture more audio clips
+ captureAudio();
+ }
+ } else if (requestCode == CAPTURE_IMAGE) {
+ // For some reason if I try to do:
+ // Uri data = intent.getData();
+ // It crashes in the emulator and on my phone with a null pointer exception
+ // To work around it I had to grab the code from CameraLauncher.java
+ try {
+ // Create entry in media store for image
+ // (Don't use insertImage() because it uses default compression setting of 50 - no way to change it)
+ ContentValues values = new ContentValues();
+ values.put(android.provider.MediaStore.Images.Media.MIME_TYPE, IMAGE_JPEG);
+ Uri uri = null;
+ try {
+ uri = this.cordova.getActivity().getContentResolver().insert(android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
+ } catch (UnsupportedOperationException e) {
+ LOG.d(LOG_TAG, "Can't write to external media storage.");
+ try {
+ uri = this.cordova.getActivity().getContentResolver().insert(android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI, values);
+ } catch (UnsupportedOperationException ex) {
+ LOG.d(LOG_TAG, "Can't write to internal media storage.");
+ this.fail(createErrorObject(CAPTURE_INTERNAL_ERR, "Error capturing image - no media storage found."));
+ return;
+ }
+ }
+ FileInputStream fis = new FileInputStream(DirectoryManager.getTempDirectoryPath(this.cordova.getActivity()) + "/Capture.jpg");
+ OutputStream os = this.cordova.getActivity().getContentResolver().openOutputStream(uri);
+ byte[] buffer = new byte[4096];
+ int len;
+ while ((len = fis.read(buffer)) != -1) {
+ os.write(buffer, 0, len);
+ }
+ os.flush();
+ os.close();
+ fis.close();
+
+ // Add image to results
+ results.put(createMediaFile(uri));
+
+ checkForDuplicateImage();
+
+ if (results.length() >= limit) {
+ // Send Uri back to JavaScript for viewing image
+ this.callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, results));
+ } else {
+ // still need to capture more images
+ captureImage();
+ }
+ } catch (IOException e) {
+ e.printStackTrace();
+ this.fail(createErrorObject(CAPTURE_INTERNAL_ERR, "Error capturing image."));
+ }
+ } else if (requestCode == CAPTURE_VIDEO) {
+ // Get the uri of the video clip
+ Uri data = intent.getData();
+ // create a file object from the uri
+ results.put(createMediaFile(data));
+
+ if (results.length() >= limit) {
+ // Send Uri back to JavaScript for viewing video
+ this.callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, results));
+ } else {
+ // still need to capture more video clips
+ captureVideo(duration);
+ }
+ }
+ }
+ // If canceled
+ else if (resultCode == Activity.RESULT_CANCELED) {
+ // If we have partial results send them back to the user
+ if (results.length() > 0) {
+ this.callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, results));
+ }
+ // user canceled the action
+ else {
+ this.fail(createErrorObject(CAPTURE_NO_MEDIA_FILES, "Canceled."));
+ }
+ }
+ // If something else
+ else {
+ // If we have partial results send them back to the user
+ if (results.length() > 0) {
+ this.callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, results));
+ }
+ // something bad happened
+ else {
+ this.fail(createErrorObject(CAPTURE_NO_MEDIA_FILES, "Did not complete!"));
+ }
+ }
+ }
+
+ /**
+ * Creates a JSONObject that represents a File from the Uri
+ *
+ * @param data the Uri of the audio/image/video
+ * @return a JSONObject that represents a File
+ * @throws IOException
+ */
+ private JSONObject createMediaFile(Uri data) {
+ File fp = new File(FileUtils.getRealPathFromURI(data, this.cordova));
+ JSONObject obj = new JSONObject();
+
+ try {
+ // File properties
+ obj.put("name", fp.getName());
+ obj.put("fullPath", "file://" + fp.getAbsolutePath());
+ // Because of an issue with MimeTypeMap.getMimeTypeFromExtension() all .3gpp files
+ // are reported as video/3gpp. I'm doing this hacky check of the URI to see if it
+ // is stored in the audio or video content store.
+ if (fp.getAbsoluteFile().toString().endsWith(".3gp") || fp.getAbsoluteFile().toString().endsWith(".3gpp")) {
+ if (data.toString().contains("/audio/")) {
+ obj.put("type", AUDIO_3GPP);
+ } else {
+ obj.put("type", VIDEO_3GPP);
+ }
+ } else {
+ obj.put("type", FileUtils.getMimeType(fp.getAbsolutePath()));
+ }
+
+ obj.put("lastModifiedDate", fp.lastModified());
+ obj.put("size", fp.length());
+ } catch (JSONException e) {
+ // this will never happen
+ e.printStackTrace();
+ }
+
+ return obj;
+ }
+
+ private JSONObject createErrorObject(int code, String message) {
+ JSONObject obj = new JSONObject();
+ try {
+ obj.put("code", code);
+ obj.put("message", message);
+ } catch (JSONException e) {
+ // This will never happen
+ }
+ return obj;
+ }
+
+ /**
+ * Send error message to JavaScript.
+ *
+ * @param err
+ */
+ public void fail(JSONObject err) {
+ this.callbackContext.error(err);
+ }
+
+
+ /**
+ * Creates a cursor that can be used to determine how many images we have.
+ *
+ * @return a cursor
+ */
+ private Cursor queryImgDB(Uri contentStore) {
+ return this.cordova.getActivity().getContentResolver().query(
+ contentStore,
+ new String[] { MediaStore.Images.Media._ID },
+ null,
+ null,
+ null);
+ }
+
+ /**
+ * Used to find out if we are in a situation where the Camera Intent adds to images
+ * to the content store.
+ */
+ private void checkForDuplicateImage() {
+ Uri contentStore = whichContentStore();
+ Cursor cursor = queryImgDB(contentStore);
+ int currentNumOfImages = cursor.getCount();
+
+ // delete the duplicate file if the difference is 2
+ if ((currentNumOfImages - numPics) == 2) {
+ cursor.moveToLast();
+ int id = Integer.valueOf(cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media._ID))) - 1;
+ Uri uri = Uri.parse(contentStore + "/" + id);
+ this.cordova.getActivity().getContentResolver().delete(uri, null, null);
+ }
+ }
+
+ /**
+ * Determine if we are storing the images in internal or external storage
+ * @return Uri
+ */
+ private Uri whichContentStore() {
+ if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
+ return android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
+ } else {
+ return android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI;
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/d61deccd/lib/cordova-android/framework/src/org/apache/cordova/CompassListener.java
----------------------------------------------------------------------
diff --git a/lib/cordova-android/framework/src/org/apache/cordova/CompassListener.java b/lib/cordova-android/framework/src/org/apache/cordova/CompassListener.java
new file mode 100755
index 0000000..401e053
--- /dev/null
+++ b/lib/cordova-android/framework/src/org/apache/cordova/CompassListener.java
@@ -0,0 +1,295 @@
+/*
+ Licensed to the Apache Software Foundation (ASF) under one
+ or more contributor license agreements. See the NOTICE file
+ distributed with this work for additional information
+ regarding copyright ownership. The ASF licenses this file
+ to you under the Apache License, Version 2.0 (the
+ "License"); you may not use this file except in compliance
+ with the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing,
+ software distributed under the License is distributed on an
+ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ KIND, either express or implied. See the License for the
+ specific language governing permissions and limitations
+ under the License.
+*/
+package org.apache.cordova;
+
+import java.util.List;
+
+import org.apache.cordova.api.CallbackContext;
+import org.apache.cordova.api.CordovaInterface;
+import org.apache.cordova.api.CordovaPlugin;
+import org.apache.cordova.api.PluginResult;
+import org.json.JSONArray;
+import org.json.JSONException;
+import org.json.JSONObject;
+
+import android.hardware.Sensor;
+import android.hardware.SensorEvent;
+import android.hardware.SensorEventListener;
+import android.hardware.SensorManager;
+import android.content.Context;
+
+import android.os.Handler;
+import android.os.Looper;
+
+/**
+ * This class listens to the compass sensor and stores the latest heading value.
+ */
+public class CompassListener extends CordovaPlugin implements SensorEventListener {
+
+ public static int STOPPED = 0;
+ public static int STARTING = 1;
+ public static int RUNNING = 2;
+ public static int ERROR_FAILED_TO_START = 3;
+
+ public long TIMEOUT = 30000; // Timeout in msec to shut off listener
+
+ int status; // status of listener
+ float heading; // most recent heading value
+ long timeStamp; // time of most recent value
+ long lastAccessTime; // time the value was last retrieved
+ int accuracy; // accuracy of the sensor
+
+ private SensorManager sensorManager;// Sensor manager
+ Sensor mSensor; // Compass sensor returned by sensor manager
+
+ private CallbackContext callbackContext;
+
+ /**
+ * Constructor.
+ */
+ public CompassListener() {
+ this.heading = 0;
+ this.timeStamp = 0;
+ this.setStatus(CompassListener.STOPPED);
+ }
+
+ /**
+ * Sets the context of the Command. This can then be used to do things like
+ * get file paths associated with the Activity.
+ *
+ * @param cordova The context of the main Activity.
+ * @param webView The CordovaWebView Cordova is running in.
+ */
+ public void initialize(CordovaInterface cordova, CordovaWebView webView) {
+ super.initialize(cordova, webView);
+ this.sensorManager = (SensorManager) cordova.getActivity().getSystemService(Context.SENSOR_SERVICE);
+ }
+
+ /**
+ * Executes the request and returns PluginResult.
+ *
+ * @param action The action to execute.
+ * @param args JSONArry of arguments for the plugin.
+ * @param callbackS=Context The callback id used when calling back into JavaScript.
+ * @return True if the action was valid.
+ * @throws JSONException
+ */
+ public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
+ if (action.equals("start")) {
+ this.start();
+ }
+ else if (action.equals("stop")) {
+ this.stop();
+ }
+ else if (action.equals("getStatus")) {
+ int i = this.getStatus();
+ callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, i));
+ }
+ else if (action.equals("getHeading")) {
+ // If not running, then this is an async call, so don't worry about waiting
+ if (this.status != CompassListener.RUNNING) {
+ int r = this.start();
+ if (r == CompassListener.ERROR_FAILED_TO_START) {
+ callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.IO_EXCEPTION, CompassListener.ERROR_FAILED_TO_START));
+ return true;
+ }
+ // Set a timeout callback on the main thread.
+ Handler handler = new Handler(Looper.getMainLooper());
+ handler.postDelayed(new Runnable() {
+ public void run() {
+ CompassListener.this.timeout();
+ }
+ }, 2000);
+ }
+ callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, getCompassHeading()));
+ }
+ else if (action.equals("setTimeout")) {
+ this.setTimeout(args.getLong(0));
+ }
+ else if (action.equals("getTimeout")) {
+ long l = this.getTimeout();
+ callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, l));
+ } else {
+ // Unsupported action
+ return false;
+ }
+ return true;
+ }
+
+ /**
+ * Called when listener is to be shut down and object is being destroyed.
+ */
+ public void onDestroy() {
+ this.stop();
+ }
+
+ /**
+ * Called when app has navigated and JS listeners have been destroyed.
+ */
+ public void onReset() {
+ this.stop();
+ }
+
+ //--------------------------------------------------------------------------
+ // LOCAL METHODS
+ //--------------------------------------------------------------------------
+
+ /**
+ * Start listening for compass sensor.
+ *
+ * @return status of listener
+ */
+ public int start() {
+
+ // If already starting or running, then just return
+ if ((this.status == CompassListener.RUNNING) || (this.status == CompassListener.STARTING)) {
+ return this.status;
+ }
+
+ // Get compass sensor from sensor manager
+ @SuppressWarnings("deprecation")
+ List<Sensor> list = this.sensorManager.getSensorList(Sensor.TYPE_ORIENTATION);
+
+ // If found, then register as listener
+ if (list != null && list.size() > 0) {
+ this.mSensor = list.get(0);
+ this.sensorManager.registerListener(this, this.mSensor, SensorManager.SENSOR_DELAY_NORMAL);
+ this.lastAccessTime = System.currentTimeMillis();
+ this.setStatus(CompassListener.STARTING);
+ }
+
+ // If error, then set status to error
+ else {
+ this.setStatus(CompassListener.ERROR_FAILED_TO_START);
+ }
+
+ return this.status;
+ }
+
+ /**
+ * Stop listening to compass sensor.
+ */
+ public void stop() {
+ if (this.status != CompassListener.STOPPED) {
+ this.sensorManager.unregisterListener(this);
+ }
+ this.setStatus(CompassListener.STOPPED);
+ }
+
+ public void onAccuracyChanged(Sensor sensor, int accuracy) {
+ // TODO Auto-generated method stub
+ }
+
+ /**
+ * Called after a delay to time out if the listener has not attached fast enough.
+ */
+ private void timeout() {
+ if (this.status == CompassListener.STARTING) {
+ this.setStatus(CompassListener.ERROR_FAILED_TO_START);
+ if (this.callbackContext != null) {
+ this.callbackContext.error("Compass listener failed to start.");
+ }
+ }
+ }
+
+ /**
+ * Sensor listener event.
+ *
+ * @param SensorEvent event
+ */
+ public void onSensorChanged(SensorEvent event) {
+
+ // We only care about the orientation as far as it refers to Magnetic North
+ float heading = event.values[0];
+
+ // Save heading
+ this.timeStamp = System.currentTimeMillis();
+ this.heading = heading;
+ this.setStatus(CompassListener.RUNNING);
+
+ // If heading hasn't been read for TIMEOUT time, then turn off compass sensor to save power
+ if ((this.timeStamp - this.lastAccessTime) > this.TIMEOUT) {
+ this.stop();
+ }
+ }
+
+ /**
+ * Get status of compass sensor.
+ *
+ * @return status
+ */
+ public int getStatus() {
+ return this.status;
+ }
+
+ /**
+ * Get the most recent compass heading.
+ *
+ * @return heading
+ */
+ public float getHeading() {
+ this.lastAccessTime = System.currentTimeMillis();
+ return this.heading;
+ }
+
+ /**
+ * Set the timeout to turn off compass sensor if getHeading() hasn't been called.
+ *
+ * @param timeout Timeout in msec.
+ */
+ public void setTimeout(long timeout) {
+ this.TIMEOUT = timeout;
+ }
+
+ /**
+ * Get the timeout to turn off compass sensor if getHeading() hasn't been called.
+ *
+ * @return timeout in msec
+ */
+ public long getTimeout() {
+ return this.TIMEOUT;
+ }
+
+ /**
+ * Set the status and send it to JavaScript.
+ * @param status
+ */
+ private void setStatus(int status) {
+ this.status = status;
+ }
+
+ /**
+ * Create the CompassHeading JSON object to be returned to JavaScript
+ *
+ * @return a compass heading
+ */
+ private JSONObject getCompassHeading() throws JSONException {
+ JSONObject obj = new JSONObject();
+
+ obj.put("magneticHeading", this.getHeading());
+ obj.put("trueHeading", this.getHeading());
+ // Since the magnetic and true heading are always the same our and accuracy
+ // is defined as the difference between true and magnetic always return zero
+ obj.put("headingAccuracy", 0);
+ obj.put("timestamp", this.timeStamp);
+
+ return obj;
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/d61deccd/lib/cordova-android/framework/src/org/apache/cordova/Config.java
----------------------------------------------------------------------
diff --git a/lib/cordova-android/framework/src/org/apache/cordova/Config.java b/lib/cordova-android/framework/src/org/apache/cordova/Config.java
new file mode 100644
index 0000000..8353668
--- /dev/null
+++ b/lib/cordova-android/framework/src/org/apache/cordova/Config.java
@@ -0,0 +1,228 @@
+/*
+ Licensed to the Apache Software Foundation (ASF) under one
+ or more contributor license agreements. See the NOTICE file
+ distributed with this work for additional information
+ regarding copyright ownership. The ASF licenses this file
+ to you under the Apache License, Version 2.0 (the
+ "License"); you may not use this file except in compliance
+ with the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing,
+ software distributed under the License is distributed on an
+ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ KIND, either express or implied. See the License for the
+ specific language governing permissions and limitations
+ under the License.
+*/
+
+package org.apache.cordova;
+
+import java.io.IOException;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Iterator;
+
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import org.apache.cordova.api.LOG;
+
+import org.xmlpull.v1.XmlPullParserException;
+
+import android.app.Activity;
+
+import android.content.res.XmlResourceParser;
+
+import android.util.Log;
+
+public class Config {
+
+ public static final String TAG = "Config";
+
+ private ArrayList<Pattern> whiteList = new ArrayList<Pattern>();
+ private HashMap<String, Boolean> whiteListCache = new HashMap<String, Boolean>();
+ private String startUrl;
+
+ private static Config self = null;
+
+ public static void init(Activity action) {
+ if (self == null) {
+ self = new Config(action);
+ }
+ }
+
+ // Intended to be used for testing only; creates an empty configuration.
+ public static void init() {
+ if (self == null) {
+ self = new Config();
+ }
+ }
+
+ // Intended to be used for testing only; creates an empty configuration.
+ private Config() {
+ }
+
+ private Config(Activity action) {
+ if (action == null) {
+ LOG.i("CordovaLog", "There is no activity. Is this on the lock screen?");
+ return;
+ }
+
+ int id = action.getResources().getIdentifier("config", "xml", action.getPackageName());
+ if (id == 0) {
+ id = action.getResources().getIdentifier("cordova", "xml", action.getPackageName());
+ LOG.i("CordovaLog", "config.xml missing, reverting to cordova.xml");
+ }
+ if (id == 0) {
+ LOG.i("CordovaLog", "cordova.xml missing. Ignoring...");
+ return;
+ }
+
+ XmlResourceParser xml = action.getResources().getXml(id);
+ int eventType = -1;
+ while (eventType != XmlResourceParser.END_DOCUMENT) {
+ if (eventType == XmlResourceParser.START_TAG) {
+ String strNode = xml.getName();
+
+ if (strNode.equals("access")) {
+ String origin = xml.getAttributeValue(null, "origin");
+ String subdomains = xml.getAttributeValue(null, "subdomains");
+ if (origin != null) {
+ this._addWhiteListEntry(origin, (subdomains != null) && (subdomains.compareToIgnoreCase("true") == 0));
+ }
+ }
+ else if (strNode.equals("log")) {
+ String level = xml.getAttributeValue(null, "level");
+ LOG.i("CordovaLog", "Found log level %s", level);
+ if (level != null) {
+ LOG.setLogLevel(level);
+ }
+ }
+ else if (strNode.equals("preference")) {
+ String name = xml.getAttributeValue(null, "name");
+ String value = xml.getAttributeValue(null, "value");
+
+ LOG.i("CordovaLog", "Found preference for %s=%s", name, value);
+ Log.d("CordovaLog", "Found preference for " + name + "=" + value);
+
+ action.getIntent().putExtra(name, value);
+ }
+ else if (strNode.equals("content")) {
+ String src = xml.getAttributeValue(null, "src");
+
+ LOG.i("CordovaLog", "Found start page location: %s", src);
+
+ if (src != null) {
+ Pattern schemeRegex = Pattern.compile("^[a-z]+://");
+ Matcher matcher = schemeRegex.matcher(src);
+ if (matcher.find()) {
+ startUrl = src;
+ } else {
+ if (src.charAt(0) == '/') {
+ src = src.substring(1);
+ }
+ startUrl = "file:///android_asset/www/" + src;
+ }
+ }
+ }
+
+ }
+
+ try {
+ eventType = xml.next();
+ } catch (XmlPullParserException e) {
+ e.printStackTrace();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ }
+
+ /**
+ * Add entry to approved list of URLs (whitelist)
+ *
+ * @param origin URL regular expression to allow
+ * @param subdomains T=include all subdomains under origin
+ */
+ public static void addWhiteListEntry(String origin, boolean subdomains) {
+ if (self == null) {
+ return;
+ }
+
+ self._addWhiteListEntry(origin, subdomains);
+ }
+
+
+ private void _addWhiteListEntry(String origin, boolean subdomains) {
+ try {
+ // Unlimited access to network resources
+ if (origin.compareTo("*") == 0) {
+ LOG.d(TAG, "Unlimited access to network resources");
+ this.whiteList.add(Pattern.compile(".*"));
+ } else { // specific access
+ // check if subdomains should be included
+ // TODO: we should not add more domains if * has already been added
+ if (subdomains) {
+ // XXX making it stupid friendly for people who forget to include protocol/SSL
+ if (origin.startsWith("http")) {
+ this.whiteList.add(Pattern.compile(origin.replaceFirst("https?://", "^https?://(.*\\.)?")));
+ } else {
+ this.whiteList.add(Pattern.compile("^https?://(.*\\.)?" + origin));
+ }
+ LOG.d(TAG, "Origin to allow with subdomains: %s", origin);
+ } else {
+ // XXX making it stupid friendly for people who forget to include protocol/SSL
+ if (origin.startsWith("http")) {
+ this.whiteList.add(Pattern.compile(origin.replaceFirst("https?://", "^https?://")));
+ } else {
+ this.whiteList.add(Pattern.compile("^https?://" + origin));
+ }
+ LOG.d(TAG, "Origin to allow: %s", origin);
+ }
+ }
+ } catch (Exception e) {
+ LOG.d(TAG, "Failed to add origin %s", origin);
+ }
+ }
+
+ /**
+ * Determine if URL is in approved list of URLs to load.
+ *
+ * @param url
+ * @return
+ */
+ public static boolean isUrlWhiteListed(String url) {
+ if (self == null) {
+ return false;
+ }
+
+ // Check to see if we have matched url previously
+ if (self.whiteListCache.get(url) != null) {
+ return true;
+ }
+
+ // Look for match in white list
+ Iterator<Pattern> pit = self.whiteList.iterator();
+ while (pit.hasNext()) {
+ Pattern p = pit.next();
+ Matcher m = p.matcher(url);
+
+ // If match found, then cache it to speed up subsequent comparisons
+ if (m.find()) {
+ self.whiteListCache.put(url, true);
+ return true;
+ }
+ }
+ return false;
+ }
+
+ public static String getStartUrl() {
+ if (self == null || self.startUrl == null) {
+ return "file:///android_asset/www/index.html";
+ }
+ return self.startUrl;
+ }
+}
http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/d61deccd/lib/cordova-android/framework/src/org/apache/cordova/ContactAccessor.java
----------------------------------------------------------------------
diff --git a/lib/cordova-android/framework/src/org/apache/cordova/ContactAccessor.java b/lib/cordova-android/framework/src/org/apache/cordova/ContactAccessor.java
new file mode 100644
index 0000000..44bed23
--- /dev/null
+++ b/lib/cordova-android/framework/src/org/apache/cordova/ContactAccessor.java
@@ -0,0 +1,199 @@
+/*
+ * Copyright (C) 2009 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.cordova;
+
+import java.util.HashMap;
+
+import android.content.Context;
+import android.util.Log;
+import android.webkit.WebView;
+
+import org.apache.cordova.api.CordovaInterface;
+import org.json.JSONArray;
+import org.json.JSONException;
+import org.json.JSONObject;
+
+/**
+ * This abstract class defines SDK-independent API for communication with
+ * Contacts Provider. The actual implementation used by the application depends
+ * on the level of API available on the device. If the API level is Cupcake or
+ * Donut, we want to use the {@link ContactAccessorSdk3_4} class. If it is
+ * Eclair or higher, we want to use {@link ContactAccessorSdk5}.
+ */
+public abstract class ContactAccessor {
+
+ protected final String LOG_TAG = "ContactsAccessor";
+ protected CordovaInterface mApp;
+ protected WebView mView;
+
+ /**
+ * Check to see if the data associated with the key is required to
+ * be populated in the Contact object.
+ * @param key
+ * @param map created by running buildPopulationSet.
+ * @return true if the key data is required
+ */
+ protected boolean isRequired(String key, HashMap<String,Boolean> map) {
+ Boolean retVal = map.get(key);
+ return (retVal == null) ? false : retVal.booleanValue();
+ }
+
+ /**
+ * Create a hash map of what data needs to be populated in the Contact object
+ * @param fields the list of fields to populate
+ * @return the hash map of required data
+ */
+ protected HashMap<String,Boolean> buildPopulationSet(JSONArray fields) {
+ HashMap<String,Boolean> map = new HashMap<String,Boolean>();
+
+ String key;
+ try {
+ if (fields.length() == 1 && fields.getString(0).equals("*")) {
+ map.put("displayName", true);
+ map.put("name", true);
+ map.put("nickname", true);
+ map.put("phoneNumbers", true);
+ map.put("emails", true);
+ map.put("addresses", true);
+ map.put("ims", true);
+ map.put("organizations", true);
+ map.put("birthday", true);
+ map.put("note", true);
+ map.put("urls", true);
+ map.put("photos", true);
+ map.put("categories", true);
+ }
+ else {
+ for (int i=0; i<fields.length(); i++) {
+ key = fields.getString(i);
+ if (key.startsWith("displayName")) {
+ map.put("displayName", true);
+ }
+ else if (key.startsWith("name")) {
+ map.put("displayName", true);
+ map.put("name", true);
+ }
+ else if (key.startsWith("nickname")) {
+ map.put("nickname", true);
+ }
+ else if (key.startsWith("phoneNumbers")) {
+ map.put("phoneNumbers", true);
+ }
+ else if (key.startsWith("emails")) {
+ map.put("emails", true);
+ }
+ else if (key.startsWith("addresses")) {
+ map.put("addresses", true);
+ }
+ else if (key.startsWith("ims")) {
+ map.put("ims", true);
+ }
+ else if (key.startsWith("organizations")) {
+ map.put("organizations", true);
+ }
+ else if (key.startsWith("birthday")) {
+ map.put("birthday", true);
+ }
+ else if (key.startsWith("note")) {
+ map.put("note", true);
+ }
+ else if (key.startsWith("urls")) {
+ map.put("urls", true);
+ }
+ else if (key.startsWith("photos")) {
+ map.put("photos", true);
+ }
+ else if (key.startsWith("categories")) {
+ map.put("categories", true);
+ }
+ }
+ }
+ }
+ catch (JSONException e) {
+ Log.e(LOG_TAG, e.getMessage(), e);
+ }
+ return map;
+ }
+
+ /**
+ * Convenience method to get a string from a JSON object. Saves a
+ * lot of try/catch writing.
+ * If the property is not found in the object null will be returned.
+ *
+ * @param obj contact object to search
+ * @param property to be looked up
+ * @return The value of the property
+ */
+ protected String getJsonString(JSONObject obj, String property) {
+ String value = null;
+ try {
+ if (obj != null) {
+ value = obj.getString(property);
+ if (value.equals("null")) {
+ Log.d(LOG_TAG, property + " is string called 'null'");
+ value = null;
+ }
+ }
+ }
+ catch (JSONException e) {
+ Log.d(LOG_TAG, "Could not get = " + e.getMessage());
+ }
+ return value;
+ }
+
+ /**
+ * Handles adding a JSON Contact object into the database.
+ * @return TODO
+ */
+ public abstract String save(JSONObject contact);
+
+ /**
+ * Handles searching through SDK-specific contacts API.
+ */
+ public abstract JSONArray search(JSONArray filter, JSONObject options);
+
+ /**
+ * Handles searching through SDK-specific contacts API.
+ * @throws JSONException
+ */
+ public abstract JSONObject getContactById(String id) throws JSONException;
+
+ /**
+ * Handles removing a contact from the database.
+ */
+ public abstract boolean remove(String id);
+
+ /**
+ * A class that represents the where clause to be used in the database query
+ */
+ class WhereOptions {
+ private String where;
+ private String[] whereArgs;
+ public void setWhere(String where) {
+ this.where = where;
+ }
+ public String getWhere() {
+ return where;
+ }
+ public void setWhereArgs(String[] whereArgs) {
+ this.whereArgs = whereArgs;
+ }
+ public String[] getWhereArgs() {
+ return whereArgs;
+ }
+ }
+}