reducing code

This commit is contained in:
macdonst 2010-09-17 10:24:22 -04:00
parent 8de6d9ce15
commit 0a2d7bf536
4 changed files with 128 additions and 121 deletions

View File

@ -69,7 +69,7 @@ var ContactAccount = function(domain, username, userid) {
var Contacts = function() { var Contacts = function() {
this.inProgress = false; this.inProgress = false;
this.records = []; this.records = new Array();
} }
// Contacts.prototype.find = function(obj, win, fail) { // Contacts.prototype.find = function(obj, win, fail) {
@ -80,28 +80,8 @@ Contacts.prototype.find = function(fields, win, fail, options) {
PhoneGap.execAsync(null, null, "Contacts", "search", [fields, options]); PhoneGap.execAsync(null, null, "Contacts", "search", [fields, options]);
}; };
Contacts.prototype.droidFoundContact = function(name, npa, email) { Contacts.prototype.droidDone = function(contacts) {
this.records = new Array(); this.win(eval('(' + contacts + ')'));
var contact = new Contact();
contact.name = new ContactName();
contact.name.formatted = name;
contact.name.givenName = name;
contact.emails = new Array();
var mail = new ContactField();
mail.type = "home";
mail.value = email;
mail.primary = true;
contact.emails.push(mail);
contact.phones = new Array();
phone = new ContactField();
phone.type = "home";
phone.value = npa;
contact.phones.push(phone);
this.records.push(contact);
};
Contacts.prototype.droidDone = function() {
this.win(this.records);
}; };
Contacts.prototype.remove = function(contact) { Contacts.prototype.remove = function(contact) {

View File

@ -219,6 +219,36 @@ document.addEventListener = function(evt, handler, capture) {
} }
}; };
/**
* If JSON not included, use our own stringify. (Android 1.6)
* The restriction on ours is that it must be an array of simple types.
*
* @param args
* @return
*/
PhoneGap.stringify = function(args) {
if (typeof JSON == "undefined") {
var s = "[";
for (var i=0; i<args.length; i++) {
if (i > 0) {
s = s + ",";
}
var type = typeof args[i];
if ((type == "number") || (type == "boolean")) {
s = s + args[i];
}
else {
s = s + '"' + args[i] + '"';
}
}
s = s + "]";
return s;
}
else {
return JSON.stringify(args);
}
};
PhoneGap.callbackId = 0; PhoneGap.callbackId = 0;
PhoneGap.callbacks = {}; PhoneGap.callbacks = {};
@ -232,7 +262,7 @@ PhoneGap.callbacks = {};
PhoneGap.exec = function(clazz, action, args) { PhoneGap.exec = function(clazz, action, args) {
try { try {
var callbackId = 0; var callbackId = 0;
var r = PluginManager.exec(clazz, action, callbackId, JSON.stringify(args), false); var r = PluginManager.exec(clazz, action, callbackId, this.stringify(args), false);
eval("var v="+r+";"); eval("var v="+r+";");
// If status is OK, then return value back to caller // If status is OK, then return value back to caller
@ -256,10 +286,10 @@ PhoneGap.execAsync = function(success, fail, clazz, action, args) {
if (success || fail) { if (success || fail) {
PhoneGap.callbacks[callbackId] = {success:success, fail:fail}; PhoneGap.callbacks[callbackId] = {success:success, fail:fail};
} }
var r = PluginManager.exec(clazz, action, callbackId, JSON.stringify(args), true); var r = PluginManager.exec(clazz, action, callbackId, this.stringify(args), true);
// If a result was returned // If a result was returned
if (r) { if ((typeof r == "string") && (r.length > 0)) {
eval("var v="+r+";"); eval("var v="+r+";");
// If status is OK, then return value back to caller // If status is OK, then return value back to caller
@ -809,7 +839,7 @@ var ContactAccount = function(domain, username, userid) {
var Contacts = function() { var Contacts = function() {
this.inProgress = false; this.inProgress = false;
this.records = []; this.records = new Array();
} }
// Contacts.prototype.find = function(obj, win, fail) { // Contacts.prototype.find = function(obj, win, fail) {
@ -820,28 +850,16 @@ Contacts.prototype.find = function(fields, win, fail, options) {
PhoneGap.execAsync(null, null, "Contacts", "search", [fields, options]); PhoneGap.execAsync(null, null, "Contacts", "search", [fields, options]);
}; };
Contacts.prototype.droidFoundContact = function(name, npa, email) { Contacts.prototype.droidFoundContact = function(contact) {
this.records = new Array(); //console.log("this is what a contact looks like");
var contact = new Contact(); //console.log(contact);
contact.name = new ContactName();
contact.name.formatted = name; //this.records.push(eval('(' + contact + ')'));
contact.name.givenName = name; console.log("we should be called anymore.");
contact.emails = new Array();
var mail = new ContactField();
mail.type = "home";
mail.value = email;
mail.primary = true;
contact.emails.push(mail);
contact.phones = new Array();
phone = new ContactField();
phone.type = "home";
phone.value = npa;
contact.phones.push(phone);
this.records.push(contact);
}; };
Contacts.prototype.droidDone = function() { Contacts.prototype.droidDone = function(contacts) {
this.win(this.records); this.win(eval('(' + contacts + ')'));
}; };
Contacts.prototype.remove = function(contact) { Contacts.prototype.remove = function(contact) {
@ -911,59 +929,60 @@ PhoneGap.addConstructor(function() {
}); });
/** /**
* this represents the mobile device, and provides properties for inspecting the model, version, UUID of the * This represents the mobile device, and provides properties for inspecting the model, version, UUID of the
* phone, etc. * phone, etc.
* @constructor * @constructor
*/ */
function Device() { function Device() {
this.available = PhoneGap.available; this.available = PhoneGap.available;
this.platform = null; this.platform = null;
this.version = null; this.version = null;
this.name = null; this.name = null;
this.gap = null; this.uuid = null;
this.uuid = null; this.phonegap = null;
try {
if (window.DroidGap) { var me = this;
this.available = true; PhoneGap.execAsync(
this.uuid = window.DroidGap.getUuid(); function(info) {
this.version = window.DroidGap.getOSVersion(); me.available = true;
this.gapVersion = window.DroidGap.getVersion(); me.platform = info.platform;
this.platform = window.DroidGap.getPlatform(); me.version = info.version;
this.name = window.DroidGap.getProductName(); me.uuid = info.uuid;
this.line1Number = window.DroidGap.getLine1Number(); me.phonegap = info.phonegap;
this.deviceId = window.DroidGap.getDeviceId(); },
this.simSerialNumber = window.DroidGap.getSimSerialNumber(); function(e) {
this.subscriberId = window.DroidGap.getSubscriberId(); me.available = false;
} console.log("Error initializing PhoneGap: " + e);
} catch(e) { alert("Error initializing PhoneGap: "+e);
this.available = false; },
} "Device", "getDeviceInfo", []);
} }
/* /*
* You must explicitly override the back button. * This is only for Android.
*
* You must explicitly override the back button.
*/ */
Device.prototype.overrideBackButton = function() {
Device.prototype.overrideBackButton = function() BackButton.override();
{
BackButton.override();
} }
/* /*
* This is only for Android.
*
* This resets the back button to the default behaviour * This resets the back button to the default behaviour
*/ */
Device.prototype.resetBackButton = function() {
Device.prototype.resetBackButton = function() BackButton.reset();
{
BackButton.reset();
} }
/* /*
* This is only for Android.
*
* This terminates the activity! * This terminates the activity!
*/ */
Device.prototype.exitApp = function() Device.prototype.exitApp = function() {
{ BackButton.exitApp();
BackButton.exitApp();
} }
PhoneGap.addConstructor(function() { PhoneGap.addConstructor(function() {
@ -971,6 +990,7 @@ PhoneGap.addConstructor(function() {
}); });
PhoneGap.addConstructor(function() { if (typeof navigator.fileMgr == "undefined") navigator.fileMgr = new FileMgr();}); PhoneGap.addConstructor(function() { if (typeof navigator.fileMgr == "undefined") navigator.fileMgr = new FileMgr();});
@ -1569,7 +1589,6 @@ PhoneGap.addConstructor(function() {
* This class provides access to notifications on the device. * This class provides access to notifications on the device.
*/ */
function Notification() { function Notification() {
} }
/** /**
@ -1601,7 +1620,7 @@ Notification.prototype.activityStop = function() {
* @param {String} colour The colour of the light. * @param {String} colour The colour of the light.
*/ */
Notification.prototype.blink = function(count, colour) { Notification.prototype.blink = function(count, colour) {
}; };
/** /**
@ -1609,16 +1628,17 @@ Notification.prototype.blink = function(count, colour) {
* @param {Integer} mills The number of milliseconds to vibrate for. * @param {Integer} mills The number of milliseconds to vibrate for.
*/ */
Notification.prototype.vibrate = function(mills) { Notification.prototype.vibrate = function(mills) {
PhoneGap.execAsync(null, null, "Device", "vibrate", [mills]);
}; };
/** /**
* Causes the device to beep. * Causes the device to beep.
* On Android, the default notification ringtone is played.
*
* @param {Integer} count The number of beeps. * @param {Integer} count The number of beeps.
* @param {Integer} volume The volume of the beep.
*/ */
Notification.prototype.beep = function(count, volume) { Notification.prototype.beep = function(count) {
PhoneGap.execAsync(null, null, "Device", "beep", [count]);
}; };
// TODO: of course on Blackberry and Android there notifications in the UI as well // TODO: of course on Blackberry and Android there notifications in the UI as well
@ -1627,21 +1647,6 @@ PhoneGap.addConstructor(function() {
if (typeof navigator.notification == "undefined") navigator.notification = new Notification(); if (typeof navigator.notification == "undefined") navigator.notification = new Notification();
}); });
Notification.prototype.vibrate = function(mills)
{
DroidGap.vibrate(mills);
}
/*
* On the Android, we don't beep, we notify you with your
* notification! We shouldn't keep hammering on this, and should
* review what we want beep to do.
*/
Notification.prototype.beep = function(count, volume)
{
DroidGap.beep(count);
}
/** /**
* This class contains position information. * This class contains position information.
* @param {Object} lat * @param {Object} lat

View File

@ -79,41 +79,69 @@ public class ContactAccessorSdk5 extends ContactAccessor {
new String[] {ContactsContract.Contacts._ID, ContactsContract.Contacts.HAS_PHONE_NUMBER, ContactsContract.Contacts.DISPLAY_NAME}, new String[] {ContactsContract.Contacts._ID, ContactsContract.Contacts.HAS_PHONE_NUMBER, ContactsContract.Contacts.DISPLAY_NAME},
ContactsContract.Contacts.DISPLAY_NAME + " LIKE ?", ContactsContract.Contacts.DISPLAY_NAME + " LIKE ?",
new String[] {searchTerm}, new String[] {searchTerm},
ContactsContract.Contacts.DISPLAY_NAME + " ASC"); ContactsContract.Contacts.DISPLAY_NAME + " ASC");
JSONArray contacts = new JSONArray();
while (cursor.moveToNext()) { while (cursor.moveToNext()) {
JSONObject contact = new JSONObject();
String contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); String contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
if (contactName.trim().length() == 0) continue; if (contactName.trim().length() == 0) continue;
String phoneNumber = "null";
String emailAddress = "null";
try {
contact.put("displayName", contactName);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID)); String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
String hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)); //String hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
if (Boolean.parseBoolean(hasPhone)) { //if (Boolean.parseBoolean(hasPhone)) {
Cursor phones = cr.query( Cursor phones = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId,
null, null); null, null);
if (phones.moveToFirst()) { if (phones.moveToFirst()) {
phoneNumber = "'" + phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)).replace('\'', '`') + "'"; Log.d(LOG_TAG, "We found a phone!");
JSONArray phoneNumbers = new JSONArray();
JSONObject phoneNumber = new JSONObject();
try {
phoneNumber.put("primary", true);
phoneNumber.put("value", phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)).replace('\'', '`'));
phoneNumbers.put(phoneNumber);
contact.put("phoneNumbers", phoneNumbers);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} }
phones.close(); phones.close();
} //}
Cursor emails = cr.query( Cursor emails = cr.query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI, ContactsContract.CommonDataKinds.Email.CONTENT_URI,
null, null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = " + contactId, ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = " + contactId,
null, null); null, null);
if (emails.moveToFirst()) { if (emails.moveToFirst()) {
// This would allow you get several email addresses Log.d(LOG_TAG, "We found an email!");
emailAddress = "'" + emails.getString(emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA)).replace('\'', '`') + "'"; JSONArray emailAddresses = new JSONArray();
JSONObject email = new JSONObject();
try {
email.put("primary", true);
email.put("value", emails.getString(emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA)).replace('\'', '`'));
emailAddresses.put(email);
contact.put("emails", emailAddresses);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} }
emails.close(); emails.close();
String contactAddJS = "javascript:navigator.service.contacts.droidFoundContact('" + contactName.replace('\'', '`') + "'," + phoneNumber + "," + emailAddress +")"; contacts.put(contact);
mView.loadUrl(contactAddJS);
} }
cursor.close(); cursor.close();
mView.loadUrl("javascript:navigator.service.contacts.droidDone();"); mView.loadUrl("javascript:navigator.service.contacts.droidDone('" + contacts.toString() + "');");
} }
} }

View File

@ -70,13 +70,7 @@ public class ContactManager implements Plugin {
//} //}
//else if (action.equals("search")) { //else if (action.equals("search")) {
if (action.equals("search")) { if (action.equals("search")) {
Log.d(LOG_TAG, "Executing search using accessor"); contactAccessor.search(args.getJSONArray(0), args.getJSONObject(1));
JSONArray fields = args.getJSONArray(0);
for (int i=0; i<fields.length(); i++) {
Log.d(LOG_TAG, "Field = " + fields.getString(i));
}
JSONObject options = args.getJSONObject(1);
contactAccessor.search(fields, options);
} }
return new PluginResult(status, result); return new PluginResult(status, result);
} catch (JSONException e) { } catch (JSONException e) {