<commit_before>/*
 * PhoneGap is available under *either* the terms of the modified BSD license *or* the
 * MIT License (2008). See http://opensource.org/licenses/alphabetical for full text.
 * 
 * Copyright (c) 2005-2010, Nitobi Software Inc.
 * Copyright (c) 2010, IBM Corporation
 */
package com.phonegap.camera;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;

import net.rim.blackberry.api.invoke.CameraArguments;
import net.rim.blackberry.api.invoke.Invoke;
import net.rim.device.api.io.Base64OutputStream;
import net.rim.device.api.io.file.FileSystemJournal;
import net.rim.device.api.io.file.FileSystemJournalEntry;
import net.rim.device.api.io.file.FileSystemJournalListener;
import net.rim.device.api.system.Characters;
import net.rim.device.api.system.EventInjector;
import net.rim.device.api.ui.UiApplication;

import org.json.me.JSONArray;

import com.phonegap.PhoneGapExtension;
import com.phonegap.api.PluginResult;
import com.phonegap.util.Logger;

public class CapturePhotoAction implements FileSystemJournalListener
{
	private static final int DATA_URL = 0;
	private static final int FILE_URI = 1;
	
	/**
	 * Maximum image encoding size (in bytes).  Obtained unofficially through trial and error.
	 */
	private static final long MAX_ENCODING_SIZE = 1500000L;
	
	private long lastUSN = 0;
	private String callbackId;
	private int destinationType = DATA_URL;

	public CapturePhotoAction(String callbackId)
	{
		this.callbackId = callbackId;
	}
	
	/**
	 * Capture a photo using device camera.  The camera is invoked using native APIs.
	 * The photo is captured by listening to file system changes, and sent back by 
	 * invoking the appropriate JS callback.
	 *
	 * @param args JSONArray formatted as [ cameraArgs ]
	 *        cameraArgs:      
     *          [ 80,                                    // quality (ignored)
     *            Camera.DestinationType.DATA_URL,       // destinationType
     *            Camera.PictureSourceType.PHOTOLIBRARY  // sourceType (ignored)]
	 * @return A CommandResult object with the INPROGRESS state for taking a photo.
	 */
	public PluginResult execute(JSONArray args) 
	{
		// get the camera options, if supplied
		if (args != null && args.length() > 1)
		{
			// determine the desired destination type: data or file URI
			Integer destType = (Integer)args.opt(1);
			this.destinationType = 
				(destType != null && destType.intValue()== FILE_URI) ? FILE_URI : DATA_URL;
		}
		
		// MMAPI interface doesn't use the native Camera application or interface
		// (we would have to replicate it).  So, we invoke the native Camera application,
		// which doesn't allow us to set any options.
		synchronized(UiApplication.getEventLock()) {
			UiApplication.getUiApplication().addFileSystemJournalListener(this);
			Invoke.invokeApplication(Invoke.APP_TYPE_CAMERA, new CameraArguments());
		}
		
		// Return null PluginResult so plugin manager does not invoke a callback. 
		// We invoked the native camera application, which runs in a separate
		// process, and must now wait for the listener to retrieve the photo taken. 
		return null;
	}

	/**
	 * Listens for file system changes.  When a JPEG file is added, we process 
	 * it and send it back.
	 */
	public void fileJournalChanged() 
	{
		// next sequence number file system will use
		long USN = FileSystemJournal.getNextUSN();
		
		for (long i = USN - 1; i >= lastUSN && i < USN; --i)
		{
			FileSystemJournalEntry entry = FileSystemJournal.getEntry(i);
			if (entry == null)
			{
				break;
			}
			
			if (entry.getEvent() == FileSystemJournalEntry.FILE_ADDED)<commit_msg>Change Camera plugin to use JS callback persistence.<commit_after>/*
 * PhoneGap is available under *either* the terms of the modified BSD license *or* the
 * MIT License (2008). See http://opensource.org/licenses/alphabetical for full text.
 * 
 * Copyright (c) 2005-2010, Nitobi Software Inc.
 * Copyright (c) 2010, IBM Corporation
 */
package com.phonegap.camera;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;

import net.rim.blackberry.api.invoke.CameraArguments;
import net.rim.blackberry.api.invoke.Invoke;
import net.rim.device.api.io.Base64OutputStream;
import net.rim.device.api.io.file.FileSystemJournal;
import net.rim.device.api.io.file.FileSystemJournalEntry;
import net.rim.device.api.io.file.FileSystemJournalListener;
import net.rim.device.api.system.Characters;
import net.rim.device.api.system.EventInjector;
import net.rim.device.api.ui.UiApplication;

import org.json.me.JSONArray;

import com.phonegap.PhoneGapExtension;
import com.phonegap.api.PluginResult;
import com.phonegap.util.Logger;

public class CapturePhotoAction implements FileSystemJournalListener
{
	private static final int DATA_URL = 0;
	private static final int FILE_URI = 1;
	
	/**
	 * Maximum image encoding size (in bytes).  Obtained unofficially through trial and error.
	 */
	private static final long MAX_ENCODING_SIZE = 1500000L;
	
	private long lastUSN = 0;
	private String callbackId;
	private int destinationType = DATA_URL;

	public CapturePhotoAction(String callbackId)
	{
		this.callbackId = callbackId;
	}
	
	/**
	 * Capture a photo using device camera.  The camera is invoked using native APIs.
	 * The photo is captured by listening to file system changes, and sent back by 
	 * invoking the appropriate JS callback.
	 *
	 * @param args JSONArray formatted as [ cameraArgs ]
	 *        cameraArgs:      
     *          [ 80,                                    // quality (ignored)
     *            Camera.DestinationType.DATA_URL,       // destinationType
     *            Camera.PictureSourceType.PHOTOLIBRARY  // sourceType (ignored)]
	 * @return A CommandResult object with the INPROGRESS state for taking a photo.
	 */
	public PluginResult execute(JSONArray args) 
	{
		// get the camera options, if supplied
		if (args != null && args.length() > 1)
		{
			// determine the desired destination type: data or file URI
			Integer destType = (Integer)args.opt(1);
			this.destinationType = 
				(destType != null && destType.intValue()== FILE_URI) ? FILE_URI : DATA_URL;
		}
		
		// MMAPI interface doesn't use the native Camera application or interface
		// (we would have to replicate it).  So, we invoke the native Camera application,
		// which doesn't allow us to set any options.
		synchronized(UiApplication.getEventLock()) {
			UiApplication.getUiApplication().addFileSystemJournalListener(this);
			Invoke.invokeApplication(Invoke.APP_TYPE_CAMERA, new CameraArguments());
		}
		
		// We invoked the native camera application, which runs in a separate
		// process, and must now wait for the listener to retrieve the photo taken. 
		// Return NO_RESULT status so plugin manager does not invoke a callback,
		// but set keep callback to true so we can invoke the callback later.
		PluginResult result = new PluginResult(PluginResult.Status.NO_RESULT);
		result.setKeepCallback(true);
		return result;
	}

	/**
	 * Listens for file system changes.  When a JPEG file is added, we process 
	 * it and send it back.
	 */
	public void fileJournalChanged() 
	{
		// next sequence number file system will use
		long USN = FileSystemJournal.getNextUSN();
		
		for (long i = USN - 1; i >= lastUSN && i < USN; --i)
		{
			FileSystemJournalEntry entry = FileSystemJournal.getEntry(i);
			if (entry == null)
			{
				break;
			}
			
			if (entry.getEvent() == FileSystemJournalEntry.FILE_ADDED)
{'fraction_non_alphanumeric': 0.09190600522193211, 'fraction_numerical': 0.008616187989556136, 'mean_word_length': 3.525103366804489, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1461449', 'n_tokens_mistral': 2436, 'n_tokens_neox': 2173, 'n_words': 704}