From 7f26e42544e4a1a4adb493061652d084e23769be Mon Sep 17 00:00:00 2001 From: Jerome Carty Date: Sun, 25 Sep 2011 22:49:08 -0400 Subject: [PATCH] Initial FileUploader extension commit --- Smartphone/FileUploader/README.md | 87 +++++ Smartphone/FileUploader/src/library.xml | 23 ++ .../webworks/io/FileUploaderExtension.java | 36 ++ .../webworks/io/FileUploaderNamespace.java | 24 ++ .../src/webworks/io/FunctionUpload.java | 309 ++++++++++++++++++ .../FileUploader/src/webworks/io/Logger.java | 136 ++++++++ 6 files changed, 615 insertions(+) create mode 100644 Smartphone/FileUploader/README.md create mode 100644 Smartphone/FileUploader/src/library.xml create mode 100644 Smartphone/FileUploader/src/webworks/io/FileUploaderExtension.java create mode 100644 Smartphone/FileUploader/src/webworks/io/FileUploaderNamespace.java create mode 100644 Smartphone/FileUploader/src/webworks/io/FunctionUpload.java create mode 100644 Smartphone/FileUploader/src/webworks/io/Logger.java diff --git a/Smartphone/FileUploader/README.md b/Smartphone/FileUploader/README.md new file mode 100644 index 00000000..592c5127 --- /dev/null +++ b/Smartphone/FileUploader/README.md @@ -0,0 +1,87 @@ +# Spinner Control +The spinner control will allow you to have a more usable interface when interacting +with <select> elements on a BlackBerry 5.0 browser. It will also allow you to keep +the same consistent feel with your UI on a BlackBerry 6.0 browser. + +**Author:** [Jerome Carty](https://github.com/jcarty) + +## Tested On + +* BlackBerry Bold 9900 v7.0.0.353 +* BlackBerry Bold 9650 v6.0.0.546 +* BlackBerry Curve 9300 v5.0.0.716 + +**Requires BlackBerry WebWorks SDK for Smartphones v2.0 or higher** + +Have a problem with this extension? [Log an Issue](https://github.com/blackberry/WebWorks-Community-APIs/issues) or contact the [Author](https://github.com/jcarty) + +## How To Configure The Extension For Use + +1. Locate your BlackBerry WebWorks SDK for Smartphone extensions directory using your File Explorer. Default path is _**C:\Program Files\Research In Motion\BlackBerry WebWorks Packager\ext**_ + +2. Create a new _**blackberry.ui.Spinner**_ directory in the _**ext**_ directory + +3. Download the source from this repository and unzip it to a location on your computer + +4. Using File Explorer browse to this extension's downloaded source code _**Smartphone\FileUploader**_ + +5. Copy the _**library.xml**_ file from the downloaded _**Smartphone\FileUploader**_ directory to your new _**ext\webworks.io.FileUploader**_ directory + +6. Copy the downloaded _**Smartphone\FileUploader\src\blackberry**_ directory to your new _**ext\webworks.io.FileUploader\webworks**_ directory + +**NOTE:** Be sure to back-up this _**ext\webworks.io.FileUploader**_ directory in your WebWorks SDK extensions directory before performing a WebWorks SDK upgrade. Simply copy it back into the _**ext**_ directory after you have completed your SDK upgrade. + +## Required Feature ID +Whenever you use the below feature id in any of your WebWorks applications this extension will be loaded for use. + + + +## Summary + + +## Code Example + + function foo() + { + var rowHeight; + var visibleRows; + + // Populate our items + var items = new Array('Barcelona', 'Beijing', 'Brasilia', 'Melbourne', 'Moscow', 'New York', 'Paris' ); + + // Figure out our height and rows based on screen size + if (screen.height < 480){ + rowHeight = 60; + visibleRows = 3; + } + else { + rowHeight = 75; + visibleRows = 4; + } + + // Configure the options + var options = {'title': 'Choose A City:', + 'rowHeight': rowHeight, + 'visibleRows': visibleRows, + 'selectedIndex': 2, + 'items' : items}; + + // Open the spin dialog + blackberry.ui.Spinner.open(options, function (selectedIndex) { + alert(selectedIndex); } + ); + } + +## Usage Information + + +_**NOTE:**_ The callback is handled asynchronously, so code that is placed directly after +the "open" function call will be executed immediately while waiting for the user's +response input. + +## Properties +**items:** +Is an array of string items you wish to display in the spinner control + +## Change Log +_Empty_ \ No newline at end of file diff --git a/Smartphone/FileUploader/src/library.xml b/Smartphone/FileUploader/src/library.xml new file mode 100644 index 00000000..8e4efe18 --- /dev/null +++ b/Smartphone/FileUploader/src/library.xml @@ -0,0 +1,23 @@ + + + + webworks.io.FileUploaderExtension + + + + + + + + + + + + + + + + Upload files to a remote server + + + \ No newline at end of file diff --git a/Smartphone/FileUploader/src/webworks/io/FileUploaderExtension.java b/Smartphone/FileUploader/src/webworks/io/FileUploaderExtension.java new file mode 100644 index 00000000..20ffdc20 --- /dev/null +++ b/Smartphone/FileUploader/src/webworks/io/FileUploaderExtension.java @@ -0,0 +1,36 @@ +package webworks.io; + +import org.w3c.dom.Document; + +import net.rim.device.api.browser.field2.BrowserField; +import net.rim.device.api.script.ScriptEngine; +import net.rim.device.api.web.WidgetConfig; +import net.rim.device.api.web.WidgetExtension; + +/** + * + * @author Jerome Carty + * + */ +public class FileUploaderExtension implements WidgetExtension { + + public String[] getFeatureList() { + return new String[] { FileUploaderNamespace.NAME }; + } + + public void loadFeature(String feature, String version, Document doc, + ScriptEngine scriptEngine) throws Exception { + + if (feature.equals(FileUploaderNamespace.NAME)) { + Logger.enableLogging(0xe995515474f898fbL, "webworks.io.FileUploader"); + scriptEngine.addExtension(feature, new FileUploaderNamespace()); + } + } + + public void register(WidgetConfig arg0, BrowserField arg1) { + } + + public void unloadFeatures(Document arg0) { + } + +} diff --git a/Smartphone/FileUploader/src/webworks/io/FileUploaderNamespace.java b/Smartphone/FileUploader/src/webworks/io/FileUploaderNamespace.java new file mode 100644 index 00000000..980c75e6 --- /dev/null +++ b/Smartphone/FileUploader/src/webworks/io/FileUploaderNamespace.java @@ -0,0 +1,24 @@ +package webworks.io; + +import net.rim.device.api.script.Scriptable; + +public class FileUploaderNamespace extends Scriptable { + + public static final String NAME = "webworks.io.FileUploader"; + + private static final String FIELD_UPLOAD = FunctionUpload.NAME; + + private FunctionUpload _uploadFunction; + + public FileUploaderNamespace() { + _uploadFunction = new FunctionUpload(); + } + + public Object getField(String name) throws Exception { + if (name.equals(FIELD_UPLOAD)) { + return _uploadFunction; + } + return super.getField(name); + } + +} diff --git a/Smartphone/FileUploader/src/webworks/io/FunctionUpload.java b/Smartphone/FileUploader/src/webworks/io/FunctionUpload.java new file mode 100644 index 00000000..58eb8f20 --- /dev/null +++ b/Smartphone/FileUploader/src/webworks/io/FunctionUpload.java @@ -0,0 +1,309 @@ +package webworks.io; + +import java.io.InputStream; +import java.io.OutputStream; +import java.util.Enumeration; +import java.util.Hashtable; +import java.util.Vector; + +import javax.microedition.io.Connector; +import javax.microedition.io.HttpConnection; +import javax.microedition.io.file.FileConnection; + +import net.rim.device.api.io.IOUtilities; +import net.rim.device.api.io.MIMETypeAssociations; +import net.rim.device.api.io.http.HttpProtocolConstants; +import net.rim.device.api.io.transport.ConnectionDescriptor; +import net.rim.device.api.io.transport.ConnectionFactory; +import net.rim.device.api.script.Scriptable; +import net.rim.device.api.script.ScriptableFunction; + +public class FunctionUpload extends ScriptableFunction { + + public static final String NAME = "upload"; + + private static final String BOUNDARY = "----0x6d6356418a05040fL"; + private static final String TD = "--"; + private static final String DCRLF = HttpProtocolConstants.HTTP_HEADER_DOUBLE_CRLF; + private static final String CRLF = HttpProtocolConstants.HTTP_HEADER_SINGLE_CRLF; + + private ScriptableFunction _successCallback = null; + private ScriptableFunction _errorCallback = null; + + public Object invoke(Object thiz, Object[] args) throws Exception { + + if (args.length == 1) { + Hashtable headers = null; + Hashtable params = null; + + Logger.info("Getting options..."); + + Scriptable config = (Scriptable) args[0]; + + Logger.info("Setting core options..."); + + String url = (String) config.getField("url"); + String filePath = (String) config.getField("file"); + String fileKey = (String) config.getField("fileKey"); + String mimeType = (String) config.getField("mimeType"); + + Logger.info("Getting headers and params..."); + + Object sHeaders = config.getField("headers"); + Object sParams = config.getField("params"); + + if (mimeType.equals(UNDEFINED)) { + mimeType = null; + } + + Logger.info("Setting headers and extra params..."); + + try { + headers = objectToHashtable(sHeaders); + params = objectToHashtable(sParams); + } catch(Throwable e) { + Logger.error("Error setting custom params and headers..." + e.getMessage()); + } + + _successCallback = (ScriptableFunction) config.getField("success"); + _errorCallback = (ScriptableFunction) config.getField("error"); + + Logger.info("Beginning upload..."); + + new UploadRunnable(url, fileKey, params, filePath, mimeType, headers); + } + return UNDEFINED; + } + + private Hashtable objectToHashtable(Object obj) throws Exception + { + Hashtable ht = null; + + if (!obj.equals(UNDEFINED) && obj != null) { + Scriptable s = (Scriptable) obj; + + Vector v = new Vector(); + s.enumerateFields(v); + + int count = v.size(); + Logger.log("Object Keys: " + String.valueOf(v.size())); + + if (count > 0) { + ht = new Hashtable(); + String fieldName; + + for (int i=0; i Show Event + * Log, or on physical devices by pressing the Alt key, followed by + * the LGLG key combination. + * + * To enable event logging, you must first call enableLogging. + * + * Logger also provides methods to write to System.out and + * System.err. + */ +public class Logger { + + /** + * Application name + */ + protected static String appName; + + /** + * Application GUID + */ + protected static long appID; + + /** + * Used to format dates into a standard format + */ + private static final SimpleDateFormat dateFormat = + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); + + /** + * Invoke this method to enable logging to the BlackBerry Event Log. + */ + public static void enableLogging(long appID, String appName) { + Logger.appName = appName; + Logger.appID = appID; + + if (EventLogger.register(appID, appName, EventLogger.VIEWER_STRING)) { + log("Logger enabled: " + "GUID=" + appID + ", name=" + appName); + } + else { + log("EventLogger registration failed."); + } + } + + /** + * Sets the minimum logging level. + */ + public static void setMinimumLoggingLevel(int level) { + EventLogger.setMinimumLevel(level); + } + + /** + * Logs formatted message to Event Log with ALWAYS_LOG level. + */ + public static void log(String msg) { + logEvent(msg, EventLogger.ALWAYS_LOG); + } + + /** + * Logs formatted message to Event Log with DEBUG_INFO level. + */ + public static void debug(String msg) { + logEvent(msg, EventLogger.DEBUG_INFO); + } + + /** + * Logs formatted message to Event Log with INFORMATION level. + */ + public static void info(String msg) { + logEvent(msg, EventLogger.INFORMATION); + } + + /** + * Logs formatted message to Event Log with WARNING level. + */ + public static void warn(String msg) { + logEvent(msg, EventLogger.WARNING); + } + + /** + * Logs formatted message to Event Log with ERROR level. + */ + public static void error(String msg) { + logEvent(msg, EventLogger.ERROR); + } + + /** + * Logs formatted message to Event Log with SEVERE_ERROR level. + */ + public static void severe(String msg) { + logEvent(msg, EventLogger.SEVERE_ERROR); + } + + /** + * Prints unformatted message to System.out. + */ + public static void out(String msg) { + System.out.println(msg); + } + + /** + * Prints unformatted message to System.err. + */ + public static void err(String msg, Throwable t) { + System.err.println(msg); + t.printStackTrace(); + } + + /** + * Logs formatted message to Event Log (if enabled) and System.out. + */ + private static void logEvent(String msg, int level) { + String message = formatMessage(msg); + EventLogger.logEvent(appID, message.getBytes(), level); + out(message); + } + + private static String formatMessage(String msg) { + StringBuffer sb = new StringBuffer(); + sb.append(appName); + sb.append(" ["); + sb.append(dateFormat.format(new Date())); + sb.append("]: "); + sb.append(msg); + return sb.toString(); + } +}