Created
April 18, 2009 11:55
-
-
Save hippocamp/97571 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
//gCam.java | |
//This file is part of the Hippocamp Source Library. | |
// | |
//Hippocamp Source Library is free software: you can redistribute it and/or modify | |
//it under the terms of the GNU General Public License as published by | |
//the Free Software Foundation, either version 3 of the License, or | |
//(at your option) any later version. | |
// | |
//Hippocamp Source Library is distributed in the hope that it will be useful, | |
//but WITHOUT ANY WARRANTY; without even the implied warranty of | |
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
//GNU General Public License for more details. | |
// | |
//You should have received a copy of the GNU General Public License | |
//along with the Hippocamp Source Library. If not, see <http://www.gnu.org/licenses/>. | |
package com.hippocamp.ghandicam; | |
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.mail.Address; | |
import net.rim.blackberry.api.mail.Message; | |
import net.rim.blackberry.api.mail.MessagingException; | |
import net.rim.blackberry.api.mail.Multipart; | |
import net.rim.blackberry.api.mail.SupportedAttachmentPart; | |
import net.rim.blackberry.api.mail.Transport; | |
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.SystemListener2; | |
import net.rim.device.api.system.USBPort; | |
import net.rim.device.api.ui.UiApplication; | |
//Stripped down version of the GandhiCam application, Screen and Settings class removed. | |
//Not meant as a usable app, to build and test in the BlackBerry emu look at | |
//'Auto-run on Startup' and 'System Module' in the JDE Project properties | |
public class gCam extends UiApplication implements FileSystemJournalListener { | |
FileConnection fileConnection = null; | |
gCam app = this; | |
boolean doCapture = false; | |
String path; | |
String title; | |
String mime; | |
//dummy params to mimic persistable settings class: | |
boolean active = true; | |
boolean captureVideo = true; | |
boolean captureAudio = true; | |
boolean capturePhoto = true; | |
String emailAddress = "myname@mycomany.net"; | |
public static void main(String[] args) { | |
new gCam().enterEventDispatcher(); | |
} | |
public gCam() { | |
addFileSystemJournalListener(this); | |
} | |
public void fileJournalChanged() { | |
//Don't do anything if USB is connected (user can drag files to the SD from desktop) | |
if (USBPort.getConnectionState() != SystemListener2.USB_STATE_CABLE_CONNECTED) { | |
//has app been initialised and does user want to save files? | |
if (active && !emailAddress.equals("")) { | |
long nextUSN = FileSystemJournal.getNextUSN(); | |
FileSystemJournalEntry entry = FileSystemJournal.getEntry(nextUSN - 1); | |
//File is created when record starts, but only written to when saved | |
if (entry.getEvent() == FileSystemJournalEntry.FILE_CHANGED) { | |
try { | |
path = "file://" + entry.getPath(); | |
if (path.endsWith(".3GP") || path.endsWith(".3gp")) { | |
title = "GandhiCam Video"; | |
mime = "video/3gpp"; | |
if (captureVideo) { | |
doCapture = true; | |
} else { | |
doCapture = false; | |
} | |
} | |
if (path.endsWith(".amr") || path.endsWith(".AMR")) { | |
title = "GandhiCam Audio"; | |
mime = "audio/amr"; | |
if (captureAudio) { | |
doCapture = true; | |
} else { | |
doCapture = false; | |
} | |
} | |
if (path.endsWith(".jpg") || path.endsWith(".JPG")) { | |
title = "GandhiCam Photo"; | |
mime = "image/jpeg"; | |
if (capturePhoto) { | |
doCapture = true; | |
} else { | |
doCapture = false; | |
} | |
} | |
if (doCapture) { | |
doCapture = false;//Reset so subsequent files don't get sent | |
//Only create the file connection if we're doing a gandhi-send | |
fileConnection = (FileConnection) Connector.open(path); | |
if (fileConnection.exists()) { | |
if (fileConnection.canRead() && fileConnection.availableSize() > 10) { | |
//Create the email | |
Message message = new Message(); | |
Address ghandicamAddress = new Address(emailAddress, title); | |
Address[] ghandicamAddressArray = new Address[1]; | |
ghandicamAddressArray[0] = ghandicamAddress; | |
message.addRecipients(Message.RecipientType.TO, ghandicamAddressArray); | |
message.setSubject(title); | |
//Get the file data | |
InputStream input = fileConnection.openInputStream(); | |
ByteArrayOutputStream dataOuput = new ByteArrayOutputStream(); | |
int i = 0; | |
while ((i = input.read()) != -1) { | |
dataOuput.write(i); | |
} | |
byte[] fileData = dataOuput.toByteArray(); | |
fileConnection.close(); | |
//Create the attachment | |
Multipart mPart = new Multipart(); | |
String fileName = path.substring(path.lastIndexOf("/".charAt(0)) + 1, path.length()); | |
SupportedAttachmentPart filePart = new SupportedAttachmentPart(mPart, mime, fileName, fileData); | |
mPart.addBodyPart(filePart); | |
message.setContent(mPart); | |
Transport.send(message); | |
} | |
} | |
} | |
} catch (IOException ex) { | |
ex.printStackTrace(); | |
} catch (MessagingException ex) { | |
ex.printStackTrace(); | |
} finally { | |
if (fileConnection != null) { | |
try { | |
fileConnection.close(); | |
} catch (IOException ex) { | |
ex.printStackTrace(); | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment