Skip to content

Commit

Permalink
Ported Contacts API
Browse files Browse the repository at this point in the history
  • Loading branch information
xmaniac committed May 18, 2009
1 parent 4b8f28d commit aea3183
Show file tree
Hide file tree
Showing 9 changed files with 173 additions and 18 deletions.
3 changes: 3 additions & 0 deletions blackberry/phonegap.jdp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ ExcludeFromBuildAll=0
src\com\nitobi\phonegap\api\Command.java
src\com\nitobi\phonegap\api\CommandManager.java
src\com\nitobi\phonegap\api\impl\CameraCommand.java
src\com\nitobi\phonegap\api\impl\ContactsCommand.java
src\com\nitobi\phonegap\api\impl\GeoLocationCommand.java
src\com\nitobi\phonegap\api\impl\InitializationCommand.java
src\com\nitobi\phonegap\api\impl\TelephonyCommand.java
Expand All @@ -35,11 +36,13 @@ src\com\nitobi\phonegap\io\QueueResourceFetcher.java
src\com\nitobi\phonegap\model\Position.java
src\com\nitobi\phonegap\PhoneGap.java
src\www\js\camera.js
src\www\js\contacts.js
src\www\js\device.js
src\www\js\geolocation.js
src\www\js\position.js
src\www\js\telephony.js
src\www\test\camera.html
src\www\test\contacts.html
src\www\test\device.html
src\www\test\images\0.png
src\www\test\images\1.png
Expand Down
15 changes: 5 additions & 10 deletions blackberry/src/com/nitobi/phonegap/PhoneGap.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import java.util.Vector;

import javax.microedition.io.HttpConnection;
import javax.microedition.io.InputConnection;

import net.rim.device.api.browser.field.BrowserContent;
import net.rim.device.api.browser.field.BrowserContentManager;
Expand All @@ -35,9 +34,7 @@
import net.rim.device.api.browser.field.RenderingOptions;
import net.rim.device.api.browser.field.RequestedResource;
import net.rim.device.api.browser.field.UrlRequestedEvent;
import net.rim.device.api.system.Application;
import net.rim.device.api.system.Display;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Screen;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.container.MainScreen;
Expand Down Expand Up @@ -121,13 +118,11 @@ public Object eventOccurred(final Event event) {
}
if (event instanceof UrlRequestedEvent) {
final String url = ((UrlRequestedEvent) event).getURL();
//if (connectionManager.isInternal(url)) {
new Thread(new AsynchronousResourceFetcher(url, new Callback() {
public void execute(final Object input) {
_browserContentManager.setContent((HttpConnection) input, PhoneGap.this, null);
}
}, connectionManager)).start();
//}
new Thread(new AsynchronousResourceFetcher(url, new Callback() {
public void execute(final Object input) {
_browserContentManager.setContent((HttpConnection) input, PhoneGap.this, null);
}
}, connectionManager)).start();
}
return null;
}
Expand Down
12 changes: 7 additions & 5 deletions blackberry/src/com/nitobi/phonegap/api/CommandManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
package com.nitobi.phonegap.api;

import com.nitobi.phonegap.api.impl.CameraCommand;
import com.nitobi.phonegap.api.impl.ContactsCommand;
import com.nitobi.phonegap.api.impl.GeoLocationCommand;
import com.nitobi.phonegap.api.impl.InitializationCommand;
import com.nitobi.phonegap.api.impl.TelephonyCommand;
Expand All @@ -37,14 +38,15 @@
public final class CommandManager {

// List of installed Commands
private Command[] commands = new Command[5];
private Command[] commands = new Command[6];

public CommandManager() {
commands[0] = new CameraCommand();
commands[1] = new VibrationCommand();
commands[2] = new TelephonyCommand();
commands[3] = new GeoLocationCommand();
commands[4] = new InitializationCommand();
commands[1] = new ContactsCommand();
commands[2] = new VibrationCommand();
commands[3] = new TelephonyCommand();
commands[4] = new GeoLocationCommand();
commands[5] = new InitializationCommand();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
import com.nitobi.phonegap.api.Command;

/**
* Switchs current application to the camera.
* Switchs current application to the camera to take a photo.
*
* @author Jose Noheda
*
Expand Down
92 changes: 92 additions & 0 deletions blackberry/src/com/nitobi/phonegap/api/impl/ContactsCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/**
* The MIT License
* -------------------------------------------------------------
* Copyright (c) 2008, Rob Ellis, Brock Whitten, Brian Leroux, Joe Bowser, Dave Johnson, Nitobi
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.nitobi.phonegap.api.impl;

import java.util.Enumeration;

import javax.microedition.pim.Contact;
import javax.microedition.pim.PIM;

import net.rim.blackberry.api.pdap.BlackBerryContact;
import net.rim.blackberry.api.pdap.BlackBerryContactList;

import com.nitobi.phonegap.api.Command;

/**
* Finds data in agenda.
*
* @author Jose Noheda
*
*/
public class ContactsCommand implements Command {

private static final int SEARCH_COMMAND = 0;
private static final String CODE = "gap://contacts";

/**
* Able to run the <i>call</i> command. Ex: gap://contacts/search/name/Joe
*/
public boolean accept(String instruction) {
return instruction != null && instruction.startsWith(CODE);
}

/**
* Invokes internal phone application.
*/
public String execute(String instruction) {
switch (getCommand(instruction)) {
case SEARCH_COMMAND:
return "navigator.ContactManager.contacts = navigator.ContactManager.contacts.concat(" + getAgendaByName("Joe") + ");";
}
return null;
}

private int getCommand(String instruction) {
String command = instruction.substring(instruction.substring(7).indexOf('/') + 1);
if (command.indexOf("search") > 0) return SEARCH_COMMAND;
return -1;
}

private String getAgendaByName(String name) {
try {
BlackBerryContactList agenda = (BlackBerryContactList) PIM.getInstance().openPIMList(PIM.CONTACT_LIST, PIM.READ_ONLY);
if (agenda != null) {
StringBuffer contacts = new StringBuffer("[");
Enumeration matches = agenda.itemsByName(name);
while (matches.hasMoreElements()) {
BlackBerryContact contact = (BlackBerryContact) matches.nextElement();
contacts.append("{email:'");
contacts.append(contact.getString(Contact.EMAIL, 0));
contacts.append("', phone:'");
contacts.append(contact.getString(Contact.TEL, 0));
contacts.append("'},");
}
return contacts.deleteCharAt(contacts.length() - 1).append("]").toString();
}
} catch (Exception ex) {
System.out.println("Exception getting contact list: " + ex.getMessage());
}
return null;
}

}
6 changes: 4 additions & 2 deletions blackberry/src/www/js/camera.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ function Camera() {
}

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

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

if (typeof navigator.camera == "undefined") navigator.camera = new Camera();
31 changes: 31 additions & 0 deletions blackberry/src/www/js/contacts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* This class represents a Contact in the manager.
* @constructor
*/
function Contact() {
this.name = "";
this.phone = "";
this.address = "";
}

/**
*
* @param {Object} successCallback
* @param {Object} errorCallback
* @param {Object} options
*/
Contact.prototype.get = function(successCallback, errorCallback, options) {

}


function ContactManager() {
this.contacts = [];
this.timestap = new Date().getTime();
}

ContactManager.prototype.get = function(successCallback, errorCallback, options) {
Device.exec("contacts", [options.operation, options.field, options.value], true);
}

if (typeof navigator.ContactManager == "undefined") navigator.ContactManager = new ContactManager();
29 changes: 29 additions & 0 deletions blackberry/src/www/test/contacts.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<html>
<head>
<script type="text/javascript" src="data:///www/js/device.js"></script>
<script type="text/javascript" src="data:///www/js/contacts.js"></script>
<script type="text/javascript">
function findContactByName(name) {
var options = {
operation: "search",
field: "name",
value: name
};
navigator.ContactManager.get(null, null, options);
if (navigator.ContactManager.contacts.length > 0) alert('Found:\nemail: ' + navigator.ContactManager.contacts[0].email + '\nphone: ' + navigator.ContactManager.contacts[0].phone);
else alert("No Joe found in contacts");
}
</script>
</head>
<body>
<p>Add a contact with name Joe first</p>
<hr/>
<p><a href="javascript:findContactByName('Joe');">Find Joe in contacts</a></p>
<hr>
<br/>
<br/>
<br/>
<br/>
<a href="data:///www/test/index.html">Back</a>
</body>
</html>
1 change: 1 addition & 0 deletions blackberry/src/www/test/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
<a href="data:///www/test/telephony.html">Test Telephony API</a><br/>
<a href="data:///www/test/location.html">Test GeoLocation API</a><br/>
<a href="data:///www/test/camera.html">Test Camera API</a><br/>
<a href="data:///www/test/contacts.html">Test Contacts API</a><br/>
</body>
</html>

0 comments on commit aea3183

Please sign in to comment.