Skip to content

Commit

Permalink
Updated Contacts, now has chooseContact method (which will invoke sto…
Browse files Browse the repository at this point in the history
…ck BlackBerry contact list chooser).
  • Loading branch information
Fil Maj committed Aug 17, 2009
1 parent f1583d5 commit 9722279
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 18 deletions.
11 changes: 4 additions & 7 deletions blackberry/src/com/nitobi/phonegap/api/impl/CameraCommand.java
Expand Up @@ -35,7 +35,7 @@
import com.nitobi.phonegap.api.Command;

/**
* Switchs current application to the camera to take a photo.
* Switches current application to the camera to take a photo.
*
* @author Jose Noheda
*
Expand All @@ -44,7 +44,8 @@ public class CameraCommand implements Command {

private static final int INVOKE_COMMAND = 0;
private static final int PICTURE_COMMAND = 1;
private static final String CODE = "gap://camera";
private static final String CODE = "PhoneGap=camera";
//private static final String

private long lastUSN = 0;
private String photoPath;
Expand All @@ -70,10 +71,6 @@ public void fileJournalChanged() {
}
};
}

/**
* Able to run the <i>camera</i> command. Ex: gap://camera/obtain
*/
public boolean accept(String instruction) {
return instruction != null && instruction.startsWith(CODE);
}
Expand All @@ -85,7 +82,7 @@ public String execute(String instruction) {
switch (getCommand(instruction)) {
case PICTURE_COMMAND:
UiApplication.getUiApplication().removeFileSystemJournalListener(listener);
return "navigator.camera.picture = '" + photoPath + "'";
return ";navigator.camera.picture = '" + photoPath + "';";
case INVOKE_COMMAND:
photoPath = null;
UiApplication.getUiApplication().addFileSystemJournalListener(listener);
Expand Down
42 changes: 36 additions & 6 deletions blackberry/src/com/nitobi/phonegap/api/impl/ContactsCommand.java
Expand Up @@ -45,6 +45,7 @@ public class ContactsCommand implements Command {

private static final int SEARCH_COMMAND = 0;
private static final int GET_ALL_COMMAND = 1;
private static final int CHOOSE_COMMAND = 2;
private static final String CODE = "PhoneGap=contacts";
private static final String CONTACT_MANAGER_JS_NAMESPACE = "navigator.ContactManager";

Expand All @@ -62,13 +63,41 @@ public String execute(String instruction) {
return getAgenda(options);
case GET_ALL_COMMAND:
return getAgenda(options);
case CHOOSE_COMMAND:
return chooseContact();
}
return null;
}
/**
*
* @param instruction
* @return
* Invokes the default BlackBerry contact chooser to allow the user to choose a contact.
* @return JSON representation of the chosen contact, which will then be sent back to JavaScript.
*/
private String chooseContact() {
try {
BlackBerryContactList agenda = (BlackBerryContactList) PIM.getInstance().openPIMList(PIM.CONTACT_LIST, PIM.READ_ONLY);
BlackBerryContact blackberryContact;
StringBuffer contacts = new StringBuffer("[");
if (agenda != null) {
blackberryContact = (BlackBerryContact) agenda.choose();
agenda.close();
ContactsCommand.addContactToBuffer(contacts, blackberryContact);
contacts.append("];");
return ";" + ContactsCommand.CONTACT_MANAGER_JS_NAMESPACE + ".contacts=" + contacts.toString() + "if (" + ContactsCommand.CONTACT_MANAGER_JS_NAMESPACE + ".choose_onSuccess) { " + ContactsCommand.CONTACT_MANAGER_JS_NAMESPACE + ".choose_onSuccess();" + ContactsCommand.CONTACT_MANAGER_JS_NAMESPACE + ".choose_onSuccess = null; };";
} else {
// TODO: If cannot get reference to Agenda, should the error or success callback be called?
return ";" + ContactsCommand.CONTACT_MANAGER_JS_NAMESPACE + ".contacts=" + contacts.append("];").toString() + ContactsCommand.CONTACT_MANAGER_JS_NAMESPACE + ".choose_onSuccess = null;";
}
} catch (Exception e) {
System.out.println("Exception getting contact list: " + e.getMessage());
// TODO: No error callbacks associated with contact chooser - what to do?
}
return null;
}

/**
* Parses the options object and returns a hash of params.
* @param instruction The cookie/string representation of the instruction.
* @return Hashtable
*/
private static Hashtable parseParameters(String instruction) {
String[] params = PhoneGap.splitString(instruction, '/', false);
Expand All @@ -89,6 +118,7 @@ private int getCommand(String instruction) {
String command = instruction.substring(instruction.indexOf('/') + 1);
if (command.startsWith("search")) return SEARCH_COMMAND;
if (command.startsWith("getall")) return GET_ALL_COMMAND;
if (command.startsWith("choose")) return CHOOSE_COMMAND;
return -1;
}
/**
Expand Down Expand Up @@ -134,14 +164,14 @@ private String getAgenda(Hashtable options) {
contacts.append("];");
// Return an assignment to the contact manager contacts array with the contacts JSON generated above.
// Also call the right onSuccess if it exists.
return ContactsCommand.CONTACT_MANAGER_JS_NAMESPACE + ".contacts=" + contacts.toString() + "if (" + ContactsCommand.CONTACT_MANAGER_JS_NAMESPACE + "." + callbackHook + "onSuccess) { " + ContactsCommand.CONTACT_MANAGER_JS_NAMESPACE + "." + callbackHook + "onSuccess() };";
return ";" + ContactsCommand.CONTACT_MANAGER_JS_NAMESPACE + ".contacts=" + contacts.toString() + "if (" + ContactsCommand.CONTACT_MANAGER_JS_NAMESPACE + "." + callbackHook + "onSuccess) { " + ContactsCommand.CONTACT_MANAGER_JS_NAMESPACE + "." + callbackHook + "onSuccess();" + ContactsCommand.CONTACT_MANAGER_JS_NAMESPACE + "." + callbackHook + "onSuccess = null;" + ContactsCommand.CONTACT_MANAGER_JS_NAMESPACE + "." + callbackHook + "onError = null; };";
} else {
// TODO: If cannot get reference to Agenda, should the error or success callback be called?
return contacts.append("];").toString();
return ";" + ContactsCommand.CONTACT_MANAGER_JS_NAMESPACE + ".contacts=" + contacts.append("];").toString() + ContactsCommand.CONTACT_MANAGER_JS_NAMESPACE + "." + callbackHook + "onSuccess = null;" + ContactsCommand.CONTACT_MANAGER_JS_NAMESPACE + "." + callbackHook + "onError = null;";
}
} catch (Exception ex) {
System.out.println("Exception getting contact list: " + ex.getMessage());
return "if (" + ContactsCommand.CONTACT_MANAGER_JS_NAMESPACE + "." + callbackHook + "onError) { " + ContactsCommand.CONTACT_MANAGER_JS_NAMESPACE + "." + callbackHook + "onError() };";
return ";if (" + ContactsCommand.CONTACT_MANAGER_JS_NAMESPACE + "." + callbackHook + "onError) { " + ContactsCommand.CONTACT_MANAGER_JS_NAMESPACE + "." + callbackHook + "onError();" + ContactsCommand.CONTACT_MANAGER_JS_NAMESPACE + "." + callbackHook + "onSuccess = null;" + ContactsCommand.CONTACT_MANAGER_JS_NAMESPACE + "." + callbackHook + "onError = null; };";
}
}
private static void addContactToBuffer(StringBuffer buff, BlackBerryContact contact) {
Expand Down
13 changes: 9 additions & 4 deletions blackberry/src/www/js/camera.js
Expand Up @@ -7,8 +7,8 @@ function Camera() {
}

Camera.prototype.launch = function () {
if (Device.hasCamera) Device.exec("camera", ["obtain"], true);
else alert("Camera not supported");
if (device.hasCamera) device.exec("camera", ["obtain"], true);
else alert("Camera not supported on this device.");
}

/**
Expand All @@ -18,8 +18,13 @@ Camera.prototype.launch = function () {
* @param {Object} options
*/
Camera.prototype.getPicture = function(successCallback, errorCallback, options) {
if (Device.hasCamera) Device.exec("camera", ["picture"], true);
else alert("Camera not supported");
if (device.hasCamera) {
if (successCallback) this.onSuccess = successCallback;
else this.onSuccess = null;
if (errorCallback) this.onError = errorCallback;
else this.onError = null;
device.exec("camera", ["picture"], true);
} else alert("Camera not supported");
}

if (typeof navigator.camera == "undefined") navigator.camera = new Camera();
6 changes: 6 additions & 0 deletions blackberry/src/www/js/contacts.js
Expand Up @@ -46,5 +46,11 @@ ContactManager.prototype.getAllContacts = function(successCallback, errorCallbac
params = this.formParams(options,params);
device.exec("contacts", params, true);
}
ContactManager.prototype.chooseContact = function(successCallback, options) {
this.choose_onSuccess = successCallback;
var params = ["choose"];
params = this.formParams(options,params);
device.exec("contacts", params, true);
}

if (typeof navigator.ContactManager == "undefined") navigator.ContactManager = new ContactManager();
11 changes: 10 additions & 1 deletion blackberry/src/www/test/contacts.html
Expand Up @@ -29,14 +29,23 @@
};
navigator.ContactManager.getAllContacts(win,fail,options);
}
function chooseContact() {
var options = {};
var win = function() {
if (navigator.ContactManager.contacts.length > 0) alert('Found:\nname: ' + navigator.ContactManager.contacts[0].name + '\nemail: ' + navigator.ContactManager.contacts[0].email + '\nphone: ' + navigator.ContactManager.contacts[0].phone);
else alert("No contacts found.");
};
navigator.ContactManager.chooseContact(win, options);
}
</script>
</head>
<body>
<p><a href="javascript:findContactsByName('Joe');">Find Joe in contacts</a></p>
<hr>
<p><a href="javascript:getAllContacts(null,null);">Get first contact in address book</a></p>
<hr>
<br/>
<p><a href="javascript:chooseContact();">Use Contact Chooser to select a Contact</a></p>
<hr>
<br/>
<br/>
<br/>
Expand Down

0 comments on commit 9722279

Please sign in to comment.